zoukankan      html  css  js  c++  java
  • 记录下python3下使用mysql8的问题

    首先使用 pip 命令来安装 mysql-connector

    pip install mysql-connector

    新建mysql.py

    #!/usr/bin/python3
    
    
    import keyword;
    import mysql.connector;
    
    mydb = mysql.connector.connect(
      host="localhost",       # 数据库主机地址
      user="test",    # 数据库用户名
      passwd="123456",   # 数据库密码
      database='test'
    )
     
    print(mydb)

    如果连接成功就会输出

    <mysql.connector.connection.MySQLConnection object at 0x000001712C9A2FA0>

    如果你的 MySQL 是 8.0 版本,mysql8的密码插件验证方式默认为caching_sha2_password,早期版本为 mysql_native_password

    执行会提示错误:

    mysql.connector.errors.NotSupportedError: Authentication plugin 'caching_sha2_password' is not supported

    可以新建一个用户,密码使用mysql_native_password加密方式就可以连接上。

    1、创建用户

    create user 'test'@'%' identified by '123456';

    2、授权权限

    grant all privileges on *.* to 'test'@'%';

    3、更新密码

    alter user 'test'@'%' identified with mysql_native_password by '123456';

    4、刷新权限

    flush privileges;

    连接时如果还是报错,就需要在连接时指定密码验证方式:

    mydb = mysql.connector.connect(
      host="localhost",       # 数据库主机地址
      user="test",    # 数据库用户名
      passwd="123456",   # 数据库密码
      database='test',
      auth_plugin='mysql_native_password'
    )
  • 相关阅读:
    MySQL官方文档-二级索引覆盖主键索引
    windows server 2008/win7 远程控制
    博客园美化日记
    MarkDown 中使用 LaTeX 数学式
    DOS命令和bat脚本
    数据链路层
    网络安全
    运输层安全协议SSL
    DNS/域名
    停止等待协议
  • 原文地址:https://www.cnblogs.com/kyuang/p/14900177.html
Copyright © 2011-2022 走看看