這裡簡單總結一下mysql shell訪問資料庫時報MySQL Error 1045 (28000): Access denied for user 'root'@'::1' (using password: YES)的原因以及如何解決這個問題 這裡測試的環境為MySQL 8.0.35,我們先來看看 ...
這裡簡單總結一下mysql shell訪問資料庫時報MySQL Error 1045 (28000): Access denied for user 'root'@'::1' (using password: YES)的原因以及如何解決這個問題
這裡測試的環境為MySQL 8.0.35,我們先來看看報錯案例:
$ mysqlsh -h localhost -P 7306 -u root -p
Please provide the password for 'root@localhost:7306': ***********
MySQL Shell 8.0.35
Copyright (c) 2016, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners.
Type '\help' or '\?' for help; '\quit' to exit.
Creating a session to 'root@localhost:7306'
MySQL Error 1045 (28000): Access denied for user 'root'@'::1' (using password: YES)
先用root賬號連接數據(socket方式),檢查用戶信息,如下所示,root賬號限定為localhost
$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 24
Server version: 8.0.35 MySQL Community Server - GPL
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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> select user,host from mysql.user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+------------------+-----------+
13 rows in set (0.00 sec)
mysql>
然後,檢查變數skip_name_resolve,如下所示,skip_name_resolve為ON
mysql> show variables like 'skip_name_resolve';
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| skip_name_resolve | ON |
+-------------------+-------+
1 row in set (0.01 sec)
mysql>
資料庫參數skip_name_resolve設置為ON,它是禁止功能變數名稱解析的,一般都推薦將這個參數設置為ON, 因此伺服器不會嘗試解析連接客戶端的名稱或每次都在主機名緩存中查找它們(甚至localhost也會被解析/搜索),根據官方文檔的解釋,它會限制@localhost的連接。
官方文檔的詳細解釋:
Depending on the network configuration of your system and the Host values for your accounts, clients may need to connect using an explicit --host option, such as --host=127.0.0.1 or --host=::1.
An attempt to connect to the host 127.0.0.1 normally resolves to the localhost account. However, this fails if the server is run
with skip_name_resolve enabled. If you plan to do that, make sure an account exists that can accept a connection. For example,
to be able to connect as root using --host=127.0.0.1 or --host=::1, create these accounts:CREATE USER 'root'@'127.0.0.1' IDENTIFIED BY 'root-password'; CREATE USER 'root'@'::1' IDENTIFIED BY 'root-password';
那麼怎麼解決這個問題呢?一共有下麵幾種方法。
方案1:skip_name_resolve設置為OFF
我們需要在參數文件my.cnf中 將參數skip-name-resolve註釋或者設置skip_name_resolve設置為OFF的.
註意事項,雖然官方文檔中,參數skip-name-resolve是Boolean類型,但是如果你像下麵這樣設置是不會生效的,具體原因不是很清楚
skip-name-resolve=0
或
skip-name-resolve=FALSE
正確的做法
方法1:
skip-name-resolve=OFF
方法2:
#skip-name-resolve 註釋掉參數
修改skip_name_resolve的值為OFF後,重啟一下MySQL實例,然後我們驗證一下測試結果。
mysql> show variables like 'skip_name_resolve';
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| skip_name_resolve | OFF |
+-------------------+-------+
1 row in set (0.01 sec)
mysql>
$ mysqlsh -h localhost -P 7306 -u root -p
MySQL Shell 8.0.35
Copyright (c) 2016, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners.
Type '\help' or '\?' for help; '\quit' to exit.
Creating a session to 'root@localhost:7306'
Fetching schema names for auto-completion... Press ^C to stop.
Your MySQL connection id is 10
Server version: 8.0.35 MySQL Community Server - GPL
No default schema selected; type \use <schema> to set one.
MySQL localhost:7306 ssl JS >
這種方案需要修改參數,需要重啟MySQL實例,所以一般來說,不建議使用。
方案2:新增賬號
如下所示,我們新增下麵賬號
CREATE USER 'root'@'::1' IDENTIFIED BY '********';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'::1';
FLUSH PRIVILEGES;
當然,如果報錯為MySQL Error 1045 (28000): Access denied for user 'root'@'127.0.0.1' (using password: YES)的話,那麼可以創建下麵用戶
CREATE USER 'root'@'127.0.0.1' IDENTIFIED BY '**********';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1';
FLUSH PRIVILEGES;
創建賬號後,mysqlsh就可以連接,不會報上面錯誤了,如下所示:
$ mysqlsh -h localhost -P 7306 -u root -p
Please provide the password for 'root@localhost:7306': ***********
Save password for 'root@localhost:7306'? [Y]es/[N]o/Ne[v]er (default No): y
MySQL Shell 8.0.35
Copyright (c) 2016, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners.
Type '\help' or '\?' for help; '\quit' to exit.
Creating a session to 'root@localhost:7306'
Fetching schema names for auto-completion... Press ^C to stop.
Your MySQL connection id is 20
Server version: 8.0.35 MySQL Community Server - GPL
No default schema selected; type \use <schema> to set one.
MySQL localhost:7306 ssl JS >
方案3:使用socket方式連接
mysql shell也可以使用socket連接,一般的方式如下:
mysqlsh -h localhost -u root -p -S /tmp/mysql.sock #根據實際情況填寫具體的mysql.sock文件
或
\connect root@localhost?socket=(/tmp/mysql.sock)
測試驗證如下所示:
$ mysqlsh -h localhost -u root -p -S /tmp/mysql.sock
Please provide the password for 'root@/tmp%2Fmysql.sock': ***********
Save password for 'root@/tmp%2Fmysql.sock'? [Y]es/[N]o/Ne[v]er (default No): y
MySQL Shell 8.0.35
Copyright (c) 2016, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners.
Type '\help' or '\?' for help; '\quit' to exit.
Creating a session to 'root@/tmp%2Fmysql.sock'
Fetching schema names for auto-completion... Press ^C to stop.
Your MySQL connection id is 22
Server version: 8.0.35 MySQL Community Server - GPL
No default schema selected; type \use <schema> to set one.
MySQL localhost JS >