mysql 8.0.16 Linux新建用户无法登录

报错原因:ERROR 1045 (28000): Access denied for user 'ippbx_admin'@'localhost' (using password: YES)。
8.0.16版本与先前的一些版本添加用户有些改变,一行指令就搞定。演示如下:
ysql> create user 'admin'@'%' identified with caching_sha2_password by 'xhai-123'
-> ;
Query OK, 0 rows affected (0.26 sec)

mysql> select user,host,authentication_string,plugin from mysql.user;
+------------------+-----------+------------------------------------------------------------------------+-----------------------+
| user | host | authentication_string | plugin |
+------------------+-----------+------------------------------------------------------------------------+-----------------------+
| admin | % | $A$005$q0{3bD]bt#>R9SeXb6Z4OS5mdwruFXD7TdiNzovrbbuI5xVxQGOSyKnL5 | caching_sha2_password |
| aplan | % |
B2E0C40A5667BF1783A28667466E1399EB00FBDF | mysql_native_password |
| ippbx_admin | 127.0.0.1 | $A$005$g^8x#aXm)T7{n94IvOO8/gPE7qqaVivgEIZ.oh/nrSVG8JCNJfskJBu6j22r. | caching_sha2_password |
| mysql.infoschema | localhost | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| mysql.session | localhost | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| mysql.sys | localhost | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| root | localhost | $A$005$wcJW
k,I\WN.&L+T%V4QQKnlXTx21I/fXRAaWPOco0PgVi6Md3KzdNjZKBB0 | caching_sha2_password |
+------------------+-----------+------------------------------------------------------------------------+-----------------------+
8 rows in set (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.20 sec)

mysql> exit
Bye
root@root:/b# mysql -u admin -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 42
Server version: 8.0.16 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> exit
Bye

可以发现以不限制登录主机的方式可以成功。

数据库中有一个ippbx_admin用户,我们使用insert into 方式添加(insert into user(host,user) values("127.0.0.1","ippbx_admin");),尝试登录却没能成功:
root@root:/b# mysql -u ippbx_admin -p
Enter password:
ERROR 1045 (28000): Access denied for user 'ippbx_admin'@'localhost' (using password: YES)

可以发现使用localhost的默认解析,而数据库中的是127.0.0.1,先前也没注意这个问题,浪费了好久时间,于是改变数据库中host字段为localhost

mysql> update user set host="localhost" where user= "ippbx_admin";
Query OK, 1 row affected (0.17 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> exit
Bye
root@root:/b# mysql -u ippbx_admin -p
Enter password:
ERROR 1045 (28000): Access denied for user 'ippbx_admin'@'localhost' (using password: YES)

发现还是没能登录,之后我们使用alter 去修改密码,注意'ippbx_admin'@'localhost' 需要与数据库保持一致
mysql> alter user 'ippbx_admin'@'localhost' identified with caching_sha2_password by 'xhai-123';
Query OK, 0 rows affected (0.13 sec)

神奇的发现可以登录了,nice
root@root:~# mysql -u ippbx_admin -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 60
Server version: 8.0.16 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql>

相关指令使用:
1、select user,host,authentication_string,plugin from mysql.user;#查看用户信息,user表中不在使用password字段,改为使用authentication_string,取值参考如下;

The caching_sha2_password and sha256_password authentication plugins provide more secure password encryption than the mysql_native_password plugin, and caching_sha2_password provides better performance than sha256_password. Due to these superior security and performance characteristics of caching_sha2_password, it is as of MySQL 8.0 the preferred authentication plugin, and is also the default authentication plugin rather than mysql_native_password.

2、GRANT ALL PRIVILEGES ON . TO 'ippbx_admin'@'localhost' WITH GRANT OPTION;#授予test账户所有权限.

PS:相同的密码在数据库中存储了不同的值,账户应该更加安全。

猜你喜欢

转载自www.cnblogs.com/xhai/p/11220945.html
今日推荐