一、用户授权
mysql> grant all privileges on *.* to 'yangxin'@'%' identified by 'yangxin123456' with grant option;
all privileges:表示将所有权限授予给用户。也可指定具体的权限,如:SELECT、CREATE、DROP等。
on:表示这些权限对哪些数据库和表生效,格式:数据库名.表名,这里写“*”表示所有数据库,所有表。如果我要指定将权限应用到test库的user表中,可以这么写:test.user
to:将权限授予哪个用户。格式:”用户名”@”登录IP或域名”。%表示没有限制,在任何主机都可以登录。比如:”yangxin”@”192.168.0.%”,表示yangxin这个用户只能在192.168.0IP段登录
identified by:指定用户的登录密码
二、刷新权限
mysql> flush privileges;
三、查看用户权限
mysql> grant select,create,drop,update,alter on *.* to 'yangxin'@'localhost' identified by 'yangxin0917' with grant option;
四、回收权限
mysql> revoke create on *.* from 'yangxin@localhost';
mysql> flush privileges;
五、删除用户
mysql> select host,user from user;
mysql> drop user 'yangxin'@'localhost';
六、用户重命名
shell> rename user 'test3'@'%' to 'test1'@'%';
七、修改密码
mysql> use mysql;
mysql5.7之前
mysql> update user set password=password('123456') where user='root';
mysql5.7之后
mysql> update user set authentication_string=password('123456') where user='root';
先设置该用户只有show database权限
grant select,insert,update,delete on redmine1.* to jira@"%" identified by "jira";
新增超级权限并允许远程访问:
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'192.168.1.3' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
FLUSH PRIVILEGES;
mysql8.0:
grant all privileges on *.* to 'root'@'localhost';
授权命令GRANT 语句的语法如下:
GRANT privileges (columns) ON what TO user IDENTIFIEDBY "password"
WITH GRANT OPTION
对用户授权
mysql>grant rights on database.* to user@host identified by "pass";
例1:
增加一个用户test1密码为abc,让他可以在任何主机上登录,并对所有数据库有查询、插入、修改、删除的权限。
grant select,insert,update,delete on *.* to test1@"%" Identified by "abc";
ON 子句中. 说明符的意思是“所有数据库,所有的表”
例2:
增加一个用户test2密码为abc, 让他只可以在localhost上登录,并可以对数据库mydb进行查询、插入、修改、删除的操作。
grant select,insert,update,delete on mydb.* to test2@localhost identified by "abc";
例子3
增加一个用户custom,他能从主机localhost、server.domain和whitehouse.gov连接。他只想要从 localhost存取bankaccount数据库,从whitehouse.gov存取expenses数据库和从所有3台主机存取customer 数据库。他想要从所有3台主机上使用口令stupid。
网友评论