6.1.5 How to Run MySQL as a Normal User如何以普通用户身份运行MySQL

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

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. 在Linux上,对于使用MySQL存储库或RPM包执行的安装,MySQL服务器mysqld应由本地mysql操作系统用户启动。Starting by another operating system user is not supported by the init scripts that are included as part of the MySQL repositories.MySQL存储库中包含的init脚本不支持由其他操作系统用户启动。

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

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

  2. 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):更改数据库目录和文件,以便user_name具有读取和写入其中文件的权限(您可能需要以Unix root用户的身份执行此操作):

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

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

    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.如果MySQL数据目录中的目录或文件是符号链接,chown -R可能不会跟在符号链接后面。如果没有,您还必须遵循这些链接并更改它们指向的目录和文件。

  3. 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. 以用户user_name身份启动服务器。另一种选择是以Unix root用户身份启动mysqld,并使用--user=user_name选项。mysqld starts, then switches to run as the Unix user user_name before accepting any connections.mysqld启动,然后在接受任何连接之前切换为以Unix用户user_name运行。

  4. 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:要在系统启动时以给定用户身份自动启动服务器,请通过在服务器数据目录中的/etc/my.cnf选项文件或my.cnf选件文件的[mysqld]组中添加用户选项来指定用户名。例如:

    [mysqld]
    user=user_name

If your Unix machine itself is not secured, you should assign passwords to the MySQL root account in the grant tables. 如果你的Unix机器本身不安全,你应该在授权表中为MySQL根帐户分配密码。Otherwise, any user with a login account on that machine can run the mysql client with a --user=root option and perform any operation. 否则,在该机器上拥有登录帐户的任何用户都可以使用--user=root选项运行mysql客户端并执行任何操作。(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.)(在任何情况下,为MySQL帐户分配密码都是一个好主意,尤其是当服务器主机上存在其他登录帐户时。) See Section 2.10.4, “Securing the Initial MySQL Account”.请参阅第2.10.4节,“保护初始MySQL帐户”