MySQL 8.0 参考手册  / 第 6 章 安全  / 6.1 一般安全问题  /  6.1.3 使 MySQL 免受攻击

6.1.3 使 MySQL 免受攻击

当您连接到 MySQL 服务器时,您应该使用密码。密码不会通过连接以明文形式传输。客户端连接序列期间的密码处理在 MySQL 4.1.1 中升级为非常安全。如果您仍在使用 pre-4.1.1 风格的密码,则加密算法不如较新的算法强。通过一些努力,可以嗅探客户端和服务器之间的流量的聪明的攻击者可以破解密码。(有关不同密码处理方法的讨论, 请参阅第 6.1.2.4 节,“MySQL 中的密码散列” 。)

所有其他信息都以文本形式传输,任何能够观看连接的人都可以阅读。如果客户端和服务器之间的连接通过不受信任的网络,并且您担心这一点,则可以使用压缩协议使流量更难破译。您还可以使用 MySQL 的内部 SSL 支持来使连接更加安全。请参阅 第 6.3 节,“使用加密连接”。或者,使用 SSH 在 MySQL 服务器和 MySQL 客户端之间获取加密的 TCP/IP 连接。您可以在http://www.openssh.org/找到开源 SSH 客户端 ,以及开源和商业 SSH 客户端的比较: http://en.wikipedia.org/wiki/Comparison_of_SSH_clients

为了使 MySQL 系统安全,您应该认真考虑以下建议:

  • 要求所有 MySQL 帐户都有密码。客户端程序不一定知道运行它的人的身份。对于客户端/服务器应用程序来说,用户可以向客户端程序指定任何用户名是很常见的。例如,任何人都可以像任何其他人一样使用mysql程序进行连接,只需调用它就 好像 没有密码一样。如果所有帐户都有密码,则使用其他用户的帐户进行连接会变得更加困难。 mysql -u other_user db_nameother_user

    有关设置密码方法的讨论,请参阅 第 6.2.10 节,“分配帐户密码”

  • 确保在数据库目录中唯一具有读取或写入权限的 Unix 用户帐户是用于运行mysqld的帐户。

  • root 切勿以 Unix用户 身份运行 MySQL 服务器。这是非常危险的,因为任何拥有 FILE特权的用户都能够使服务器创建文件为root(例如,~root/.bashrc)。为了防止这种情况, mysqld拒绝运行 as root除非使用该--user=root选项明确指定。

    mysqld可以(并且应该)作为普通的非特权用户运行。您可以创建一个单独的 Unix 帐户mysql,使一切更加安全。仅将此帐户用于管理 MySQL。要以不同的 Unix 用户身份启动mysqld ,请在指定服务器选项的选项文件组中 user指定用户名的例如: [mysqld]my.cnf

    [mysqld]
    user=mysql

    这会导致服务器以指定用户身份启动,无论您是手动启动还是使用 mysqld_safemysql.server启动它。有关详细信息,请参阅 第 6.1.5 节,“如何以普通用户身份运行 MySQL”

    以非 Unix 用户身份 运行mysqldroot并不意味着您需要更改表root中的用户名 userMySQL 帐户的用户名与 Unix 帐户的用户名无关

  • Do not grant the FILE privilege to nonadministrative users. Any user that has this privilege can write a file anywhere in the file system with the privileges of the mysqld daemon. This includes the server's data directory containing the files that implement the privilege tables. To make FILE-privilege operations a bit safer, files generated with SELECT ... INTO OUTFILE do not overwrite existing files and are writable by everyone.

    The FILE privilege may also be used to read any file that is world-readable or accessible to the Unix user that the server runs as. With this privilege, you can read any file into a database table. This could be abused, for example, by using LOAD DATA to load /etc/passwd into a table, which then can be displayed with SELECT.

    To limit the location in which files can be read and written, set the secure_file_priv system to a specific directory. See Section 5.1.7, “Server System Variables”.

  • 不要向非管理用户授予PROCESSSUPER特权。mysqladmin processlist的输出SHOW PROCESSLIST显示了当前正在执行的任何语句的文本,因此任何被允许查看服务器进程列表的用户都可能能够看到其他用户发出的语句,例如UPDATE user SET password=PASSWORD('not_secure').

    mysqld为有特权的用户保留一个额外的连接SUPER ,这样root即使所有正常连接都在使用中,MySQL 用户也可以登录并检查服务器活动。

    The SUPER privilege can be used to terminate client connections, change server operation by changing the value of system variables, and control replication servers.

  • Do not permit the use of symlinks to tables. (This capability can be disabled with the --skip-symbolic-links option.) This is especially important if you run mysqld as root, because anyone that has write access to the server's data directory then could delete any file in the system! See Section 8.12.3.2, “Using Symbolic Links for MyISAM Tables on Unix”.

  • Stored programs and views should be written using the security guidelines discussed in Section 23.6, “Stored Object Access Control”.

  • If you do not trust your DNS, you should use IP addresses rather than host names in the grant tables. In any case, you should be very careful about creating grant table entries using host name values that contain wildcards.

  • If you want to restrict the number of connections permitted to a single account, you can do so by setting the max_user_connections variable in mysqld. The CREATE USER and ALTER USER statements also support resource control options for limiting the extent of server use permitted to an account. See Section 13.7.1.2, “CREATE USER Statement”, and Section 13.7.1.1, “ALTER USER Statement”.

  • 如果插件目录可由服务器写入,则用户可以使用 将可执行代码写入目录中的文件SELECT ... INTO DUMPFILE。这可以通过 plugin_dir将服务器设置为只读或设置 为可以安全写入 secure_file_priv的目录来防止。SELECT