Sites down - Database connection error (2): Could not connect to MySQL

Created:

2016-11-16 12:53:53 UTC

Modified:

2017-04-24 11:30:25 UTC

1

Was this article helpful?


Have more questions?

Submit a request

Sites down - Database connection error (2): Could not connect to MySQL

Applicable to:

  • Plesk 11.5 for Windows

Symptoms

Some domains are returning this error:

Database connection error (2): Could not connect to MySQL

Cause

The error means that the some domain's script is trying to connect to a MySQL database, but is failing to do so.

Resolution

First, find the script and make sure it is using the correct database name and credentials when connecting. For illustration purposes, let us assume that the script is using these credentials:

Database: testdb
Host: localhost
Username: testuser
Password: test
  1. Log in to the server via SSH and try to connect to the same database manually using the above credentials:

    # mysql -h localhost -utestuser -ptest testdb
  2. If the connection fails, make sure that the needed database, user, and password indeed exist on the server. Even if they are shown in the Plesk web interface, check if they exist in MySQL directly.

For example, it may be that the testuser is missing from the mysql.user table. In that case, add the user manually by running these commands in the shell:

    # MYSQL_PWD=`cat /etc/psa/.psa.shadow` mysql -u admin mysql
mysql > GRANT USAGE ON *.* TO 'testuser'@'%' IDENTIFIED BY 'test';
mysql > GRANT ALL PRIVILEGES ON `testdb`.* TO 'testuser'@'%';
mysql > FLUSH PRIVILEGES;``
Have more questions? Submit a request
Please sign in to leave a comment.