ERROR nova.servicegroup.drivers.db DBConnectionError: (pymysql.err.OperationalError) (2013, 'Lost connection to MySQL server during query') (Background on this error at: http://sqlalche.me/e/e3q8)


轉載

原文地址:http://www.myexception.cn/database/2199157.html

 

openstack中數據庫連接數太多--pymysql.err.OperationalError,1040, u'Too many connections'

1.出現問題:

openstack運行過程中出現如下問題:

OperationalError: (pymysql.err.OperationalError) (1040, u'Too many connections')

DBConnectionError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on 'controller' ([Errno 111] ECONNREFUSED)") [SQL: u'SELECT 1']

2.查看 mysql 狀態

 2.1查看 mysql 的最大連接數

mysql> show variables like 'max_connections';
+-----------------+-------+
| Variable_name  | Value  |
+-----------------+-------+
| max_connections | 214   |
+-----------------+-------+
1 row in set (0.00 sec)

2.2 查看服務器響應的最大連接數

mysql> show global status like 'Max_used_connections';
+----------------------+-------+
| Variable_name    | Value     |
+----------------------+-------+
| Max_used_connections | 215   |
+----------------------+-------+
1 row in set (0.00 sec)

對於 mysql 服務器最大連接數值的設置范圍比較理想的是:服務器響應的最大連接數值占服務器上限連接數值的比例值在 10% 以上,如果在 10% 以下,說明 mysql 服務器最大連接上限值設置過高。

Max_used_connections / max_connections * 100%

 

這的服務器響應的最大連接數已經達到了 mysql 的最大連接數的最大值

3.問題分析

3.1 max_connections

MySQL無論如何都會保留一個用於管理員(SUPER)登陸的連接,用於管理員連接數據庫進行維護操作,即使當前連接數已經達到了max_connections。因此MySQL的實際最大可連接數為 max_connections+1

這個參數實際起作用的最大值(實際最大可連接數)為16384,即該參數最大值不能超過16384,即使超過也以16384為准; 增加max_connections參數的值,不會占用太多系統資源。系統資源(CPU、內存)的占用主要取決於查詢的密度、效率等; 該參數設置過小的最明顯特征是出現”Too many connections”錯誤;

3.2 mysql 最大連接數 214 問題

如果我設置連接小於214時,比如 200,那么實際連接數就是 200,也就是說,我的配置文件是沒有問題的。

查 MySQL 官方文檔,里面說了

The maximum number of connections MySQL can support depends on the quality of the thread library on a given platform, the amount of RAM available, how much RAM is used for each connection, the workload from each connection, and the desired response time. Linux or Solaris should be able to support at 500 to 1000 simultaneous connections routinely and as many as 10,000 connections if you have many gigabytes of RAM available and the workload from each is low or the response time target undemanding. Windows is limited to (open tables × 2 + open connections) < 2048 due to the Posix compatibility layer used on that platform.

Increasing open-files-limit may be necessary. Also see Section 2.5, “Installing MySQL on Linux”, for how to raise the operating system limit on how many handles can be used by MySQL.

大概意思是 MySQL 能夠支持的最大連接數量受限於操作系統,必要時可以增大 open-files-limit。換言之,連接數與文件打開數有關。

4 問題解決

更改 MySQL 在 Linux 的最大文件描述符限制,編輯/usr/lib/systemd/system/mariadb.service文件,在文件[Service]下添加:

LimitNOFILE=65535
LimitNPROC=65535

 

保存后,執行下面命令,使配置生效

# systemctl daemon-reload
# systemctl restart  mariadb.service

 

問題解決

MariaDB [(none)]> show variables like 'max_connections';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 4096  |
+-----------------+-------+


免責聲明!

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



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