DCL, Data Control Language, 数据控制语言
用于控制数据权限
1. 创建新用户
1 2 3 4 5 6 7 create user root@'%' identified WITH mysql_native_password BY 'password' ;grant all privileges on *.* to root@'%' with grant option ;flush privileges ;CREATE USER 'raven' @localhost IDENTIFIED BY 'password' GRANT ALL PRIVILEGES ON *.* TO 'raven' @'%' IDENTIFIED BY 'password' WITH GRANT OPTION ;flush privileges ;
2. 修改用户
1 2 3 ALTER USER 'root' @'localhost' IDENTIFIED BY 'root' PASSWORD EXPIRE NEVER ; ALTER USER 'root' @'%' IDENTIFIED WITH mysql_native_password BY 'password' ; ALTER USER 'root' @'localhost' IDENTIFIED WITH mysql_native_password BY 'password' ;
或者使用 mysqladmin:
1 /usr/bin/mysqladmin -u root password new-password
3. 查看支持的引擎
4. 查看 MySQL 参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 show variables like '%partition%' ;show variables like '%max_connections%' ;show variables like 'collation_%' ;show variables like 'character_set_%' ;show variables like '%cache%' ;reset query cache ;show status ;show global status ;show global status like 'Created_tmp%' ;select @@tx_isolation;set session transaction isolation level @isolation_level;set global transaction isolation level serializable ;
5. 查看连接列表
1 mysql -e 'show processlist\G' | grep 'State:' | sort | uniq -c | sort -rn
6. explain
1 2 3 explain @sql ;explain partitions @sql ;
7. 查看表状态
1 2 3 show table status like 'table_name' show [session |global ] status like @status_name;
8. 查看实时 sql 记录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 set global general_log = 'ON' ;set global general_log_file = '/var/log/mysql/general_log.log' ;set global log_output = 'TABLE' ;select * from mysql.general_log order by event_time desc ;set global general_log = 'OFF' ;truncate table mysql.general_log;
9. 查看慢查询
1 2 3 4 5 6 7 8 9 10 11 12 show variables like 'slow_query%' ;show variables like 'long_query_time' ;set global slow_query_log='ON' ; set long_query_time = 1 ;select sleep (2 )
10. 分析 SQL 执行时间
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 show variables like 'PROFILING' ;set profiling ='ON' ;show profiles ;set profiling ='OFF' ;set @query_id=1 ;select state, sum (duration ) as total_r, ROUND ( 100 * sum (duration ) / ( select sum (duration ) from information_schema.profiling where query_id = @quer_id ), 2 ) as pct_r, count (*) as calls, sum (duration ) / count (*) as "r/call" from information_schema.profilingwhere query_id = @query_idgroup by stateorder by total_r desc ;
11. 表结构优化建议
1 2 SELECT column_name FROM table_name procedure analyse();
12. 定期回收闲置的数据库空间
1 2 3 optimize table table_name;alter table table_name engine = innodb ;alter table table_name force ;
13. 导入文件
1 source /file/to/source.sql
1 mysql < /file/to/source.sql
14. 导出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 mysql -N -e "select * from dual" > /file/to/result.txt mysqldump $database $table_name > /file/to/dump.sql mysqldump -d --add-drop-table $database $table_name > /file/to/dump.sql mysqldump -t -c $database $table_name > /file/to/dump.sql mysqldump -d -y --skip-comments --skip-quote-names --compress $database > /file/to/dump.sql
15. 查看 bin log
1 2 3 4 5 6 show variables like 'log_%' ;show master status ;show master logs ;show binary logs ;
1 2 mysqlbinlog mysql-bin.000004 | tail; mysqlbinlog mysql-bin.000004 > file.txt
16. Resource