MySQL ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)的真正原因

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS PostgreSQL,集群系列 2核4GB
简介:

在博客Linux mysql 5.6: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)里 面,我介绍了一下安装MySQL后登陆MySQL时会遇到ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) 这个错误,当时不知道真正的原因,搜索了一些网上的资料,测试验证了如何解决这个问题,但是一直不知道具体的原因。今天终于把这个问题给彻底搞清楚了。

下面我以MySQL 5.6.20为例子,刨析一下这个错误的真正原因,如下所示

[root@DB-Server tmp]# rpm -ivh MySQL-server-advanced-5.6.20-1.rhel5.x86_64.rpm
Preparing...                ########################################### [100%]
   1:MySQL-server-advanced  ########################################### [100%]
warning: user mysql does not exist - using root
warning: group mysql does not exist - using root
2015-03-26 10:49:06 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2015-03-26 10:49:07 5082 [Note] InnoDB: Using atomics to ref count buffer pool pages
2015-03-26 10:49:07 5082 [Note] InnoDB: The InnoDB memory heap is disabled
2015-03-26 10:49:07 5082 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2015-03-26 10:49:07 5082 [Note] InnoDB: Memory barrier is not used
2015-03-26 10:49:07 5082 [Note] InnoDB: Compressed tables use zlib 1.2.3
2015-03-26 10:49:07 5082 [Note] InnoDB: Using Linux native AIO
2015-03-26 10:49:07 5082 [Note] InnoDB: Using CPU crc32 instructions
2015-03-26 10:49:07 5082 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2015-03-26 10:49:07 5082 [Note] InnoDB: Completed initialization of buffer pool
2015-03-26 10:49:07 5082 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2015-03-26 10:49:07 5082 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2015-03-26 10:49:07 5082 [Note] InnoDB: Database physically writes the file full: wait...
2015-03-26 10:49:07 5082 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2015-03-26 10:49:07 5082 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2015-03-26 10:49:08 5082 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2015-03-26 10:49:08 5082 [Warning] InnoDB: New log files created, LSN=45781
2015-03-26 10:49:08 5082 [Note] InnoDB: Doublewrite buffer not found: creating new
2015-03-26 10:49:08 5082 [Note] InnoDB: Doublewrite buffer created
2015-03-26 10:49:08 5082 [Note] InnoDB: 128 rollback segment(s) are active.
2015-03-26 10:49:08 5082 [Warning] InnoDB: Creating foreign key constraint system tables.
2015-03-26 10:49:08 5082 [Note] InnoDB: Foreign key constraint system tables created
2015-03-26 10:49:08 5082 [Note] InnoDB: Creating tablespace and datafile system tables.
2015-03-26 10:49:08 5082 [Note] InnoDB: Tablespace and datafile system tables created.
2015-03-26 10:49:08 5082 [Note] InnoDB: Waiting for purge to start
2015-03-26 10:49:08 5082 [Note] InnoDB: 5.6.20 started; log sequence number 0
2015-03-26 10:49:08 5082 [Note] RSA private key file not found: /var/lib/mysql//private_key.pem. Some authentication plugins will not work.
2015-03-26 10:49:08 5082 [Note] RSA public key file not found: /var/lib/mysql//public_key.pem. Some authentication plugins will not work.
A random root password has been set. You will find it in '/root/.mysql_secret'.
2015-03-26 10:49:09 5082 [Note] Binlog end
2015-03-26 10:49:09 5082 [Note] InnoDB: FTS optimize thread exiting.
2015-03-26 10:49:09 5082 [Note] InnoDB: Starting shutdown...
2015-03-26 10:49:10 5082 [Note] InnoDB: Shutdown completed; log sequence number 1625977
 
 
2015-03-26 10:49:10 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2015-03-26 10:49:10 5104 [Note] InnoDB: Using atomics to ref count buffer pool pages
2015-03-26 10:49:10 5104 [Note] InnoDB: The InnoDB memory heap is disabled
2015-03-26 10:49:10 5104 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2015-03-26 10:49:10 5104 [Note] InnoDB: Memory barrier is not used
2015-03-26 10:49:10 5104 [Note] InnoDB: Compressed tables use zlib 1.2.3
2015-03-26 10:49:10 5104 [Note] InnoDB: Using Linux native AIO
2015-03-26 10:49:10 5104 [Note] InnoDB: Using CPU crc32 instructions
2015-03-26 10:49:10 5104 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2015-03-26 10:49:10 5104 [Note] InnoDB: Completed initialization of buffer pool
2015-03-26 10:49:10 5104 [Note] InnoDB: Highest supported file format is Barracuda.
2015-03-26 10:49:10 5104 [Note] InnoDB: 128 rollback segment(s) are active.
2015-03-26 10:49:10 5104 [Note] InnoDB: Waiting for purge to start
2015-03-26 10:49:10 5104 [Note] InnoDB: 5.6.20 started; log sequence number 1625977
2015-03-26 10:49:10 5104 [Note] RSA private key file not found: /var/lib/mysql//private_key.pem. Some authentication plugins will not work.
2015-03-26 10:49:10 5104 [Note] RSA public key file not found: /var/lib/mysql//public_key.pem. Some authentication plugins will not work.
2015-03-26 10:49:10 5104 [Note] Binlog end
2015-03-26 10:49:10 5104 [Note] InnoDB: FTS optimize thread exiting.
2015-03-26 10:49:10 5104 [Note] InnoDB: Starting shutdown...
2015-03-26 10:49:12 5104 [Note] InnoDB: Shutdown completed; log sequence number 1625987
 
 
 
 
A RANDOM PASSWORD HAS BEEN SET FOR THE MySQL root USER !
You will find that password in '/root/.mysql_secret'.
 
