MySQL/MariaDB connection errors fall into a few main types. Check the error message first.
| Error message | Cause |
|---|---|
ERROR 1045 (28000): Access denied | Password, user, or host mismatch |
Can't connect to MySQL server on 'localhost' | MySQL daemon not running or port blocked |
Can't connect to local MySQL server through socket | Socket file path mismatch |
Host 'x.x.x.x' is not allowed to connect | No remote access privilege |
1. ERROR 1045 — Access denied
Verify the password
# Try connecting with the root password
mysql -u root -p
# Specific user
mysql -u myuser -p mydbIf you forgot the password — reset it
# Stop the MySQL service
sudo systemctl stop mysql
# Start without authentication
sudo mysqld_safe --skip-grant-tables &
# Connect, then change the password
mysql -u root-- MySQL 5.7 and earlier
UPDATE mysql.user SET authentication_string=PASSWORD('new_password') WHERE User='root';
FLUSH PRIVILEGES;
-- MySQL 8.0+
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
FLUSH PRIVILEGES;
EXIT;# Restart normally
sudo systemctl restart mysqlCheck users and privileges
-- After connecting as root
SELECT user, host, authentication_string FROM mysql.user;
-- Grant privileges to a specific user
GRANT ALL PRIVILEGES ON mydb.* TO 'myuser'@'localhost' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;2. Can't connect to MySQL server — daemon not running
# Check service status
sudo systemctl status mysql # Ubuntu/Debian
sudo systemctl status mariadb # MariaDB
# Start if it is not running
sudo systemctl start mysql
sudo systemctl enable mysql
# Confirm the port is actually listening
sudo ss -tlnp | grep 3306# Check error logs (cause of startup failure)
sudo journalctl -u mysql -n 50
sudo tail -50 /var/log/mysql/error.log3. Missing socket file or path mismatch
Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'
# Find the actual socket file location
sudo find /var/run /tmp -name "*.sock" 2>/dev/null
sudo find /run -name "mysqld.sock" 2>/dev/null
# Check the socket path in the MySQL configuration
sudo grep -r "socket" /etc/mysql/If the socket path is different, specify it when connecting:
mysql -u root -p --socket=/tmp/mysql.sockOr set the default socket path in ~/.my.cnf:
[client]
socket = /tmp/mysql.sock4. Remote access not allowed
Error: Host 'x.x.x.x' is not allowed to connect to this MySQL server
-- After connecting as root, create an account that allows remote access
CREATE USER 'myuser'@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON mydb.* TO 'myuser'@'%';
FLUSH PRIVILEGES;# Check whether MySQL is listening on 127.0.0.1 only
sudo grep -r "bind-address" /etc/mysql/
# bind-address = 127.0.0.1 → change to 0.0.0.0, then restart
sudo systemctl restart mysqlIf you open remote access, configure the firewall (UFW) so that port 3306 is allowed only from trusted IPs.
sudo ufw allow from TRUSTED_IP to any port 3306
Quick diagnosis
sudo systemctl status mysql # service status
sudo ss -tlnp | grep 3306 # port listening
sudo tail -30 /var/log/mysql/error.log # error log
mysql -u root -p --verbose # verbose connectionAI tools may assist drafting. Check the evidence in each guide for whether commands were run and which environment was used. Confirm the current docs for your version before applying instructions, and email us about errors. email us
Questions & answers
Ask a question about this guide. We'll review and reply.