最近搞了个网站,访问量一般,但是发布系统以后过一段时间就宕机了,看日志是mysql连接数导致的,以下是解决办法:
系统环境:CentOS release 6.5 (Final),10.0.16-MariaDB,MySQL 5.5.21
错误信息:
Host is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'
ygmysql - conn_mysql() -- host:
115.29.237.28; error_msg: (
1129, "Host '123.57.78.100' is
blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'"
)
ygmysql - conn_mysql() --
host: 115.29.237.28; error_msg: (1129, "Host '123.57.78.100' is
blocked because of many connection errors; unblock with 'mysqladmin
flush-hosts'")
ygmysql - conn_mysql() -- host:
115.29.237.28; error_msg: (1129, "Host '123.57.78.100' is
blocked because of many connection errors; unblock with 'mysqladmin
flush-hosts'")
ygmysql - conn_mysql() -- host:
115.29.237.28; error_msg: (1129, "Host '123.57.78.100' is
blocked because of many connection errors; unblock with 'mysqladmin
flush-hosts'")
原因分析:
同一个客户端IP地址,在短时间内访问服务器的MySQL产生了太多的错误,超过mysql数据库max_connect_errors的最大值,因此被服务器MySQL中断了数据库连接而导致的阻塞!
解决方法:
1、提高允许的max_connect_errors数量(治标不治本)
方法1) 命令行修改
修改max_connection_errors的数量为1000
mysql -h 123.57.78.101 -P 3306
-uroot -p123456
set global max_connect_errors=1000;
show variables like
'%max_connect_errors%';
命令行修改,只是临时修改,重启MySQL后会失效;如果需要永久生效,需要在my.cnf配置文件中修改才行!
方法2)配置文件修改
登陆进入Mysql数据库查看max_connect_errors
mysql -h 123.57.78.101 -P 3306
-uroot -p123456
show variables like
'%max_connect_errors%';
max_connect_errors 默认是10 或 100,修改方法如下:
vim /etc/my.cnf
添加一行
max_connect_errors=1000
保存后,如下图:
重启MySQL,修改才会生效!
/etc/init.d/mysqld restart
2、使用mysqladmin flush-hosts 命令清理一下hosts文件
如果不知道mysqladmin在哪个目录下,可以使用命令查找:which mysqladmin
方法1) 命令修改flush-hosts
/usr/bin/mysqladmin flush-hosts -h 123.57.78.101 -P 3306 -uroot -p123456
备注:其中服务器IP地址,端口号,用户名,密码,上面参数都可以根据需要来添加和修改;
配置有master/slave主从数据库的要把主库和从库都修改一遍的
第二步,在数据库中进行,执行命令如下:
flush hosts;
方法2)重启数据库
/etc/init.d/mysqld restart