用户和权限

  1. /*进入mysql*/
  2. use mysql;
  3. /*创建用于复制操作的用户*/
  4. create user 'test'@'localhost' identified with mysql_native_password by 'test';
  5. /*从节点分片授权*/
  6. GRANT REPLICATION SLAVE ON *.* TO 'test'@'localhost';
  7. /*授权*/
  8. grant all privileges on *.* to 'test'@'localhost';
  9. /*刷新授权表信息*/
  10. FLUSH PRIVILEGES;

修改用户密码

  1. ALTER USER 'root'@'localhost' identified with mysql_native_password BY '123456';

查看数据库用户

  1. select user,host from mysql.user;

数据库操作

新建

  1. CREATE DATABASE IF NOT EXISTS demodb DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci;

为数据库新建用户并赋权限

  1. /*创建新的用户*/
  2. create user 'demo'@'%' identified with mysql_native_password by 'demo001';
  3. /*给用户赋数据库权限*/
  4. GRANT ALL PRIVILEGES ON demodb.* TO 'demo'@'%';
  5. flush privileges;

字符集

查看字符集

  1. show variables like 'character_set%';

修改字符集

  1. set character_set_client=gbk;

备份

  1. # 备份MySQL里的全部数据库
  2. mysqldump -u root -p 123456 --all-databases > c:\\all_database_fullbak.sql
  3. # 备份MySQL数据库
  4. mysqldump -u root -p 123456 kcgl> c:\\database_fullbak.sql
  5. # 备份MySQL数据库
  6. mysqldump -h hostname -u username -p password -database databasename > backupfile.sql
  7. # 直接将MySQL数据库压缩备份
  8. mysqldump -h hostname -u username -p password -database databasename | gzip > backupfile.sql.gz

还原

正常情况

  1. # Bin目录下还原MySQL数据库的命令
  2. mysql -u username -p password databasename < backupfile.sql
  3. # 还原MySQL数据库的命令
  4. mysql -h hostname -u username -p password databasename < backupfile.sql
  5. # 还原压缩的MySQL数据库
  6. gunzip < backupfile.sql.gz | mysql -u username -p password databasename

全备份还原单库

  1. 从全备份文件中将需要的库的建表语句和INSERT数据拿出来,然后再导入

    1. sed -n '/^-- Current Database: `db_test001`/,/^-- Current Database: `/p' all_database_fullbak.sql > db_test001.sql
  2. 导入库中

    1. mysql -uroot -p < db_test001.sql

    全备份还原单表

  3. 先删除一个表: sys_user

  4. 从全备份中提取出该表的建表语句

    1. sed -e'/./{H;$!d;}' -e 'x;/CREATE TABLE `sys_user`/!d;q' all_database_fullbak.sql > sys_user.sql
  5. 提取该表的insert into语句

    1. grep -i 'INSERT INTO `sys_user`' all_database_fullbak.sql >> sys_user.sql
  6. 导入到对应的库中

    1. mysql -uroot -p <sys_user.sql
  7. 查看数据

    1. mysql> select count(*) from sys_user;

    锁表

    锁表后数据库所有用户都只能进行读操作

上锁

  1. flush tables with read lock;

解锁

  1. unlock tables;

只读模式

只读模式可以限定普通用户进行数据修改的操作,但不会限定具有super权限的用户的数据修改操作;

设置数据库只读

  1. set global read_only=1;

取消数据库只读

  1. set global read_only=0;

定位列名

  1. select * from information_schema.COLUMNS where COLUMN_NAME='列名';

修改数据库名

MyISAM

直接修改数据库文件夹名称

Innodb

最保险的方法就是备份导出数据,新建库重新导入。

修改表名

修改单个表的

  1. alter table tbl_name rename [to|as] new_tbl_name

可以修改多个表的

  1. rename table tbl_name to new_tbl_name [,tbl_name2 TO new_tbl_name2...]

修改列

数据库大小写

参考

数据库关键字和保留字

https://dev.mysql.com/doc/refman/8.0/en/keywords.html

Group By

ONLY_FULL_GROUP_BY

这个配置在MySQL 5.7以后是默认开启的。

  1. mysql> select @@sql_mode;
  2. +-----------------------------------------------------------------------------------------------------------------------+
  3. | @@sql_mode |
  4. +-----------------------------------------------------------------------------------------------------------------------+
  5. | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
  6. +-----------------------------------------------------------------------------------------------------------------------+
  7. 1 row in set (0.04 sec)

示例

执行下列的SQL语句

  1. SELECT creator,modifier FROM `sys_user` GROUP BY creator;

