mysql8.x实践系列(3)Qt客户端连接mysql报错:Authentication plugin ‘caching_sha2_password‘ reported error

一、现象描述

Qt客户端远程连接mysql8.x服务器,会报错:

Authentication plugin 'caching_sha2_password' reported error:Authentication require secure connection.

因为在mysql8之后,加密规则默认是caching_sha2_password。caching_sha2_password 是MySQL 8.0.4引入的一个新的身份验证插件,它的特点被其命名揭露无疑:

  • sha2_password:其实就是 sha256_password,这是 MySQL5.6 就引入的身份验证插件,其优点是对加盐密码进行多轮 SHA256 哈希,以确保哈希转换更安全。其缺点为它要求使用安全连接或使用 RSA 密钥对进行密码交换的未加密连接,因此其身份验证的效率较低。
  • caching:在 sha256_password 的基础上增加缓存,有缓存的情况下不需要加密连接或 RSA 密钥对,以达到安全和效率并存。

二、解决办法

1、桌面-开始-鼠标右键-运行-cmd

2、进入mysql的路径

cd C:\Program Files\MySQL\MySQL Server 8.0\bin

3、命令格式说明

mysql --ssl-mode=DISABLED -u <username> -p<password> -h <host> -P<port>

4、连接

(1)错误的连接方式

C:\Program Files\MySQL\MySQL Server 8.0\bin>./mysql.exe -u root -pMypwd123456$ -h 192.168.216.100 -P3306
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2061 (HY000): Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.

【分析】mysql8默认使用插件caching_sha2_password,client远程连接报这个错误,我们需要拿到server的public key来加密password。

【解决】加参数可以解决:--get-server-public-key

(2)正确的连接方式1,使用--get-server-public-key
C:\Program Files\MySQL\MySQL Server 8.0\bin>./mysql.exe -u root -pMypwd123456$ -h 192.168.216.100 -P3306 --get-server-public-key

C:\Users\hello>cd C:\Program Files\MySQL\MySQL Server 8.0\bin
C:\Program Files\MySQL\MySQL Server 8.0\bin>./mysql.exe -u root -pMypwd123456$ -h 192.168.216.100 -P3306 --get-server-public-key
'.' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

C:\Program Files\MySQL\MySQL Server 8.0\bin>mysql -u root -pMypwd123456$ -h 192.168.216.100 -P3306 --get-server-public-key
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.31 MySQL Community Server - GPL

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

如果root用户登陆成功,有了缓存,则下次认证时,未加密连接不再要求使用RSA密钥对。

(3)正确的连接方式2,使用--server-public-key-path
把公钥文件从mysql datadir拷贝出来,例如从C:\ProgramData\MySQL\MySQL Server 8.0\Data\public_key.pem"到"C:\Users\hello\Downloads\public_key.pem",然后再
C:\Program Files\MySQL\MySQL Server 8.0\bin>./mysql.exe -u root -pMypwd123456$ -h 192.168.216.100 -P3306 --server-public-key-path="C:\Users\hello\Downloads\public_key.pem"

三、Qt客户端怎么远程连接MySQL8.x?

1、先额外启动一个进程,cmd命令远程连接加参数,使得mysql服务器后台有了认证的缓存,下次未加密连接就不再要求使用RSA密钥对。

mysql.exe文件是官方自带的客户端工具(mysqld.exe文件是服务器,一个字母之差),可以拷贝自安装路径C:\Program Files\MySQL\MySQL Server 8.0\bin

 mysql.exe是绿色软件,带上libcrypto-1_1-x64.dll和libssl-1_1-x64.dll依赖库即可使用。

void Widget::init()
{
    QProcess *pProces = new QProcess(this);
    connect(pProces, SIGNAL(readyRead()), this, SLOT(on_read()));
    QStringList arguments;
    arguments << "-u"
         << "root"
         << "-pMypwd123456$"
         << "-h"
         << "192.168.216.100"
         << "-P3306"
         << "--get-server-public-key";
    pProces->start("./mysql.exe", arguments); // mysql.exe拷贝来自安装路径C:\Program Files\MySQL\MySQL Server 8.0\bin

    if (pProces->waitForStarted()) //如果是startDetached表示守护者进程,该函数总会提示失败
    {
        qDebug() << "启动成功";
    }
    else
    {
        qDebug() << "启动失败 error:" << pProces->errorString();
    }

    while (!pProces->waitForFinished(200))
    {
        qDebug() << "wait";
        QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
        break;
    }
}

void Widget::on_read()
{
    QProcess *pProces = (QProcess *)sender();
    qDebug() << QString::fromLocal8Bit(pProces->readAll());
}

2、接下来就是qt的数据库常规操作了

// 创建数据库连接
QSqlDatabase ConnectionPool::createConnection(const QString &connectionName)
{
    static int sn = 0;

    //先检查数据库连接对象前置条件
    if (!checkDatabase())
    {
        qDebug() << m_pInfo->databaseType << "checkDatabase false";
        return QSqlDatabase();
    }

    //创建一个新的数据库连接
    QSqlDatabase db = QSqlDatabase::addDatabase(m_pInfo->databaseType, connectionName);

    if ("QSQLITE" == m_pInfo->databaseType)
    {
        db.setDatabaseName(m_pInfo->databaseName);
    }
    else // eg."QMYSQL"
    {
        db.setHostName(m_pInfo->hostName);
        if (m_pInfo->port != 0)
        {
            db.setPort(m_pInfo->port);
        }

        db.setDatabaseName(m_pInfo->databaseName);
        db.setUserName(m_pInfo->username);
        db.setPassword(m_pInfo->password);
    }

    if (db.open()) //打开数据库
    {
        qDebug() << QString("Connection created:%1, type:%2, sn:%3").arg(connectionName).arg(m_pInfo->databaseType).arg(++sn);

        if (db.driver()->hasFeature(QSqlDriver::Transactions))
        {
            qDebug() << m_pInfo->databaseType << "support Transaction";
        }
        else
        {
            qDebug() << m_pInfo->databaseType << "not support Transaction";
        }

        return db;
    }
    else
    {
        qDebug() << m_pInfo->databaseType << "Create connection error:" << db.lastError().text();

        if (QSqlDatabase::contains(connectionName))
        {
            QSqlDatabase::removeDatabase(connectionName); //关闭数据库
            qDebug() << QString("Connection deleted: %1").arg(connectionName);
        }

        return QSqlDatabase();
    }
}

