MySQL使用者遠端登入問題
SQL/">MySQL基本簡單操作
今天來水一篇,差點被這個遠端登入搞死了,所以記錄下這個問題。
先使用status
命令檢視一下MySQL
版本,這裡可以可以看到我用的是是8.0.12
版本。
mysql> status -------------- mysqlVer 8.0.12 for Linux on x86_64 (MySQL Community Server - GPL) Connection id:14 Current database:test Current user:root@localhost SSL:Not in use Current pager:stdout Using outfile:'' Using delimiter:; Server version:8.0.12 MySQL Community Server - GPL Protocol version:10 Connection:Localhost via UNIX socket Server characterset:utf8mb4 Dbcharacterset:utf8mb4 Client characterset:latin1 Conn.characterset:latin1 UNIX socket:/var/run/mysqld/mysqld.sock Uptime:25 min 14 sec Threads: 3Questions: 201Slow queries: 0Opens: 333Flush tables: 2Open tables: 309Queries per second avg: 0.132 --------------
如果什麼都不設定,那麼用PHP連線時會報錯:
Warning: mysqli::__construct(): The server requested authentication method unknown to the client [caching_sha2_password] Warning: mysqli::__construct(): (HY000/2054): The server requested authentication method unknown to the client
caching_sha2_password
報錯是因為加密規則的原因。
可以在mysql
資料庫下檢視相關資訊。
mysql> use mysql; mysql> select host, user, authentication_string, plugin from user;
解決方法
首先修改遠端使用者加密規則
mysql> ALTER USER 'root'@'%' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER; Query OK, 0 rows affected (0.03 sec)
然後更新密碼
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456'; Query OK, 0 rows affected (0.08 sec)
重新整理許可權
mysql> flush privileges; Query OK, 0 rows affected (0.01 sec)
搞定!
授權所有許可權遠端登入
GRANT ALL ON *.* TO 'root'@'%' WITH GRANT OPTION; 8.0版本的授權命令
重新整理許可權
mysql> flush privileges;