mariadb登陆报错: 1698 - Access denied for user

通过shell终端中可以免密码登陆mysql -uroot,但是使用navicat for mariadb登陆报错:1698 - Access denied for user 'root'@'localhost'

Server version: 10.1.38-MariaDB-0+deb9u1 Debian 9.8

MariaDB [mysql]> select user,host,plugin from user where user='root'\G;
*************************** 1. row ***************************
  user: root
  host: %
plugin: unix_socket
1 row in set (0.00 sec)

可以看到root仅仅允许本地unix socket登陆。

关于unix_socket的介绍:

The unix_socket authentication plugin allows the user to use operating system credentials when connecting to MariaDB via the local Unix socket file. This Unix socket file is defined by the socket system variable.
The unix_socket authentication plugin works by calling the getsockopt system call with the SO_PEERCRED socket option, which allows it to retrieve the uid of the process that is connected to the socket. It is then able to get the user name associated with that uid. Once it has the user name, it will authenticate the connecting user as the MariaDB account that has the same user name.
From : https://mariadb.com/kb/en/authentication-plugin-unix-socket/

要解决这个问题,我们可以mysql.user表中root用户所在行的plugin字段修改为空,修改后root从shell终端登陆也需要密码。为了避免忘记root密码后的麻烦,我选择新创建一个数据库用户。

# myql -uroot
MariaDB [(none)]> use mysql;
Database changed
MariaDB [mysql]> create user 'sure'@'%' identified by '1234567'
Query OK, 0 rows affected (0.00 sec)
MariaDB [mysql]> select user,host,plugin from user;
+------+------+-------------+
| user | host | plugin      |
+------+------+-------------+
| root | %    | unix_socket |
| sure | %    |             |
+------+------+-------------+
MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

可以看到,新创建用户sure,plugin字段值默认就为空,到这里问题得到解决。

When the plugin column is empty, MariaDB defaults to authenticating accounts with either the mysql_native_password or the mysql_old_password plugins. It decides which based on the hash used in the value for the Password column. When there’s no password set or when the 4.1 password hash is used, (which is 41 characters long), MariaDB uses the mysql_native_password plugin. The mysql_old_password plugin is used with pre-4.1 password hashes, (which are 16 characters long).
From: https://mariadb.com/kb/en/mysqluser-table/#authentication-plugin

大致是说plugin字段为空时,默认会使用默认的基于密码登陆方式。

mysql.user表各字段含义:https://mariadb.com/kb/en/mysqluser-table/

mariadb更新用户密码https://www.techonthenet.com/mariadb/change_password.php

猜你喜欢

转载自blog.csdn.net/lx1848/article/details/106592967