-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmysql.performance.xml
296 lines (273 loc) · 10.4 KB
/
mysql.performance.xml
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V5.0//EN"
"/usr/share/xml/docbook/schema/dtd/4.5/docbookx.dtd" [
<!ENTITY article.author.xml SYSTEM "../common/article.author.xml">
<!ENTITY book.info.legalnotice.xml SYSTEM "../common/book.info.legalnotice.xml">
<!ENTITY book.info.abstract.xml SYSTEM "../common/book.info.abstract.xml">
]>
<article xml:base="http://netkiller.github.io/journal/"
xmlns="http://docbook.org/ns/docbook" xml:lang="zh-cn">
<articleinfo>
<title>MySQL性能调整</title>
<subtitle></subtitle>
&article.author.xml;
&book.info.legalnotice.xml;
<abstract>
</abstract>
&book.info.abstract.xml;
<keywordset>
<keyword></keyword>
<keyword></keyword>
<keyword></keyword>
<keyword></keyword>
</keywordset>
<pubdate>$Date$</pubdate>
<release>$Id$</release>
</articleinfo>
<section>
<title>系统部分</title>
<para>max_connections</para>
<screen>
<![CDATA[
mysql> show variables like '%connections%';
+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| max_connections | 4096 |
| max_user_connections | 0 |
+----------------------+-------+
2 rows in set (0.00 sec)
]]>
</screen>
<para>connect_timeout</para>
<screen>
<![CDATA[
mysql> show variables like '%timeout%';
+-----------------------------+----------+
| Variable_name | Value |
+-----------------------------+----------+
| connect_timeout | 10 |
| delayed_insert_timeout | 300 |
| innodb_flush_log_at_timeout | 1 |
| innodb_lock_wait_timeout | 50 |
| innodb_rollback_on_timeout | OFF |
| interactive_timeout | 30 |
| lock_wait_timeout | 31536000 |
| net_read_timeout | 30 |
| net_write_timeout | 60 |
| rpl_stop_slave_timeout | 31536000 |
| slave_net_timeout | 3600 |
| wait_timeout | 30 |
+-----------------------------+----------+
12 rows in set (0.00 sec)
]]>
</screen>
<section>
<title>关于 Sleep 进程</title>
<screen>
<![CDATA[
mysql> show processlist;
+----+------+-----------------+-----------------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------------+-----------------+---------+------+-------+------------------+
| 5 | app | 127.0.0.1:35551 | app.example.com | Sleep | 4444 | | NULL |
| 6 | app | 127.0.0.1:35552 | app.example.com | Sleep | 4444 | | NULL |
| 7 | app | 127.0.0.1:35554 | app.example.com | Sleep | 4444 | | NULL |
| 8 | app | 127.0.0.1:35556 | app.example.com | Sleep | 4444 | | NULL |
| 9 | app | 127.0.0.1:35555 | app.example.com | Sleep | 4444 | | NULL |
| 10 | app | 127.0.0.1:35557 | app.example.com | Sleep | 4444 | | NULL |
| 11 | app | 127.0.0.1:35558 | app.example.com | Sleep | 4444 | | NULL |
| 12 | app | 127.0.0.1:35559 | app.example.com | Sleep | 4444 | | NULL |
| 13 | app | 127.0.0.1:35560 | app.example.com | Sleep | 4444 | | NULL |
| 14 | app | 127.0.0.1:35561 | app.example.com | Sleep | 35 | | NULL |
| 15 | app | 127.0.0.1:35562 | app.example.com | Sleep | 4439 | | NULL |
| 16 | app | 127.0.0.1:35563 | app.example.com | Sleep | 4439 | | NULL |
| 17 | app | 127.0.0.1:35565 | app.example.com | Sleep | 4439 | | NULL |
| 18 | app | 127.0.0.1:35566 | app.example.com | Sleep | 4439 | | NULL |
| 19 | app | 127.0.0.1:35567 | app.example.com | Sleep | 4439 | | NULL |
| 20 | app | 127.0.0.1:35568 | app.example.com | Sleep | 4439 | | NULL |
| 21 | app | 127.0.0.1:35569 | app.example.com | Sleep | 4439 | | NULL |
| 22 | app | 127.0.0.1:35570 | app.example.com | Sleep | 4439 | | NULL |
| 23 | app | 127.0.0.1:35571 | app.example.com | Sleep | 4439 | | NULL |
| 24 | app | 127.0.0.1:35572 | app.example.com | Sleep | 38 | | NULL |
| 25 | app | 127.0.0.1:35575 | app.example.com | Sleep | 4432 | | NULL |
| 26 | app | 127.0.0.1:35576 | app.example.com | Sleep | 4432 | | NULL |
| 27 | app | 127.0.0.1:35578 | app.example.com | Sleep | 4432 | | NULL |
| 28 | app | 127.0.0.1:35579 | app.example.com | Sleep | 4432 | | NULL |
| 29 | app | 127.0.0.1:35580 | app.example.com | Sleep | 4432 | | NULL |
| 30 | app | 127.0.0.1:54330 | app.example.com | Sleep | 10 | | NULL |
| 31 | app | 127.0.0.1:1081 | app.example.com | Query | 0 | init | show processlist |
+----+------+-----------------+-----------------+---------+------+-------+------------------+
27 rows in set (0.09 sec)
mysql>
]]>
</screen>
<screen>
show variables where variable_name in ('wait_timeout','interactive_timeout')
</screen>
<para>与Sleep线程有关的配置 'wait_timeout'与'interactive_timeout'默认是8小时,也就是说Sleep 8小时才会关闭,如果你的服务器访问量很低,可以适当加快线程回收。</para>
<screen>
<![CDATA[
mysql> show variables where variable_name in ('wait_timeout','interactive_timeo
ut');
+---------------------+-------+
| Variable_name | Value |
+---------------------+-------+
| interactive_timeout | 28800 |
| wait_timeout | 28800 |
+---------------------+-------+
2 rows in set (0.47 sec)
]]>
</screen>
<para>另外PHP与Java两个群体在数据库处理上不同。PHP主张块连接快释放,让出更多的资源给其他服务。而Java多采用连接池独占数据库资源,连接池设置多少连接数就对应数据库创建多少持久连接,所以你会看到你并没有访问数据库但show processlist;会看到很多Sleep连接。</para>
<para>两个群起所采用的方式,并无好坏,都能达到最终目的。但使用连接池需要注意当MySQL最大连接数等于连接池连接数时来自其他服务器的请求将无法连接。</para>
</section>
</section>
<section>
<title>线程部分</title>
<screen>
<![CDATA[
mysql> show variables like '%thread_handling%';
+-----------------+---------------------------+
| Variable_name | Value |
+-----------------+---------------------------+
| thread_handling | one-thread-per-connection |
+-----------------+---------------------------+
1 row in set (0.01 sec)
mysql> show status like 'thread%';
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| Threads_cached | 16 |
| Threads_connected | 8 |
| Threads_created | 24 |
| Threads_running | 2 |
+-------------------+-------+
4 rows in set (0.00 sec)
thread_cache_hit=(connections-thread_created)/connections*100%
开启 pool-of-threads 模式
]]>
</screen>
</section>
<section>
<title>查询缓存部分</title>
<para>查看缓存设置</para>
<screen>
<![CDATA[
mysql> show variables like '%query_cache%';
+------------------------------+----------+
| Variable_name | Value |
+------------------------------+----------+
| have_query_cache | YES |
| query_cache_limit | 1048576 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 16777216 |
| query_cache_type | OFF |
| query_cache_wlock_invalidate | OFF |
+------------------------------+----------+
6 rows in set (0.01 sec)
]]>
</screen>
<para>查看缓存状态</para>
<screen>
<![CDATA[
mysql> show status like '%Qcache%';
+-------------------------+----------+
| Variable_name | Value |
+-------------------------+----------+
| Qcache_free_blocks | 1 |
| Qcache_free_memory | 16759680 |
| Qcache_hits | 0 |
| Qcache_inserts | 0 |
| Qcache_lowmem_prunes | 0 |
| Qcache_not_cached | 192 |
| Qcache_queries_in_cache | 0 |
| Qcache_total_blocks | 1 |
+-------------------------+----------+
8 rows in set (0.00 sec)
]]>
</screen>
</section>
<section>
<title>缓冲区部分</title>
<screen>
<![CDATA[
mysql> show variables like '%sort_buffer_size%';
+-------------------------+---------+
| Variable_name | Value |
+-------------------------+---------+
| innodb_sort_buffer_size | 1048576 |
| myisam_sort_buffer_size | 8388608 |
| sort_buffer_size | 262144 |
+-------------------------+---------+
3 rows in set (0.00 sec)
]]>
</screen>
</section>
<section>
<title>部分</title>
<screen>
<![CDATA[
mysql> show variables like '%low_priority_updates%';
+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| low_priority_updates | OFF |
+----------------------+-------+
1 row in set (0.00 sec)
mysql> show variables like 'low_priority_updates';
+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| low_priority_updates | OFF |
+----------------------+-------+
1 row in set (0.01 sec)
mysql> set global low_priority_updates=ON;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
]]>
</screen>
<para>global变量需要退出重新进入才能生效。</para>
<screen>
<![CDATA[
mysql> show variables like 'low_priority_updates';
+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| low_priority_updates | ON |
+----------------------+-------+
1 row in set (0.01 sec)
]]>
</screen>
<para>SESSION 会立即生效</para>
<screen>
<![CDATA[
mysql> set session low_priority_updates=ON;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'low_priority_updates';
+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| low_priority_updates | ON |
+----------------------+-------+
1 row in set (0.00 sec)
]]>
</screen>
</section>
<section>
<title>默认编码设置</title>
<screen>
<![CDATA[
character-set-server=utf8
collation_server=utf8_general_ci
]]>
</screen>
</section>
<section>
<title>SECTION</title>
<screen>
<![CDATA[
]]>
</screen>
</section>
</article>