优化MySQL Server:
查看MySQL Server参数:
-- 查看变量
mysql> SHOW VARIABLES LIKE 'innodb_buffer%';
+-------------------------------------+----------------+
| Variable_name | Value |
+-------------------------------------+----------------+
| innodb_buffer_pool_dump_at_shutdown | OFF |
| innodb_buffer_pool_dump_now | OFF |
| innodb_buffer_pool_filename | ib_buffer_pool |
| innodb_buffer_pool_instances | 8 |
| innodb_buffer_pool_load_abort | OFF |
| innodb_buffer_pool_load_at_startup | OFF |
| innodb_buffer_pool_load_now | OFF |
| innodb_buffer_pool_size | 134217728 |
+-------------------------------------+----------------+
8 rows in set (0.00 sec)
-- 查看状态
mysql> SHOW STATUS LIKE 'Innodb_buffer%';
+---------------------------------------+-------------+
| Variable_name | Value |
+---------------------------------------+-------------+
| Innodb_buffer_pool_dump_status | not started |
| Innodb_buffer_pool_load_status | not started |
| Innodb_buffer_pool_pages_data | 973 |
| Innodb_buffer_pool_bytes_data | 15941632 |
| Innodb_buffer_pool_pages_dirty | 0 |
| Innodb_buffer_pool_bytes_dirty | 0 |
| Innodb_buffer_pool_pages_flushed | 3131 |
| Innodb_buffer_pool_pages_free | 7216 |
| Innodb_buffer_pool_pages_misc | 2 |
| Innodb_buffer_pool_pages_total | 8191 |
| Innodb_buffer_pool_read_ahead_rnd | 0 |
| Innodb_buffer_pool_read_ahead | 0 |
| Innodb_buffer_pool_read_ahead_evicted | 0 |
| Innodb_buffer_pool_read_requests | 52173 |
| Innodb_buffer_pool_reads | 600 |
| Innodb_buffer_pool_wait_free | 0 |
| Innodb_buffer_pool_write_requests | 17154 |
+---------------------------------------+-------------+
-- 你也可以直接使用mysqladmin来查看
mysqladmin -u -p VARIABLES | grep innodb_buffer; 查看帮助文档:
-- 所有参数
mysqld --verbose --help|more
-- 某个参数
mysqld --verbose --help| grep character-set-server
2014-09-03 22:38:36 5595 [Warning] Setting lower_case_table_names=2 because file system for /usr/local/var/mysql/ is case insensitive
2014-09-03 22:38:36 5595 [Note] Plugin 'FEDERATED' is disabled.
-C, --character-set-server=name
character-set-server utf8
影响MySQL性能的重要参数:
key_buffer_size的设置:
mysqld --verbose --help|grep key-buffer-size
2014-09-03 22:44:44 5631 [Warning] Setting lower_case_table_names=2 because file system for /usr/local/var/mysql/ is case insensitive
2014-09-03 22:44:44 5631 [Note] Plugin 'FEDERATED' is disabled.
--key-buffer-size=# The size of the buffer used for index blocks for MyISAM
key-buffer-size 8388608
即设置索引块的缓存大小,我们可以自己设置索引块:
--添加索引块mycache
SET GLOBAL mycache.key_buffer_size=10240;
--删除索引块mycache
SET GLOBAL mycache.key_buffer_size=0;
但上面的设置在mysql重启后会失效,我们可以配置my.cnf来初始化缓存块:
key_buffer_size=4G
mycache.key_buffer_size=2G
init_file=/path/to/mysql_init.sql
-- mysql_init.sql可以为
CACHE INDEX db1.table1, db1.table2, db2.table3 IN mycache;
table_cache的设置:
mysql> show status like 'Open%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| Open_files | 18 |
| Open_streams | 0 |
| Open_table_definitions | 153 |
| Open_tables | 146 |
| Opened_files | 7925 |
| Opened_table_definitions | 0 |
| Opened_tables | 0 |
+--------------------------+-------+
innodb_buffer_pool_size:
innodb存储引擎使用的表数据和索引数据的最大内存缓存大小,默认128M,该值越大,磁盘I/O越小。
在专用服务器上,该值可设置为物理内存的80%,但这需要考虑系统中物理内存调度的问题。
innodb_flush_log_at_trx_commit:
控制缓冲区的数据写入到日志文件及日志文件数据刷新到磁盘的操作时机。
该参数的值可为0,1,2:
0:日志缓冲每秒一次地被写到日志文件,并且对日志文件做向磁盘刷新的操作,但在一个事务提交后不做任何事情。
1():每个事务提交时,日志缓冲被写到日志文件,并且执行日志文件到磁盘刷新的操作。
2:每个事务提交时,日志缓冲被写到日志文件,但不执行日志文件到磁盘刷新的操作,对日志文件每秒向磁盘做一次刷新操作。
innodb_additional_mem_pool_size:
用于存储数据库结构和内部数据结构的内存池大小,默认为8M,该参数在5.6中被废弃,在5.7中移除。
innodb_lock_wait_timeout:
等待锁的超时时间,默认为50秒。
innodb_support_xa:
是否支持分布式事务,默认支持。
innodb_log_buffer_size:
日志缓存大小,默认为8M。
innodb_log_file_size:
日志文件大小,每个日志组中的日志文件大小,默认值为48M。
具体详情可参考:
不吝指正。