四、零零星星的知识点

1、要求使用安全连接或使用 RSA 密钥对进行密码交换的未加密连接是什么意思?

caching_sha2_password 对密码安全性要求更高,要求用户认证过程中在网络传输的密码是加密的:

  • 如果是 SSL 加密连接,则使用 SSL 证书和密钥对来完成 "对称加密密钥对(在TSL握手中生成)" 的交换,后续使用“对称加密密钥对” 加密密码和数据。具体见:MySQL:SSL 连接浅析
  • 如果是非 SSL 加密连接,则在连接建立时客户端使用 MySQL Server 端的 RSA 公钥加密用户密码,Server 端使用 RSA 私钥解密验证密码的正确性,可以防止密码在网络传输时被窥探。

tips:SSL加密连接会不止会加密用户密码,还会加密数据(SQL请求、返回的结果);非加密连接只使用 RSA 密钥对进行用户密码的加密。

2、未加密连接是怎么使用 RSA 密钥对进行密码交换的?

当用户验证成功后,会把用户密码哈希缓存起来。新连接客户端发起登录请求时,MySQL Server 端会判断是否命中缓存,如果没有缓存,对于未加密的连接,caching_sha2_password 插件要求连接建立时使用 RSA 进行加密密码交换,否则报错,其过程为:

  • 客户端如果拥有服务端的 RSA 公钥,则使用 --server-public-key-path 选项指定 RSA 公钥文件;
  • 客户端使用 RSA 公钥对用户密码进行加密,请求连接;
  • 服务端使用 RSA 私钥进行解密,验证密码的正确性。

如果客户端没有保存服务端的 RSA 公钥文件,也可以使用 --get-server-public-key 选项从服务器请求公钥,则在建立连接时,服务端会先将 RSA 公钥发送给客户端。

如果 --server-public-key-path、--get-server-public-key 都没有指定,则会报下面这个经典的错误:

ERROR 2061 (HY000): Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.

3、在 MySQL 8.0.4 之后创建的所有新用户将默认使用 caching_sha2_password 作为他们的身份验证插件。做主从复制的时候也会碰到这种情况,cmd需要加get_master_public_key=1;

客户端如果拥有服务端的 RSA 公钥,则使用 --server-public-key-path 选项指定RSA 公钥文件;
客户端使用 RSA 公钥对用户密码进行加密,请求连接;
服务端使用 RSA 私钥进行解密,验证密码的正确性。

4、RSA 密钥对保存在哪里?

 C:\ProgramData\MySQL\MySQL Server 8.0\my.ini字段datadir记录了当前保存的数据路径。

RSA密钥对默认保存在MySQL的datadir下,用于非SSL连接时的密码加密交换:使用RSA公钥加密密码,使用RSA私钥解密。

private_key.pem      RSA公钥
public_key.pem       RSA私钥

5、密码哈希缓存何时失效?

当用户验证成功后,密码哈希会缓存起来,缓存会在以下情况被清理:

  • 当用户的密码被更改时;
  • 当使用 RENAME USER 重命名用户时;
  • 执行 FLUSH PRIVILEGES 时;
  • MySQL重启。

6、怎么查看用户的加密类型

mysql> use mysql;
Database changed
mysql> SELECT Host, User, plugin from user;
+-----------+------------------+-----------------------+
| Host      | User             | plugin                |
+-----------+------------------+-----------------------+
| %         | root             | caching_sha2_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session    | caching_sha2_password |
| localhost | mysql.sys        | caching_sha2_password |
| localhost | root             | caching_sha2_password |
+-----------+------------------+-----------------------+
5 rows in set (0.00 sec)

7、查看公钥文件的内容

mysql> SHOW STATUS LIKE "Caching_sha2_password_rsa_public_key";

8、如果你不想使用默认的 caching_sha2_password 插件,也可以使用一些其他的插件创建帐户,你必须明确指定插件。例如,使用 mysql_native_password 插件,使用此语句:

CREATE USER 'nativeuser'@'localhost'
IDENTIFIED WITH mysql_native_password BY 'password';

9、遗留问题

MySQL安装时出现current root password的解决方法??

Reconfigure,界面需要密码输入,但是总是check失败,网上找的方法说是remove server,重装!囧,,, 

五、参考文献

https://www.jianshu.com/p/d677bb316ab0

https://www.cnblogs.com/zgrey/p/15398633.html

MySQL :: MySQL 8.0 Reference Manual :: 6.4.1.2 Caching SHA-2 Pluggable Authentication

MySQL :: MySQL 8.0.4 : New Default Authentication Plugin : caching_sha2_password

https://docs.oracle.com/cd/E17952_01/mysql-shell-8.0-en/mysqlsh.html

https://docs.oracle.com/cd/E17952_01/mysql-8.0-en/caching-sha2-pluggable-authentication.html

猜你喜欢

转载自blog.csdn.net/libaineu2004/article/details/127568510