执行该语句后,报错如下图image.png
关闭连接重连后,再次执行相同的语句,显示正常的结果,不再有错误(如下图)
image.png
如何在不关闭ONLY_FULL_GROUP_BY的情况下,实现正常的查询呢?使用ANY_VALUE函数

  1. SELECT creator,any_value(modifier) FROM `sys_user` GROUP BY creator;

image.png

判断是否包含某个字符串的方法

like

  1. SELECT * FROM 表名 WHERE 字段名 like "%字符%";

find_in_set()

find_in_set(str1,str2) 函数是返回str2中str1所在的位置索引+1,str2必须以”,”分割开。

  1. SELECT find_in_set('3','3,6,13,1,24,33,36') -- 结果为 1

locate(字符,字段名)

使用locate(字符,字段名)函数,如果包含,返回>0的数,否则返回0

查询表索引碎片

  1. show table status from kkk like 'frag_tab_myisam' \G;
  1. SELECT CONCAT(table_schema, '.', table_name) AS TABLE_NAME
  2. ,engine AS TABLE_ENGINE
  3. ,table_type AS TABLE_TYPE
  4. ,table_rows AS TABLE_ROWS
  5. ,CONCAT(ROUND(data_length / ( 1024 * 1024), 2), 'M') AS TB_DATA_SIZE
  6. ,CONCAT(ROUND(index_length / ( 1024 * 1024), 2), 'M') AS TB_IDX_SIZE
  7. ,CONCAT(ROUND((data_length + index_length )
  8. / ( 1024 * 1024 ), 2), 'M') AS TOTAL_SIZE
  9. ,CASE WHEN data_length =0 THEN 0
  10. ELSE ROUND(index_length / data_length, 2) END AS TB_INDX_RATE
  11. ,CONCAT(ROUND( data_free / 1024 / 1024,2), 'MB') AS TB_DATA_FREE
  12. ,CASE WHEN (data_length + index_length) = 0 THEN 0
  13. ELSE ROUND(data_free/(data_length + index_length),2)
  14. END AS TB_FRAG_RATE
  15. FROM information_schema.TABLES
  16. ORDER BY data_free DESC;
  17. SELECT CONCAT(table_schema, '.', table_name) AS TABLE_NAME
  18. ,engine AS TABLE_ENGINE
  19. ,table_type AS TABLE_TYPE
  20. ,table_rows AS TABLE_ROWS
  21. ,CONCAT(ROUND(data_length / ( 1024 * 1024), 2), 'M') AS TB_DATA_SIZE
  22. ,CONCAT(ROUND(index_length / ( 1024 * 1024), 2), 'M') AS TB_IDX_SIZE
  23. ,CONCAT(ROUND((data_length + index_length )
  24. / ( 1024 * 1024 ), 2), 'M') AS TOTAL_SIZE
  25. ,CASE WHEN data_length =0 THEN 0
  26. ELSE ROUND(index_length / data_length, 2) END AS TB_INDX_RATE
  27. ,CONCAT(ROUND( data_free / 1024 / 1024,2), 'MB') AS TB_DATA_FREE
  28. ,CASE WHEN (data_length + index_length) = 0 THEN 0
  29. ELSE ROUND(data_free/(data_length + index_length),2)
  30. END AS TB_FRAG_RATE
  31. FROM information_schema.TABLES
  32. WHERE ROUND(DATA_FREE/1024/1024,2) >=50
  33. ORDER BY data_free DESC;
  34. SELECT TABLE_SCHEMA
  35. ,TABLE_NAME
  36. ,ENGINE
  37. ,ROUND(((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024), 2) AS SIZE_MB
  38. ,ROUND(DATA_FREE/1024/1024,2) AS FREE_SIZ_MB
  39. FROM information_schema.TABLES
  40. WHERE DATA_FREE >=10*1024*1024
  41. ORDER BY FREE_SIZ_MB DESC;

清理索引

  1. 我们可以通过一个nop操作(不产生对数据实质影响的操作), 来修改表.比如: 表的引擎为innodb , 可以 alter table xxx engine innodb(修改表的引擎类型为其默认类型会重新调整数据,但不会影响数据)
  2. optimize table 表名 ,也可以修复

注意: 修复表的数据及索引碎片,就会把所有的数据文件重新整理一遍,使之对齐.
   这个过程,如果表的行数比较大,也是非常耗费资源的操作.
   所以,不能频繁的修复.

事务超时死锁解决

  1. show full processlist;
  2. select * from information_schema.innodb_trx;
  3. select * from information_schema.innodb_locks;
  4. SELECT * FROM information_schema.innodb_lock_waits;

相关资料