标题: MySQL Shell连接数据库报MySQL Error 1045 (28000)错误浅析 [打印本页] 作者: 拉不拉稀肚拉稀 时间: 2023-12-21 04:56 标题: MySQL Shell连接数据库报MySQL Error 1045 (28000)错误浅析 这里简单总结一下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<br>Please provide the password for 'root@localhost:7306': ***********<br>MySQL Shell 8.0.35<br><br>Copyright (c) 2016, 2023, Oracle and/or its affiliates.<br>Oracle is a registered trademark of Oracle Corporation and/or its affiliates.<br>Other names may be trademarks of their respective owners.<br><br>Type '\help' or '\?' for help; '\quit' to exit.<br>Creating a session to 'root@localhost:7306'<br>MySQL Error 1045 (28000): Access denied for user 'root'@'::1' (using password: YES)<br>
$ mysql -uroot -p<br>Enter password: <br>Welcome to the MySQL monitor. Commands end with ; or \g.<br>Your MySQL connection id is 24<br>Server version: 8.0.35 MySQL Community Server - GPL<br><br>Copyright (c) 2000, 2023, Oracle and/or its affiliates.<br><br>Oracle is a registered trademark of Oracle Corporation and/or its<br>affiliates. Other names may be trademarks of their respective<br>owners.<br><br>Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.<br><br>mysql> select user,host from mysql.user;<br>+------------------+-----------+<br>| user | host |<br>+------------------+-----------+<br>| mysql.infoschema | localhost |<br>| mysql.session | localhost |<br>| mysql.sys | localhost |<br>| root | localhost |<br>+------------------+-----------+<br>13 rows in set (0.00 sec)<br><br>mysql> <br>
mysql> show variables like 'skip_name_resolve';<br>+-------------------+-------+<br>| Variable_name | Value |<br>+-------------------+-------+<br>| skip_name_resolve | ON |<br>+-------------------+-------+<br>1 row in set (0.01 sec)<br><br>mysql><br>
mysql> show variables like 'skip_name_resolve';<br>+-------------------+-------+<br>| Variable_name | Value |<br>+-------------------+-------+<br>| skip_name_resolve | OFF |<br>+-------------------+-------+<br>1 row in set (0.01 sec)<br><br>mysql><br>
复制代码
$ mysqlsh -h localhost -P 7306 -u root -p<br>MySQL Shell 8.0.35<br><br>Copyright (c) 2016, 2023, Oracle and/or its affiliates.<br>Oracle is a registered trademark of Oracle Corporation and/or its affiliates.<br>Other names may be trademarks of their respective owners.<br><br>Type '\help' or '\?' for help; '\quit' to exit.<br>Creating a session to 'root@localhost:7306'<br>Fetching schema names for auto-completion... Press ^C to stop.<br>Your MySQL connection id is 10<br>Server version: 8.0.35 MySQL Community Server - GPL<br>No default schema selected; type \use <schema> to set one.<br> MySQL localhost:7306 ssl JS > <br>
复制代码
这种方案需要修改参数,需要重启MySQL实例,所以一般来说,不建议使用。
方案2:新增账号
如下所示,我们新增下面账号
CREATE USER 'root'@'::1' IDENTIFIED BY '********';<br>GRANT ALL PRIVILEGES ON *.* TO 'root'@'::1';<br>FLUSH PRIVILEGES;<br>
复制代码
当然,如果报错为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 '**********';<br>GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1';<br>FLUSH PRIVILEGES;<br>
复制代码
创建账号后,mysqlsh就可以连接,不会报上面错误了,如下所示:
$ mysqlsh -h localhost -P 7306 -u root -p<br>Please provide the password for 'root@localhost:7306': ***********<br>Save password for 'root@localhost:7306'? [Y]es/[N]o/Ne[v]er (default No): y<br>MySQL Shell 8.0.35<br><br>Copyright (c) 2016, 2023, Oracle and/or its affiliates.<br>Oracle is a registered trademark of Oracle Corporation and/or its affiliates.<br>Other names may be trademarks of their respective owners.<br><br>Type '\help' or '\?' for help; '\quit' to exit.<br>Creating a session to 'root@localhost:7306'<br>Fetching schema names for auto-completion... Press ^C to stop.<br>Your MySQL connection id is 20<br>Server version: 8.0.35 MySQL Community Server - GPL<br>No default schema selected; type \use <schema> to set one.<br> MySQL localhost:7306 ssl JS > <br>
$ mysqlsh -h localhost -u root -p -S /tmp/mysql.sock<br>Please provide the password for 'root@/tmp%2Fmysql.sock': ***********<br>Save password for 'root@/tmp%2Fmysql.sock'? [Y]es/[N]o/Ne[v]er (default No): y<br>MySQL Shell 8.0.35<br><br>Copyright (c) 2016, 2023, Oracle and/or its affiliates.<br>Oracle is a registered trademark of Oracle Corporation and/or its affiliates.<br>Other names may be trademarks of their respective owners.<br><br>Type '\help' or '\?' for help; '\quit' to exit.<br>Creating a session to 'root@/tmp%2Fmysql.sock'<br>Fetching schema names for auto-completion... Press ^C to stop.<br>Your MySQL connection id is 22<br>Server version: 8.0.35 MySQL Community Server - GPL<br>No default schema selected; type \use <schema> to set one.<br> MySQL localhost JS > <br>