Home >Database >Mysql Tutorial >Why Can\'t I Connect to MySQL as \'root\' in Ubuntu 16.04?

Why Can\'t I Connect to MySQL as \'root\' in Ubuntu 16.04?

Barbara Streisand
Barbara StreisandOriginal
2024-10-30 11:53:02794browse

Why Can't I Connect to MySQL as 'root' in Ubuntu 16.04?

Database Access Denied for 'root' User in Ubuntu 16.04

When accessing a database using a web server in Ubuntu 16.04, you may encounter the error "SQLSTATE[HY000] [1698] Access denied for user 'root'@'localhost'". This issue arises despite creating new users with all privileges.

Root User Restrictions in MySQL 5.7

The root cause of this error lies in the changes introduced in MySQL 5.7, which restricts the use of the 'root' user without sudo privileges. In previous versions, users could access MySQL as 'root' without elevating their privileges using the command "mysql -u root". However, in MySQL 5.7, this approach is no longer possible.

Workaround

To resolve this issue, you need to create a new user with the necessary privileges and use that user instead of 'root' when establishing database connections from your PHP application or other non-command line tools.

PHP Code

In your PHP code, replace the user name and password with the credentials of the newly created user:

<code class="php">protected $name = 'new_user';
protected $pass = 'new_password';</code>

Root Access from Command Line

While you cannot use the 'root' user directly in GUI or non-command line applications, you can still access MySQL as 'root' from the command line using sudo:

sudo mysql -u root

Remember that this approach elevates your privileges and should only be used when necessary. It is recommended to create a dedicated user for database management and use that user instead of 'root' for security reasons.

The above is the detailed content of Why Can\'t I Connect to MySQL as \'root\' in Ubuntu 16.04?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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