Home > Database > Mysql Tutorial > body text

Why Am I Getting \'Access Denied\' for MySQL User \'root\' on Ubuntu 16.04?

Linda Hamilton
Release: 2024-11-02 12:56:30
Original
305 people have browsed it

Why Am I Getting

Access Denied for MySQL User 'root' Using Ubuntu 16.04

When attempting to connect to a MySQL database from a web server on Ubuntu 16.04, users may encounter the following error:

SQLSTATE[HY000] [1698] Access denied for user 'root'@'localhost'
Copy after login

This issue occurs because, in MySQL 5.7, the default authentication mechanism has changed, and the root user can no longer be accessed without elevated privileges.

Solution:

To resolve this error, users must create a new MySQL user with the necessary privileges instead of using the root user. The following steps outline the process:

  1. Create a New MySQL User:
sudo mysql -u root -p
Copy after login
Enter your root password when prompted.
CREATE USER new_user@localhost IDENTIFIED BY 'new_password';
Copy after login
Replace 'new_user' with the desired username and 'new_password' with a strong password.
  1. Grant Privileges to the New User:
GRANT ALL PRIVILEGES ON *.* TO new_user@localhost;
Copy after login
This grants the new user full access to all databases and tables.
  1. Configure Web Server to Use New User:

Modify the PHP code in the provided question to use the newly created user credentials, replacing 'root' with 'new_user' and 'root' with 'new_password'.

  1. Flush Privileges:
FLUSH PRIVILEGES;
Copy after login
This updates the privileges table with the changes made.
  1. Exit MySQL:
EXIT;
Copy after login

By following these steps, users can connect to their MySQL database from a web server on Ubuntu 16.04 without encountering the "Access denied" error.

The above is the detailed content of Why Am I Getting \'Access Denied\' for MySQL User \'root\' on Ubuntu 16.04?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!