简体   繁体   中英

Wordpress - Error establishing a database connection

Having changed nothing on my Wordpress based site, a few days ago it simply stopped functioning. Prior to that, I was getting a larger than normal number of "site down' messages from a service that I use to monitor this site.

Now, all I get when trying to access the site is the error listed in the title. ONLY the message "Error establishing a database connection" shows on the page - nothing else.

I have other blog sites running on the same server and they are all still functioning. I can access the database for this website with phpmyadmin no problem. I have also done a "repair" on the wp-options table through phpmyadmin (recommended on another site).

Lastly, I created a new php file that makes a connection to the database and then echo's data from the database upon connection. I transferred all database connection data directly from the wp-config file for this site to this new php file, so it's connecting with the exact same info.

No problems connecting with this file.

However, if I clear out this file and then place only the following code in it:

    <?php
    require_once '/home/bestsynt/public_html/wp-config.php';
global $current_user;
get_currentuserinfo();
$level = $current_user->user_level;
echo $level;
    ?>

I receive the exact same error message.

Since this is a relatively high traffic site and it's been down now for two days. Since the problem doesn't appear to be on the "server/host" end, I'm not likely to get much support on that end. It's something to do with Wordpress itself, but I have no idea what.

在此处输入图片说明

Add 127.0.0.7:3307 port in database host instead of localhost and MySQL port 3307 or your MySQL port no. after database host

Most of the problems installing Wordpress related to: "Error establishing a database connection", are caused for a bad configuration in wp-config.php

define('WP_HOME','http://www.mysite.com');
define('WP_SITEURL','http://www.mysite.come');
define('DB_NAME', 'mydbname');
/** MySQL database username */
define('DB_USER', 'dbusername');
/** MySQL database password */
define('DB_PASSWORD', 'dbpassword');
/** MySQL hostname */
define('DB_HOST', 'localhost'); //If you are not using default port number
  • We must be sure that the user "dbusername" has the right persmissions over the database "mydbname"
  • the localhost requires or not setting a port.
  • by default try using the user and password "root" that has all the permissions granted:

    define('DB_USER', 'root');

    define('DB_PASSWORD', 'root');

before that You need to make sure that you:

  • Have a database created for the Wordpress installation.
  • Have a database user and password that is granted permission to access that database.

go to WordPress folder open config file change your database name in DB NAME , username DB_USER,password DB_PASSWORD db host in DB_HOST or

define('WP_ALLOW_REPAIR', true);

add this in your config file

One other scenario that causes this is due to Mysql8 users that are not set to have native password. You may have to do something as simple as

ALTER USER 'jeffrey'@'localhost'
  IDENTIFIED WITH mysql_native_password
             BY 'password';

and this will fix the issue. Just ran into it with a fresh WP install, and could not figure out why the credentials looked correct, but WP would keep saying it could not connect. Finally enabled debug:

define( 'WP_DEBUG', true );
define( 'WP_DEBUG_DISPLAY', true );

and this showed the actual error that was happening: "The server requested authentication method unknown to the client".

Hope this helps someone!

The very first thing you need to do is to turn on diagnostics so you can see exactly what the error is. In your wp-config.php file turn on WP_DEBUG...

define( 'WP_DEBUG', true );

Then restart your server. You will now see a detailed error message that will help identify and fix the problem.

As it turned out, it was a missing entry in my wp-options table within my wordpress database.

The following insert command issues through the SQL tab within Phpmyadmin solved the problem:

INSERT INTO `wp_options` VALUES (1,0,'siteurl','http://bestsyntheticoil.com/','yes')

Not sure exactly how this record was deleted since I haven't been working in that database. But, at least it's fixed now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM