0

I'm getting Error establishing a database connection when installing WP.

The following prints "Connected" when putting into wp-config.php:

$mysqli_connection = mysqli_connect(
  $_dbsettings['host'],
  $_dbsettings['user'],
  $_dbsettings['pass'],
  trim( $_dbsettings['path'], '/' ),
  $_dbsettings['port']
);

if ($mysqli_connection->connect_error) {
   echo "Not connected, error: " . $mysqli_connection->connect_error;
}
else {
   echo "Connected.";
}

What's going on?

Update:

I use heroku-wp, more about the wp-config.php here.

2 Answers 2

1

Maybe if you configured the wp-config.php file like the documentation says (see https://wordpress.org/support/article/editing-wp-config-php/ )

Sample wp-config.php file (database section), per the above link:

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'database_name_here' );
/** MySQL database username */
define( 'DB_USER', 'username_here' );
/** MySQL database password */
define( 'DB_PASSWORD', 'password_here' );
/** MySQL hostname */
define( 'DB_HOST', 'localhost' );

Your code is proper for making a 'manual' connection to the database, but it does not set the DEFINES that WP will use for connections.

5
  • Values stored in $_dbsettings are fed into define(...).
    – haxpanel
    Feb 22, 2020 at 19:32
  • @haxpanel - I can't find any docs for $_dbsettings feeding into the database DEFINEs. Got a link? Feb 23, 2020 at 1:49
  • Please have a look at github.com/xyu/heroku-wp/blob/master/public/wp-config.php
    – haxpanel
    Feb 23, 2020 at 22:21
  • So those settings are only valid on a Heroku hosting environment? Which may not be what the OP is using? (Wasn't mentioned; I suspect not.) My answer is still correct for the majority of WP hosting places, I'd bet. Feb 23, 2020 at 22:33
  • Values from $_dbsettings are fed into the define(...) which is practically the same as what your code does.
    – haxpanel
    Feb 24, 2020 at 8:15
1

Apart from the check that you have to do with your WordPress Database Credentials, also you can check and repair your database

Navigate to your root WordPress file directory and find the wp-config. This file contains your WordPress installation settings and configuration. You can add this line of code of the bottom :

define( 'WP_ALLOW_REPAIR', true );

This line will enable you from one hand to optimize and on the other hand to repair your database by navigating to www.yourwebsite.com/wp-admin/maint/repair.php (you can replace the “yourwebsite.com” with your actual URL). Then will see a screen with two options repair database and repair and optimize your database. Feel free and choose your option, but the option of repair and optimize will take longer.

In the end, please keep in mind that the database repair page is not secure because anyone can access that URL without having to login. Once you have done with this process and you are done repairing your database, then be sure to remove the line of code you added to wp-config, I refer to define( 'WP_ALLOW_REPAIR', true );

1
  • Unfortunately this did not make any effect. I'm having the issue without having done any changes in the database, DB is empty. WP should create tables as part of the installation, however it seems to be unable to connect. :-/
    – haxpanel
    Feb 23, 2020 at 22:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.