Fix: Cannot Connect to MySQL on VPS (Access Denied or Port Blocked)

Estimated reading: 2 minutes

If you’re getting “Access denied for user” or can’t connect to MySQL remotely on your VPS, here’s how to diagnose and fix the most common causes.

Cause 1: Wrong Username or Password

Test your MySQL credentials locally on the VPS first:

mysql -u your_username -p

Enter the password when prompted. If you get “Access denied”, reset the password:

# Log in as root
mysql -u root -p

# Reset a user's password
ALTER USER 'your_username'@'localhost' IDENTIFIED BY 'NewStrongPassword123!';
FLUSH PRIVILEGES;
EXIT;

Cause 2: MySQL Is Not Running

Check if MySQL is running:

systemctl status mysql

If it’s stopped or failed, start it:

systemctl start mysql
systemctl enable mysql

If MySQL fails to start, check the error log:

journalctl -u mysql -n 50 --no-pager

Cause 3: Remote Connections Blocked (Port 3306)

By default, MySQL only listens on localhost and port 3306 may be blocked by your firewall. To allow a specific IP to connect remotely:

1. Allow port 3306 in UFW:

# Only allow your specific IP (recommended)
ufw allow from YOUR_IP to any port 3306

# Or allow all (not recommended for production)
ufw allow 3306

2. Make MySQL listen on all interfaces — edit /etc/mysql/mysql.conf.d/mysqld.cnf and change:

bind-address = 127.0.0.1

to:

bind-address = 0.0.0.0

Then restart MySQL: systemctl restart mysql

3. Grant remote access for the user:

mysql -u root -p

GRANT ALL PRIVILEGES ON your_database.* TO 'your_username'@'YOUR_IP' IDENTIFIED BY 'your_password';
FLUSH PRIVILEGES;
EXIT;

Cause 4: MySQL Crashed Due to Low Memory

On low-RAM VPS plans (512MB–1GB), MySQL can crash when memory runs out. Fix by adding a swap file. See: How to Set Up a Swap File on Your VPS.

Still Having Issues?

Contact Veerhost support at veerhost.com/support and include the exact error message and MySQL version (mysql --version).