報錯原因: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 '******'
-> ;
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 '******';
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>
另一種是保持和數據庫中127.0.0.1一致,在命令行上添加地址,我們就不用修改成localhost登錄
root@root:~# mysql -u ippbx_admin -p
Enter password:
ERROR 1045 (28000): Access denied for user 'ippbx_admin'@'localhost' (using password: YES)
root@root:~# mysql -u ippbx_admin -h 127.0.0.1 -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 79
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:相同的密碼在數據庫中存儲了不同的值,賬戶應該更加安全。