mysql調優最大連接數


一、mysql調優

1.1 報錯:

Mysql: error 1040: Too many connections

1.2 原因:

1、訪問量過高,MySQL服務器抗不住,這個時候就要考慮增加從服務器分散讀壓力。
2、MySQL配置文件中max_connections值過小,默認151。

1.3 最優計算方法:

服務器響應的最大連接數值占服務器上限連接數值的比例值在10%以上,如果在10%以下,說明mysql服務器最大連接上限值設置過高。

Max_used_connections / max_connections * 100% = 2/151 *100% ≈ 1%

1.4 法一,臨時生效方法

#默認最大連接數
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) 備注: 1、max_connections :是對整個服務器的用戶限制,整個服務器只能開這么多session,而不考慮用戶。這個參數實際起作用的最大值(實際最大可連接數)為16384
2、max_user_connections:限制每個用戶的session連接個數,例如max_user_connections=1 ,那么用戶u1只能連接的session數為1,如果還有用戶u2,還是可以連接,但是連接數仍然為1。 

 

1.5 法二:永久生效方法

[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   #調優最大連接數報錯

#重啟服務

/etc/init.d/mysqld restart


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM