Introduction:
When attempting to access a MySQL database from PHP or phpMyAdmin, you may encounter the following error:
SQLSTATE[HY000] [1698] Access denied for user 'root'@'localhost'
This error indicates that the user attempting to connect to the database lacks the necessary privileges.
Solution:
A recent change in MySQL 5.7 prevents the root user from connecting to the database without elevated privileges. To resolve this issue, follow these steps:
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON *.* TO 'newuser'@'localhost' WITH GRANT OPTION;
Modify the PHP code to connect using the new user credentials:
<code class="php">protected $name = 'newuser'; protected $pass = 'password';</code>
If you wish to continue using the root user, you will need to elevate your privileges by running commands with sudo:
<code class="sh">sudo mysql -u root</code>
Additional Considerations:
The above is the detailed content of How to Fix 'SQLSTATE[HY000] [1698] Access denied for user 'root'@'localhost'' Error in Ubuntu 16.04?. For more information, please follow other related articles on the PHP Chinese website!