MySQL优化建议
一、系统优化
1、IO schedule #推荐使用deadline/noop
cat /sys/block/sda/queue/scheduler #查看方式
echo noop > /sys/block/hda/queue/scheduler #修改方式
2、操作系统 #推荐使用xfs、ext4
3、kernel #修改/etc/sysctl.conf
vm.swappiness = 0 #最大限度使用物理内存,尽量不适用swap
vm.dirty_backgroup_ratio = 10 #脏数据占内存的10%刷新数据到磁盘
vm.dirty_ratio = 20 #占20%时,暂时停止其他IO操作,强制刷磁盘
net.ipv4.tcp_syncookies = 1 表示开启SYN Cookies。当出现SYN等待队列溢出时,启用cookies来处理,可防范少量SYN攻击,默认为0,表示关闭;
net.ipv4.tcp_tw_reuse = 1 表示开启重用。允许将TIME-WAIT sockets重新用于新的TCP连接,默认为0,表示关闭;
net.ipv4.tcp_tw_recycle = 1 表示开启TCP连接中TIME-WAIT sockets的快速回收,默认为0,表示关闭。
二、MySQL参数优化
1、query cache type, #建议关闭QC
如果数据表被更改,那么和这个数据表相关的全部Cache全部都会无效,并删除之
这里“数据表更改”包括: INSERT, UPDATE,DELETE, TRUNCATE, ALTER TABLE, DROP TABLE, or DROP DATABASE等
2、table_open_cache #建议增大
mysql >show global status like 'open%tables%';
+---------------+--------+
| Variable_name | Value |
+---------------+--------+
| Open_tables | 1536 | #当前打开的表的数量
| Opened_tables | 219289 | #打开过的表的数量
+---------------+--------+
Opened_tables
The number of tables that have been opened. If Opened_tables is big, your table_open_cache value is probably too small.
3、innodb_buffer_pool_size #建议增大
目前大小是2G,增加buffer pool大小,缓存尽量多的数据在内存中
目前常用的Innodb表(除去大表)在4G左右,建议设置4G
4、thread_cache_size #建议增大
>show variables like '%thread_ca%';
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| thread_cache_size | 64 | #目前是64
+-------------------+-------+
mysql >show global status like '%threads%c%';
+-------------------+--------+
| Variable_name | Value |
+-------------------+--------+
| Threads_cached | 63 |
| Threads_connected | 8 |
| Threads_created | 159040 | #表示创建过的连接
+-------------------+--------+
Threads_created
The number of threads created to handle connections. If Threads_created is big, you may want to increase the thread_cache_size value.
5、innodb_flush_method #建议修改为O_DRIECT
>show variables like 'innodb_flush_m%';
+---------------------+-------+
| Variable_name | Value |
+---------------------+-------+
| innodb_flush_method | |
+---------------------+-------+
说明:
Defines the method used to flush data to InnoDB data files and log files, which can affect I/O throughput.
If innodb_flush_method is set to NULL on a Unix-like system, the fsync option is used by default.
fsync: InnoDB uses the fsync() system call to flush both the data and log files. fsync is the default setting.
O_DIRECT: InnoDB uses O_DIRECT (or directio() on Solaris) to open the data files, and uses fsync() to flush both the data and log files. This option is available on some GNU/Linux versions, FreeBSD, and Solaris.
建议使用: O_DIRECT, 这样减少操作系统级别VFS的缓存使用内存过多和Innodb本身的buffer的缓存冲突,同时也算是给操作系统减少点压力。