Intermittent SQLSTATE[HY000] [2002] Connection refused
and SQLSTATE[HY000]: General error: 2006 MySQL server has gone away
with MariaDB on Debian 11 (Oracle VM)
Question:
I’ve been experiencing frequent issues with my MariaDB setup on Debian 11, which is running on Oracle VM. The errors I’m seeing alternate between:
SQLSTATE[HY000] [2002] Connection refused
SQLSTATE[HY000]: General error: 2006 MySQL server has gone away
Setup Details:
- MariaDB version: 11.5.2-MariaDB, client 15.2 for debian-linux-gnu (x86_64)
- OS: Debian 11 (on Oracle VM)
- PHP version: PHP8.3
- The database is accessed via both
mysqli
and PDO. - The MariaDB server is running locally, accessed via both TCP and UNIX socket.
- Remote access with HeidiSQL works fine.
Steps I’ve Tried:
- Adjusted settings like
max_allowed_packet
,wait_timeout
,interactive_timeout
,innodb_buffer_pool_size
, andconnect_timeout
— but the problem persists. - Verified that MariaDB is running correctly using
systemctl status mariadb
. - Tested the connection manually via both socket and TCP:
mysql -u root -p
(local UNIX socket)mysql -h 127.0.0.1 -P 3306 -u root -p
(TCP)
Both methods work manually, but the errors still appear in the application.
- Checked firewall settings and confirmed that port 3306 is open.
- Reviewed
/var/log/mysql/error.log
for errors, but found nothing directly explaining the issue. - PHP connection pooling (persistent connections) is in use via
PDO::ATTR_PERSISTENT
, but the issue remains.
Observations:
- The “Connection refused” error occurs sporadically.
- The “MySQL server has gone away” error seems more frequent after long-running or large queries.
- System resources seem sufficient (
htop
shows moderate to zero CPU and memory usage). - This setup works fine on XAMPP (Windows), but not on Debian 11 with Oracle VM.
Questions:
- What could cause these intermittent connection issues, especially with the alternating errors?
- Could this be related to how MariaDB handles connections on Debian 11 (Oracle VM) vs XAMPP on Windows (which works fine)?
- Are there any specific MariaDB, Oracle VM, or network settings I might have missed that could stabilize the connection?
4
I was able to solve the issue by switching to Debian 12 and PHP 8.2. It seems the problem was related to using Debian 11 and PHP 8.3, although I’m not entirely sure whether the root cause was Debian 11, PHP 8.3, or the interaction between the two.
After making the switch, everything works as expected:
- MariaDB no longer causes connection issues.
- Both local Symfony connections and remote connections via HeidiSQL are stable and perform as expected.
Addendum:
I’ve finally identified the root cause of the issue. The problem was related to the use of the MariaDB setup script from mariadb.com:
curl -LsS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | bash
Even though this is an official script, it seems to have caused instability in my environment. When I removed that line and installed MariaDB directly from the official Debian repositories, everything worked perfectly, and MariaDB has been stable ever since.
The issue had nothing to do with PHP (as I initially thought), because I experienced connection problems even when using a remote client like HeidiSQL.
So, if you’re running into similar issues, I recommend avoiding the setup script from mariadb.com and sticking with the packages provided in the Debian repos. This solved all the connection problems for me.