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

 

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

时间: 2024-10-28 22:57:53

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

ERROR 1045 (28000): Access denied for user 'root'@'localhost' 的解决方法

风信网(ithov.com)原创文章:今天在测试一款bacula开源备份软件的时候,需要使用到mysql数据库,当运行以下命令时报错误如下: [root@localhost etc]# ./grant_mysql_privileges ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) Error creating privileges. 登陆到msyql中去解决该问题: [root

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: NO)里 面,我介绍了一下安装MySQL后登陆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: NO)

案例环境:              操作系统 :Red Hat Enterprise Linux Server release 5.7 (Tikanga) 64 bit           数据库版本 : Mysql 5.6.19 64 bit 案例介绍: 今 天开始学习mysql,遂先安装了Mysql 5.6.19 64bit 版本的数据库,结果安装成功了,但是使用root登录时遇到了ERROR 1045 (28000): Access denied for user 'root'@'loc

mysql5.7 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

初次安装mysql,net start mysql,然后输入mysql -u root -p,出现enter password,我直接点击回车,结果出现如果下错误:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO).网上看了很多方法,都是千篇一律的,没有解决我的问题使用:set password for 'root'@'localhost' =password('');flush p

MySQL 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) 的解决办法和原因 这两天下载了MySQL5.7.11进行安装,发现到了初次使用输入密码的时候,不管怎样都进不去,即使按照网上说的在mysqld 下面添加skip-grant-tables也是不行,后来研究了两天,终于找出原因和解决办法. 复制代码 代码如下: [mysqlld] skip-grant-tables: 原因

centOS下mysql-ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

系统centOS-6.3 方法操作很简单,如下:# /etc/init.d/MySQL stop# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &# mysql -u root mysql # mysql>use mysql ;mysql> UPDATE user SET Password=PASSWORD('newpassword') where USER='root' and host='root'

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

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) root@mysql ~]# mysql -p -u root Enter password:  ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) ERROR 1045 (28000): Access denied

解决mariadb grant ERROR 1045 (28000): Access denied for user

用mariadb也有一段时间了,常用命令和语法,基本没变.比较复杂一点的,例如replication,也没有发现根mysql有什么不同的地方. 不过,今天真发现有不同的地方了,mariadb的权限管理根mysql不一样,mysql可以创建一个根root账户同等权限的账户,但是mariadb就不行了,写法上也所不同. 1,mariadb  grant授权报错    MariaDB [(none)]> grant all privileges on *.* TO tank@'192.168.%' I

mysql Access denied for user ‘root’@’localhost’ (using password: YES)解决方法_Mysql

今天在启动mysql时出现以下问题: [root@www ~]# mysql -u root -p Enter password: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) 网上的答案是各种各样的,最终解决问题的方法总结为以下,好多都是没有设置初始密码造成此问题的. 解决方法如下: [root@www ~]# service mysqld stop #先关闭mysql服务