在安装MySQL后,为了确保服务器的安全性,建议使用mysql_secure_installation
工具对MySQL进行安全加固。这个工具可以帮助我们完成一些关键的安全配置,包括设置强密码、移除匿名用户、限制root
用户的远程登录以及清理默认的测试数据库等。以下是一个完整的加固过程记录,并对每一步进行了详细解释。
1. 启动mysql_secure_installation
打开终端,输入以下命令启动mysql_secure_installation
工具:
sudo mysql_secure_installation
这个命令会启动MySQL的安全配置向导,帮助我们逐步完成安全设置。
2. 设置密码验证组件
向导的第一步是询问是否要设置密码验证组件:
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: n
- 解释:密码验证组件可以检查密码的强度,确保用户设置的密码足够安全。如果你选择“是”(按
y
或Y
),系统会要求你设置一个强密码。如果你选择“否”(按其他任意键),则不会启用密码验证组件。 - 建议:如果你希望增强MySQL的安全性,建议选择“是”。但如果你只是在本地测试环境中使用MySQL,可以选择“否”以跳过这一步。
在本例中,我选择了“否”(按n
),因为我们只是在测试环境中进行配置。
3. 跳过root
用户密码设置
接下来,向导会提示:
Skipping password set for root as authentication with auth_socket is used by default.
If you would like to use password authentication instead, this can be done with the "ALTER_USER" command.
See https://dev.mysql.com/doc/refman/8.0/en/alter-user.html#alter-user-password-management for more information.
- 解释:默认情况下,MySQL使用
auth_socket
插件进行身份验证,这意味着root
用户可以通过操作系统用户验证,而无需密码。如果你希望改用密码验证,可以使用ALTER USER
命令手动设置。 - 建议:如果你希望使用密码验证,可以通过以下命令设置
root
用户的密码:ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
在本例中,我们跳过了这一步,因为默认的auth_socket
验证方式已经足够安全。
4. 移除匿名用户
向导会提示是否移除匿名用户:
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) : n
- 解释:匿名用户允许任何人无需密码即可登录MySQL,这在生产环境中是一个巨大的安全隐患。移除匿名用户可以防止未经授权的访问。
- 建议:在生产环境中,强烈建议选择“是”(按
y
或Y
)以移除匿名用户。
在本例中,我选择了“否”(按n
),因为我们只是在测试环境中进行配置。
5. 禁止root
用户远程登录
向导会提示是否禁止root
用户远程登录:
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) : n
- 解释:限制
root
用户只能从localhost
登录可以有效防止远程攻击者通过网络猜测root
密码。 - 建议:在生产环境中,强烈建议选择“是”(按
y
或Y
)以禁止root
用户远程登录。如果你确实需要远程管理数据库,可以为远程管理创建一个具有有限权限的专门用户。
在本例中,我选择了“否”(按n
),因为我们只是在本地测试环境中进行配置。
6. 移除test
数据库
向导会提示是否移除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.Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
- 解释:
test
数据库在MySQL安装时默认创建,且通常具有较宽松的访问权限,允许任何人访问。在生产环境中,这种设置可能会带来安全风险,因此建议移除。 - 建议:在生产环境中,强烈建议选择“是”(按
y
或Y
)以移除test
数据库。
在本例中,我选择了“是”(按y
),并成功移除了test
数据库及其访问权限。
7. 重新加载权限表
最后,向导会提示是否重新加载权限表:
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
- 解释:重新加载权限表可以确保刚才所做的所有更改(如移除匿名用户、移除
test
数据库等)立即生效。 - 建议:强烈建议选择“是”(按
y
或Y
),以确保所有更改立即生效。
在本例中,我选择了“是”(按y
),并成功重新加载了权限表。
8. 完成
完成上述步骤后,向导会显示以下信息:
Success.All done!
这表示MySQL的安全加固过程已经完成。
总结
通过使用mysql_secure_installation
工具,我们可以轻松地完成MySQL的安全加固。虽然在测试环境中可以选择跳过某些步骤,但在生产环境中,强烈建议启用密码验证组件、移除匿名用户、禁止root
用户远程登录以及移除test
数据库。这些措施可以显著提高MySQL服务器的安全性。
希望这篇文章对你有所帮助!如果你有任何问题或建议,请随时留言。