ERROR 1698 (28000): Access denied for user 'root'@'localhost'

Posted Mr.Yao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ERROR 1698 (28000): Access denied for user 'root'@'localhost'相关的知识,希望对你有一定的参考价值。

安装完mysql之后使用root账户登录出现1698错误

pi@raspberrypi:~ $ mysql -uroot
ERROR 1698 (28000): Access denied for user ‘root‘@‘localhost‘
pi@raspberrypi:~ $

查看Mysql官方文档发现

Using Socket Pluggable Authentication

The socket plugin checks whether the socket user name (the operating system user name) matches the MySQL user name specified by the client program to the server. If the names do not match, the plugin checks whether the socket user name matches the name specified in the authentication_string column of the mysql.user system table row. If a match is found, the plugin permits the connection.(套接字插件检查套接字用户名(操作系统用户名)是否与客户端程序指定的MySQL用户名匹配到服务器。如果名称不匹配,插件将检查套接字用户名是否与mysql.user用户系统表行。如果找到匹配项,则插件允许连接。)

系统当前的用户是pi,要使用root账户必须使用sudo命令获得root身份再与Mysql连接。所以使用sudo mysql -uroot可以进入Mysql命令行。

pi@raspberrypi:~ $ sudo service mysql restart
pi@raspberrypi:~ $ mysql -uroot
ERROR 1698 (28000): Access denied for user ‘root‘@‘localhost‘
pi@raspberrypi:~ $ sudo mysql -uroot
Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MariaDB connection id is 33
Server version: 10.0.28-MariaDB-2+b1 Raspbian testing-staging

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type ‘help;‘ or ‘h‘ for help. Type ‘c‘ to clear the current input statement.

MariaDB [(none)]>

解决办法

1. 更改plugin(身份验证插件)

长期以来,MySQL支持不同的身份验证插件,在此使用mysql_native_password传统的身份验证方法,不是很安全(它仅使用密码的哈希值),但是与较旧的驱动程序兼容。

  1. sudo mysql -u root进入MySQL命令行
  2. update user set plugin="mysql_native_password" where user="root"; 要设置密码的话多加一条update user set password=PASSWORD("1989") where user="root";
  3. flush privileges;
  4. exit;
  5. sudo service mysql restart
pi@raspberrypi:~ $ sudo service mysql restart
pi@raspberrypi:~ $ mysql -uroot
Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MariaDB connection id is 32
Server version: 10.0.28-MariaDB-2+b1 Raspbian testing-staging

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type ‘help;‘ or ‘h‘ for help. Type ‘c‘ to clear the current input statement.

MariaDB [(none)]>

没有设置密码直接使用mysql -u root即可进入MySQL命令行。

2. 创建一个新的账户(pi)

  1. sudo mysql -u root进入MySQL命令行
  2. USE mysql
  3. CREATE USER ‘YOU_SYSTEM_USER‘@‘localhost‘ IDENTIFIED BY ‘‘;
  4. GRANT ALL PRIVILEGES ON *.* TO ‘YOUR_SYSTEM_USER‘@‘localhost‘;
  5. UPDATE user SET plugin=‘auth_socket‘ WHERE User=‘YOUR_SYSTEM_USER‘;
  6. FLUSH PRIVILEGES;
  7. exit;
  8. sudo service mysql restart

使用mysql -upi直接进入MySQL命令行。

pi@raspberrypi:~ $ mysql -upi
Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MariaDB connection id is 34
Server version: 10.0.28-MariaDB-2+b1 Raspbian testing-staging

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type ‘help;‘ or ‘h‘ for help. Type ‘c‘ to clear the current input statement.

MariaDB [(none)]>

查看用户表

pi@raspberrypi:~ $ mysql -upi
Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MariaDB connection id is 34
Server version: 10.0.28-MariaDB-2+b1 Raspbian testing-staging

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type ‘help;‘ or ‘h‘ for help. Type ‘c‘ to clear the current input statement.

MariaDB [(none)]> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mysql]> select user,host,plugin,password from user;
+------+-----------+-----------------------+----------+
| user | host      | plugin                | password |
+------+-----------+-----------------------+----------+
| root | localhost | mysql_native_password |          |
| pi   | localhost | unix_socket           |          |
+------+-----------+-----------------------+----------+
2 rows in set (0.00 sec)

MariaDB [mysql]>

以上是关于ERROR 1698 (28000): Access denied for user 'root'@'localhost'的主要内容,如果未能解决你的问题,请参考以下文章

ERROR 1698 (28000): Access denied for user 'root'@'localhost'

ERROR 1698 (28000): Access denied for user 'root'@'localhost' 解决方法

错误 1698 (28000): 拒绝用户 'root'@'localhost' 的访问

ERROR 1698 (28000): Access denied for user ‘root‘@‘localhost‘

ERROR 1698 (28000): Access denied for user ‘root‘@‘localhost‘

mysql安装以后无法登陆的的解决方法((ERROR 1698 (28000): Access denied for user 'root'@'localhost'))