简体   繁体   中英

"Error establishing a database connection" when config Wordpress + mysql 8.0x + nginx on macOS

I'am facing "Error establishing a database connection" by setting up wordpress on macOS. Here is my steps:

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'wordpress_database_***' );

/** MySQL database username */
define( 'DB_USER', 'db_user' );

/** MySQL database password */
define( 'DB_PASSWORD', 'db_pass' );

/** MySQL hostname */
define( 'DB_HOST', 'localhost' );

/** Database Charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8' );

/** The Database Collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '' );
  • Create db + user by commandline
$ sudo mysql -u admin -p
mysql> CREATE DATABASE db_name;
mysql> CREATE USER 'db_user'@'localhost' IDENTIFIED BY 'db_pass';
mysql> GRANT ALL PRIVILEGES ON db_name.* TO 'db_user'@'localhost';
mysql> FLUSH PRIVILEGES;
mysql> exit
$ sudo mysql.server restart
$ sudo nginx // start nginx 
  • Finally, I load mydomain.com (config in vhosts). And be always getting issues ("Error establishing a database connection")

Anyone experienced, pease give me some helps, Thanks

Finally, I found the solution.

  • With mysql 8.0x, the create user with mysql> CREATE USER 'db_user'@'localhost' IDENTIFIED BY 'db_pass'; , will assign authentication type as 'caching_sha2_password'.

  • I just change the command to create user as mysql> CREATE USER 'db_user'@'localhost' IDENTIFIED WITH mysql_native_password BY 'db_pass';

Now my wordpress site work perfectly.

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