wp-cli / config-command

Generates and reads the wp-config.php file.
MIT License
38 stars 36 forks source link

WP Config Create is broken with 2.9.0 if socks MySQL connectivity is being used #169

Closed aonsyed closed 11 months ago

aonsyed commented 1 year ago

Bug Report

Describe the current, buggy behavior

Since 2.9.0 last night, wp config create doesn't work if --dbhost=localhost:/var/run/mysqld/mysqld.sock is used, there is no issues with with the sock or configuration and everything works correctly if the config is manually created, only the create command is not working

Describe how other contributors can replicate this bug

Make sure that your database server is set to listen to socks and TCP port. Run something like this

/usr/local/bin/wp config create --dbname=dbname --dbuser=dbuser --dbpass=somevalue --dbprefix=wp_ --url=site.com --dbhost=localhost:/var/run/mysqld/mysqld.sock --allow-root --skip-themes --skip-packages --skip-plugins --path=/var/www/site.com/htdocs

And it will give you this error and there won't be a file

Error: Database connection error (2002) Connection refused

Describe what you would expect as the correct outcome

However if you run this

/usr/local/bin/wp config create --dbname=dbname --dbuser=dbuser --dbpass=somevalue --dbprefix=wp_ --url=site.com --dbhost=localhost --allow-root --skip-themes --skip-packages --skip-plugins --path=/var/www/site.com/htdocs

If will show you success message and fix config file will be created

Let us know what environment you are running this on

Tested this on Ubuntu 18, 20, 22 with multiple versions of Percona and mariaDB, wp --info remains identical

Provide a possible solution

Rolling back to 2.8.1

Provide additional context/Screenshots

N/A

If applicable, add screenshots to help explain (you can just drag&drop images into the Github issue).

swissspidy commented 1 year ago

Thanks for your report!

This might be related to #158

In the meantime, have you tried running wp config create with the --skip-check flag to skip the database connection check?

aonsyed commented 1 year ago

Hi

The connection test can be modified as this

$mysql = mysqli_init();
mysqli_report(MYSQLI_REPORT_STRICT);

try {
    if (file_exists($assoc_args['dbhost'])) {
        // If dbhost is a path to a socket
        mysqli_real_connect($mysql, null, $assoc_args['dbuser'], $assoc_args['dbpass'], null, null, $assoc_args['dbhost']);
    } else {
        // If dbhost is a hostname or IP address
        mysqli_real_connect($mysql, $assoc_args['dbhost'], $assoc_args['dbuser'], $assoc_args['dbpass']);
    }
} catch (mysqli_sql_exception $exception) {
    WP_CLI::error('Database connection error (' . $exception->getCode() . ') ' . $exception->getMessage());
}
danielbachhuber commented 1 year ago

@aonsyed Up for submitting a pull request with your suggested changes?

Here is some guidance on our pull request best practices.

wojtekn commented 1 year ago

@danielbachhuber I'm going to work on this.

schlessera commented 11 months ago

Fixed via #171