一、安装MySQL:

sudo apt install mysql-server mysql-client


二、设置密码:

第一步:首先查看MySQL默认生成的随机密码:

sudo cat /etc/mysql/debian.cnf

可以看到类似下面的输出:

# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host     = localhost
user     = debian-sys-maint
password = CnoJI9sJ9rT12345
socket   = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host     = localhost
user     = debian-sys-maint
password = CnoJI9sJ9rT12345
socket   = /var/run/mysqld/mysqld.sock


第二步:然后使用以上的user和password进行登录:

mysql -u debian-sys-maint -p

输入以上password的值后登录成功。


第三步:修改root密码:

alter user "root"@"localhost" identified with mysql_native_password by "root密码";
flus privileges;

即可启动root账号的密码。