• 记录下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'
    )
  • 相关阅读:
    flush logs
    slave-skip-errors,sql_slave_skip_counter
    稀饭
    table
    profiles
    索引使用规范
    innodb_rollback_on_timeout
    mysql账号管理
    跨库复制
    linux.sh
  • 原文地址:https://www.cnblogs.com/kyuang/p/14900177.html
Copyright © 2020-2023  润新知