杨记

碎片化学习令人焦虑,系统化学习使人进步

0%

CentOS安装MySQL

原文链接:How To Install MySQL on CentOS 8 - CodeProject

如何在CentOS 8上安装MySQL_寒冰屋的博客-CSDN博客_centos8安装mysql

Initial Server Setup with CentOS 8 | DigitalOcean

CentOS7安装MySQL8_风随心飞飞的博客-CSDN博客_centos7安装mysql8

前提

要完成本教程,您需要一台运行CentOS 8的服务器。该服务器应该有一个具有管理权限的非root用户和一个配置了firewalld.的防火墙。

安装防火墙firewalld,默认允许ssh连接

1
2
3
dnf install firewalld -y	# install firewalld
systemctl start firewalld # 启动firewalld服务
systemctl status firewalld # 查看firewalld状态,是否运行

image-20221022172705888

Now that the service is up and running, we can use the firewall-cmd utility to get and set policy information for the firewall.

1
firewall-cmd --permanent --list-all		# 列出允许的服务

image-20221022173010915

1
2
3
4
5
6
7
8
# see the additional services that you can enable by name, type
firewall-cmd --get-services

# add a service that should be allowed, use the --add-service flag
firewall-cmd --permanent --add-service=http # add the http service and allow incoming TCP traffic to port 80

# The configuration will update after you reload the firewall
firewall-cmd --reload

安装MySQL

在CentOS 8上,MySQL版本8可从默认存储库中获得。

运行以下命令来安装mysql-server包和它的一些依赖项:

1
sudo dnf install mysql-server

MySQL就安装在您的服务器上,但尚未运行。您刚刚安装的软件包将MySQL配置为名为mysqld.servicesystemd服务。 为了使用MySQL,您需要使用以下systemctl命令启动它:

1
sudo systemctl start mysqld.service

要检查服务是否正常运行,请运行以下命令。请注意,对于许多systemctl命令(包括start和status,如此处所示),您不需要在服务名称之后包含.service:

1
sudo systemctl status mysqld

如果MySQL启动成功,输出将显示MySQL服务处于活动状态:

image-20221022153257752

还可以设置MySQL在服务器启动时启动:

1
2
sudo systemctl enable mysqld
sudo systemctl disable mysqld # 服务器启动时不启动

image-20221022153604979

成功启动后的root的初始密码在日志文件中/var/log/mysqld.log,如:A temporary password is generated for root@localhost: 8yjrrdhb+ipM

保护MySQL

使用预装MySQL实例的shell脚本来加强数据库的安全性。MySQL包含一个安全脚本,允许您更改一些默认配置选项以提高MySQL的安全性。

要使用安全脚本,请运行以下命令:

1
sudo mysql_secure_installation

这将引导您完成一系列提示,询问您是否要对MySQL安装的安全选项进行某些更改。第一个提示将询问您是否要设置验证密码插件,您可以使用它来测试MySQL密码的强度。

如果您选择设置验证密码插件,脚本将要求您选择密码验证级别。最强级别(您通过输入选择2)将要求您的密码长度至少为8个字符,并且包括大写、小写、数字和特殊字符的混合:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
OutputSecuring 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: Y

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: 2

无论您是否选择设置验证密码插件,下一个提示都是为MySQL root用户设置密码。输入并确认您选择的安全密码

如果您使用了验证密码插件,您将收到有关新密码强度的反馈。然后脚本将询问您是要继续使用刚刚输入的密码还是要输入新密码。假设您对刚刚输入的密码强度感到满意,请输入Y继续执行脚本:

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

之后,您可以按Y,然后按ENTER以接受所有后续问题的默认值。这将删除一些匿名用户和测试数据库,禁用远程root登录,并加载这些新规则,以便MySQL立即尊重您所做的更改。

测试MySQL

通过连接该mysqladmin工具(一个允许您运行管理命令的客户端)来验证您的安装并获取有关它的信息。使用以下命令以root (-u root)身份连接到MySQL,提示输入密码(-p),并返回安装版本:

1
mysqladmin -u root -p version

image-20221022154113269

在服务器连接MySQL:mysql -u root -p ,输入密码看是否连接成功

设置防火墙允许mysql服务:firewall-cmd --permanent --add-service=msyql

重新加载防火墙配置:firewall-cmd --reload

使用root身份进入mysql并创建用户:CREATE USER yang IDENTIFIED BY 'abcdef';

  • 创建用户yang,密码abcdef,可以从任意主机连接MySQL

使用一台主机,使用yang登录msyql:mysql -h107.12.13.14 -P3306 -uyang -p

  • 假设你的服务器IP为107.12.13.14,使用的主机需要安装和启动mysql服务,不然mysql命令识别不了

欢迎关注我的其它发布渠道