SQLSTATE[HY000] [1040] Too many connections 解决方法

当网站遇到 SQLSTATE[HY000] [1040] Too many connections 怎么办?

这个错误表示 服务器的当前连接数超过了配置的最大允许连接数(默认 151),导致新连接被拒绝。通常发生在高并发网站(如 WordPress、Laravel)中,原因包括应用未及时释放连接、突发流量或配置不足。别担心,它通常可快速修复,但需结合监控避免复发。

在 MySQL 世界中遇到的最常见错误之一是臭名昭著的错误 1040:

SQLSTATE[HY000] [1040] Too many connections 解决方法-1

实际上,这意味着 MySQL 实例已达到其允许的最大客户端连接数限制。在连接关闭之前,服务器将不会接受任何新连接。

我想讨论一些防止这种情况的实用建议,或者如果你发现自己处于这种情况,如何恢复。

精确调整 max_connections 参数

此设置定义了 MySQL 实例可接受的最大连接数。设置最大连接数的“原因”取决于服务器的可用资源和应用程序的使用模式。允许不受控制的连接可能会导致服务器崩溃,这可能比阻止进一步的连接“更糟糕”。Max_connections 是一个用于保护服务器的值,而不是修复与劫持连接相关的问题。

与服务器的每个连接都会消耗固定数量的开销,例如管理连接的“线程”和用于管理连接的内存,以及可变资源(例如用于创建内存表的内存)。测量应用程序的资源模式并找到超过该连接数的危险点非常重要。

Percona 监控和管理 (PMM) 可以帮助您找到这些值。查看内存使用模式、正在运行的线程,并将它们与连接数关联起来。PMM 还可以显示连接活动的峰值,让您了解距离阈值的距离。请根据服务器的资源限制进行相应的调整。

下面看到的是一个具有非常稳定的连接模式的服务器,并且最大使用量和最大连接数之间有很大空间。

MySQL:如何修复Too many connections 错误:(HY000/1040):连接数过多

首先调整mysql配置文件,然后重新启动MySQL

#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see

# this is read by the standalone daemon and embedded servers
[server]

# this is only for the mysqld standalone daemon
[mysqld]

#
# * Basic Settings
#

#user                    = mysql
pid-file                = /run/mysqld/mysqld.pid
basedir                 = /usr
#datadir                 = /var/lib/mysql
#tmpdir                  = /tmp

# Broken reverse DNS slows down connections considerably and name resolve is
# safe to skip if there are no "host by domain name" access grants
#skip-name-resolve

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address            = 127.0.0.1

#
# * Fine Tuning
#

key_buffer_size         = 1024M
max_allowed_packet      = 1024M
thread_stack            = 192K
thread_cache_size       = 1024
table_open_cache        = 4096
tmp_table_size          = 1024M
max_heap_table_size     = 1024M
open_files_limit        = 0
myisam_recover_options  = BACKUP
max_connections         = 10000
interactive_timeout     = 60
wait_timeout            = 60

#key_buffer_size        = 128M
#max_allowed_packet     = 1G
#thread_stack           = 192K
#thread_cache_size      = 8
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
#myisam_recover_options = BACKUP
#max_connections        = 1000
#table_cache            = 64



#
# * Logging and Replication
#

# Note: The configured log file or its directory need to be created
# and be writable by the mysql user, e.g.:
# $ sudo mkdir -m 2750 /var/log/mysql
# $ sudo chown mysql /var/log/mysql



# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# Recommend only changing this at runtime for short testing periods if needed!
#general_log_file       = /var/log/mysql/mysql.log
#general_log            = 1

# When running under systemd, error logging goes via stdout/stderr to journald
# and when running legacy init error logging goes to syslog due to
# /etc/mysql/conf.d/mariadb.conf.d/50-mysqld_safe.cnf
# Enable this if you want to have error logging into a separate file
#log_error = /var/log/mysql/error.log
# Enable the slow query log to see queries with especially long duration
#log_slow_query_file    = /var/log/mysql/mariadb-slow.log
#log_slow_query_time    = 10
#log_slow_verbosity     = query_plan,explain
#log-queries-not-using-indexes
#log_slow_min_examined_row_limit = 1000
slow_query_log = 0


# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replica, see README.Debian about other
#       settings you may need to change.
#server-id              = 1
#log_bin                = /var/log/mysql/mysql-bin.log
expire_logs_days        = 2
#max_binlog_size        = 100M

#
# * SSL/TLS
#

# For documentation, please read
# https://mariadb.com/kb/en/securing-connections-for-client-and-server/
#ssl-ca = /etc/mysql/cacert.pem
#ssl-cert = /etc/mysql/server-cert.pem
#ssl-key = /etc/mysql/server-key.pem
#require-secure-transport = on

#
# * Character sets
#

# MySQL/MariaDB default is Latin1, but in Debian we rather default to the full
# utf8 4-byte character set. See also client.cnf
character-set-server  = utf8mb4
collation-server      = utf8mb4_unicode_ci

#
# * InnoDB
#

# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!
# Most important is to give InnoDB 80 % of the system RAM for buffer use:
# https://mariadb.com/kb/en/innodb-system-variables/#innodb_buffer_pool_size
#innodb_buffer_pool_size = 8G

innodb_buffer_pool_size = 40G
innodb_buffer_pool_instances = 16
innodb_log_file_size = 12G
innodb_flush_log_at_trx_commit = 2
innodb_flush_method = O_DIRECT
innodb_io_capacity = 2000
innodb_read_io_threads = 8
innodb_write_io_threads = 8
innodb_thread_concurrency = 0
innodb_lock_wait_timeout = 10
innodb_concurrency_tickets = 5000
innodb_autoinc_lock_mode = 2
query_cache_type = 0


# this is only for embedded server
[embedded]

# This group is only read by MariaDB servers, not by MySQL.
# If you use the same .cnf file for MySQL and MariaDB,
# you can put MariaDB-only options here
[mariadb]

# This group is only read by MariaDB-10.11 servers.
# If you use the same .cnf file for MariaDB of different versions,
# use this group for options that older servers don't understand
[mariadb-10.11]
评分

留下评论

您的邮箱地址不会被公开。 必填项已用 * 标注