Error Establishing a Database Connection

I ran into this error while working on a client site the other day. The steps that I took to solve the issue included the following:

Step 1: Check if you can connect to the database in another way

Sometimes you may be able to connect to the database via the command line using the mysql command (mysql -u username) or via a remote MySQL client (like Sequel Pro or MySQL WorkBench), but you can’t connect to the database via your actual code because you have incorrect database credentials in the code. This was not the case for me as I was unable to connect to the database via the command line nor via a remote MySQL client and my database credentials were the same for all three.

Step 2: Restart your MySQL Service

I tried this using the following command, and it worked!

sudo service mysql restart

Your mysql service may not even be running in which case, you will need to run the following command:

sudo service mysql start

Step 3: Restart Your Server

If you don’t have command line access or you’ve tried the above and it didn’t work, you may need to restart your server. This will likely update certain services and start certain services – and it will likely solve your problem!

If you do have command line access, after restarting, your server may have started Apache but you’re using NGINX for your web server. So, you want to run the following command to stop apache services:

sudo service apache2 stop

And, you’ll want to use the following command to start NGINX:

sudo service nginx start

Leave a Reply

Your email address will not be published. Required fields are marked *