mysql> SHOW VARIABLES LIKE 'read_%_size';
+----------------------+--------+
| Variable_name | Value |
+----------------------+--------+
| read_buffer_size | 131072 |
| read_rnd_buffer_size | 262144 |
+----------------------+--------+
2 rows in set (0.00 sec)
步骤三:MySQL线程重用和开表控制
分析“已打开表的数量/当前可缓存表的数量”,比值不超过95%就基本正常。
1)查看当前已打开、一共打开过多少个表
mysql> SHOW GLOBAL STATUS LIKE 'open%tables';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Open_tables | 23 |
| Opened_tables | 72 |
+---------------+-------+
2 rows in set (0.01 sec)
2)查看当前可缓存多少个打开的表
mysql> SHOW VARIABLES LIKE 'table_open_cache';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| table_open_cache | 2000 |
+------------------+-------+
1 row in set (0.00 sec)
步骤四:MySQL调整示例:记录慢查询
1)调整my.cnf配置文件,启用慢查询
[root@dbsvr1 ~]# vim /etc/my.cnf
[mysqld]
.. ..
slow_query_log=1 //启用慢查询
slow_query_log_file=mysql-slow.log //制定慢查询日志文件
long_query_time=5 //查询耗时超过5秒才记录
log_queries_not_using_indexes=1 //记录未使用索引的查询
[root@dbsvr1 ~]# service mysql restart
Shutting down MySQL..... [确定]
Starting MySQL.... [确定]
2)查看慢查询日志(mysqldumpslow工具)
[root@dbsvr1 ~]# mysqldumpslow /var/lib/mysql/mysql-slow.log
Reading mysql slow query log from /var/lib/mysql/mysql-slow.log
Count: 1 Time=0.00s (0s) Lock=0.00s (0s) Rows=0.0 (0), 0users@0hosts
.. ..
3)了解与查询相关的缓存选项
查看当前的查询缓存大小:
mysql> SHOW VARIABLES LIKE 'query_cache%';
+------------------------------+---------+
| Variable_name | Value |
+------------------------------+---------+
| query_cache_limit | 1048576 | //超过此大小则不再缓存
| query_cache_min_res_unit | 4096 |
| query_cache_size | 1048576 | //缓存空间的大小
| query_cache_type | OFF |
| query_cache_wlock_invalidate | OFF |
+------------------------------+---------+
5 rows in set (0.00 sec)
查看当前的查询缓存统计数据:
mysql> SHOW GLOBAL STATUS LIKE 'qcache%';
+-------------------------+---------+
| Variable_name | Value |
+-------------------------+---------+
| Qcache_free_blocks | 1 |
| Qcache_free_memory | 1031368 | //缓存中的空闲内存
| Qcache_hits | 0 |
| Qcache_inserts | 0 |
| Qcache_lowmem_prunes | 0 |
| Qcache_not_cached | 100 | //不适合缓存的数量
| Qcache_queries_in_cache | 0 |
| Qcache_total_blocks | 1 |
+-------------------------+---------+
8 rows in set (0.00 sec)
步骤五:关于MySQL状态和相关变量的查看
1)查看服务器的相关状态值(运行中动态变化)
使用SHOW GLOBAL STATUS语句,可结合LIKE条件做模糊过滤。
默认有400多个状态值:
mysql> SHOW GLOBAL STATUS\G
*************************** 1. row ***************************
Variable_name: Aborted_clients
Value: 0
*************************** 2. row ***************************
Variable_name: Aborted_connects
Value: 0
*************************** 3. row ***************************
Variable_name: Binlog_cache_disk_use
Value: 0
*************************** 4. row ***************************
Variable_name: Binlog_cache_use
Value: 0
*************************** 5. row ***************************
Variable_name: Binlog_stmt_cache_disk_use
Value: 0
.. .. //省略中间的大量状态值
.. ..
*************************** 435. row ***************************
Variable_name: Threads_connected
Value: 1
*************************** 436. row ***************************
Variable_name: Threads_created
Value: 1
*************************** 437. row ***************************
Variable_name: Threads_running
Value: 1
*************************** 438. row ***************************
Variable_name: Uptime
Value: 5322
*************************** 439. row ***************************
Variable_name: Uptime_since_flush_status
Value: 2283
439 rows in set (0.00 sec)
2)查看服务器的运行选项(一般为静态限制,可通过my.cnf文件配置,或SET修改)
使用SHOW VARIABLES语句,也可结合LIKE条件做模糊过滤。
默认也有400多个(接近500个)配置选项: