How to Diagnose and Fix MySQL Connection Issues When Server Shows “Running” Status


25 views

The classic "Unable to connect to MySQL server" error while the service shows as running can be particularly frustrating. Here's how I typically approach this issue:

# Basic status check
sudo systemctl status mysql
# or
sudo service mysql status

Just because the service is running doesn't guarantee it's accepting connections. Let's test connectivity:

# Command line connection test
mysqladmin -u root -p ping
# Expected output: mysqld is alive

Based on my experience, these are the most likely causes:

# Check if MySQL is listening on expected port
netstat -tlnp | grep mysql
# Typical output should show either:
# tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN
# or
# tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN

Permission problems often masquerade as connection failures:

# Check user privileges
mysql -u root -p -e "SELECT Host,User FROM mysql.user;"

Let's examine the critical configuration parameters:

# Check bind-address in my.cnf
grep -E 'bind-address|skip-networking' /etc/mysql/my.cnf
# For newer installations, check:
grep -E 'bind-address|skip-networking' /etc/mysql/mysql.conf.d/mysqld.cnf

Since you're using Webuzo, these additional checks might help:

# Verify Webuzo's MySQL port configuration
cat /usr/local/Webuzo/etc/config.inc.php | grep MYSQL_PORT
# Check alternative configuration locations
ls -la /usr/local/Webuzo/etc/my*

Don't overlook basic network barriers:

# Check iptables rules
sudo iptables -L -n
# Test local connection
telnet 127.0.0.1 3306

Try connecting with different parameters to isolate the issue:

# Force TCP connection
mysql --protocol=TCP -u username -p
# Or specify host explicitly
mysql -h 127.0.0.1 -u username -p

MySQL error logs often hold the golden ticket:

# Find log location
mysql -u root -p -e "SHOW VARIABLES LIKE 'log_error';"
# Then examine the log
sudo tail -50 /var/log/mysql/error.log

When your MySQL server shows as running in terminal (systemctl status mysql) but applications fail to connect, you're dealing with a classic connectivity configuration issue. The "Unable to connect to MySQL server" error typically indicates network accessibility problems rather than service availability.

First, confirm MySQL is actually listening for connections:

sudo netstat -tulnp | grep mysql
# Expected output showing listening ports (usually 3306):
# tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      1234/mysqld

If you see 127.0.0.1 instead of 0.0.0.0, MySQL is only accepting local connections.

Edit /etc/mysql/my.cnf or appropriate config file (location varies by OS/distro):

[mysqld]
bind-address = 0.0.0.0
skip-networking = OFF
skip-name-resolve = ON  # Recommended for performance

After changes, restart MySQL:

sudo systemctl restart mysql

Connect locally and check user permissions:

mysql -u root -p
SELECT Host, User FROM mysql.user;
# Ensure your application user has correct host access:
GRANT ALL PRIVILEGES ON *.* TO 'webuzo_user'@'%' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;

Check firewall rules (example for UFW):

sudo ufw allow 3306/tcp
sudo ufw reload

For SELinux (if enabled):

sudo setsebool -P mysqld_connect_any 1

Verify remote connectivity using basic tools:

telnet your_server_ip 3306
# Or for more detailed check:
mysqladmin -h your_server_ip -u username -p ping

Webuzo sometimes uses non-standard socket locations. Check its configuration:

cat /usr/local/webuzo/config/mysql.json
# Ensure socket path matches actual MySQL socket:
ls -l /var/run/mysqld/mysqld.sock

When all else fails, use tcpdump to analyze connection attempts:

sudo tcpdump -i any port 3306 -nn -v
# Look for SYN packets without ACK responses indicating blocked connections