MySQL Study之--“too many connections”錯誤解決方案

來源:互聯網
上載者:User

標籤:mysql

MySQL Study之--“too many connections”錯誤解決方案

錯誤資訊如下:
Can not connect to MySQL server

Error: Too many connections
Errno.: 1040

Similar error report has beed dispatched to administrator before.

以下是mysql.com網站的相關說明: 
       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. 
The number of connections allowed is controlled by the max_connections system variable. Its default value is100. If you need to support more connections, you should restart mysqld with a larger value for this variable.
      mysqld actually allows max_connections+1 clients to connect. The extra connection is reserved for use by accounts that have the SUPER privilege. By granting the SUPER privilege to administrators and not to normal users (who should not need it), an administrator can connect to the server and useSHOW PROCESSLIST to diagnose problems even if the maximum number of unprivileged clients are connected. See Section 13.5.4.19, “SHOW PROCESSLIST Syntax”. 
      The maximum number of connections MySQL can support depends on the quality of the thread library on a given platform. Linux or Solaris should be able to support 500-1000 simultaneous connections, depending on how much RAM you have and what your clients are doing. Static Linux binaries provided by MySQL AB can support up to 4000connections. 

解決方案:


1、修改my.cnf設定檔

[[email protected] ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
innodb_data_file_path=ibdata1:12M;ibdata2:10M:autoextend
sql_mode=STRICT_TRANS_TABLES ,NO_ENGINE_SUBSTITUTION
slow_query_log=true
slow_query_log_file = "/var/lib/mysql/rh64-slow.log"
long_query_time=1
log-queries-not-using-indexes=true
max_connections=1000

2、重啟mysql server

[[email protected] ~]# service mysql restart
Shutting down MySQL (Percona Server)..                     [  OK  ]
Starting MySQL (Percona Server).                           [  OK  ]
[[email protected] ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.25-73.1-log Percona Server (GPL), Release 73.1, Revision 07b797f
Copyright (c) 2009-2015 Percona LLC and/or its affiliates
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> show variables like ‘%connect%‘;

+-----------------------------------------------+-----------------+| Variable_name                                 | Value           |+-----------------------------------------------+-----------------+| character_set_connection                      | utf8            || collation_connection                          | utf8_general_ci || connect_timeout                               | 10              || disconnect_on_expired_password                | ON              || extra_max_connections                         | 1               || init_connect                                  |                 || max_connect_errors                            | 100             || max_connections                               | 1000            || max_user_connections                          | 0               || performance_schema_session_connect_attrs_size | 512             |+-----------------------------------------------+-----------------+10 rows in set (0.02 sec)

在my.cnf修改參數後,需要重新啟動mysql server,如果不能重啟,也可以查詢串連的進程,然後嘗試用kill id關閉一些進程。

mysql> show processlist;
+----+------+-----------+------+---------+------+-------+------------------+-----------+---------------+
| Id | User | Host      | db   | Command | Time | State | Info             | Rows_sent | Rows_examined |
+----+------+-----------+------+---------+------+-------+------------------+-----------+---------------+
|  1 | root | localhost | NULL | Query   |    0 | init  | show processlist |         0 |             0 |
+----+------+-----------+------+---------+------+-------+------------------+-----------+---------------+
1 row in set (0.00 sec)

mysql> kill  id   //查詢出的進程 id

總結,解決問題的最終方法:

1.修改設定檔/etc/my.cnf,調整串連參數

2.檢查程式碼,對於沒有關閉的連結及時進行關閉


本文出自 “天涯客的blog” 部落格,請務必保留此出處http://tiany.blog.51cto.com/513694/1725955

MySQL Study之--“too many connections”錯誤解決方案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.