mysqli_real_connect() – Access denied for user ‘root’@’localhost’

After migrating a development environment to a new server a problem connecting WordPress to the MySQL database appeared.

Using the MySQL root user with no password for a WordPress installation used to be fine:

PHP
wp-config.php
/** MySQL database username */
define('DB_USER', 'root');

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

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

How ever since MySQL 5.7 using the root user with no password seems to be a dealbreaker, resulting in the following error for WordPress:

Warning: mysqli_real_connect(): (HY000/1698): Access denied for user 'root'@'localhost' in /var/www/html/site/public_html/wp-includes/wp-db.php on line 1635
Access denied for user 'root'@'localhost'
Error establishing a database connection

The solution can be to create a new MySQL user, give it all the permissions and use it with WordPress:

bash
Creating another MySQL user
bash> sudo mysql -u root -p
mysql> CREATE USER 'user'@'localhost' IDENTIFIED BY 'pass';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'user'@'localhost' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;

and then modify your wp-config.php accordingly:

PHP
wp-config.php
/** MySQL database username */
define('DB_USER', 'user');

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

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

Leave a comment

Your email address will not be published. Required fields are marked *