Home/Engineer/Troubleshooting/Completely Fixing MySQL/Mari…
TroubleshootingBeginnerUbuntuDebianCentOSRHELMySQLMariaDBERROR 1045

Completely Fixing MySQL/MariaDB Connection Errors — ERROR 1045 · Can't connect · socket

Causes and fixes for MySQL/MariaDB connection errors by type, including ERROR 1045 (28000): Access denied, Can't connect to MySQL server on localhost, and a missing socket file.

Contents
  1. 1. ERROR 1045 — Access denied
  2. 2. Can't connect to MySQL server — daemon not running
  3. 3. Missing socket file or path mismatch
  4. 4. Remote access not allowed
  5. Quick diagnosis

MySQL/MariaDB connection errors fall into a few main types. Check the error message first.

Error messageCause
ERROR 1045 (28000): Access deniedPassword, 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 socketSocket file path mismatch
Host 'x.x.x.x' is not allowed to connectNo remote access privilege

1. ERROR 1045 — Access denied

Verify the password

Bash
# Try connecting with the root password
mysql -u root -p

# Specific user
mysql -u myuser -p mydb

If you forgot the password — reset it

Bash
# 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
SQL
-- 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;
Bash
# Restart normally
sudo systemctl restart mysql

Check users and privileges

SQL
-- 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

Bash
# 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
Bash
# Check error logs (cause of startup failure)
sudo journalctl -u mysql -n 50
sudo tail -50 /var/log/mysql/error.log

3. Missing socket file or path mismatch

Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

Bash
# 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:

Bash
mysql -u root -p --socket=/tmp/mysql.sock

Or set the default socket path in ~/.my.cnf:

INI
[client]
socket = /tmp/mysql.sock

4. Remote access not allowed

Error: Host 'x.x.x.x' is not allowed to connect to this MySQL server

SQL
-- 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;
Bash
# 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 mysql

If 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

Bash
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 connection
#MySQL#MariaDB#ERROR 1045#Access denied#Troubleshooting#socket#Databases
Editorial note

AI 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

Topic-related official docs (auto-linked · not per-claim sources)MySQL manual ↗

Questions & answers

Ask a question about this guide. We'll review and reply.