錯提示如下
#/home/binbin.zhengbb/ssh/update_dns.sh
ERROR 1040 (08004): Too many connections
ERROR 1040 (08004): Too many connections
出現此錯誤的原因,一種是訪問量確實很高,MySQL伺服器頂不住,這個時候就要考慮增加從伺服器分散讀壓力,另外一種情況是MySQL設定檔中max_connections值過小。
查詢MySQL的最大串連數:
| 代碼如下 |
複製代碼 |
mysql> show variables like 'max_connections'; +-----------------+-------+ | Variable_name | Value | +-----------------+-------+ | max_connections | 100 | +-----------------+-------+ 1 row in set (0.00 sec) |
查詢MySQL響應的最大串連數:
| 代碼如下 |
複製代碼 |
mysql> show global status like 'max_used_connections'; +----------------------+-------+ | Variable_name | Value | +----------------------+-------+ | Max_used_connections | 5 | +----------------------+-------+ 1 row in set (0.00 sec) |
說明:本地環境沒什麼參考價值,但是就上面的資料而言,MySQL過去所響應的最大串連數小於其允許的最大串連數,所以不會出現1040錯誤。
MySQL比較理想的最大串連數計算方式為:
max_used_connections / max_connections * 100% ≈ 85%
即最大串連數占上限串連數的85%左右,如果發現比例在10%以下,MySQL伺服器串連數上限設定的過高了。
方法一:直接修改mysql
| 代碼如下 |
複製代碼 |
mysql> show variables; | max_connections | 100 mysql> set GLOBAL max_connections=1500; |
方法二:修改設定檔
| 代碼如下 |
複製代碼 |
[Intranet root@inc-dp-149-47 /root] #vi /etc/my.cnf [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql # Default to using old password format for compatibility with mysql 3.x # clients (those using the mysqlclient10 compatibility package). old_passwords=1 log-bin=/var/lib/mysql/mysql_bin_log/log-bin expire_logs_days=7 log-slow-queries=/var/log/mysqld_slow_query.log set-variable=max_connections=1500 [mysqld_safe] log-error=/var/log/mysqld.log #log-update=/var/log/mysqld_update.log pid-file=/var/run/mysqld/mysqld.pid |
最後重啟我們的mysql資料庫伺服器就可以了。