分布式数据库基础与应用
上QQ阅读APP看书,第一时间看更新

1.2.2 MySQL的安装

MySQL的安装步骤如下。

步骤1:在Ubuntu中,默认情况下只有最新版本的MySQL被包含在APT软件包存储库中。要安装MySQL,只需更新服务器上的包索引并安装默认包apt-get,具体如下。

# 命令1
sudo apt-get update
# 命令2
sudo apt-get install mysql-server

在安装MySQL服务时,系统会提示是否继续,这时输入Y即可继续安装。本步骤所涉及的内容如图1-7~图1-9所示。

图1-7 更新服务器上的包索引并安装默认包apt-get

图1-8 输入Y继续安装MySQL

图1-9 安装过程中弹出的界面

步骤2:初始化配置,具体如下。

ubuntu@ubuntu:~$ sudo mysql_secure_installation
[sudo] password for ubuntu:
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
# 修改密码
VALIDATE PASSWORD PLUGIN 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 plugin?
# 是否启用密码安全级别校验
Press y|Y for Yes, any other key for No: y # 启用密码安全级别校验
There are three levels of password validation policy:
# 0 长度 >= 8, 低级别; 1 长度 >= 8, 数字 + 混合大小写 + 特殊字符; 2 长度 >= 8, 强长度,
# 数字 + 混合大小写 + 特殊字符和字典
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: 0 # 测试可以选择低级别
Please set the password for root here.
# 输入新密码
New password:
Re-enter new password:
Estimated strength of the password: 25
# 是否继续使用所提供的密码
Do you wish to continue with the password provided?(Press y|Y for Yes, any other
key for No) : Y # 是
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 in
tended only for testing, and to make the installation go a bit smoother.
# You should remove them before moving into a production environment.
# 是否删除匿名账户,MySQL数据库默认在user表中,有一条记录,其host字段为localhost,user
# 字段为空,password字段为空。该记录表明MySQL数据库中具有一个匿名账户,可以通过本地localhost
# 域名连接数据库。根据项目需求进行相应设置即可
Remove anonymous users? (Press y|Y for Yes, any other key for No) : N # 否
  ... skipping.
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) : Y
Success.
# 默认情况下,MySQL附带一个名为“test”的数据库,任何人都可以访问。这仅用于测试,在进入生产前
# 应将其移除
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.
# 是否删除test
Remove test database and access to it? (Press y|Y for Yes, any other key for
No): N # 否
  ... skipping.
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) : Y # 是
Success.
All done!

步骤3:使用systemctl status mysql.service命令检查MySQL服务状态,如图1-10所示。可以看出,Active为active (running),这说明MySQL服务状态是正常的。

图1-10 MySQL服务状态

步骤4:MySQL客户端连接服务器。使用客户端工具MySQL进入MySQL命令提示符下,连接MySQL。下面是一个从命令行中连接MySQL服务器的简单实例。

ubuntu@ubuntu:~$ sudo mysql -uroot -p
[sudo] password for ubuntu:
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.33-0ubuntu0.16.04.1 (Ubuntu)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

登录成功后会出现MySQL命令提示窗口,其上可以执行任何SQL语句。退出MySQL命令提示窗口可以使用EXIT命令,具体如下。

mysql> EXIT
Bye