When using a browser to access a website built on WordPress, you sometimes encounter the "Error establishing database connection"or"Error establishing a database connection" error message. This article will detail the cause and solution of this problem to help you quickly get your website back up and running.
Description of the problem
When you visit a WordPress website in your browser, you may see the following error message:
- "Error establishing database connection."
- "Error establishing a database connection"
This usually means that WordPress is not connecting properly to theMySQL Database, causing the site to fail to load.
Cause of the problem
The problem is usually caused by the accessed WordPress web program not being able to connect to the MySQL database properly. Common causes include:
- MySQL database service not running
- Incorrect database login credentials (user name, password or database name)
- Database server address error
- Database corruption
prescription
To resolve this issue, you need to check if the MySQL database service is running properly and reconfigure the database connection information in your WordPress website program. Below are the specific steps to do this:
1. Check MySQL database service status
Log in to your MySQL database server and execute the following command to view the status of the MySQL database service:
systemctl status mysql
If the MySQL database service is running in the state active (running)
, indicating that the database service is running normally, displays the following message:
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2023-07-03 15:57:24 CST; 1h 30min ago
...
If the MySQL database service is running in the state inactive (dead)
, indicating that the MySQL database service has been stopped, displays the following message:
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Mon 2023-07-03 14:57:34 CST; 1h 30min ago
...
In this case, the MySQL database service needs to be started. Execute the following command to start the service:
systemctl start mysql
If you are prompted with an ERROR error during startup, you need to modify the database service configuration according to the error message and then re-execute the startup command.
2. Testing MySQL database connections
After confirming that the MySQL database service is running properly, test whether you can log in to the MySQL database properly. Execute the following command:
mysql -u -h -p
Description:
<User>
: MySQL database username<IP>
: IP address of the MySQL database<Password>
: MySQL database password
If you are prompted to log in successfully, but the database password is wrong, you need to change the password of MySQL database again. Please refer to the official MySQL documentation for details.
3. Modify the WordPress configuration file
After confirming that the database password is correct and you can log in properly, change the database connection information in the WordPress website program. Edit wp-config.php
file by replacing the file's DB_PASSWORD
The database password corresponding to the field is replaced with the correct MySQL database password.
with the CentOS 7 operating system,wp-config.php
The file path is /usr/share/nginx/html/wordpress
As an example, execute the following command:
vim /usr/share/nginx/html/wordpress/wp-config.php
Find the following:
/** MySQL database password */
define('DB_PASSWORD', 'your_database_password');
commander-in-chief (military) 'your_database_password'
Replace the MySQL database password with the correct one.
To save and exit the file, type :wq
and then press Enter
Key.
summarize
With the above steps, you can effectively solve the WordPress website prompt "Error establishing database connection"or"Error establishing a database connection"Error. Ensuring that the MySQL database service is running properly and that the database connection information is properly configured in the WordPress configuration file is key to getting your website back up and running.