数据库介绍
数据库结构模型
数据库结构模型主要有:
- 层次模型
- 网状结构
- 关系模型
关系模型:
二维关系:row(行),column(列)
数据库管理系统:DBMS(database managenment system)
关系数据库管理系统:RDBMS(Relational database managenment system)
RDBMS专业名词
常见的数据库:
- 关系型数据库
- mysql
- 5.7(原开发团队的最后一版本,免费)
- 8.0(oracle收购后的第一版本,收费)
- mariadb10.0(mysql原开发团队的第一版本,免费)
- oracle
- MSSQL
- mysql
- nosql数据库:
- mongodb
- 缓存数据库:
- redis
- memcache
- 缓存数据库:
- mongodb
- 轻量型数据库:
- sqlite
常见的关系型数据库管理系统
- MySQL:MySQL,MariaDB,Percona-Server
- PostgreSQL:简称为pgsql
- Oracle
- MSSQL
SQL:Structure Query Language,结构化查询语言
约束:constraint,向数据表提供的数据要遵守的限制
约束名 | 作用 |
---|---|
主键约束 | 一个或多个字段的组合,填入数据必须能在本表中唯一标识本行。且必须提供数据,不能为空(NOT NULL), 一表只能存在一个,例如ID。 |
惟一约束 | 一个或多个字段按的组合,填入数据必须能在本表中唯一标识本行。允许为空。(NULL), 一表可以存在多个。 |
外键约束 | 一个表中某字段可填入数据取决于另一个表的主键已有的数据 |
检查性约束 | 约束用于限制列中的值的范围。 |
索引:将表中的一个或多个字段中的数据复制一份另存,并且这些数据需要按特定次序排序存储
关系型数据库的常见组件
关系型数据库的常见组件有:
- 数据库:database
- 表:table
- 索引:index
- 视图:view
- 用户:user
- 权限:privilege
- 存储过程:procedure
- 触发器:trigger
- 事件调度器:event scheduler
SQL语句
SQL语句有三种类型:
- DDL:Data Defination Language,数据定义语言
- DML:DataManipulation Laanguage,数据操纵语言
- DCL:Data Control Language,数据控制语言
SQL语句类型 | 对应操作 |
---|---|
DDL | CREATE:创建 DROP:删除 ALTER:修改 |
DML | INSERT:向表中插入数据 DELETE:删除表中数据 UPDATE:更新表中数据 SELECT:查询表中数据 |
DCL | GRANT:授权 REVOKE:移除授权 |
mysql的安装与配置
mysql的安装
CentOS8中的yum源中只有Mysql8的版本,若想要安装Mysql5.7则需要进行以下配置:
首先,关闭CentOS8中Mysql默认的yum仓库
[root@localhost ~]# dnf remove @mysql
Warning: failed loading '/etc/yum.repos.d/mssql-server.repo', skipping.
Last metadata expiration check: 2 days, 4:32:54 ago on 2021年04月26日 17时30分44秒 CST.
Unable to match profile in argument mysql
Dependencies resolved.
Nothing to do.
Complete!
[root@localhost ~]# dnf module reset mysql && sudo dnf module disable mysql
Warning: failed loading '/etc/yum.repos.d/mssql-server.repo', skipping.
CentOS Stream 8 - AppStream 7.0 kB/s | 4.4 kB 00:00
CentOS Stream 8 - BaseOS 6.7 kB/s | 3.9 kB 00:00
CentOS Stream 8 - Extras 2.6 kB/s | 1.5 kB 00:00
Dependencies resolved.
Nothing to do.
Complete!
Warning: failed loading '/etc/yum.repos.d/mssql-server.repo', skipping.
Last metadata expiration check: 0:00:01 ago on 2021年04月28日 22时03分46秒 CST.
Dependencies resolved.
====================================================================
Package Architecture Version Repository Size
====================================================================
Disabling modules:
mysql
Transaction Summary
====================================================================
Is this ok [y/N]: y
Complete!
因为目录还没有EL8版本的Mysql仓库,所以我们这里用EL7代替,创建一个新的仓库文件:
[root@localhost ~]# vim /etc/yum.repos.d/mysql-community.repo
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch
/
enabled=1
gpgcheck=0
[mysql-connectors-community]
name=MySQL Connectors Community
baseurl=http://repo.mysql.com/yum/mysql-connectors-community/el/7/$b
asearch/
enabled=1
gpgcheck=0
[mysql-tools-community]
name=MySQL Tools Community
baseurl=http://repo.mysql.com/yum/mysql-tools-community/el/7/$basear
ch/
enabled=1
gpgcheck=0
开始安装Mysql5.7版本,并且临时的使用仓库
[root@localhost ~]# dnf --enablerepo=mysql57-community install mysql-community-server
Warning: failed loading '/etc/yum.repos.d/mssql-server.repo', skipping.
MySQL 5.7 Community Server 942 kB/s | 2.2 MB 00:02
MySQL Connectors Community 115 kB/s | 107 kB 00:00
MySQL Tools Community 444 kB/s | 630 kB 00:01
Dependencies resolved.
====================================================================
Package Arch Version Repository Size
====================================================================
Installing:
mysql-community-server x86_64 5.7.34-1.el7 mysql57-community 173 M
Installing dependencies:
mysql-community-client x86_64 5.7.34-1.el7 mysql57-community 25 M
mysql-community-common x86_64 5.7.34-1.el7 mysql57-community 310 k
mysql-community-libs x86_64 5.7.34-1.el7 mysql57-community 2.4 M
ncurses-compat-libs x86_64 6.1-7.20180224.el8
baseos 331 k
net-tools x86_64 2.0-0.52.20160912git.el8
baseos 322 k
Transaction Summary
====================================================================
Install 6 Packages
Total download size: 202 M
Installed size: 877 M
Is this ok [y/N]: y
Downloading Packages:
(1/6): net-tools-2.0-0.52.20160912g 716 kB/s | 322 kB 00:00
(2/6): ncurses-compat-libs-6.1-7.20 724 kB/s | 331 kB 00:00
(3/6): mysql-community-common-5.7.3 328 kB/s | 310 kB 00:00
(4/6): mysql-community-libs-5.7.34- 807 kB/s | 2.4 MB 00:02
(5/6): mysql-community-client-5.7.3 2.2 MB/s | 25 MB 00:11
(6/6): mysql-community-server-5.7.3 3.2 MB/s | 173 MB 00:54
--------------------------------------------------------------------
Total 3.6 MB/s | 202 MB 00:56
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : mysql-community-common-5.7.34-1.el7.x86_ 1/6
Installing : mysql-community-libs-5.7.34-1.el7.x86_64 2/6
Running scriptlet: mysql-community-libs-5.7.34-1.el7.x86_64 2/6
Installing : net-tools-2.0-0.52.20160912git.el8.x86_6 3/6
Running scriptlet: net-tools-2.0-0.52.20160912git.el8.x86_6 3/6
Installing : ncurses-compat-libs-6.1-7.20180224.el8.x 4/6
Installing : mysql-community-client-5.7.34-1.el7.x86_ 5/6
Running scriptlet: mysql-community-server-5.7.34-1.el7.x86_ 6/6
Installing : mysql-community-server-5.7.34-1.el7.x86_ 6/6
Running scriptlet: mysql-community-server-5.7.34-1.el7.x86_ 6/6
[/usr/lib/tmpfiles.d/mysql.conf:23] Line references path below legacy directory /var/run/, updating /var/run/mysqld → /run/mysqld; please update the tmpfiles.d/ drop-in file accordingly.
Verifying : ncurses-compat-libs-6.1-7.20180224.el8.x 1/6
Verifying : net-tools-2.0-0.52.20160912git.el8.x86_6 2/6
Verifying : mysql-community-client-5.7.34-1.el7.x86_ 3/6
Verifying : mysql-community-common-5.7.34-1.el7.x86_ 4/6
Verifying : mysql-community-libs-5.7.34-1.el7.x86_64 5/6
Verifying : mysql-community-server-5.7.34-1.el7.x86_ 6/6
Installed products updated.
Installed:
mysql-community-client-5.7.34-1.el7.x86_64
mysql-community-common-5.7.34-1.el7.x86_64
mysql-community-libs-5.7.34-1.el7.x86_64
mysql-community-server-5.7.34-1.el7.x86_64
ncurses-compat-libs-6.1-7.20180224.el8.x86_64
net-tools-2.0-0.52.20160912git.el8.x86_64
Complete!
mysql的配置
安装完成后启动Mysql5.7,并设置开机自启Mysql5.7
[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# systemctl enable mysqld
[root@localhost ~]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled;>
Active: active (running) since Wed 2021-04-28 22:10:46 CST; 10s >
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Main PID: 55422 (mysqld)
Tasks: 27 (limit: 8756)
Memory: 301.0M
CGroup: /system.slice/mysqld.service
└─55422 /usr/sbin/mysqld --daemonize --pid-file=/var/run>
四月 28 22:10:43 localhost.localdomain systemd[1]: Starting MySQL S>
四月 28 22:10:46 localhost.localdomain systemd[1]: Started MySQL Se>
lines 1-13/13 (END)
获取mysql的初始密码
[root@localhost ~]# grep 'A temporary password' /var/log/mysqld.log |tail -1
2021-04-28T14:10:44.097144Z 1 [Note] A temporary password is generated for root@localhost: tGeuN79wUX/O
修改Mysql的密码以及配置
[root@localhost ~]# mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:
The existing password for the user account root has expired. Please set a new password.
New password:
Re-enter new password:
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.
Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y//修改root用户的密码
New password:
Re-enter new password:
Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y //确认使用此密码吗?
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y //删除匿名用户
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n //禁止root远程登录?
... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y //删除测试数据库?
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y //重新加载权限表
Success.
All done!
mysql的程序组成
- 客户端:
- mysql:CLI交互式客户端程序
- mtsql_secure_installation:安全初始化,强烈建议安装哇按此后执行此命令
- mysqldump:mysql备份工具
- mysqladmin
- 服务器端
- mysqld
mysql工具的使用
//语法:mysql [OPTIONS] [database]
//常用的OPTIONS:
-u[USERNAME] //指定用户名,默认为root
-h[HOST] //指定服务器主机,默认为localhost,推荐使用ip地址
-p[PASSWORD] //指定用户的密码
-P[PORT] //指定数据库监听的端口
-V //查看当前使用的mysql版本
-e //不登录mysql执行sql语句后退出
查看mysql的版本
[root@localhost ~]# mysql -V
mysql Ver 14.14 Distrib 5.7.34, for Linux (x86_64) using EditLine wrapper
指定用户和密码
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.7.34 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
服务器监听的两种socket地址
socket类型 | 说明 |
---|---|
ip socket | 默认监听在tcp的3306端口,支持远程通信 |
unix sock | 监听在sock文件上(/tmp/mysql.sock,/var/lib/mysql/mysql.sock) 仅支持本地通信 server地址只能是:localhost,127.0.0.1 |
mysql数据库的语法
DDL语法
数据库语法
查看数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
创建数据库
mysql> create database hanao;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hanao |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
删除数据库
mysql> DROP DATABASE hanao
-> ;
Query OK, 0 rows affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
表操作
进入数据库
mysql> use hanao
Database changed
创建数据表
mysql> create table student(id int(11) not null auto_increment primary key,name varchar(100) not null,age tinyint(4));
Query OK, 0 rows affected (0.01 sec)
mysql> desc student;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(100) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+--------------+------+-----+---------+----------------+
删除数据表
mysql> show tables;
+-----------------+
| Tables_in_hanao |
+-----------------+
| student |
+-----------------+
1 row in set (0.00 sec)
mysql> drop table student;
Query OK, 0 rows affected (0.00 sec)
mysql> show tables;
Empty set (0.00 sec)
用户操作
mysql用户账号由两部分组成,如’USERNAME’@‘HOST’,表示此USERNAME只能从此HOST上远程登录
这里(‘USERNAME’@‘HOST’)的HOST用于限制此用户可通过哪些主机远程连接mysql程序,其值可为:
ip地址,如:192.168.247.1
通配符
- %:匹配任意长度的任意字符,常用于设置允许从任何主机登录
- _:匹配任意单个字符
创建数据库用户
mysql> create user 'hanao'@'192.168.247.137' identified by 'Ha153624.';
Query OK, 0 rows affected (0.00 sec)
使用新创建的账户密码登录
[root@localhost ~]# mysql -uhanao -pHa153624. -h192.168.247.137
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 26
Server version: 5.7.34 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
删除数据库用户
mysql> drop user 'hanao'@'192.168.247.137';
Query OK, 0 rows affected (0.00 sec)
查看命令SHOW
查看支持的所有编码类型
mysql> show character set;
+----------+---------------------------------+---------------------+--------+
| Charset | Description | Default collation | Maxlen |
+----------+---------------------------------+---------------------+--------+
| big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 |
| dec8 | DEC West European | dec8_swedish_ci | 1 |
| cp850 | DOS West European | cp850_general_ci | 1 |
| hp8 | HP West European | hp8_english_ci | 1 |
| koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 |
| latin1 | cp1252 West European | latin1_swedish_ci | 1 |
| utf8 | UTF-8 Unicode | utf8_general_ci | 3 |
……
查看当前数据库支持的所有存储引擎
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)
查看数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hanao |
| mysql |
| performance_schema |
| sys |
+--------------------+
查看表结构
mysql> desc hanao.student;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(100) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+--------------+------+-----+---------+----------------+
查看表的创建命令
mysql> show create table hanao.student;
+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| student | CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`age` tinyint(4) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=latin1 |
+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
查看表的状态
mysql> show table status like 'student'\G
*************************** 1. row ***************************
Name: student
Engine: InnoDB
Version: 10
Row_format: Dynamic
Rows: 11
Avg_row_length: 1489
Data_length: 16384
Max_data_length: 0
Index_length: 0
Data_free: 0
Auto_increment: 12
Create_time: 2021-04-29 17:15:26
Update_time: 2021-04-29 17:21:04
Check_time: NULL
Collation: latin1_swedish_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)
获取帮助
mysql> help create table;
Name: 'CREATE TABLE'
Description:
Syntax:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
(create_definition,...)
[table_options]
[partition_options]
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
[(create_definition,...)]
[table_options]
[partition_options]
[IGNORE | REPLACE]
[AS] query_expression
DML语法
DML操作包括(insert)、删(delete)、改(update)、查(select),均属于针对表的操作、
insert语法
mysql> insert student(name,age) values('hanao',22),('ha',22);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
| 7 | lisi | 50 |
| 8 | chenshuo | 10 |
| 9 | wangwu | 3 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
| 12 | hanao | 22 |
| 13 | ha | 22 |
+----+-------------+------+
13 rows in set (0.00 sec)
select语法
字段表示法
表示符 | 含义 |
---|---|
* | 所有字段 |
as | 字段别名 |
条件判断语句where
操作类型 | 常用操作符 |
---|---|
操作符 | >,<,>=,<=,=,!= between [column] and [column] like:模糊匹配 rlike:基于正则表达式进行模式匹配 is not null:非空 is null:空 |
条件逻辑操作 | and or not |
order by:排序,默认为升序(ASC)
order by语句 | 含义 |
---|---|
order by [column] | 根据字段进行升序排序 |
order by [column] | 根据字段进行降序排序 |
order by [column] limit 2 | 根据字段进行升序排序并只取前两行 |
order by [column] limit 1,2 | 更加字段进行升序排序并且略过第1个结果取后面的2个结果 |
select查询语句 //查询student表中年龄在5到30直接以年龄倒叙排序其中第1条不显示并且只显示7条
mysql> select * from student where age between 5 and 30 order by age desc limit 1,7;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 5 | zhangshan | 26 |
| 3 | wangqing | 25 |
| 2 | jerry | 23 |
| 12 | hanao | 22 |
| 13 | ha | 22 |
| 1 | tom | 20 |
| 6 | zhangshan | 20 |
+----+-----------+------+
7 rows in set (0.00 sec)
update语法
update语法 //将student表中名为shangshan的年龄改为25
mysql> update student set age = 25 where name = 'zhangshan';
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> select * from student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 25 |
| 6 | zhangshan | 25 |
| 7 | lisi | 50 |
| 8 | chenshuo | 10 |
| 9 | wangwu | 3 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
| 12 | hanao | 22 |
| 13 | ha | 22 |
+----+-------------+------+
13 rows in set (0.00 sec)
delete语法
delete语法 //删除姓名为zhangshan并且id为5的数据
mysql> delete from student where name = 'zhangshan' and id = 5;
Query OK, 1 row affected (0.00 sec)
mysql> select * from student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 6 | zhangshan | 25 |
| 7 | lisi | 50 |
| 8 | chenshuo | 10 |
| 9 | wangwu | 3 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
| 12 | hanao | 22 |
| 13 | ha | 22 |
+----+-------------+------+
12 rows in set (0.00 sec)
truncate语法
truncate与delete的区别:
语句类型 | 特点 |
---|---|
delete | delete删除表内容时仅删除内容,但会保留表结构 delete语句每次删除一行 并在事务日志中为所删除的每行记录一项 可以通过回滚事务日志恢复数据 非常占用空间 |
truncate | 删除表中所有数据,且无法恢复 表结构、约束和索引等保持不变,新添加的行计数值重置为初始值 执行速度比delete快,且使用的系统和事务日志资源少 通过释放存储表数据所用的数据页来删除数据 只在事务日志中记录页的释放 对于有外键约束引用的表,不能使用truncate table 删除数据 不能用于加入了索引的视图的表 |
mysql> select * from student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 6 | zhangshan | 25 |
| 7 | lisi | 50 |
| 8 | chenshuo | 10 |
| 9 | wangwu | 3 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
| 12 | hanao | 22 |
| 13 | ha | 22 |
+----+-------------+------+
12 rows in set (0.00 sec)
mysql> truncate student;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from student;
Empty set (0.00 sec)
DCL语法
创建授权grant
权限类型
权限类型 | 含义 |
---|---|
all | 所有权限 |
select | 读取内容的权限 |
insert | 插入内容的权限 |
update | 修改内容的权限 |
delete | 删除内容的权限 |
指定要操作的对象
表示方式 | 含义 |
---|---|
* . * | 所有库的所有表 |
database | 指定库的所有表 |
database.table | 指定库的指定表 |
授权root用户在所有位置上远程登录访问所有数据库,并设置登录密码
mysql> grant all on *.* to 'root'@'%' identified by 'Ha153624.';
Query OK, 0 rows affected, 1 warning (0.00 sec)
查看当前用户的授权信息
mysql> show grants;
+---------------------------------------------------------------------+
| Grants for root@localhost |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+---------------------------------------------------------------------+
2 rows in set (0.00 sec)
查看指定用户的授权信息
mysql> show grants for 'root'@'%';
+-------------------------------------------+
| Grants for root@% |
+-------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' |
+-------------------------------------------+
1 row in set (0.00 sec)
取消授权REVOKE
mysql> revoke all on *.* from 'root'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql服务进程启动时会读取mysql库中的所有授权表到内存中:
- grant或revoke等执行权限操作会保存于表中,mysql服务进程会自动重读授权表,并更新到内存中
- 对于不能够或不能及时重读授权表的命令,可使用flush privileges命令手动让mysql的服务进程重读授权表,或者重启mysql服务,(生产环境中一般不重启服务)