MySql 授予用户权限

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11300328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:02:21  来源:igfitidea点击:

MySql grant user permission

mysqlgrant

提问by Anuj

I want to create a new user in MySql. I do not want that new user to do much with my existing databases [I just want to grant Select privilege to him], but he can do anything and everything with a new database which he creates.

我想在 MySql 中创建一个新用户。我不希望那个新用户对我现有的数据库做太多事情 [我只想授予他 Select 权限],但他可以使用他创建的新数据库做任何事情。

Firstly, is there a way to grant permission as per the database owner? If it is possible, then that is the ideal thing I am looking for. And if not, then how do I restrict a particular user from accessing [only Select privilege] some specific database only, allowing him to do anything he wants with the remaining ones?

首先,有没有办法根据数据库所有者授予权限?如果可能,那么这就是我正在寻找的理想事物。如果没有,那么我如何限制特定用户仅访问 [仅选择权限] 某些特定数据库,允许他对其余数据库做任何他想做的事情?

回答by HeatfanJohn

From the MySQL grant documentation:

来自 MySQL授权文档

CREATE USER 'jeffrey'@'localhost' IDENTIFIED BY 'mypass';
GRANT SELECT ON *.* TO 'jeffrey'@'localhost';
GRANT ALL ON db1.* TO 'jeffrey'@'localhost';

The first command creates the user. The second grants select on all databases and tables. The third command grants all access to all tables in db1.

第一个命令创建用户。第二个授予对所有数据库和表进行选择。第三个命令授予对 db1 中所有表的所有访问权限。

Is there anything else specific you are looking to do?

你还有什么特别想做的事情吗?

回答by Anuj

To provide a specific user with a permission, you can use this framework:

要向特定用户提供权限,您可以使用此框架:

GRANT [type of permission] ON [database name].[table name] TO ‘[username]'@'localhost'; 
GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost';

The asterisks in this command refer to the database and table (respectively) that they can access—this specific command allows to the user to read, edit, execute and perform all tasks across all the databases and tables.

该命令中的星号分别表示它们可以访问的数据库和表——该特定命令允许用户读取、编辑、执行和执行跨所有数据库和表的所有任务。

Once you have finalized the permissions that you want to set up for your new users, always be sure to reload all the privileges.

确定要为新用户设置的权限后,请务必重新加载所有权限。

 FLUSH PRIVILEGES;

For more about permission you can read this article https://www.digitalocean.com/community/articles/how-to-create-a-new-user-and-grant-permissions-in-mysql

有关权限的更多信息,您可以阅读本文 https://www.digitalocean.com/community/articles/how-to-create-a-new-user-and-grant-permissions-in-mysql

For the list of permissions, see the MySQL Manual page Privileges Provided by MySQL.

有关权限列表,请参阅 MySQL 手册页MySQL 提供的权限

回答by Anil Chahal

Open mysql command prompt.

打开 mysql 命令提示符。

To create a new user when host is localhost then use this command

要在主机为 localhost 时创建新用户,请使用此命令

CREATE user 'test_user'@'localhost' identified by 'some_password';

for any host use %, like this

对于任何主机使用 %,像这样

CREATE user 'test_user'@'%' identified by 'some_password';

Once the user is created, you need to Grant some access. Use following command for this.

创建用户后,您需要授予一些访问权限。为此使用以下命令。

GRANT SELECT,INSERT,UPDATE
ON database_name.table_name
TO 'test_user'@'localhost';

After successful execution of above query, test_user can select, insert and update in table_name (name of table) of database_name (name of database).

成功执行上述查询后,test_user 可以在database_name(数据库名称)的table_name(表名称)中进行选择、插入和更新。

回答by Ebisa

grant privilegeis given in data base like this

grant privilege像这样在数据库中给出

grant privilege on object to user

objectis any data base table or relation and usermight be the whom the privilege is provided to him.

object是任何数据库表或关系,并且user可能是向他提供特权的人。

Example

例子

grant select,insert,update,on object name to user name

grant select on employee to john with grant option;

revoke delete on employee from john.