1. 程式人生 > >mysql8關於遠端登入授權遇到的問題

mysql8關於遠端登入授權遇到的問題

問題:

mysql> grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
 corresponds to your MySQL server version for the right syntax
  to use near 'identified by '123456' with grant option' at line 1
mysql>

mysql> grant all on *.* to 'root'@'%';
ERROR 1410 (42000): You are not allowed to create a user with GRANT
mysql>

由於對mysql不太熟悉,原因不知道,可能是版本問題。

最後新建一個test使用者並授權成功,如下:

mysql> create user 'test'@'%' identified with mysql_native_password by '123456';
Query OK, 0 rows affected (0.12 sec)

mysql> select user,host from user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| test             | %         |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
5 rows in set (0.00 sec)

mysql> grant all privileges on . to 'test'@'%' with grant option;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '. to 'test'@'%' with grant option' at line 1
mysql>
mysql> grant all on *.* to 'test'@'%';
Query OK, 0 rows affected (0.12 sec)

mysql> grant all on *.* to 'root'@'%';
ERROR 1410 (42000): You are not allowed to create a user with GRANT
mysql>
mysql> flush privileges;     # 重新整理許可權
Query OK, 0 rows affected (0.04 sec)
mysql>

MySQL8和5的密碼加密方式不同,mysql_native_password是5的加密方式。mysql已經將之前的mysql_native_password認證,修改成了caching_sha2_password認證方式。所以,使用類似於navicat或是sqlyog這些客戶端時,預設使用還是mysql_native_password認證方式,所以即使輸入正確的使用者和密碼依然登入不成功。

測試:

PS C:\WINDOWS\system32> mysql -utest -h 192.168.13.3 -p
Enter password: ******
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 8.0.12 MySQL Community Server - GPL

Copyright (c) 2000, 2018, 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>