问题
在Ubuntu安装MySQL时建议直接使用系统命令安装,如果使用官网下载安装包的方式非常复杂。
安装配置
1.安装
bash
sudo apt-get update
我在安装的时候这一步都出现问题,见了鬼了,排查后是因为网络问题,无法访问清华镜像导致的问题,后来通过更换阿里云镜像解决(阿里云镜像有点 慢,他们说科大的比较快)。如果这一步正常运行就不用管了。
##########################################################################
ps:如何更换镜像源
1.备份原来的源,将以前的源备份一下,以防以后可以用的。
bash
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
2.打开/etc/apt/sources.list文件
bash
sudo vim /etc/apt/sources.list
3.清空文件内容,替换为下面内容(第一行你不要删)
bash
#添加阿里源
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
3.更新
bash
sudo apt-get update
如出现依赖问题,解决方式如下:
bash
sudo apt-get -f install
####################################################################
然后使用如下命令安装
bash
sudo apt install mysql-server
完成后:

2.配置
1.查看MySQL状态
bash
sudo systemctl status mysql

2.配置密码
安装后的初始用户密码是随机的,需要查看一下再修改。
bash
sudo cat /etc/mysql/debian.cnf

登录
bash
mysql -u debian-sys-maint -p
随后输入密码

成功进入
更改密码
bash
use mysql;
select user,plugin from user;

更改密码设置
bash
update user set plugin='mysql_native_password' where user ='root';

刷新配置
bash
flush privileges;
更改密码
bash
alter user 'root'@'localhost' identified by '1234';

这一步可能会报错,如下

这时要改一下密码策略
bash
set global validate_password.policy = 0;
set global validate_password.length = 4;
降低密码的校验规则之后,再次执行上述修改密码的指令。
退出

重新登录

成功!
创建远程用户
默认的root用户只能当前节点localhost访问,是无法远程访问的,我们还需要创建一个root账户,用户远程访问
bash
create user 'root'@'%' IDENTIFIED WITH mysql_native_password BY '1234';
分配权限
bash
grant all on *.* to 'root'@'%';
然后就可以使用root用户通过任意ip远程连接了。