博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(28000): Access denied for user 'root'@'127.0.0.1' (using password: YES)
阅读量:5309 次
发布时间:2019-06-14

本文共 4575 字,大约阅读时间需要 15 分钟。

 

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

  

 

 

主要错误信息为“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@'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)

转载于:https://www.cnblogs.com/kerrycode/p/7597258.html

你可能感兴趣的文章
Spark的启动进程详解
查看>>
使用命令创建数据库和表
查看>>
机器视觉:SSD Single Shot MultiBox Detector
查看>>
201521123044 《Java程序设计》第1周学习总结
查看>>
MIT Scheme 的基本使用
查看>>
程序员的“机械同感”
查看>>
在16aspx.com上下了一个简单商品房销售系统源码,怎么修改它的默认登录名和密码...
查看>>
c++回调函数
查看>>
linux下Rtree的安装
查看>>
【Java】 剑指offer(53-2) 0到n-1中缺失的数字
查看>>
Delphi中ListView类的用法
查看>>
多米诺骨牌
查看>>
Linq 学习(1) Group & Join--网摘
查看>>
asp.net 调用前台JS调用后台,后台掉前台JS
查看>>
Attribute(特性)与AOP
查看>>
苹果手表:大方向和谷歌一样,硬件分道扬镳
查看>>
Competing Consumers Pattern (竞争消费者模式)
查看>>
Android面试收集录15 Android Bitmap压缩策略
查看>>
PHP魔术方法之__call与__callStatic方法
查看>>
ubuntu 安装后的配置
查看>>