[乐意黎原创]MySQL抛Warning: PDO::query(): MySQL server has gone away in

最近乐意黎在转移主机时(新主机: Centos 7 +php7+apache +mysql ),页面访问MySQL在抛错:
Warning
: PDO::query(): MySQL server has gone away in /usr/local/apache/websites/index.php on line 111

Warning: PDO::query(): Error reading result set's header in /usr/local/apache/websites/index.php on line 111

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away in /usr/local/apache/websites/index.php:111 Stack trace: #0 /usr/local/apache/websites/index.php(111): PDO->query('SELECT * FROM `...') #1 {main} thrown in /usr/local/apache/websites/index.php on line 111


解决办法:应用时必须重启mysql数据库~~  systemctl 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


乐意黎原创

2018-06-23 00:24

发布了430 篇原创文章 · 获赞 415 · 访问量 925万+
展开阅读全文

PDOMySQL服务器已经消失,无法捕获异常

10-07

<div class="post-text" itemprop="text"> <p><strong>General Description</strong><br> I'm working a chatsystem using websockets (Ratchet). The chat works perfectly fine, but I'm having some issue with my database class.</p> <p><strong>The Problem</strong><br> During development, the chatsystem obviously isn't visited a lot. This causes the MySQL server to close the connection eventually. To solve that issue, I've written some exception handlers to reconnect to the MySQL server. But somehow this doesn't work. Here's the bare minimum of how my code looks like:</p> <pre><code><?php Class Database { private $host = DB_HOST; private $user = DB_USER; private $pass = DB_PASS; private $dbname = DB_NAME; private $dbh; private $error; private $stmt; public function __construct(){ if($this->dbh === null){ $this->reconnect(); } } private function reconnect(){ // Set DSN $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname; // Set options $options = array( PDO::ATTR_PERSISTENT => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ); // Create a new PDO instanace try{ $this->dbh = new PDO($dsn, $this->user, $this->pass, $options); } // Catch any errors catch(PDOException $e){ $this->error = $e->getMessage(); return $this->error; } } /* Execute query */ public function execute(){ try{ return $this->stmt->execute(); } catch(PDOException $e){ $this->reconnect(); try{ return $this->stmt->execute(); } catch(PDOException $e){ $this->error = $e->getMessage(); return $this->error; } } } </code></pre> <p>Of course there are many more simular methods, but those are not important.</p> <p>The error I'm being presented with is the following:</p> <blockquote> <p>PHP Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away in /var/www/clients/client1/web4/web/src/Pdo.php:43 Stack trace:<br> /var/www/clients/client1/web4/web/src/Pdo.php(43): PDOStatement->execute()</p> </blockquote> <p>That error points to <code>return $this->stmt->execute();</code> in the <code>execute()</code> method above.</p> <ol> <li>How is it possible that the error is telling me <em>"Uncaught PDOException"</em> while I'm clearly catching it?</li> <li>How is the MySQL server even able to disconnect while I'm not using a persistent connection? <code>PDO::ATTR_PERSISTENT => false,</code></li> </ol> </div> 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览