ubuntu20.04安装mysql8

发布于:2023-01-14 ⋅ 阅读:(929) ⋅ 点赞:(0)

安装

sudo apt-get install mysql-server

验证

systemctl status mysql

在这里插入图片描述

远程连接配置

1、打开文件注释掉bind-address=127.0.0.1

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

2、重启服务

systemctl restart mysql

3、连接MySQL

sudo mysql -u root -p

4、创建远程连接账户并授权

mysql> use mysql;
mysql> CREATE USER 'root'@'%' IDENTIFIED BY '你的密码';
mysql> grant all privileges on *.* to 'root'@'%';

5、远程连接时报错plugin caching_sha2_password could not be loaded 执行

 set global validate_password.policy=LOW;
 set global validate_password.length=6;
 mysql> alter user 'root'@'%' identified with mysql_native_password by '123456';

6、刷新权限

mysql> FLUSH PRIVILEGES;

7、查看用户

mysql> select host, user, authentication_string, plugin from user;

8、链接报错

SQLSTATE[HY000] [2054] The server requested authentication method
unknown to

原因:php用的老的加密方法连接mysql 不支持mysql8新的加密方式
解决:
确保新添加的用户可以正常使用,需要把配置文件修改了

vi /etc/mysql/mysql.conf.d/mysqld.cnf
#打开该文件,在 [mysqld] 模块下面添加一句
default_authentication_plugin=mysql_native_password

更改加密方式

alter user 'root'@'localhost' identified with mysql_native_password by '123456(你的密码)';
flush privileges;

以上修改完成以后,本地链接没有问题,但是远程连接还是不行,需要修改 host 为 %

update user set host = '%' where user ='root';

假如上一步修改不成功,提示有重复数据 Duplicate entry ‘%-root’ for key ‘user.PRIMARY’,删除其他的信息

delete from user where user='root' and host ='%';