1、mysql調優mysql
1.1 報錯:sql
Mysql: error 1040: Too many connectionsbash
1.2 緣由:服務器
一、訪問量太高,MySQL服務器抗不住,這個時候就要考慮增長從服務器分散讀壓力。
二、MySQL配置文件中max_connections值太小,默認151。
1.3 最優計算方法:session
服務器響應的最大鏈接數值佔服務器上限鏈接數值的比例值在10%以上,若是在10%如下,說明mysql服務器最大鏈接上限值設置太高。socket
Max_used_connections / max_connections * 100% = 2/151 *100% ≈ 1%
#默認最大鏈接數 mysql> show variables like '%max_connections%'; +-----------------+-------+ | Variable_name | Value | +-----------------+-------+ | max_connections | 151 | +-----------------+-------+ 1 row in set (0.00 sec) #調整最大鏈接數 mysql> set global max_connections = 1000; Query OK, 0 rows affected (0.00 sec) #查看最大鏈接數 mysql> show variables like '%max_connections%'; +-----------------+-------+ | Variable_name | Value | +-----------------+-------+ | max_connections | 1000 | +-----------------+-------+ 1 row in set (0.00 sec)
#限制每一個用戶session鏈接數 mysql> show global status like 'Max_used_connections'; +----------------------+-------+ | Variable_name | Value | +----------------------+-------+ | Max_used_connections | 152 | +----------------------+-------+ 1 row in set (0.00 sec) 備註: 一、max_connections :是對整個服務器的用戶限制,整個服務器只能開這麼多session,而不考慮用戶。這個參數實際起做用的最大值(實際最大可鏈接數)爲16384
二、max_user_connections:限制每一個用戶的session鏈接個數,例如max_user_connections=1 ,那麼用戶u1只能鏈接的session數爲1,若是還有用戶u2,仍是能夠鏈接,可是鏈接數仍然爲1。
1.5 法二:永久生效方法code
[root@a1 mysql]# egrep -v "#|^$" my.cnf [mysqld_safe] err-log=/var/log/mysqld.log pid-file=/var/lib/mysql/mysqld.pid default-character-set = utf8 [mysqld] port = 3306 socket = /tmp/mysql.sock innodb_buffer_pool_size=2GB #調優建立事務報錯 basedir = /roobo/server/mysql datadir = /roobo/mysqldata/database pid-file = /var/lib/mysql/mysql.pid character_set_server = utf8 log-bin = /roobo/server/mysql/data/mysql-bin server_id = 1 log-error=/var/log/mysqld.log max_connections=1000 #調優最大鏈接數報錯
#重啓服務server
/etc/init.d/mysqld restart