8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

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

打开官方英文原文 ↗

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

MySQL 8.4 参考手册 / 8 安全 / 8.1 一般安全问题 / 8.1.5 如何以普通用户身份运行 MySQL
中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

8.1.5 如何以普通用户身份运行 MySQL8.1.5 How to Run MySQL as a Normal User

在 Windows 上,你可以使用普通用户帐户将服务器作为 Windows 服务运行。On Windows, you can run the server as a Windows service using a normal user account.

在 Linux 上,对于使用 MySQL 仓库或 RPM 包进行的安装,MySQL 服务器 mysqld 应该由本地 mysql 操作系统用户启动。通过其他操作系统用户启动是不被 MySQL 仓库包含的初始化脚本支持的。On Linux, for installations performed using a MySQL repository or RPM packages, the MySQL server mysqld should be started by the local mysql operating system user. Starting by another operating system user is not supported by the init scripts that are included as part of the MySQL repositories.

在 Unix(或使用 tar.gz 包安装的 Linux)上,MySQL 服务器 mysqld 可以由任何用户启动和运行。然而,出于安全原因,你应该避免以 Unix root 用户身份运行服务器。要将 mysqld 更改为以普通无特权的 Unix 用户 user_name 运行,必须执行以下操作:On Unix (or Linux for installations performed using tar.gz packages) , the MySQL server mysqld can be started and run by any user. However, you should avoid running the server as the Unix root user for security reasons. To change mysqld to run as a normal unprivileged Unix user user_name, you must do the following:

  1. 如果服务器正在运行,请先停止它(使用 mysqladmin shutdown)。Stop the server if it is running (use mysqladmin shutdown).

  2. 更改数据库目录和文件,使 user_name 具有读取和写入其中文件的权限(您可能需要以 Unix root 用户身份执行此操作):Change the database directories and files so that user_name has privileges to read and write files in them (you might need to do this as the Unix root user):

    $> chown -R user_name /path/to/mysql/datadir
    

    如果不这样做,当服务器以 user_name 身份运行时,将无法访问数据库或表。If you do not do this, the server cannot access databases or tables when it runs as user_name.

    如果 MySQL 数据目录中的目录或文件是符号链接,chown -R 可能不会为您跟随符号链接。如果没有,您也必须跟随这些链接并更改它们所指向的目录和文件。If directories or files within the MySQL data directory are symbolic links, chown -R might not follow symbolic links for you. If it does not, you must also follow those links and change the directories and files they point to.

  3. 以用户 user_name 启动服务器。另一种方法是以 Unix root 用户身份启动 mysqld,并使用 --user=user_name 选项。mysqld 启动后,会在接受任何连接之前切换为以 Unix 用户 user_name 运行。Start the server as user user_name. Another alternative is to start mysqld as the Unix root user and use the --user=user_name option. mysqld starts, then switches to run as the Unix user user_name before accepting any connections.

  4. 要在系统启动时自动以指定用户启动服务器,请通过在 /etc/my.cnf 配置文件或服务器数据目录中的 my.cnf 配置文件的 [mysqld] 组中添加 user 选项来指定用户名。例如:To start the server as the given user automatically at system startup time, specify the user name by adding a user option to the [mysqld] group of the /etc/my.cnf option file or the my.cnf option file in the server's data directory. For example:

    [mysqld]
    user=user_name
    

如果您的 Unix 机器本身未被安全保护,您应该为授权表中的 MySQL root 账户设置密码。否则,任何拥有该机器登录账户的用户都可以使用 mysql 客户端并使用 --user=root 选项执行任意操作。(无论如何,为 MySQL 账户设置密码都是一个好主意,尤其是在服务器上存在其他登录账户时。)参见 第 2.9.4 节,“保护初始 MySQL 账户”。If your Unix machine itself is not secured, you should assign passwords to the MySQL root account in the grant tables. Otherwise, any user with a login account on that machine can run the mysql client with a --user=root option and perform any operation. (It is a good idea to assign passwords to MySQL accounts in any case, but especially so when other login accounts exist on the server host.) See Section 2.9.4, “Securing the Initial MySQL Account”.