你的服务器是不是经常出现这样的错误呢?可能是过一两天出现一次,也可能是一两个小时出现一次。
, p( r/ h+ `) o
! x: j- n r5 ~8 ~5 M错误信息如下:
# E3 |/ N! M; F1 KCan not connect to MySQL server
: n k0 x+ i% p* `" {
Error: Too many connections
3 L6 \+ I* `: ]) i2 o
Errno.: 1040
7 S Q3 h2 c% G$ i
8 \% x7 ~* S$ d. b$ KSimilar error report has beed dispatched to administrator before.
1 a, ~3 O$ j& Q$ u! h
! i& B: X0 o1 R3 t
- k$ X: o" A& Q0 Y9 O! B- Y. d! r/ c, J
从官方文档知道linux上面编译安装的mysql默认的连接为100个,这样对于网站的需求来说是远远不够的。
" ^# m* f1 W" h! Z+ s
文档:
http://dev.mysql.com/doc/refman/5.0/en/too-many-connections.html! z7 I8 U( S0 V0 }% H
3 s$ J) M1 E4 Q1 o- f, T! U
If you get a Too many connections error when you try to connect to the mysqld server, this means that all available connections are in use by other clients.
9 `" p5 {( D o/ w9 o7 O7 j3 y# ?9 Z! F5 e* W
The number of connections allowed is controlled by the max_connections system variable. Its default value is 100. If you need to support more connections, you should restart mysqld with a larger value for this variable.
- D0 Z: b* D- h3 A9 l2 g
b6 _2 D; l+ T9 m) x
mysql官方告诉我们需要修改max_connections的值,那么我们怎么去修改呢?有两种方法
8 T( }4 v. E7 Q5 n# O; d3 `- N4 V
1、修改配置文件文件
* T d$ V+ N: Z8 q
修改/etc/my.cnf这个文件,在[mysqld]中新增max_connections=N,如果你没有这个文件请从编译源码中的support-files文件夹中复制你所需要的*.cnf文件为到/etc/my.cnf。我使用的是my-medium.cnf,中型服务器配置。例如我的[mysqld]的内容如下
: L9 B) c, j$ |1 y9 s' b; {4 h5 w
7 M7 c1 y `" Y/ h8 k[mysqld]
7 [3 n) F3 @! D5 N9 Z! N
port = 3306
! e7 _+ p9 }4 |8 @* y
socket = /tmp/mysql.sock
! u+ U3 W1 x* C1 j1 d
skip-locking
8 \. J7 c& }( Z' U' t
key_buffer = 160M
& x$ O( O/ Q1 J8 ?% v
max_allowed_packet = 1M
( {. a" J; c; ^8 F% S F" ftable_cache = 64
( L: b6 }% ^3 X" B, n3 Gsort_buffer_size = 512K
- e" Z& v! t/ u/ Znet_buffer_length = 8K
3 Q; d4 `( E# B: T b
read_buffer_size = 256K
`, j3 `1 F2 f/ U) Q
read_rnd_buffer_size = 512K
0 [$ q* G0 C) q0 bmyisam_sort_buffer_size = 8M
/ B V T, M3 t8 V
max_connections=1000
- m& w# o9 i/ n* }: Z
* b Z# V4 B" ]
由于对mysql还不是很熟悉,所以很多参数没有修改。哈哈。。
# G7 ~( D. v- b1 D5 I, f: R% U
4 h0 i( l0 [% w" B; f2、非使用mysqld脚本自动启动的用户。
( T. F) q! z; D+ _0 ~) E- E
修改$MYSQL_HOME/bin/mysqld_safe文件
( }" n& N; Y. E: L
例如:/usr/local/mysql/bin/mysqld_safe这个文件
5 t5 p) [% G& O, ~grep -n 'max_connection' $MYSQL_HOME/bin/mysqld_safe
0 r, C, k+ z% N
修改对应行号的max_connections参数值