方法一(推荐)


更改 Ubuntu 软件源为 清华镜像

1
2
3
4
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse

下载较新的 MySQL 8.0

1
sudo apt install mysql-server-8.0

此时会有相关的配置提示。按提示配置下一步即可快速安装完成。

方法二


1.打开终端安装 MySQL

1
sudo apt install mysql-server

2.初始化

1
sudo mysql_secure_installation

3.紧接着会提示:

1
2
3
4
5
6
7
8
9
10
Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No:

这里输入 yes

4.然后下一步:

1
2
3
4
5
6
7
There are three levels of password validation policy:

LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG:

意思是设置一个密码强度等级。

密码验证策略有三个级别:

  • 低长度>=8
  • 中等长度>=8、数字、大小写混合和特殊字符
  • 强长度>=8,数字,大小写混合,特殊字符和字典文件

输入0=低,1=中,2=强:

5.输入完后提示

1
2
3
Securing the MySQL server deployment.
Enter password for user root:
Re-enter New password:

这里是输入 MySQL root 用户的密码

6.然后会告诉你密码估计强度为多少(阈值),是否使用这个密码。这里输入 yes

1
2
Estimated strength of the password: 50 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :

7.默认情况下MySQL安装有一个匿名用户,允许登陆而不用密码。适用于测试环境。提示告诉你是否需要把它删除。

1
2
3
4
5
6
7
8
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) :

如果需要删除就 yes,否则 no

8.这里提示是否不允许本地用户可以远程登陆 MySQL

1
2
3
4
5
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) :

允许就 no 不允许 yes (个人推荐不允许)

9.默认情况下,MySQL 附带一个名为 test 的数据库,任何人都可以访问它,这里提示是否将它移除。

1
2
3
4
5
6
7
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) :

移除 yes 不移除 no

10.最后,提示是否将决定立即生效:

1
2
3
4
5
6
7
8
9
10
 - Dropping test database...
Success.

- Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) :

输入 yes 即可

Success.

All done!