常用mysql库

percona:https://www.percona.com/software/mysql-database/percona-server
mariadb:https://downloads.mariadb.org/
Oracle:https://dev.mysql.com/downloads/

常用SQL

  1. # 条件批量更新
  2. UPDATE categories SET
  3. display_order = CASE id
  4. WHEN 1 THEN 3
  5. WHEN 2 THEN 4
  6. WHEN 3 THEN 5
  7. END
  8. WHERE id IN (1,2,3)
  9. # 查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断
  10. select * from people
  11. where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
  12. # 删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录
  13. delete from people
  14. where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
  15. and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1)
  16. # 查找表中多余的重复记录(多个字段)
  17. select * from vitae a
  18. where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
  19. # 删除表中多余的重复记录(多个字段),只留有rowid最小的记录
  20. delete from vitae a
  21. where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
  22. and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)

CRUD

  1. CREATE DATABASE IF NOT EXISTS db DEFAULT CHARACTER SET utf8mb4 DEFAULT COLLATE utf8mb4_general_ci;

备份与恢复

  1. //备份
  2. mysqldump -u{user} -p{Password} -B {db1} {db2} > sql_bak.sql
  3. -d, --no-data 只备份表结构
  4. -t, --no-create-info 只备份数据,不备份create table
  5. -n,--no-create-db 不备份create database,可被-A或-B覆盖
  6. //恢复
  7. mysql -u{user} -p{Password} -p < sql_bak.sql

docker start

  1. chown 1001 /www/data/docker/mysql/data
  2. docker pull percona/percona-server:5.7
  3. docker run --name mysql -p 4306:3306 -v /www/data/docker/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=Mydb2018 -d percona/percona-server:5.7

mysql

CREATE user zc IDENTIFIED BY ‘Zc2018mysql’; grant all privileges on . to ‘zc’@’%’ identified by ‘Zc2018mysql’; v5.7修改密码 update user set authentication_string=password(‘Mydb2018’) where user=’zc’; grant all privileges on . to ‘qizai’@’%’ identified by ‘Mydb2018’; 2

创建数据库

create database test default charset utf8mb4 collate utf8mb4_general_ci;

设置不验证密码复杂度

set global validate_password_policy=0;

授权

grant all privileges on test.* to ‘data’@’%’ identified by ‘mysql#password’;

创建用户

CREATE user local_rsync IDENTIFIED BY ‘Local#rsync2018’;

授权

GRANT CREATE,INSERT,SELECT,UPDATE ON intbee TO ‘local_rsync’@’10.0.1.%’ IDENTIFIED BY ‘Local#rsync2018’; FLUSH PRIVILEGES;

直接修改授权

select user,host,grant_priv from mysql.user; 查看授权 update mysql.user set grant_priv=’Y’ where user=’zc’; 更新授权

FLUSH PRIVILEGES;

查看授权

SHOW GRANTS FOR ‘root’@’10.0.1.%’;

mysql8.0

  1. ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';
  2. ALTER USER 'root'@'%' IDENTIFIED BY 'Mydb2019';
  3. FLUSH PRIVILEGES;

关键配置参数

  1. expire_logs_days=15 #binlog保留时间15天,修改后需要通过flush logs;才能删除旧文件
  2. max_binlog_size=1G #binlog大小

锁问题定位

  1. 查看数据库版本
  2. select version();
  3. 查看表状态
  4. show open tables from intbee;
  5. 分析行锁定命令
  6. show status like '%lock%';
  7. show status like 'innodb_row_lock%';
  8. show variables like '%timeout%';
  9. 查看事务锁等待状态情况(优先使用,遇到表无法插入,由于之前的读线程事务没有提交,导致无法插入)
  10. select * from information_schema.innodb_trx;
  11. select from information_schema.innodb_locks;#正在锁的事务
  12. select from information_schema.innodb_lock_waits;#等待锁的事务
  13. 查看锁日志详细(包含最近的死锁日志)
  14. show engine innodb status \G;
  15. 查看表状态
  16. show table status like 'table_name'\G;
  17. show table status from db_name like 'table_name%'\G;
  18. 查询是否锁表
  19. show OPEN TABLES where In_use > 0;
  20. 数据库引擎
  21. show variables like '%engine%';
  22. 事务隔离级别
  23. select @@global.tx_isolation, @@session.tx_isolation, @@tx_isolation;
  24. 查看gap锁开启状态
  25. show variables like 'innodb_locks_unsafe_for_binlog';
  26. 查看自增锁模式
  27. show variables like 'innodb_autoinc_lock_mode';
  28. 分析sql日志
  29. mysqlbinlog --start-datetime='2018-04-23 00:00:00' --stop-datetime='2018-04-24 00:00:00' /www/log/mysql/bin-file.00009* |grep 'table_name' -B 6 -A 6 >>table_name_bin.sql
  30. 参数解析
  31. Innodb_row_lock_current_waits:当前等待锁的数量
  32. Innodb_row_lock_time:系统启动到现在锁定的总时间长度(重要)
  33. Innodb_row_lock_time_avg:每次等待所花平均时间(重要)
  34. Innodb_row_lock_time_max:系统启动到现在 等待最长的一次所花的时间
  35. Innodb_row_lock_waits:系统启动到现在 总共等待的次数(重要)