Fix common MySQL connection errors


Common connection errors

MySQL connection errors can have several causes. This guide helps you identify and fix the most frequent problems.


Error 1: "Can't connect to MySQL server"

Possible causes:

  • MySQL is not running
  • Port blocked by firewall
  • Incorrect IP address or host
  • MySQL is not listening on the correct interface

Solutions:

1. Verify MySQL is running:

sudo systemctl status mysql
# Or
sudo systemctl status mysqld

If not running, start it:

sudo systemctl start mysql
sudo systemctl enable mysql

2. Verify MySQL is listening:

sudo netstat -tlnp | grep 3306
# Or
sudo ss -tlnp | grep 3306

You should see something like: 0.0.0.0:3306 or 127.0.0.1:3306

3. Check firewall:

sudo ufw status
sudo ufw allow 3306/tcp

4. Check bind-address configuration:

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Verify that bind-address is configured correctly: - 127.0.0.1 for local connections only - 0.0.0.0 for remote connections


Error 2: "Access denied for user"

Possible causes:

  • Incorrect password
  • User doesn't exist
  • User doesn't have permissions to connect from that host

Solutions:

1. Verify credentials:

mysql -u user -p

If it works from terminal but not from application, the problem may be in application configuration.

2. Verify user exists:

mysql -u root -p
SELECT user, host FROM mysql.user WHERE user='username';

3. Verify user permissions:

SHOW GRANTS FOR 'user'@'localhost';
SHOW GRANTS FOR 'user'@'%';

4. Grant permissions if necessary:

GRANT ALL PRIVILEGES ON database_name.* TO 'user'@'localhost';
FLUSH PRIVILEGES;

Error 3: "Too many connections"

Cause: Maximum limit of simultaneous connections reached.

Solutions:

1. See current connections:

mysql -u root -p
SHOW PROCESSLIST;
SHOW STATUS LIKE 'Threads_connected';

2. See maximum limit:

SHOW VARIABLES LIKE 'max_connections';

3. Increase connection limit:

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Add or modify:

[mysqld]
max_connections = 200

Restart MySQL:

sudo systemctl restart mysql

4. Close inactive connections:

mysql -u root -p
KILL process_id;

Error 4: "Connection refused"

Possible causes:

  • MySQL is not running
  • Incorrect port
  • Firewall blocking the port

Solutions:

1. Verify MySQL is running:

sudo systemctl status mysql

2. Verify port:

mysql -u root -p -e "SHOW VARIABLES LIKE 'port';"

3. Check firewall:

sudo ufw status
sudo iptables -L -n | grep 3306

Error 5: "Host is not allowed to connect"

Cause: The host you're trying to connect from is not allowed.

Solutions:

1. See allowed hosts:

mysql -u root -p
SELECT user, host FROM mysql.user WHERE user='username';

2. Allow connection from any host (less secure):

GRANT ALL PRIVILEGES ON database_name.* TO 'user'@'%' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;

3. Allow connection from specific IP (more secure):

GRANT ALL PRIVILEGES ON database_name.* TO 'user'@'192.168.1.100' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;

Error 6: "Unknown database"

Cause: The specified database doesn't exist.

Solutions:

1. List available databases:

mysql -u root -p
SHOW DATABASES;

2. Create database if it doesn't exist:

CREATE DATABASE database_name CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

3. Verify name in configuration:

Check that the database name in your application is correct (case-sensitive on Linux).


Error 7: "Lost connection to MySQL server"

Possible causes:

  • Connection timeout
  • MySQL restarted
  • Network problems
  • Very long query

Solutions:

1. Increase timeout:

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Add:

[mysqld]
wait_timeout = 600
interactive_timeout = 600

2. Check MySQL logs:

sudo tail -f /var/log/mysql/error.log

3. Check MySQL status:

sudo systemctl status mysql

Error 8: "SSL connection error"

Cause: Problem with SSL configuration.

Solutions:

1. Disable SSL verification (development only):

In connection, add: ?ssl-mode=DISABLED

2. Configure SSL correctly:

mysql -u root -p
SHOW VARIABLES LIKE '%ssl%';

Diagnostic tools

1. Test connection from terminal:

mysql -h localhost -u user -p
mysql -h 127.0.0.1 -u user -p
mysql -h SERVER_IP -u user -p

2. Verify network connectivity:

telnet SERVER_IP 3306
# Or
nc -zv SERVER_IP 3306

3. See MySQL logs:

sudo tail -f /var/log/mysql/error.log
sudo tail -f /var/log/mysql/mysql.log

4. See MySQL processes:

mysql -u root -p
SHOW PROCESSLIST;
SHOW STATUS;

Troubleshooting checklist

  • ✅ Verify MySQL is running
  • ✅ Verify credentials (username and password)
  • ✅ Verify database exists
  • ✅ Verify user permissions
  • ✅ Verify firewall configuration
  • ✅ Verify bind-address configuration
  • ✅ Verify MySQL logs
  • ✅ Verify network connectivity
  • ✅ Verify connection limits

Problem prevention

  • Monitor connections: Regularly check how many connections are active
  • Use persistent connections: In web applications, use connection pooling
  • Close connections correctly: Make sure to close connections when not in use
  • Configure appropriate timeouts: Avoid very short or very long timeouts
  • Make regular backups: To be able to restore if there are problems

Need help?

If you can't resolve the connection error, open a ticket from the billing.baires.host panel or contact us for support. Include complete error messages and steps you've already tried.

You can also reach us through our social media:

Was this answer helpful? 0 Users Found This Useful (0 Votes)