Home Website TutorialsWordPress How to fix “Error establishing a database connection” on WordPress

How to fix “Error establishing a database connection” on WordPress

by Thạch Phạm
Published: Last Updated on

In this article, AZDIGI will guide you to fix the error: Error establishing a database connection when accessing your WordPress website. This error is displayed because your WordPress website cannot connect to the database server, so to handle this error, we will have several cases:

  • WordPress website cannot connect to the database due to wrong connection information.
  • The database prefix does not match the $table_prefix value declared in wp-config.phbp
  • The server is overloaded, causing the MySQL service can’t work.
  • If using VPS/Dedicated Server, there may not be enough hard disk space for MySQL to write data.

Check connection information in wp-config.php

In the WordPress source code, wp-config.php is the most important file. This is where you define the details for WordPress to connect to your database. If you change the MySQL database password, you need to update the database information again. The database information section of a page has the following form:

AZDIGI Tutorial
define('DB_NAME', 'database-name');
define('DB_USER', 'database-username');
define('DB_PASSWORD', 'database-password');
define('DB_HOST', 'localhost');    

Your DB_Host value may not be localhost but depending on the host, it will be different. Usually, shared hosting has a value of localhost. If you are using MySQL remote from another site you may need to keep an eye on updates.

If everything in the wp-config.php file is correct (be sure to check for typos), then there is definitely something wrong with your hosting/server setup.

Check User and Database permissions

You need to make sure that the user configured in wp-config.php has full permission to manipulate the database. And in cPanel, you update the permissions as follows:

chrome lIMRgPSpXj 1
  • You check the database permissions for users inside the MySQL admin page.
chrome dB1Lqwarlg 1
  • You check if the user has full rights to the database in the Add User To Database section
chrome rs2pj5MpXV 1
  • You click the tick on full permissions and click Change.
chrome dw0nU95LA7 1

Check table prefix value

You should make sure that your prefix value is set to the same as on the database, for example, in wp-config.php the value:

$table_prefix = 'wp_';

On my database must also be the correct wp_. As shown below, I set it as wp0l_

  • Note: You can set it as you like
chrome j0mJiZ9ckA 1

Check that the mysql extension in PHP is enabled.

Check PHP’s Mysql, Mysqli extension. Hosting using Cloudlinux will be equipped with a PHP Selector function that allows enabling/disabling PHP Extension. Now you need to check if the Mysql Extension is enabled or not.

chrome XSBpP6VrBF
  • For PHP 5, you need to make sure the following extensions are enabled:
Screenshot 81
Screenshot 82
  • For PHP 7.x, you need to make sure the following extensions are enabled:
Screenshot 79
Screenshot 80

Try Repair WordPress Database

Maybe sometimes your database error, you can try to fix the database error by adding the following line to the file: wp-config.php

define('WP_ALLOW_REPAIR', true);

Then you access the link: https://tên_web_site_của_bạn.com/wp-admin/maint/repair.php

If all of the above methods fail, you can contact your hosting administrator to support you.

Đánh giá

Tham gia nhóm hỗ trợ Server - Hosting

Tham gia nhóm Hỗ trợ Server - Hosting & WordPress để cùng nhau hỏi đáp và hỗ trợ các vấn đề về WordPress, tối ưu máy chủ/server.

Tham gia ngay

Bài viết cùng chuyên mục

AZDIGI – Không chỉ là đơn vị hàng đầu trong lĩnh vực Web Hosting và Máy chủ, chúng tôi mong muốn mang lại những kiến thức bổ ích nhất và luôn cập nhật thường xuyên cho cộng đồng người đam mê thiết kế website, công nghệ,…

Vui lòng không sao chép nội dung nếu chưa xin phép. Designed and Developed by PenciDesign