首页 > 数据库 >MySQL Shell连接数据库报MySQL Error 1045 (28000)错误浅析

MySQL Shell连接数据库报MySQL Error 1045 (28000)错误浅析

时间:2023-11-15 12:12:57浏览次数:45  
标签:Shell name skip resolve MySQL root 浅析 localhost

这里简单总结一下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 > 

标签:Shell,name,skip,resolve,MySQL,root,浅析,localhost
From: https://www.cnblogs.com/kerrycode/p/17833536.html

相关文章

  • 记一次线上问题引发的对 Mysql 锁机制分析
    背景最近双十一开门红期间组内出现了一次因Mysql死锁导致的线上问题,当时从监控可以看到数据库活跃连接数飙升,导致应用层数据库连接池被打满,后续所有请求都因获取不到连接而失败整体业务代码精简逻辑如下:@Transactionpublicvoidservice(Integerid){delete(id);......
  • .NET6中的await原理浅析
    前言看过不少关于await的原理的文章,也知道背后是编译器给转成了状态机实现的,但是具体是怎么完成的,回调又是如何衔接的,一直都没有搞清楚,这次下定决心把源码自己跑了下,终于豁然开朗了本文的演示代码基于VS2022+.NET6示例publicclassProgram{staticint......
  • 记一次线上问题引发的对 Mysql 锁机制分析 | 京东物流技术团队
    背景最近双十一开门红期间组内出现了一次因Mysql死锁导致的线上问题,当时从监控可以看到数据库活跃连接数飙升,导致应用层数据库连接池被打满,后续所有请求都因获取不到连接而失败整体业务代码精简逻辑如下:@Transactionpublicvoidservice(Integerid){delete(id);inse......
  • mysql 操作详细教程
    MySQL是一种流行的关系型数据库管理系统,用于存储和管理数据。下面是MySQL操作的详细教程:安装MySQL:首先,你需要下载和安装MySQL。你可以从MySQL官方网站(https://www.mysql.com)上下载适合你操作系统的安装程序,并按照安装向导进行安装。启动MySQL服务器:安装完成后,你需要启动My......
  • 数据库性能查看-查看MySQL数据库操作记录
    测试orm或者Django序列化器的时候,我需要知道他们都干了什么,这就需要打开MySQL的操作日志进入MySQL的客户端命令界面:showvariableslike'gen%';+------------------+---------------------------------+|Variable_name|Value|+----------......
  • 为什么MySQL不建议使用delete删除数据?
    MySQL并不直接建议禁止使用DELETE语句删除数据,但是在某些情况下,使用DELETE可能会带来一些潜在的问题,特别是在大型数据库中。下面我将详细介绍为什么在某些情况下MySQL不建议过度使用DELETE语句来删除数据,并探讨其可能带来的影响。1.DELETE操作的影响DELETE语句用于从表中删除......
  • Mysql主键不要使用uuid或者不连续不重复雪花id
    一、简介     mysql官方推荐不要使用uuid或者不连续不重复的雪花id(long形且唯一,单机递增),而是推荐连续自增的主键id,官方的推荐是auto_increment,那么为什么不建议采用uuid,使用uuid究竟有什么坏处?二、对比    2.1、自增主键id         自增......
  • mysql跨库事务XA
    XA的性能很低,但是没得选的时候,也是个方案<?PHP$dbtest1=newmysqli("127.0.0.1","public","public","dbtest1")ordie("dbtest1连接失败");$dbtest2=newmysqli("127.0.0.1public","public","dbtest2&qu......
  • 实现批量插入和更新(mysql)
    在实际数据库应用中,经常需要实现插入或更新(插入新数据,如果已存在则更新已有数据)的功能。然而,在处理大量数据时,频繁的数据库I/O操作可能导致性能问题。MySQL批量插入和更新使用INSERT...ONDUPLICATEKEYUPDATEMySQL提供了INSERT...ONDUPLICATEKEYUPDATE语法,通......
  • 定期删除日志shell脚本
    #!/bin/sh#dest:切割日志,只保留30天,每日00:10运行日志目录格式/var/log/YYYY-MM-DD#请赋予脚本执行权限!#crontab-e创建任务写入100***/当前脚本绝对路径#crontab-l查看任务#find递归找,所以删除后会报错找不到,其实已经删除输出重定向即可find/var/log-typ......