mysql数据库权限管理

常用命令

CREATE USER 'haima'@'host' IDENTIFIED BY '123456';
grant all privileges on *.* to joe@localhost identified by ‘123′;
show grants;
flush privileges;

mysql数据库权限管理

# 查看权限
# 使用mysql数据库
mysql> use mysql;
# 查看所有用户和权限
# 查询主机用户名密码:5.7版本之前的
mysql> select host,user,plugin,password from user;
# 查询主机用户名密码:5.7版本之后的,包括5.7
mysql> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
mysql> select host,user,plugin,authentication_string from mysql.user;
mysql> select host,user,plugin,authentication_string from user;
mysql> select host,user,plugin,authentication_string from userG;

# 查看当前用户(自己)权限:
mysql> show grants;


# 创建用户
CREATE USER 'username'@'host' IDENTIFIED BY 'password';

# 说明
username:你将创建的用户名
host:指定该用户在哪个主机上可以登陆,如果是本地用户可用localhost,如果想让该用户可以从任意远程主机登陆,可以使用通配符%
password:该用户的登陆密码,密码可以为空,如果为空则该用户可以不需要密码登陆服务器

如:
CREATE USER 'user01'@'localhost' IDENTIFIED BY '123456'; 
CREATE USER 'user01'@'10.246.34.85' IDENDIFIED BY '123456';
CREATE USER 'user01'@'%' IDENTIFIED BY '123456';
CREATE USER 'user01'@'%' IDENTIFIED BY '';
CREATE USER 'user01'@'%';


#修改用户密码
ALTER USER 'haima'@'%' IDENTIFIED BY '12345';
flush privileges;

# 用户授权登陆
# 如果想指定部分权限给一用户,可以这样来写: 
grant 权限1,权限2,…权限n on 数据库名称.表名称 to 用户名@用户地址 identified by ‘连接口令’;
grant select,update on 库名.表名 to username@host identified by '1234';

# 说明
权限1,…权限n代表select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file等14个权限。

当权限1,…权限n被all privileges或者all代替,表示赋予用户全部权限。

当数据库名称.表名称被*.*代替,表示赋予用户操作服务器上所有数据库所有表的权限。

用户地址可以是localhost,也可以是ip地址、机器名字、域名。也可以用’%'表示从任何地址连接。

‘连接口令’不能为空,否则创建失败。

例如:

mysql>grant select,drop on vtdc.employee to joe@10.163.225.87 identified by ‘123′;

给来自10.163.225.87的用户joe分配可对数据库vtdc的employee表进行select,drop等操作的权限,并设定口令为123。

mysql>grant all privileges on vtdc.* to joe@10.163.225.87 identified by ‘123′;

给来自10.163.225.87的用户joe分配可对数据库vtdc所有表进行所有操作的权限,并设定口令为123。

mysql>grant all privileges on *.* to joe@10.163.225.87 identified by ‘123′;

给来自10.163.225.87的用户joe分配可对所有数据库的所有表进行所有操作的权限,并设定口令为123。

mysql>grant all privileges on *.* to joe@localhost identified by ‘123′;

给本机用户joe分配可对所有数据库的所有表进行所有操作的权限,并设定口令为123。


# 添加用户权限: databasename.tablename写你的库和表名
GRANT ALL ON databasename.tablename TO 'xld_test'@'%'; 
grant select ON * TO 'haima'@'%'; 
grant select,insert,update,delete on databasename.* to 'test'@'127.0.0.1' identifi
ed by '123456';


REVOKE ALL ON *.* TO 'xld_test'@'%'; # 撤销用户权限:
REVOKE SELECT ON `test`.* FROM 'haima'@'%'; # 撤销用户test库的查询权限:

drop user 'xld_test'@'%'; # 删除用户及权限 :

flush privileges; # 刷新当前权限配置

服务器只读模式**

SHOW VARIABLES LIKE '%read_only%'; #查看只读状态

SET GLOBAL super_read_only=1; #super权限的用户只读状态 1.只读 0:可写
SET GLOBAL read_only=1; #普通权限用户读状态 1.只读 0:可写
那么我们在做数据迁移的时候不想发生任何数据的修改,包括super权限修改也要限制。

可以用锁表:

mysql> flush tables with read lock;
Query OK, 0 rows affected (0.18 sec)
使用root账号测试:
mysql>  delete from student where sid=13;
ERROR 1223 (HY000): Can't execute the query because you have a conflicting read
lock
解锁测试:
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
mysql>  delete from student where sid=13;
Query OK, 0 rows affected (0.00 sec)
[Haima的博客] http://www.cnblogs.com/haima/
原文地址:https://www.cnblogs.com/haima/p/14345838.html