1、用户管理

1.1、查询用户

use mysql

-- 5.6以下的版本
select host,user,password from user;

-- 5.7以上的版本
select host,user,authentication_string from user;

1.2、创建用户

1.2.1、create user

-- 创建用户:test1,test2 , 此方式创建默认是无权限,只允许登陆
-- 单个用户创建
create user test1@'%' identified by 'test1';

-- 多个用户创建
create user test1@'%' identified by 'test1',test2@'%' identified by 'test2';

1.2.2、grant

-- 创建两个用户test3和test4,同时设置授权
-- 创建单个户
grant select on cjgl.* to 'test3'@'%' identified by 'test3';

-- 同时创建多个用户
grant select on cjgl.* to 'test3'@'%' identified by 'test3','test4'@'%' identified by 'test4';

1.2.3、手动插入insert into

-- 5.6以下版本
insert into mysql.user(host,user,password,ssl_cipher,x509_issuer,x509_subject) values ('%','test5',password('test5'),'','','');

-- 5.7以上版本
insert into mysql.user(host,user,authentication_string,ssl_cipher,x509_issuer,x509_subject) values ('%','test5',password('test5'),'','','');

-- 手动插入,必须要执行刷新,才生效
flush privileges;

1.3、修改用户名

-- 将test1,test2,修改用户名为:user1,user2
-- 修改单个用户
rename user 'test1'@'%' to 'user1'@'%';

-- 修改多个用户
rename user 'test1'@'%' to 'user1'@'%','test2'@'%' to 'user2'@'%';

1.4、修改用户密码

1.4.1、已知root密码,修改root的密码

-- 已知root密码,进行root密码的修改
-- -p password root 指定新的密码
[root@localhost ~]# mysqladmin -S /usr/local/mysql/data/mysql.sock -uroot -p password root
Enter password: -- 输入原因的密码 
mysqladmin: [Warning] Using a password on the command line interface can be insecure.

1.4.2、登陆root,修改指定用户名的密码

-- 修改user1的密码
set password for 'user1'@'%'=password('new_pwd');

1.4.3、使用update修改用户密码

-- 5.6以下的版本
update mysql.user set password=password('user2') where user='user2' and host='%';
flush privileges;

-- 5.7以上的版本
update mysql.user set authentication_string=password('user2') where user='user2' and host='%';
flush privileges;

1.4.4、忘记root密码的处理方法

1、增加忽略权限的配置
[root@localhost ~]# cat /etc/my.cnf
[mysqld]
-- 增加此行
skip-grant-tables

2、重启mysql服务

3、免密码,登陆mysql

4、使用update修改密码
update mysql.user set authentication_string=password('root123') where user='root' and host='localhost';
flush privileges;

5、删除配置信息skip-grant-tables,重启mysql服务

1.5、删除用户

1.5.1、删除方式1:drop user

-- 删除user1、user2用户
drop user user1@'%',user2@'%';

1.5.2、删除方式2:delete

-- 删除test5用户
delete from mysql.user where user='test5' and host='%';
flush privileges;

2、权限管理

2.1、权限介绍

1、什么是权限?
权限是指登录到MySQL服务器的用户,能够对数据库对象执行何种操作的规则集合。
所有的用户权限都存储在mysq1 数据库的6个权限表中,在MySQL启动时,服务器会将数据库中的各种权限信息读入到内存,你确定用户可进行的操作。

2、权限的好处
为用户分配合理的权限可以有效保证数据库的安全性,不合理的授权会使数据库存在安全隐患。


3、MySQL的权限类型
3.1、全局层级:使用ON *.*语法赋予权限。
3.2、数据库层级:使用ON db_name.*语法赋予权限。
3.3、表层级:使用ON db_name.tb1_name语法赋予权限。
3.4、列层级:使用SELECT(co11,co12…)、INSERT(co11 , co12…)和UPDATE(co11,co12…)语法授予权限。
3.5、存储过程﹑函数级:使用execute on procedure或execute on function语法授予权限。

2.2、权限管理

2.2.1、查看权限

show grants for test1@'%';
+-----------------------------------+
| Grants for test1@%                |
+-----------------------------------+
| GRANT USAGE ON *.* TO 'test1'@'%' | -- 默认权限
+-----------------------------------+

2.2.2、分配权限

-- 1、分配test1用户,具有增删改查的权限
grant select,insert,delete,update on cjgl.* to test1@'%';

-- 查询分配结果
mysql> show grants for test1@'%';
+-----------------------------------------------------------------+
| Grants for test1@%                                              |
+-----------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'test1'@'%'                               |
| GRANT SELECT, INSERT, UPDATE, DELETE ON `cjgl`.* TO 'test1'@'%' |
+-----------------------------------------------------------------+


-- 2、分配test2用户,只能修改某几列的数
grant update(ssex,age),select on cjgl.student to test2@'%';

mysql> show grants for test2@'%';
+-------------------------------------------------------------+
| Grants for test2@%                                          |
+-------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'test2'@'%'                           |
| GRANT UPDATE (age, ssex) ON `cjgl`.`student` TO 'test2'@'%' |
+-------------------------------------------------------------+


-- 3、分配执行存储过程的权限

-- 创建一个存储过程
delimiter $$
create procedure getStudentNames() 
READS SQL DATA
begin
select sname from student limit 5;
end$$

-- 给test1用户分配,执行getStudentNames存储过程的权限
grant execute on procedure cjgl.getStudentNames to 'test1'@'%';

2.2.3、回收权限

-- 回收存储过程的权限
revoke execute on procedure cjgl.getStudentNames from 'test1'@'%';

-- 回收select 权限
revoke select on cjgl.* from 'test1'@'%';

-- 回收所有权限
revoke all privileges,grant option from 'test1'@'%';

 

原文地址:http://www.cnblogs.com/ygbh/p/16924628.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性