8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

8.1.1 安全指南8.1.1 Security Guidelines

任何在连接到互联网的计算机上使用 MySQL 的人都应该阅读本节,以避免最常见的安全错误。Anyone using MySQL on a computer connected to the Internet should read this section to avoid the most common security mistakes.

在讨论安全性时,有必要充分考虑保护整个服务器主机(不仅是 MySQL 服务器)免受所有类型的适用攻击:窃听、篡改、重放和拒绝服务。这里不涵盖可用性和容错的所有方面。In discussing security, it is necessary to consider fully protecting the entire server host (not just the MySQL server) against all types of applicable attacks: eavesdropping, altering, playback, and denial of service. We do not cover all aspects of availability and fault tolerance here.

MySQL 对用户可能尝试执行的所有连接、查询和其他操作使用基于访问控制列表(ACL)的安全性。此外,还支持 MySQL 客户端和服务器之间的 SSL 加密连接。这里讨论的许多概念并非 MySQL 特有;几乎所有应用程序都适用相同的一般理念。MySQL uses security based on Access Control Lists (ACLs) for all connections, queries, and other operations that users can attempt to perform. There is also support for SSL-encrypted connections between MySQL clients and servers. Many of the concepts discussed here are not specific to MySQL at all; the same general ideas apply to almost all applications.

