马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这里简单总结一下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>
复制代码 先用root账号连接数据(socket方式),检查用户信息,如下所示,root账号限定为localhost- $ 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>
复制代码 然后,检查变量skip_name_resolve,如下所示,skip_name_resolve为ON- 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>
复制代码 数据库参数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.
- <p>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<br>
- with skip_name_resolve enabled. If you plan to do that, make sure an account exists that can accept a connection. For example,<br>
- to be able to connect as root using --host=127.0.0.1 or --host=::1, create these accounts:</p>
- [code]CREATE USER 'root'@'127.0.0.1' IDENTIFIED BY 'root-password';
- CREATE USER 'root'@'::1' IDENTIFIED BY 'root-password';
复制代码 [/code]
那么怎么解决这个问题呢?一共有下面几种方法。
方案1:skip_name_resolve设置为OFF
我们需要在参数文件my.cnf中 将参数skip-name-resolve注释或者设置skip_name_resolve设置为OFF的.
注意事项,虽然官方文档中,参数skip-name-resolve是Boolean类型,但是如果你像下面这样设置是不会生效的,具体原因不是很清楚- skip-name-resolve=0 <br>或<br>skip-name-resolve=FALSE<br>
复制代码 正确的做法- 方法1:<br>skip-name-resolve=OFF<br><br>方法2:<br>#skip-name-resolve 注释掉参数<br>
复制代码 修改skip_name_resolve的值为OFF后,重启一下MySQL实例,然后我们验证一下测试结果。- 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>
复制代码 方案3:使用socket方式连接
mysql shell也可以使用socket连接,一般的方式如下:- mysqlsh -h localhost -u root -p -S /tmp/mysql.sock #根据实际情况填写具体的mysql.sock文件<br>或<br>\connect root@localhost?socket=(/tmp/mysql.sock)<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>
复制代码 扫描上面二维码关注我如果你真心觉得文章写得不错,而且对你有所帮助,那就不妨帮忙“推荐"一下,您的“推荐”和”打赏“将是我最大的写作动力!本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接.
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |