这篇文章在前文对缓存调优参数说明的基础上,根据一个具体的设定示例给出查询和设定的命令。
确认系统当前设定
- 使用如下命令确认系统的当前状态
show variables where variable_name in ('tmp_table_size', \
'max_heap_table_size',\
'back_log',\
'read_buffer_size',\
'read_rnd_buffer_size',\
'thread_cache_size',\
'query_cache_type',\
'query_cache_size',\
'wait_timeout',\
'join_buffer_size',\
'sort_buffer_size');
* 执行示例
```sql
mysql> show variables where variable_name in ('tmp_table_size', \
-> 'max_heap_table_size',\
-> 'back_log',\
-> 'read_buffer_size',\
-> 'read_rnd_buffer_size',\
-> 'thread_cache_size',\
-> 'query_cache_type',\
-> 'query_cache_size',\
-> 'wait_timeout',\
-> 'join_buffer_size',\
-> 'sort_buffer_size');
+----------------------+----------+
| Variable_name | Value |
+----------------------+----------+
| back_log | 80 |
| join_buffer_size | 262144 |
| max_heap_table_size | 16777216 |
| query_cache_size | 1048576 |
| query_cache_type | OFF |
| read_buffer_size | 131072 |
| read_rnd_buffer_size | 262144 |
| sort_buffer_size | 262144 |
| thread_cache_size | 9 |
| tmp_table_size | 16777216 |
| wait_timeout | 28800 |
+----------------------+----------+
11 rows in set (0.00 sec)
mysql>
- 修改配置文件
- 官方的mysql镜像配置文件:/etc/mysql/mysql.conf.d/mysqld.cnf
- 建议设定示例(需根据相关性能需求与机器状况进行调整)
设定建议
为了能够持久化的保持,而不至于重启或者容器重新生成后恢复默认状态,需要设定到配置文件中。
# cache settings
tmp_table_size=256M
max_heap_table_size=128M
key_buffer_size=128M
back_log=256
read_buffer_size=1M
read_rnd_buffer_size=1M
thread_cache_size=64
query_cache_type=1
query_cache_size=128M
wait_timeout=120
join_buffer_size=1M
sort_buffer_size=1M
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
设定示例
由于官方镜像中没有vi,所以可以使用cat + Heredocument直接追加到设定文件。
# cat <<EOF >>/etc/mysql/mysql.conf.d/mysqld.cnf
> # cache settings
tmp_table_size=256M
max_heap_table_size=128M
key_buffer_size=128M
back_log=256
read_buffer_size=1M
read_rnd_buffer_size=1M
thread_cache_size=64
query_cache_type=1
query_cache_size=128M
wait_timeout=120
join_buffer_size=1M
sort_buffer_size=1M> > > > > > > > > > > >
> EOF
#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 重新启动mysql服务,再次确认设定
mysql> show variables where variable_name in ('tmp_table_size', \
-> 'max_heap_table_size',\
-> 'back_log',\
-> 'read_buffer_size',\
-> 'read_rnd_buffer_size',\
-> 'thread_cache_size',\
-> 'query_cache_type',\
-> 'query_cache_size',\
-> 'wait_timeout',\
-> 'join_buffer_size',\
-> 'sort_buffer_size');
+----------------------+-----------+
| Variable_name | Value |
+----------------------+-----------+
| back_log | 256 |
| join_buffer_size | 1048576 |
| max_heap_table_size | 134217728 |
| query_cache_size | 134217728 |
| query_cache_type | ON |
| read_buffer_size | 1048576 |
| read_rnd_buffer_size | 1048576 |
| sort_buffer_size | 1048576 |
| thread_cache_size | 64 |
| tmp_table_size | 268435456 |
| wait_timeout | 120 |
+----------------------+-----------+
11 rows in set (0.00 sec)
mysql>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 设定总结
设定参数 | 修改前 | 修改后 |
---|---|---|
tmp_table_size | 16M | 256M |
max_heap_table_size | 16M | 128M |
back_log | 80 | 256 |
read_buffer_size | 128K | 1M |
read_rnd_buffer_size | 256K | 1M |
thread_cache_size | 9 | 64 |
query_cache_type | ON | OFF |
query_cache_size | 1M | 128M |
wait_timeout | 28800 | 120 |
join_buffer_size | 256K | 1M |
sort_buffer_size | 256K | 1M |
注:后续会对此设定进行跟踪。