If WordPress cannot connect to the database, it displays a public error message: Error Establishing a Database Connection: Vultr.
As part of our Vultr Cloud Managed Services, we assist our customers with several WordPress queries.
Today, let us discuss useful steps to resolve database issues.
Error Establishing a Database Connection: Vultr
This error can occur due to low RAM or disk space, database corruption, plugin errors, misbehaving themes, incorrect password, MySQL errors, and more.
Moving ahead, let us see how our Support Techs troubleshoot this error for our customers.
Before that, we need to check the server console at Vultr. If we find an “out of memory” error on the screen, we reboot the instance. This can often temporarily fix the problem.
Make Backups
First and foremost, it is vital to have a reliable backup.
Take a Snapshot
Vultr snapshots allow us to restore an exact point in time backup in case something goes wrong.
Verify MySQL
It is possible for the MySQL daemon to stop. We verify it is running with ps and grep. It will return us a one-line with the process ID and command line.
# ps -ax | grep '[m]ysqld'
1342 ? Sl 0:01 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid
Review MySQL Logs
Then we review the log files for information. We check for messages regarding incorrect passwords, corrupt tables, memory or disk space issues, etc.
This information is beneficial for diagnosing the root cause of the database problem.
To do so, we use tail. It will give the most recent 50 entries in error.log.
# tail -n50 /var/log/mysql/error.log
Backup the Database
Prior to making any database changes, we make a backup. In addition, this process verifies that WordPress has the correct database credentials.
- Initially, we connect to the server as root with SSH or the Vultr Console.
- Then we locate the database credentials with grep. This returns the WordPress connection information. We ensure that DB_HOST is ‘localhost’.
# grep 'DB_USER\|DB_NAME\|DB_PASSWORD\|DB_HOST' /var/www/html/wp-config.php define( 'DB_NAME', 'example_DB_name' ); define( 'DB_USER', 'example_username' ); define( 'DB_PASSWORD', 'example_password' ); define( 'DB_HOST', 'localhost' );
- To back up, we use mysqldump. In the MySQL prompt, we provide the database password. We can choose a location. For security, we ensure the backup is outside the /var/www directory.
# mysqldump -u example_username -p example_DB_name > /root/mysqlbak.sql
- We create the backup file in a reasonable size, larger than a few KB. It is a text file, we can also inspect it with less.
# ls -l /root/mysqlbak.sql # less /root/mysqlbak.sql
If the password is incorrect, mysqldump will report the error:
mysqldump: Got error: 1045: Access denied for user 'example_username'@'localhost' (using password: YES) when trying to connect
Reset the Database Password
We can skip this part if we succeed with the mysqldump backup.
We may fail to create a backup using the credentials from wp-config.php. In such a case, we reset the database password. Only do it if the WordPress information is incorrect and you do not know the correct password.
- Edit wp-config.php.
# nano /var/www/html/wp-config.php
- We search for the line:
define( 'DB_PASSWORD', 'example_password' );
Enter a new, strong password. Do not change any of the other database information.
- Eventually, we save the file and exit the editor.
- We verify the database information in the file and make sure the DB_HOST is localhost.
# grep 'DB_USER\|DB_NAME\|DB_PASSWORD\|DB_HOST' /var/www/html/wp-config.php define( 'DB_NAME', 'example_DB_name' ); define( 'DB_USER', 'example_username' ); define( 'DB_PASSWORD', 'example_password' ); define( 'DB_HOST', 'localhost' );
- Stop MySQL.
# service mysql stop
- Then we create a folder for the runtime socket and grant access to the MySQL user:
# mkdir -p /var/run/mysqld # chown mysql:mysql /var/run/mysqld
- We launch mysqld_safe with the –skip-grant-tables parameter and fork it into the background with &.
# mysqld_safe --skip-grant-tables &
- Hit ENTER to regain the prompt. Log into MySQL as root:
# mysql -u root
- Then to set the new password, we run:
mysql> use example_DB_name; mysql> FLUSH PRIVILEGES; mysql> GRANT ALL PRIVILEGES ON example_DB_name.* TO "example_username"@"localhost" IDENTIFIED BY "new_example_password"; mysql> FLUSH PRIVILEGES; mysql> EXIT
- Finally, we restart the VPS to verify MySQL starts properly at boot.
# reboot
Repair the WordPress Database
Another possible method for database repair is to add a directive to wp-config.php.
- Initially, we edit wp-config.php:
# nano /var/www/html/wp-config.php
- Then we insert the WP_ALLOW_REPAIR directive just above the line, “That’s all, stop editing! Happy blogging“:
define( 'WP_ALLOW_REPAIR', true ); /* That's all, stop editing! Happy blogging. */
- Eventually, we save the file. Then exit the editor.
- Visit the following URL:
http://www.example.com/wp-admin/maint/repair.php
- We can select either Repair Database or Repair and Optimize Database. Both options repair the database. Optimization also removes deleted rows from tables, defragments, and compresses the database to improve performance.
- Finally, we remove the WP_ALLOW_REPAIR directive from wp-config.php to prevent unauthorized use.
[Need help with the troubleshooting? We are here for you]
Conclusion
In short, we saw how our Support Techs resolve the Vultr error.
0 Comments