WordPress Database Error establishing a database connection
I recently did a site migration, and migrated back again because of a master-slave synchronization problem, but I didn’t know how to do another site’s database connection to Error when I solved the synchronization. error is shown in the figure (Error establishing a database connection)
determine whether the password is correct
- Go to the website directory to check the database information of
Test the password with the database login information
mysql -uUsername -p
- If you can log in successfully, check if the relevant table permissions exist, if not, cut the root user to change the password.
# I'm on 5.7 here update mysql.user set authentication_string=password('passwd') where user='Username' and Host = 'localhost'; # flush privileges flush privileges;
- If root can’t switch either, change the database configuration, mainly by adding the following to
[mysqld]in the mysql configuration file.
#Bypass password authentication skip-grant-tables
- If that doesn’t work, try a database restart
- If you can cut through the table
- If you can’t, switch the root user to give the relevant privileges
mysql -uroot -p
grant all privileges on `database_name`. * to 'user_name'@'hostname';
Repair the database
- Add the following code to wp-config.php
- Log in to repair
https://yourdomain.com/wp-admin/maint/repair.php, and click on repair.
- If the repair is not good, it depends on the database backup situation, I finally started debug mode (wp-config.php enabled, logged into the background to see the error to check the problem is not authorized to the right permissions) plus the database backup to restore. It’s important to back up your data, but I’ve stepped in a hole, full of tears.
define( 'WP_DEBUG', true );