MySQL 常见问题整理
从节点落后主节点很大
show slave status\G;
中 Seconds_Behind_Source 的值很大,mysql 占用磁盘 IO 过高,可以优化
# sync_binlog = 1 表示每次事务提交都会将binlog的缓存写入磁盘,优化设置为 1000
mysql> show variables like '%sync_binlog%'
mysql> set global sync_binlog=1000;
# 设置 innodb_flush_log_at_trx_commit MySql 日志何时写入硬盘
mysql> show variables like '%innodb_flush_log%';
+--------------------------------+-------+
| Variable_name | Value |
+--------------------------------+-------+
| innodb_flush_log_at_timeout | 1 |
| innodb_flush_log_at_trx_commit | 1 |
+--------------------------------+-------+
rows in set (0.00 sec)
mysql> set global innodb_flush_log_at_trx_commit=2;
Query OK, 0 rows affected (0.00 sec)
innodb_flush_log_at_trx_commit
参数的值
0
log buffer 将每秒一次地写入 log file 中,并且 log file 的 flush(刷到磁盘)操作同时进行
- 该模式下在事务提交的时候,不会主动触发写入磁盘的操作
1
每次事务提交,都会将 innodb 日志缓存写入磁盘,此时对磁盘效率影响很大
2
每次事务提交时 mysql 都会把 log buffer 的数据写入 log file,flush(刷到磁盘)操作并不会同时进行,MySQL 会每秒执行一次 flush(刷到磁盘)操作
Error 1005 (HY000): Can’t create table ‘#sql-1_1a43f93’ (errno: 28)
Error 1114 (HY000): The table ’’ is full
Error 1366 (HY000): Incorrect string value: ‘\xFC\x00\x00\x00\x00\x00…’ for column
Error 2006: MySQL server has gone away
[mysqld]
wait_timeout=90000
docker rootless cannot access ‘/var/lib/mysql-files’: Permission denied
> show global variables like '%secure_file_priv%';
# 启动时,使用 --secure-file-priv="/var/lib/mysql-files" 参数指定
> show global variables like '%keyring_file_data%';
+-------------------+--------------------------------+
| Variable_name | Value |
+-------------------+--------------------------------+
| keyring_file_data | /var/lib/mysql-keyring/keyring |
+-------------------+--------------------------------+
1 row in set (0.00 sec)
[mysqld]
keyring_encrypted_file_data = /var/lib/mysql-keyring
Package for query is too large (xxxxxxxx > 4194304). You can change this value on the server by setting the max_allowed_package variable
show VARIABLES like '%max_allowed_packet%';
# 100M
set global max_allowed_packet = 100*1024*1024;
[mysqld]
# max_allowed_packet=100*1024*1024
max_allowed_packet=100M
ibdata1 文件过大问题
[mysqld]
innodb_file_per_table=1
MySQL 日志归档
sudo chmod -R 755 /var/log/mysql/
/etc/logrotate.d/mysql-log
/var/log/mysql/*.log {
daily
rotate 7
missingok
compress
delaycompress
notifempty
create 644 mysql mysql # create 644 999 999
sharedscripts
postrotate
# mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -e "FLUSH ERROR LOGS; FLUSH GENERAL LOGS; FLUSH SLOW LOGS; FLUSH BINARY LOGS;"
# docker exec -it <container-name> mysqladmin -p\${MYSQL_ROOT_PASSWROD} flush-logs
/usr/bin/mysqladmin flush-logs
endscript
}
测试
logrotate -vf /etc/logrotate.d/mysql-log
如果是 docker 容器中,mysqladmin
可以使用命令
docker exec -it xxx mysqladmin -p\${MYSQL_ROOT_PASSWROD} xxx