(28000): Access denied for user 'root'@'127.0.0.1' (using password: YES)

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: 在一台测试服务器测试Python脚本时,执行Python脚本时报如下错误:        主要错误信息为“operation the sql fail!1045 (28000): Access denied for user 'root'@'127.0.0.1' (using password: YES)”。

 

在一台测试服务器测试Python脚本时,执行Python脚本时报如下错误:

  

clip_image001

 

 

主要错误信息为“operation the sql fail!1045 (28000): Access denied for user 'root'@'127.0.0.1' (using password: YES)”。 部分测试脚本如下所示,如下所,mysql.connector.connect的host为127.0.0.1 其它账号信息做了脱敏处理.

 

def record_server_info():
    try:
        server_ip = get_host_ip();
 
        server_name= str.strip(get_host_name());
 
        server_system=platform.system();
 
        linux_dis =platform.linux_distribution();
        os_version =''
        os_version = ' '.join(linux_dis)
 
        os_bitinfo = platform.architecture()
        os_bit =os_bitinfo[0][0:2]
 
        processor = str.strip(commands.getoutput("cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c"))
 
        cpu_slot= commands.getoutput("cat /proc/cpuinfo | grep 'model name' | sort | uniq | wc -l");
 
        cpu_core =  multiprocessing.cpu_count();
 
        processor_core =int(cpu_core)/int(cpu_slot);
 
        memory = get_physical_memory()
 
        disk_space = get_disk_info()["capacity"]/1024/1024;
  
 
        dbcon = mysql.connector.connect(
            host='127.0.0.1',
            user='root',
            passwd='123456',
            database='mysql'
        )
        cursor = dbcon.cursor()
        sql_tex =( "insert into db_server_info("
                   "factory_cd      ,"
                   "server_name     ,"
                   "server_ip       ,"
                   "server_type     ,"
                   "server_system   ,"
                   "is_production   ,"
                   "os_version      ,"
                   "os_patch        ,"
                   "os_bit          ,"
                   "processors      ,"
                   "cpu_slot        ,"
                   "processors_core ,"
                   "memory          ,"
                   "disk_space      ,"
                   "server_purpose)  "
                   "values(%(factory_cd)s,%(server_name)s,%(server_ip)s,%(server_type)s,%(server_system)s,%(is_production)s,%(os_version)s,%(os_patch)s,%(os_bit)s,%(processors)s,%(cpu_slot)s,%(processors_core)s,%(memory)s,%(disk_space)s,%(server_purpose)s)")
        data={'factory_cd':factory_cd, 'server_name':server_name, 'server_ip':server_ip,"server_type":server_type,"server_system":server_system,"is_production":is_productin,"os_version":os_version, "os_patch":'',"os_bit":int(os_bit), "processors":processor,"cpu_slot":cpu_slot, "processors_core":processor_core,"memory":memory,"disk_space":disk_space,"server_purpose":''}
 
        cursor.execute('truncate table db_server_info')
        dbcon.commit()
        cursor.execute(sql_tex, data)
        dbcon.commit()
 
    except mysql.connector.Error as e:
        print('operation the sql fail!{0}'.format(e))
    finally:
        cursor.close;
        dbcon.close;

 

 

mysql -u root -p 测试登录MySQ发现没有问题,但是一检查发现,如果不指定host的情况下,当前用户为root@localhost,而root@'127.0.0.1'的密码为空。

 

mysql> select current_user();
+----------------+
| current_user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
 
mysql> show grants for root@'localhost';
+---------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost                                                                                                 |
+---------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*9B67EF98D94F2B81011D6D2CEDE4' WITH GRANT OPTION|
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION                                                              |
+---------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
 
mysql> show grants for root@'127.0.0.1';
+---------------------------------------------------------------------+
| Grants for root@127.0.0.1                                           |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' WITH GRANT OPTION |
+---------------------------------------------------------------------+
1 row in set (0.00 sec)
 
mysql> 

 

立即检查my.cnf的配置,发现可能某次测试时,设置了参数 skip-name-resolve, 后面忘记取消了。由于启动mysqld时使用了'--skip-name-resolve'参数,此种情况下由于不做域名解析,127.0.0.1和localhost对mysql数据库来讲,是不同的主机,而root@'127.0.0.1'为空密码,所以Python脚本执行时,访问MySQL报错(28000): Access denied for user 'root'@'127.0.0.1' (using password: YES)

相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
4月前
|
数据库
Access denied for user ‘xxx’@‘localhost‘(using password:YES)
Access denied for user ‘xxx’@‘localhost‘(using password:YES)
|
5月前
|
关系型数据库 MySQL 数据库
【已解决】ERROR 1045 - Access denied for user ‘root‘@‘localhost‘ (using password: YES)
【已解决】ERROR 1045 - Access denied for user ‘root‘@‘localhost‘ (using password: YES)
Access denied for user ''@'localhost' (using password: NO)问题的解决
Access denied for user ''@'localhost' (using password: NO)问题的解决
126 0
|
关系型数据库 MySQL 数据安全/隐私保护
|
安全 关系型数据库 MySQL
解决mysql:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO/YES)
解决mysql:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO/YES)
875 0
|
SQL 关系型数据库 MySQL
Access denied for user ‘root‘ @‘123.233.244.218‘(using password:YES)的解决方法
Access denied for user ‘root‘ @‘123.233.244.218‘(using password:YES)的解决方法
238 1
Access denied for user ‘root‘ @‘123.233.244.218‘(using password:YES)的解决方法
|
关系型数据库 MySQL 数据库
ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
388 0
ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
|
关系型数据库 MySQL 数据库
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES/NO)
初次安装mysql,net start mysql,然后输入mysql -u root -p, 出现enter password,我直接点击回车,结果出现如果下错误: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)。
4343 1
|
关系型数据库 MySQL 数据库连接
ERROR 1045 (28000): Access denied for user ‘ODBC‘@‘localhost‘ (using password: NO)
ERROR 1045 (28000): Access denied for user ‘ODBC‘@‘localhost‘ (using password: NO)
ERROR 1045 (28000): Access denied for user ‘ODBC‘@‘localhost‘ (using password: NO)
|
关系型数据库 MySQL 数据安全/隐私保护
ERROR 1142 (42000): GRANT command denied to user ** 或 ERROR 1045 (28000): Access denied for user '**'@'localhost' (using password: YES)
ERROR 1142 (42000): GRANT command denied to user ** 或 ERROR 1045 (28000): Access denied for user '**'@'localhost' (using password: YES)
796 0