MySql容器版更加简单,也比较符合我用于本地测试的需求,因此这里我采用Docker镜像进行安装。
1、安装
docker search mysqldocker pull mysqldocker run -itd --name Mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=xxx mysql:latest
2、连接
首次通过应用程序连接mysql数据库时,报如下错误:connect to DB fail:this authentication plugin is not supported。查询度娘后得知Mysql新版本(8.0以上)将root用户使用的plugin更新成caching_sha2_password。需要修改mysql的plugin模式。
登录mysql输入如下命令可以看到:
mysql> select user,plugin from mysql.user;+------------------+-----------------------+| user | plugin |+------------------+-----------------------+| root | caching_sha2_password || mysql.infoschema | caching_sha2_password || mysql.session | caching_sha2_password || mysql.sys | caching_sha2_password || root | caching_sha2_password |+------------------+-----------------------+
解决办法有:
(1)降级,使用老版本mysql。
(2)将root的plugin改成mysql_native_password。
这里改成:ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '密码';
这行代码有两层含义,第一:修改root的密码为’密码’,摒弃原来的旧密码。第二:使用mysql_native_password对新密码进行编码。
然后再启动应用,还是一样报错。再看mysql.user,发现还有一个host “%” 的root用户:
mysql> select host,user,plugin from mysql.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 | mysql_native_password |+-----------+------------------+-----------------------+
将这个用户也改了:ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '密码';
再看:
mysql> select host,user,plugin from mysql.user;+-----------+------------------+-----------------------+| host | user | plugin |+-----------+------------------+-----------------------+| % | root | mysql_native_password || localhost | mysql.infoschema | caching_sha2_password || localhost | mysql.session | caching_sha2_password || localhost | mysql.sys | caching_sha2_password || localhost | root | mysql_native_password |+-----------+------------------+-----------------------+
改成功了,重启容器,再尝试用应用程序连接数据库。
// 若发现还是连接失败,并报错:**this user requires mysql native password authentication**
// 在连接mysql的url上加上?allowNativePasswords=true,这次正常了。
3、参考文献
Mysql5.7报错get db conn fail this authentication plugin is not supported
mysql错误:this authentication plugin is not supported
