mysql –h hostname|hostIP –P port –u username –p DatabaseName –e "SQL语句"
下面详细介绍命令中的参数:
create user zhang3 identified by '123_qwerQWER'; # 默认hosts是 %
flush privileges; #立即生效
---------------------------------------------------------
create user 'kangshifu'@'localhost' identified by '123_qwerQWER'; # 创建指定host的用户
update mysql.user set user='li4' where user='zhang3';
flush privileges;
drop user li4; # 默认删除host为%的用户
drop user 'kangshifu'@'localhost'; #指定删除host的用户
flush privileges;
---------------------------------------------------------
delete from mysql.user where host='%' and user='zhang3'; # delete也可以删除用户
flush privileges;
select host, user from user;
或
select host, user from mysql.user;
set password for 'zhang3'@'%'='122_qwerQWER';
show privileges;
权限控制主要是出于安全因素,因此需要遵循以下几个 经验原则 :
注意: 命令行字母可小写
GRANT 权限1,权限2,…权限n ON 数据库名称.表名称 TO 用户名@用户地址 [IDENTIFIED BY ‘密码口令’];
给li4用户用本地命令行方式,授予atguigudb这个库下的所有表的插删改查的权限。
GRANT SELECT,INSERT,DELETE,UPDATE ON atguigudb.* TO li4@localhost ;
授予通过网络方式登录的joe用户 ,对所有库所有表的全部权限,密码设为123。注意这里唯独不包括grant的权限
GRANT ALL PRIVILEGES ON *.* TO joe@'%' IDENTIFIED BY '123';
实例如下
grant select on atguigu.* to zhang3@%;
grant select on custom.users to 'zhang3'@'%';
show grants for 'root'@'%'\G
show grants for 'zhang3'@'%'\G
REVOKE 权限1,权限2,…权限n ON 数据库名称.表名称 FROM 用户名@用户地址;
#收回全库全表的所有权限
REVOKE ALL PRIVILEGES ON *.* FROM joe@'%';
#收回mysql库下的所有表的插删改查权限
REVOKE SELECT,INSERT,UPDATE,DELETE ON mysql.* FROM joe@localhost;
desc mysql.user;
select * from mysql.user\G
DESCRIBE mysql.db;
procs_priv表 tables_priv表和 columns_priv表 需要时自行了解
CREATE ROLE 'role_name'[@'host_name'] [,'role_name'[@'host_name']]...
CREATE ROLE 'manager'@'localhost';
GRANT privileges ON table_name TO 'role_name'[@'host_name'];
show privileges\G
SHOW GRANTS FOR 'manager';
REVOKE privileges ON tablename FROM 'rolename';
注意, 如果你删除了角色,那么用户也就失去了通过这个角色所获得的所有权限 。
DROP ROLE role [,role2]...
DROP ROLE 'school_read';
GRANT role [,role2,...] TO user [,user2,...];
GRANT 'school_read' TO 'kangshifu'@'localhost';
show variables like 'activate_all_roles_on_login';
SET GLOBAL activate_all_roles_on_login=ON;
这条 SQL 语句的意思是,对 所有角色永久激活 。运行这条语句之后,用户才真正拥有了赋予角色的所有
权限。
REVOKE role FROM user;
REVOKE 'school_read' FROM 'kangshifu'@'localhost';
SHOW GRANTS FOR 'kangshifu'@'localhost';