那些平台可以给网站做外链,分类达人的作用,科技部网站改版方案,黄骅市做网站原创作者#xff1a; 管长龙 译作者#xff1a;Carlos Tutte、Marcos Albe 翻译#xff1a;管长龙在我们学习 MySQL 或从事 MySQL DBA 工作期间#xff0c;时常会遇到#xff1a;“我尝试连接到 MySQL 并且收到1045 错误#xff0c;但我确定我的用户和密码都没问题”。不…原创作者 管长龙 译作者Carlos Tutte、Marcos Albe 翻译管长龙在我们学习 MySQL 或从事 MySQL DBA 工作期间时常会遇到“我尝试连接到 MySQL 并且收到1045 错误但我确定我的用户和密码都没问题”。不管你现在是否是高手还是高高手都不可避免曾经在初学的时候犯过一些很初级的错误例如用户名密码都填错了。而且工作一段时间后也偶尔会遇到一些不常见错误原因。一、连接错误的主机[rootlocalhost ~]# mysql -u root -p123456mysql: [Warning] Using a password on the command line interface can be insecure.ERROR 1045 (28000): Access denied for user rootlocalhost (using password: YES)如果未指定要连接的主机(使用 -h 标志)则 MySQL 客户端将尝试连接到 localhost 实例同时您可能尝试连接到另一个主机端口实例。修复仔细检查您是否尝试连接到 localhost或者确保指定主机和端口(如果它不是 localhost)[rootlocalhost ~]# mysql -u root -p123456 -h -P 3306二、用户不存在[rootlocalhost ~]# mysql -u nonexistant -p123456 -h localhostmysql: [Warning] Using a password on the command line interface can be insecure.ERROR 1045 (28000): Access denied for user nonexistantlocalhost (using password: YES)修复仔细检查用户是否存在mysql SELECT User FROM mysql.user WHERE Usernonexistant;Empty set (0.00 sec)如果用户不存在请创建一个新用户mysql CREATE USER nonexistantlocalhost IDENTIFIED BY sekret;Query OK, 0 rows affected (0.00 sec)mysql FLUSH PRIVILEGES;Query OK, 0 rows affected (0.01 sec)三、用户存在但客户端主机无权连接[rootlocalhost ~]# mysql -u nonexistant -p123456mysql: [Warning] Using a password on the command line interface can be insecure.ERROR 1045 (28000): Access denied for user nonexistantlocalhost (using password: YES)修复您可以通过以下查询检查 MySQL 允许连接的主机用户/主机mysql SELECT Host, User FROM mysql.user WHERE Usernonexistant;--------------------------| Host | User |--------------------------| 192.168.0.1 | nonexistant |--------------------------1 row in set (0.00 sec)如果需要检查客户端连接的 IP可以使用以下 Linux 命令来获取服务器 IP[rootlocalhost ~]# ip address | grep inet | grep -v inet6inet 127.0.0.1/8 scope host loinet 192.168.0.20/24 brd 192.168.0.255 scope global dynamic wlp58s0或公共IP[rootlocalhost ~]# dig short myip.opendns.com resolver1.opendns.com177.128.214.181然后您可以创建具有正确主机(客户端 IP)的用户或使用(通配符)来匹配任何可能的 IPmysql CREATE USER nonexistant% IDENTIFIED BY 123456;Query OK, 0 rows affected (0.00 sec)四、密码错误或者用户忘记密码mysql CREATE USER nonexistant% IDENTIFIED BY 123456;Query OK, 0 rows affected (0.00 sec)修复检查和/或重置密码您无法从 MySQL 以纯文本格式读取用户密码因为密码哈希用于身份验证但您可以将哈希字符串与“PASSWORD”函数进行比较mysql SELECT Host, User, authentication_string, PASSWORD(forgotten) FROM mysql.user WHERE Usernonexistant;----------------------------------------------------------------------------------------------------------------| Host | User | authentication_string | PASSWORD(forgotten) |----------------------------------------------------------------------------------------------------------------| 192.168.0.1 | nonexistant | *AF9E01EA8519CE58E3739F4034EFD3D6B4CA6324 | *70F9DD10B4688C7F12E8ED6C26C6ABBD9D9C7A41 || % | nonexistant | *AF9E01EA8519CE58E3739F4034EFD3D6B4CA6324 | *70F9DD10B4688C7F12E8ED6C26C6ABBD9D9C7A41 |----------------------------------------------------------------------------------------------------------------2 rows in set, 1 warning (0.00 sec)我们可以看到 PASSWORD(forgotten)哈希与 authentication_string 列不匹配这意味着 password string forgotten 不是正确的登录密码。如果您需要覆盖密码可以执行以下查询mysql set password for nonexistant% hello$!world;Empty set (0.00 sec)五、Bash 转换密码中的特殊字符[rootlocalhost ~]# mysql -u nonexistant -phello$!worldmysql: [Warning] Using a password on the command line interface can be insecure.ERROR 1045 (28000): Access denied for user nonexistantlocalhost (using password: YES)修复通过在单引号中包装密码来防止 bash 解释特殊字符[rootlocalhost ~]# mysql -u nonexistant -phello$!worldmysql: [Warning] Using a password on the command line interface can be insecure...mysql六、SSL 是必须的但客户没有使用mysql create user ssluser% identified by 123456;Query OK, 0 rows affected (0.00 sec)mysql alter user ssluser% require ssl;Query OK, 0 rows affected (0.00 sec)...[rootlocalhost ~]# mysql -u ssluser -p123456mysql: [Warning] Using a password on the command line interface can be insecure.ERROR 1045 (28000): Access denied for user ssluserlocalhost (using password: YES)修复添加 -ssl-mode 标志(-ssl 标志已弃用但也可以使用)[https://dev.mysql.com/doc/relnotes/mysql/5.7/en/news-5-7-11.html][rootlocalhost ~]# mysql -u ssluser -p123456 --ssl-modeREQUIRED...mysql最后如果您真的被锁定并需要绕过身份验证机制以重新获得对数据库的访问权限请执行以下几个简单步骤停止实例编辑 my.cnf 并在 [mysqld] 下添加 skip-grant-tables(这样可以在不提示输入密码的情况下访问 MySQL)。在 MySQL 8.0 上跳过网络是自动启用的(只允许从 localhost 访问 MySQL)但对于以前的 MySQL 版本建议在 [mysqld] 下添加 -skip-networking启动实例使用 root 用户访问(mysql -uroot -hlocalhost);发出必要的 GRANT / CREATE USER / SET PASSWORD 以纠正问题(可能设置一个已知的 root 密码将是正确的事情SET PASSWORD FOR rootlocalhostS0vrySekr3t停止实例编辑 my.cnf 并删除 skip-grant-tables 和 skip-networking再次启动 MySQL您应该能够使用 roothost 从 root 用户登录并对 root 用户执行任何其他必要的纠正操作。本文按常见到复杂的顺序将可能报 1045 的错误原因全部列举出来看完了还不赶快收藏参考https://www.percona.com/blog/2019/07/05/fixing-a-mysql-1045-error/