安装php5-mysqlnd后MySQL连接断开及连接数异常问题
Hey there, let’s tackle this connection problem you’re seeing after switching from php5-mysql to php5-mysqlnd. That weird value 18446744073709551613 in phpinfo()? That’s actually the unsigned 64-bit integer representation of -1—a clear sign there’s an internal counting overflow or connection tracking bug with mysqlnd in your setup. Here’s what you can try to fix this:
Step 1: Tune MySQLnd Configuration in php.ini
First, let’s make sure your mysqlnd settings are configured correctly. Open your Apache PHP config file (usually at /etc/php5/apache2/php.ini) and verify these values:
- Set
mysqlnd.collect_statistics = On(this ensures connection stats are tracked properly; sometimes this gets disabled accidentally) - Adjust connection timeouts to prevent premature disconnections:
mysqlnd.net_read_timeout = 60 mysqlnd.net_write_timeout = 60
Save the file and restart Apache with sudo service apache2 restart.
Step 2: Confirm MySQLnd Module is Loaded
Sometimes the mysqlnd module doesn’t load correctly after installation. Run this command to check:
php -m | grep mysqlnd
If mysqlnd doesn’t show up in the output, enable it manually and restart Apache:
sudo php5enmod mysqlnd sudo service apache2 restart
Step 3: Check MySQL Connection Limits
It’s possible your MySQL server is hitting its maximum connection limit, which can cause unstable connections and messed-up stats. Check your current max connections setting:
mysql -u root -p -e "SHOW VARIABLES LIKE 'max_connections';"
If the value is low (the default is often 151), increase it in your MySQL config (/etc/mysql/my.cnf):
max_connections = 200
Save the file and restart MySQL:
sudo service mysql restart
Step 4: Update PHP Packages (Critical for Ubuntu 14.04)
Ubuntu 14.04’s PHP 5.5 packages have known bugs with mysqlnd, especially around connection tracking. Make sure all your PHP packages are up to date:
sudo apt-get update && sudo apt-get upgrade php5-mysqlnd php5-cli php5-common
After updating, restart both Apache and MySQL to apply changes.
Step 5: Dig Into Error Logs for Clues
Don’t skip this! Error logs will give you specific details about why connections are dropping:
- Apache error log:
/var/log/apache2/error.log - MySQL error log:
/var/log/mysql/error.log
Look for entries like "Connection timed out", "Too many connections", or "Lost connection to MySQL server"—these will point you directly to the root cause.
If you’re using persistent connections (like mysql_pconnect or PDO’s PDO::ATTR_PERSISTENT), try disabling them temporarily. Older mysqlnd versions had bugs with persistent connection counting that caused this overflow issue.
内容的提问来源于stack exchange,提问作者koalacombatsystems

