在 MySQL 中创建新用户并授予它对一个数据库的完全访问权限

2021-11-20 00:00:00 mysql

我想在 MySQL 中创建一个新用户,并只授予它对一个数据库的完全访问权限,例如 dbTest,我使用类似 create database dbTest; 的命令创建该数据库.执行此操作的 MySQL 命令是什么?

I want to create a new user in MySQL and give it full access only to one database, say dbTest, that I create with a command like create database dbTest;. What would be the MySQL commands to do that?

推荐答案

试试这个来创建用户:

CREATE USER 'user'@'hostname';

试试这个让它访问数据库dbTest:

Try this to give it access to the database dbTest:

GRANT ALL PRIVILEGES ON dbTest.* To 'user'@'hostname' IDENTIFIED BY 'password';

如果您在同一台机器上运行访问 MySQL 的代码/站点,则主机名将是 localhost.

If you are running the code/site accessing MySQL on the same machine, hostname would be localhost.

现在,崩溃了.

GRANT - 这是用于创建用户和授予数据库、表等权限的命令.

GRANT - This is the command used to create users and grant rights to databases, tables, etc.

ALL PRIVILEGES - 这告诉它用户将拥有所有标准权限.但是,这不包括使用 GRANT 命令的权限.

ALL PRIVILEGES - This tells it the user will have all standard privileges. This does not include the privilege to use the GRANT command however.

dbtest.* - 这指示 MySQL 应用这些权限以在整个 dbtest 数据库中使用.如果您愿意,您可以将 * 替换为特定的表名或存储例程.

dbtest.* - This instructions MySQL to apply these rights for use in the entire dbtest database. You can replace the * with specific table names or store routines if you wish.

TO 'user'@'hostname' - 'user' 是您正在创建的用户帐户的用户名.注意:你必须在那里有单引号.'hostname' 告诉 MySQL 用户可以连接哪些主机.如果您只想从同一台机器上使用它,请使用 localhost

TO 'user'@'hostname' - 'user' is the username of the user account you are creating. Note: You must have the single quotes in there. 'hostname' tells MySQL what hosts the user can connect from. If you only want it from the same machine, use localhost

IDENTIFIED BY 'password' - 正如您所猜到的,这会为该用户设置密码.

IDENTIFIED BY 'password' - As you would have guessed, this sets the password for that user.

相关文章