在一台测试服务器测试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@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)”

(28000): Access denied for user 'root'@'127.0.0.1' (using password: YES)的更多相关文章

  1. ERROR 1045 (28000): Access denied for user 'root'@'127.0.0.1' (using password: YES)

    我的原因是在配置文件my.ini [mysqld]项,在其后加入了一句:skip-name-resolve 导致授权出现这个错误,把skip-name-resolve这项屏蔽了就好了. 场景2:对所有 ...

  2. Ubuntu下MySQL报错:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

    在Ubuntu下 想要登录mysql数据库 root@JD:~# mysql -uroot -p 报错 ERROR 1045 (28000): Access denied for user 'root ...

  3. MySQL密码正确却无法本地登录,ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

    MySQL密码正确却无法本地登录 报错如下: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password ...

  4. Linux mysql 5.6: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

    案例环境: 操作系统 :Red Hat Enterprise Linux Server release 5.7 (Tikanga) 64 bit 数据库版本 : Mysql 5.6.19 64 bit ...

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

    在博客Linux mysql 5.6: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: N ...

  6. 【转载】重置密码解决MySQL for Linux错误 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

    重置密码解决MySQL for Linux错误 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using passwor ...

  7. centos7 上安装mysql5.7后登录报错ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: Yes 或者No)

    原文转载自以下链接:https://blog.csdn.net/keepd/article/details/77151006 安装完mysql后会有个临时密码去日志查看,但是查看登录修改密后还是不行 ...

  8. ERROR 1698 (28000): Access denied for user 'root'@'localhost' 解决方法

    之前MySQL服务端本机上使用密码登陆root账号是没有问题的,但是今天不知道是因为动了哪里,登陆失败并有这个错误代码: ~$ mysql -u root -p Enter password: ERR ...

  9. mac下mysql 1045 (28000): Access denied for user 'root'@'localhost' (using password:

    新入了mac pro,安装好mysql后,用终端进入mysql遇到个问题: 1045 (28000): Access denied for user 'root'@'localhost' (using ...

随机推荐

  1. Linux命令:useradd

    Linux下:useradd 等价于 adduser     Aix下:useradd 来自为知笔记(Wiz)

  2. 编译和解释性语言和python运行方式

    1.编译型语言和解释性语言 编译型语言:在执行之前需要一个专门的编译过程,把程序编译成为机器语言的文件,运行时不需要重新翻译,直接使用编译的结果就行了.程序执行效率高,依赖编译器,跨平台性差些.如C. ...

  3. Effective Java 之-----谨慎的覆盖clone方法

    1.概述 如果clone方法返回一个由构造器创建的对象,它就得到有错误的类.因此,如果覆盖了非final类中的clone方法,则应该返回一个通过调用super.clone得到的对象.如果类的所有超类都 ...

  4. 分析AJAX抓取今日头条的街拍美图并把信息存入mongodb中

    今天学习分析ajax 请求,现把学得记录, 把我们在今日头条搜索街拍美图的时候,今日头条会发起ajax请求去请求图片,所以我们在网页源码中不能找到图片的url,但是今日头条网页中有一个json 文件, ...

  5. web项目,ftl文件中的路径引入问题

    路径问题的引入方式:绝对路径.相对路径 区别如图: =================================================

  6. python3图像识别库安装与使用

    pytesseract库的安装 因为用的win10,就直说windows上面的安装了.其实就是pip安装就完事了. $ pip install pytesseract 安装了这个还不算完,得安装Tes ...

  7. Java并发(一)——线程安全的容器(上)

    Java中线程安全的容器主要包括两类: Vector.Hashtable,以及封装器类Collections.synchronizedList和Collections.synchronizedMap: ...

  8. Python数据结构之二——tuple(元组)

    Python版本:3.6.2  操作系统:Windows  作者:SmallWZQ 列表和元组是Python中最常见的内建序列.元组与列表一样,但是tuple一旦创建就不能修改.创建元组的语法非常简单 ...

  9. [Python Study Notes] 抉择--Python2.x Or Python 3.x

    In summary : Python 2.x is legacy, Python 3.x is the present and future of the language Python 3.0 w ...

  10. TinyMce 使用初探

    https://www.cnblogs.com/nkxyf/p/3883586.html 参考:http://www.tinymce.com/    官网 http://www.tinymce.com ...