MySQL服务安全加固
MySQL服务安全加固
数据库管理人员可以参考本文档进行 MySQL 数据库系统的安全配置加固,提高数据库的安全性,确保数据库服务稳定、安全、可靠地运行。
漏洞发现
可以使用安骑士企业版自动检测您的服务器上是否存在 MySQL 漏洞问题,或者您也可以自己排查您服务器上的 MySQL 服务是否存在安全问题。
安全加固
帐号安全
禁止 Mysql 以管理员帐号权限运行
以普通帐户安全运行 mysqld,禁止以管理员帐号权限运行 MySQL 服务。在 /etc/my.cnf 配置文件中进行以下设置。
<span>[<span>mysql<span>.<span>server<span>]</span></span></span></span></span>
<span>user<span>=<span>mysql</span></span></span>
避免不同用户间共享帐号
参考以下步骤。
创建用户。
<span>mysql<span>><span> mysql<span>><span> <span>insert <span><span>into</span></span></span></span></span></span></span></span>
<span>mysql<span>.<span><span>user<span>(<span>Host<span>,<span><span>User<span>,<span><span>Password<span>,<span>ssl_cipher<span>,<span>x509_issuer<span>,<span>x509_sub </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
<span>ject<span>)<span> <span>values<span>(<span><span>"localhost"<span>,<span><span>"pppadmin"<span>,<span><span>password<span>(<span><span>"passwd"<span>),<span><span>‘‘<span>,<span><span>‘‘<span>,<span><span>‘‘<span>);</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
执行以上命令可以创建一个 phplamp 用户。
使用该用户登录 MySQL 服务。
<span>mysql<span>><span><span>exit<span>;</span></span></span></span></span>
<span>@><span>mysql <span>-<span>u phplamp <span>-<span><span>p </span></span></span></span></span></span></span>
<span><span>@>输入密码</span></span>
<span><span>mysql<span><span>><span>登录成功</span></span></span></span></span>
删除无关帐号
DROP USER 语句可用于删除一个或多个 MySQL 账户。使用 DROP USER 命令时,必须确保当前账号拥有 MySQL 数据库的全局 CREATE USER 权限或 DELETE 权限。账户名称的用户和主机部分分别与用户表记录的 User 和 Host 列值相对应。
执行
DROP USER user;
语句,您可以取消一个账户和其权限,并删除来自所有授权表的帐户权限记录。
口令
检查账户默认密码和弱密码。口令长度需要至少八位,并包括数字、小写字母、大写字母和特殊符号四类中的至少两种类型,且五次以内不得设置相同的口令。密码应至少每 90 天进行一次更换。
您可以通过执行以下命令修改密码。
<span> mysql<span>><span> update<span> user <span><span>set<span> <span>password<span>=<span>password<span>(<span>‘test!p3‘<span>)<span> <span>where<span> <span>user<span>=<span><span>‘root‘<span>;</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
<span> mysql<span>><span> flush privileges<span><span>;</span></span></span></span></span>
授权
在数据库权限配置能力范围内,根据用户的业务需要,配置其所需的最小权限。
查看数据库授权情况。
<span>mysql<span>><span> use mysql<span>;</span></span></span></span>
<span>mysql<span>><span> select <span>*<span> from user<span>;</span></span></span></span></span></span>
<span>mysql<span>><span>select <span>*<span> from db<span>;</span></span></span></span></span></span>
<span>mysql<span>><span>select <span>*<span> from host<span>;</span></span></span></span></span></span>
<span>mysql<span>><span>select <span>*<span> from tables_priv<span>;</span></span></span></span></span></span>
<span>mysql<span>><span>select <span>*<span> from columns_priv<span>;</span></span></span></span></span></span>
通过 revoke 命令回收不必要的或危险的授权。
<span><span>mysql<span><span>><span><span> <span>help revoke</span></span></span></span></span></span></span>
<span><span>Name<span>:<span> <span><span>‘REVOKE‘</span></span></span></span></span></span>
<span><span>Description<span><span>:</span></span></span></span>
<span><span>Syntax<span><span>:</span></span></span></span>
<span><span>REVOKE</span></span>
<span>priv_type <span><span>[(<span><span>column_list<span><span>)]</span></span></span></span></span></span></span>
<span> <span>[,<span> priv_<span>type <span>[(<span>column_list<span>)]]<span> <span>...</span></span></span></span></span></span></span></span></span>
<span> <span>ON <span>[<span>object_<span>type<span>]</span></span></span></span></span></span>
<span> <span>{</span></span>
<span><span> <span><span>*</span></span></span></span>
<span> <span><span>|<span><span> <span><span>*.*</span></span></span></span></span></span></span>
<span> <span><span>|<span><span> db_name<span><span>.*</span></span></span></span></span></span></span>
<span> <span><span>|<span><span> db_name<span><span>.<span><span>tbl_name</span></span></span></span></span></span></span></span></span>
<span> <span><span>|<span><span> tbl_name</span></span></span></span></span>
<span> <span><span>|<span><span> db_name<span><span>.<span><span>routine_name</span></span></span></span></span></span></span></span></span>
<span> <span>}</span></span>
<span> <span>FROM<span> user <span>[,<span> user<span>]<span> <span><span>...</span></span></span></span></span></span></span></span></span>
开启日志审计功能
数据库应配置日志功能,便于记录运行状况和操作行为。
MySQL服务有以下几种日志类型:
- 错误日志: -log-err
- 查询日志: -log (可选)
- 慢查询日志: -log-slow-queries (可选)
- 更新日志: -log-update
二进制日志: -log-bin
找到 MySQL 的安装目录,在 my.ini 配置文件中增加上述所需的日志类型参数,保存配置文件后,重启 MySQL 服务即可启用日志功能。例如,
<span><span>#Enter <span>a name <span>for the binary log. Otherwise <span>a default name will be used. </span></span></span></span></span>
<span><span>#<span><span>log-bin= </span></span></span></span>
<span><span>#Enter <span>a name <span>for the query log file. Otherwise <span>a default name will be used. </span></span></span></span></span>
<span><span>#<span><span>log= </span></span></span></span>
<span><span>#Enter <span>a name <span>for the error log file. Otherwise <span>a default name will be used. </span></span></span></span></span>
<span><span>log<span>-<span><span>error<span>=</span></span></span></span></span></span>
<span><span>#Enter <span>a name <span>for the update log file. Otherwise <span>a default name will be used. </span></span></span></span></span>
<span><span>#<span><span>log-update=</span></span></span></span>
该参数中启用错误日志。如果您需要启用其他的日志,只需把对应参数前面的 “#” 删除即可。
日志查询操作说明
- 执行
show variables like ‘log_%‘;
命令可查看所有的 log。 - 执行
show variables like ‘log_bin‘;
命令可查看具体的 log。
安装最新补丁
确保系统安装了最新的安全补丁。
注意: 在保证业务及网络安全的前提下,并经过兼容性测试后,安装更新补丁。
如果不需要,应禁止远程访问
禁止网络连接,防止猜解密码攻击、溢出攻击、和嗅探攻击。
注意: 仅限于应用和数据库在同一台主机的情况。
如果数据库不需要远程访问,可以禁止远程 TCP/IP 连接,通过在 MySQL 服务器的启动参数中添加
--skip-networking
参数使 MySQL 服务不监听任何 TCP/IP 连接,增加安全性。您可以使用 安全组 进行内外网访问控制,建议不要将数据库高危服务对互联网开放。
设置可信 IP 访问控制
通过数据库所在操作系统的防火墙限制,实现只有信任的 IP 才能通过监听器访问数据库。
<span> mysql<span>><span> GRANT ALL PRIVILEGES ON db<span>.*</span></span></span></span>
<span> <span>·-><span> <span>-><span> TO <span>用户名@<span><span>‘IP子网/掩码‘<span>;</span></span></span></span></span></span></span></span></span>
连接数设置
根据您的机器性能和业务需求,设置最大、最小连接数。
在 MySQL 配置文件(my.conf 或 my.ini)的 [mysqld] 配置段中添加
max_connections = 1000
,保存配置文件,重启 MySQL 服务后即可生效。