MySQL: PDO::query(): MySQL server has gone away in

2018年09月11日 | 1125

api访问MySQL在出现抛错:
2018-09-11 09:56:38 [121.33.214.51][-][io5lhbbesif66vi3fhb1psln55][error][yii\base\ErrorException:2] exception 'yii\base\ErrorException' with message 'PDO::__construct(): MySQL server has gone away' in 


解决办法:应用时必须重启mysql数据库~~  centos7.0 systemctl restart mysqld   centos6.5 services restart mysqld

A . 在mysql配置文件/etc/my.cnf中添加或修改为以下配置:


[mysqld_safe]
log-error=/usr/local/mysql/logs/mysqld.log
pid-file=/var/run/mysqld.pid


##########################
# time out
##########################
connect_timeout = 20

wait_timeout = 2880000
interactive_timeout = 2880000



##########################
# connection
##########################
max_connections = 2000
max_user_connections = 1900
max_connect_errors = 100000
max_allowed_packet = 1G

##########################


当打开 [mysqld_safe] 时,请执行以下操作


[root@aerchi.com mysql]# mkdir logs

[root@aerchi.com mysql]# echo "" > /usr/local/mysql/logs/mysqld.log
[root@aerchi.com mysql]# chown -R mysql:mysql /usr/local/mysql/logs/mysqld.log


B. 修改 连接MySQL的php文件

增加 PDO::ATTR_PERSISTENT => false, PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,

如下:

$PDO = @new PDO("mysql:host=$DB_HOST;dbname=$DB_NAME","$DB_USER","$DB_PASSWORD", array(PDO::ATTR_PERSISTENT => false, PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true, PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8';"));


C. 修改 php.ini
vim  /etc/php.ini

mysqli.reconnect = Off 

修改成:

mysqli.reconnect = On