How to fix 'Error establishing a database connection' in WordPress
Oct 16, 2025 am 10:38 AMIf you see "Error establishing a database connection" on WordPress, try these steps: 1. Check database credentials in wp-config.php. 2. Ensure the database server is running. 3. Repair the database using WordPress repair mode. 4. Increase PHP memory limit. 5. Test connection with a standalone script. 6. Restart web and database services.
If you are seeing the "Error establishing a database connection" message on your WordPress site, it means WordPress cannot communicate with the database. Here are several methods to resolve this issue:
The operating environment of this tutorial: Dell XPS 13, Windows 11
1. Verify Database Credentials in wp-config.php
This step ensures that WordPress is using the correct database name, username, password, and host. A typo or incorrect detail here will prevent connection.
- Access your website files via FTP or your hosting file manager.
- Locate the wp-config.php file in the root directory.
- Open the file and check the following lines:
define('DB_NAME', 'your_database_name'); define('DB_USER', 'your_username'); define('DB_PASSWORD', 'your_password'); define('DB_HOST', 'localhost');
- Confirm these values match your actual database credentials from your hosting panel.
- Save changes and reload your site.
2. Check if the Database Server is Running
The MySQL or MariaDB service might be stopped due to server issues or crashes. Restarting the service can restore connectivity.
- Log in to your hosting control panel (e.g., cPanel, Plesk).
- Navigate to the MySQL Databases section.
- Verify that the database status is active. If not, look for an option to restart the database server.
- If using a VPS or dedicated server, use SSH to run:
sudo systemctl status mysql
orsudo systemctl status mariadb
- If inactive, start it with:
sudo systemctl start mysql
3. Repair Corrupted WordPress Database
Database corruption can cause connection failures. WordPress has a built-in repair mode to fix common issues.
- Edit the wp-config.php file again.
- Add this line above the "That's all, stop editing!" comment:
define('WP_ALLOW_REPAIR', true);
- Visit https://yoursite.com/wp-admin/maint/repair.php in your browser.
- Click “Repair Database” or “Repair and Optimize Database.”
- After completion, remove or comment out the added line for security.
4. Increase PHP Memory Limit
Insufficient memory can interrupt database processes. Increasing the limit may resolve unexpected disconnections.
- Edit the wp-config.php file.
- Add this line:
define('WP_MEMORY_LIMIT', '256M');
- Alternatively, edit the php.ini file on your server and set:
memory_limit = 256M
- Save the file and restart your web server if necessary.
5. Test Database Connection with a Standalone Script
Use a simple PHP script to isolate whether the issue is specific to WordPress or a broader server configuration problem.
- Create a file named test-db.php in your site’s root folder.
- Add the following code, replacing placeholders with your database details:
<?php $connection = mysqli_connect('localhost', 'your_username', 'your_password', 'your_database'); if (!$connection) { die('Connection failed: ' . mysqli_connect_error()); } echo 'Connected successfully'; ?>
- Access the file via browser: https://yoursite.com/test-db.php.
- If it fails, the issue is server-side. If it succeeds, the problem lies within WordPress files or configuration.
6. Restart Web and Database Services
Temporary glitches in services can break connections. Restarting them often restores normal operation.
- If you have server access via SSH, run:
sudo systemctl restart apache2
orsudo systemctl restart nginx
- Then restart the database:
sudo systemctl restart mysql
- Wait a few moments and reload your WordPress site.
The above is the detailed content of How to fix 'Error establishing a database connection' in WordPress. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

ArtGPT
AI image generator for creative art from text prompts.

Stock Market GPT
AI powered investment research for smarter decisions

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

This error is usually caused by hardware problems in the storage device and can be solved by checking connections, scanning with CHKDSK, updating drivers, detecting hard disk health, and adjusting disk policies. 1. Check whether the hard disk connection is normal. External hard disk can be replaced with cables or interfaces. The internal hard disk needs to be restarted and plugged and confirmed to be recognized by BIOS; 2. Run the chkdskX:/f/r command as an administrator to scan and repair disk errors, and execute after restart; 3. Update or reinstall the hard disk driver through the device manager; 4. Use CrystalDiskInfo to view the SMART status. If there is a bad channel warning, immediately backup the data and replace the hard disk; 5. Modify the disk strategy for external hard disks to "better performance" or "quickly delete". If the above steps are invalid,

Tohideyourphonenumberwhenmakingacall,useoneofthesemethods:1.Dial67beforethenumberforatemporaryblock,whichworksonmostphonesandcarriersbutnotwithtoll-freeoremergencynumbers.2.ContactyourcarriertopermanentlyblockyourcallerID,anduse82beforeanumbertounblo

TheissueiscausedbytheFAT32filesystem,whichlimitsindividualfilestounder4GB;2.CheckyourUdisk’sformatviaFileExploreronWindowsorDiskUtilityonMac;3.ReformatthedrivetoexFATforcross-platformcompatibilityandlargefilesupport,orNTFSforWindows-onlyuse,afterback

CheckfordebrisintheheadphonejackorUSB-Cportandcleanit,asadirtyportcancausethephoneto誤detectaccessoriesandswitchtospeaker.2.Reviewaccessibilitysettings—onAndroid,gotoSettings>Accessibility>Hearing;oniPhone,gotoSettings>Accessibility>CallAu

The "0xc000007b" error is usually caused by system environment problems. The solutions are as follows: 1. Update or reinstall the graphics card driver, especially after replacing the graphics card or reinstalling the system; 2. Install or repair the Microsoft VisualC Redistributable package, it is recommended to install both x86 and x64 versions; 3. Run the system file checker (SFC) and DISM commands to repair the system files; 4. Check the program compatibility to match 32/64 bits, try to run as an administrator or set the compatibility mode.

EnsureBluetoothisenabledviaActionCenterandSettings,checkingforphysicalswitchesorfunctionkeysifnotvisible.2.Runthebuilt-inBluetoothtroubleshooterinSettingstoautomaticallyfixcommonissues.3.UpdatetheBluetoothdriverthroughDeviceManager,oruninstallandrein

Donotdouble-clickanunexpected.lnkfileonaUSBstick,asitmayexecutemalware.2.Right-clickthefileandcheckitsProperties,especiallytheTargetfield,toseeifitpointstoasuspiciousexecutableorscript.3.EnableviewingofhiddenfilesviaFileExplorerandFolderOptionstoreve

Mappednetworkdrivesnotconnectingcanbefixedbyfollowingthesesteps:1.Verifynetworkandshareaccessbyensuringthedeviceisonthesamenetwork,testingthepathviaRun(\??\IPor\??\ComputerName),confirmingfilesharingisenabled,andcheckingfirewallsettingsblockSMB(port4