运行 MySQL 时,请遵循以下指南:When running MySQL, follow these guidelines:

  • 绝不要给任何人(除了 MySQL root 账户)访问 mysql 系统数据库中的 user 表的权限! 这一点至关重要。Do not ever give anyone (except MySQL root accounts) access to the user table in the mysql system database! This is critical.

  • 了解 MySQL 访问权限系统的工作原理(参见 第 8.2 节,“访问控制和账户管理”)。使用 GRANT 和 REVOKE 语句来控制对 MySQL 的访问。不要授予超过必要的权限。切勿向所有主机授予权限。Learn how the MySQL access privilege system works (see Section 8.2, “Access Control and Account Management”). Use the GRANT and REVOKE statements to control access to MySQL. Do not grant more privileges than necessary. Never grant privileges to all hosts.

    清单:Checklist:

    • 尝试 mysql -u root。如果你能够在不输入密码的情况下成功连接到服务器,那么任何人都可以以 MySQL root 用户身份连接到你的 MySQL 服务器,并拥有全部权限!请查看 MySQL 安装说明,特别注意有关设置 root 密码的信息。参考 第 2.9.4 节,“保护初始 MySQL 账户”。Try mysql -u root. If you are able to connect successfully to the server without being asked for a password, anyone can connect to your MySQL server as the MySQL root user with full privileges! Review the MySQL installation instructions, paying particular attention to the information about setting a root password. See Section 2.9.4, “Securing the Initial MySQL Account”.

    • 使用 SHOW GRANTS 语句检查哪些账户具有访问权限。然后使用 REVOKE 语句移除不必要的权限。Use the SHOW GRANTS statement to check which accounts have access to what. Then use the REVOKE statement to remove those privileges that are not necessary.

  • 不要在数据库中存储明文密码。如果你的计算机被攻破,入侵者可以获取完整的密码列表并使用它们。相反,应使用SHA2()或其他单向哈希函数,并存储哈希值。Do not store cleartext passwords in your database. If your computer becomes compromised, the intruder can take the full list of passwords and use them. Instead, use SHA2() or some other one-way hashing function and store the hash value.

    为了防止使用彩虹表进行密码恢复,不要在明文密码上使用这些函数;相反,应选择一个字符串作为盐,并使用 hash(hash(密码) 盐) 的值。To prevent password recovery using rainbow tables, do not use these functions on a plain password; instead, choose some string to be used as a salt, and use hash(hash(password)+salt) values.

  • 假设所有密码都可能受到使用已知密码列表的自动破解尝试,以及基于公开可获得的关于您的信息(例如社交媒体帖子)进行的有针对性猜测。不要选择由容易破解或猜测的内容组成的密码,比如字典词、专有名、运动队名称、首字母缩略词或常见短语,尤其是当它们与您相关时。如果大写字母、数字替代和添加以及特殊字符的使用方式是可预测的,也没有帮助。此外,不要选择您在任何地方见过用作示例的密码,或者其变体,即使它被呈现为强密码示例。Assume that all passwords will be subject to automated cracking attempts using lists of known passwords, and also to targeted guessing using publicly available information about you, such as social media posts. Do not choose passwords that consist of easily cracked or guessed items such as a dictionary word, proper name, sports team name, acronym, or commonly known phrase, particularly if they are relevant to you. The use of upper case letters, number substitutions and additions, and special characters does not help if these are used in predictable ways. Also do not choose any password you have seen used as an example anywhere, or a variation on it, even if it was presented as an example of a strong password.

    相反,选择尽可能长且不可预测的密码。这并不意味着组合必须是难以记住和复制的随机字符字符串,尽管如果你拥有能够生成、填充并安全存储此类密码的密码管理软件,这是一个不错的方法。包含多个单词的短语密码容易创建、记住和复制,并且比由单个修改过的单词或可预测字符序列组成的典型用户密码安全得多。要创建安全的短语密码,确保其中的单词和其他元素不是已知的短语或引用,顺序不可预测,并且最好彼此之间没有任何先前关系。Instead, choose passwords that are as long and as unpredictable as possible. That does not mean the combination needs to be a random string of characters that is difficult to remember and reproduce, although this is a good approach if you have, for example, password manager software that can generate and fill such passwords and store them securely. A passphrase containing multiple words is easy to create, remember, and reproduce, and is much more secure than a typical user-selected password consisting of a single modified word or a predictable sequence of characters. To create a secure passphrase, ensure that the words and other items in it are not a known phrase or quotation, do not occur in a predictable order, and preferably have no previous relationship to each other at all.

  • 投资防火墙。这至少可以保护你免受任何软件中 50%的漏洞攻击。将 MySQL 置于防火墙后或放在隔离区(DMZ)中。Invest in a firewall. This protects you from at least 50% of all types of exploits in any software. Put MySQL behind the firewall or in a demilitarized zone (DMZ).

    清单:Checklist:

    • 尝试使用如 nmap 这样的工具从互联网扫描你的端口。MySQL 默认使用端口 3306。此端口不应可从不受信任的主机访问。作为检查 MySQL 端口是否开放的简单方法,可以从某台远程机器尝试以下命令,其中 server_host 是运行 MySQL 服务器的主机名或 IP 地址:Try to scan your ports from the Internet using a tool such as nmap. MySQL uses port 3306 by default. This port should not be accessible from untrusted hosts. As a simple way to check whether your MySQL port is open, try the following command from some remote machine, where server_host is the host name or IP address of the host on which your MySQL server runs:

      $> telnet server_host 3306
      

      如果 telnet 挂起或连接被拒绝,则说明端口被阻塞,这是你希望的状态。如果你能够连接并看到一些乱码字符,说明端口是开放的,应当在防火墙或路由器上关闭它,除非你确实有充分的理由保持其开放。If telnet hangs or the connection is refused, the port is blocked, which is how you want it to be. If you get a connection and some garbage characters, the port is open, and should be closed on your firewall or router, unless you really have a good reason to keep it open.

  • 访问 MySQL 的应用程序不应信任用户输入的任何数据,并且应使用适当的防御性编程技术编写。参见 第 8.1.7 节,“客户端编程安全指南”。Applications that access MySQL should not trust any data entered by users, and should be written using proper defensive programming techniques. See Section 8.1.7, “Client Programming Security Guidelines”.

  • 不要通过互联网传输明文(未加密)数据。这些信息可以被所有有时间和能力截取它的人访问,并用于他们自己的目的。相反,应使用加密协议,如 SSL 或 SSH。MySQL 支持内部 SSL 连接。另一种技术是使用 SSH 端口转发来为通信创建加密(且压缩)的通道。Do not transmit plain (unencrypted) data over the Internet. This information is accessible to everyone who has the time and ability to intercept it and use it for their own purposes. Instead, use an encrypted protocol such as SSL or SSH. MySQL supports internal SSL connections. Another technique is to use SSH port-forwarding to create an encrypted (and compressed) tunnel for the communication.

  • 学习使用 tcpdump 和 strings 工具。在大多数情况下,你可以通过发出如下命令来检查 MySQL 数据流是否未加密:Learn to use the tcpdump and strings utilities. In most cases, you can check whether MySQL data streams are unencrypted by issuing a command like the following:

    $> tcpdump -l -i eth0 -w - src or dst port 3306 | strings
    

    这在 Linux 下可用,并且在其他系统下进行小的修改后也应该可用。This works under Linux and should work with small modifications under other systems.

    Warning

    如果你没有看到明文数据,这并不总是意味着信息实际上已被加密。如果你需要高安全性,请咨询安全专家。If you do not see cleartext data, this does not always mean that the information actually is encrypted. If you need high security, consult with a security expert.