You must change that password on your first connect,
no other statement but 'SET PASSWORD' will be accepted.
See the manual for the semantics of the 'password expired' flag.
 
Also, the account for the anonymous user has been removed.
 
In addition, you can run:
 
  /usr/bin/mysql_secure_installation
 
which will also give you the option of removing the test database.
This is strongly recommended for production servers.
 
See the manual for more instructions.
 
Please report any problems at http://bugs.mysql.com/
 
The latest information about MySQL is available on the web at
 
  http://www.mysql.com
 
Support MySQL by buying support/licenses at http://shop.mysql.com
 
WARNING: Found existing config file /usr/my.cnf on the system.
Because this file might be in use, it was not replaced,
but was used in bootstrap (unless you used --defaults-file)
and when you later start the server.
The new default config file was created as /usr/my-new.cnf,
please compare it with your file and take the changes you need.

 

MySQL 的安装日志信息中,你会看到MySQL生成了root用户的随机密码(如下截图所示),并将这个随机密码放置在/root/.mysql_secret 中。并且强制在第一次登陆时修改root用户的密码。Mysql 5.6及以后版本出处于安全考虑,root密码不为空。所以在MySQL的安装过程中,会生成一个随机密码,但是对于我们这样的初学者,不了解情况,于是 便从网上搜索如何解决问题的资料,而问题关键在此,很多做技术的人,了解不够深入,又喜欢随波逐流,停留在问题的表层,并未深入研究。而高手又不屑于这类 简单的问题。

clip_image001

A RANDOM PASSWORD HAS BEEN SET FOR THE MySQL root USER !

You will find that password in '/root/.mysql_secret'.

You must change that password on your first connect,

no other statement but 'SET PASSWORD' will be accepted.

See the manual for the semantics of the 'password expired' flag.

Also, the account for the anonymous user has been removed.

知道了错误的前因后果,那么很容易就搞定这个问题了。找到随机密码,登录数据库后修改密码。问题解决

image

 

[root@DB-Server tmp]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.6.20-enterprise-commercial-advanced
 
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
 
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> show databases;
ERROR 1820 (HY000): You must SET PASSWORD before executing this statement
mysql> set password=password('wer@34');
Query OK, 0 rows affected (0.03 sec)
 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

clip_image002

相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
18天前
|
关系型数据库 MySQL 网络安全
DBeaver连接MySQL提示Access denied for user ‘‘@‘ip‘ (using password: YES)
“Access denied for user ''@'ip' (using password: YES)”错误通常与MySQL用户权限配置或网络设置有关。通过检查并正确配置用户名和密码、用户权限、MySQL配置文件及防火墙设置,可以有效解决此问题。希望本文能帮助您成功连接MySQL数据库。
32 4
|
4月前
|
SQL 关系型数据库 MySQL
mysql密码错误-ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using passwor:yes)
这篇文章提供了解决MySQL数据库"Access denied for user 'root'@'localhost' (using password: YES)"错误的方法,通过跳过密码验证、修改root密码,然后重启服务来解决登录问题。
mysql密码错误-ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using passwor:yes)
|
5月前
|
安全 关系型数据库 MySQL
【Python】已解决:pymysql.err.OperationalError:(2003 “Can’t connect to MySQL server on ‘localhost’ ([WinEr
【Python】已解决:pymysql.err.OperationalError:(2003 “Can’t connect to MySQL server on ‘localhost’ ([WinEr
687 1
|
5月前
|
关系型数据库 MySQL 数据库连接
Cant‘t connect to MySQL server on ‘localhost‘ (10038)
Cant‘t connect to MySQL server on ‘localhost‘ (10038)
|
8天前
|
关系型数据库 MySQL 数据库
Python处理数据库:MySQL与SQLite详解 | python小知识
本文详细介绍了如何使用Python操作MySQL和SQLite数据库,包括安装必要的库、连接数据库、执行增删改查等基本操作,适合初学者快速上手。
71 15
|
2天前
|
SQL 关系型数据库 MySQL
数据库数据恢复—Mysql数据库表记录丢失的数据恢复方案
Mysql数据库故障: Mysql数据库表记录丢失。 Mysql数据库故障表现: 1、Mysql数据库表中无任何数据或只有部分数据。 2、客户端无法查询到完整的信息。
|
9天前
|
关系型数据库 MySQL 数据库
数据库数据恢复—MYSQL数据库文件损坏的数据恢复案例
mysql数据库文件ibdata1、MYI、MYD损坏。 故障表现:1、数据库无法进行查询等操作;2、使用mysqlcheck和myisamchk无法修复数据库。
|
13天前
|
SQL 关系型数据库 MySQL
MySQL导入.sql文件后数据库乱码问题
本文分析了导入.sql文件后数据库备注出现乱码的原因,包括字符集不匹配、备注内容编码问题及MySQL版本或配置问题,并提供了详细的解决步骤,如检查和统一字符集设置、修改客户端连接方式、检查MySQL配置等,确保导入过程顺利。
|
21天前
|
关系型数据库 MySQL 数据库
GBase 数据库如何像MYSQL一样存放多行数据
GBase 数据库如何像MYSQL一样存放多行数据
|
1月前
|
SQL 关系型数据库 MySQL
12 PHP配置数据库MySQL
路老师分享了PHP操作MySQL数据库的方法,包括安装并连接MySQL服务器、选择数据库、执行SQL语句(如插入、更新、删除和查询),以及将结果集返回到数组。通过具体示例代码,详细介绍了每一步的操作流程,帮助读者快速入门PHP与MySQL的交互。
37 1