01. 授权问题

0.如果mysql没有初始化密码,那么首先初始化密码

mysql –u root

mysql>update user set password=PASSWORD('123456') where user='root';

1. 假设有两台主机,本文中的所有实验都是在这两台主机下进行测试

 A主机:192.168.3.48(Master)

 B主机:192.168.3.8(Slaver)

-->登陆A主机

-->在A主机查看所有的数据库

-->在A主机切换数据库到mysql,查看A主机中数据库mysql下的所有table

-->查看目前A主机中的所有用户:发现A主机只有本地用户root

  

 

 

 

 

 


  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

-->在B主机上尝试使用root账户登录A主机的MySQL服务器:提示不允许登录访问

 

 

---------------------------------------华丽丽的分割线---------------------------------------

第一种方式

-->在A主机上创建并授权B主机可以登录的新的用户:用户名test,密码test,B主机的IP是192.168.3.8

-->查看A主机中此时的用户,发现多了test用户,说明创建成功

-->查看test用户的权限:发现是USAGE,表明B主机只有登录A主机mysql服务器的权限其他什么都不能操作

-->在B主机上使用test,test登录,发现登录成功,但是没有任何权限

-->在A主机上授予test@192.168.3.8所有权限

-->B主机退出重新登录用户名test,密码test,此时在B主机上就可以对A主机上的mysql服务器进行任何操作了,相当于A主机上的root用户

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

第二种方式

 

第三种方式

 

 

 

 

 

 

1.通过改表设置所有的IP可以登录

mysql -u root –p

mysql>use mysql;

mysql>update user set host = '%' where user = 'root';

mysql>select host, user from user;

使用root帐号和root的原始密码可以在任何主机登录

 

2.通过授权所有的IP可以登录

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;

flush privileges;

 授权用户root使用密码123456从任意主机连接到mysql服务器

 

 

3.通过授权指定的IP可以登录

GRANT ALL PRIVILEGES ON *.* TO 'root'@'218.12.50.60' IDENTIFIED BY 'mark' WITH GRANT OPTION;

flush privileges;

授权用户root使用密码mark从指定ip为218.12.50.60的主机连接到mysql服务器:

 

4.权限限制

授权表达式:

grant 权限 on 数据库对象 to 用户 

数据对象为 *.*:对mysql服务器中所有的数据库和表进行授权

数据对象为dbname.*:对mysql服务器中单个数据库dbname下的所有表进行授权

数据对象为dbname.user:对mysql服务器中单个数据库dbname下的user表进行授权

解除授权表达式

revoke 权限 on 数据库对象 from 用户 

数据对象的界定与上面相同

mysql权限表达式

授权普通用户对数据库testdb的所有表CRUD的权限:

       grant select on testdb.* 'general_user'@'%'  

       grant insert on testdb.* to 'general_user'@'%'  

       grant update on testdb.* to 'general_user'@'%' 
 
       grant delete on testdb.* to 'general_user'@'%' 

       grant select(id,name,birth) on testdb.* 'general_user'@'%' 

可以直接使用:

       grant select, insert, update, delete on testdb.* to 'general_user'@'%'  


授权数据库开发人员权限:

       grant create on testdb.* to developer@'192.168.0.%';  

       grant alter  on testdb.* to developer@'192.168.0.%';  

       grant drop   on testdb.* to developer@'192.168.0.%';  
     
       grant references on testdb.* to developer@'192.168.0.%'; 

       grant create temporary tables on testdb.* to developer@'192.168.0.%';  

       grant index on testdb.* to developer@'192.168.0.%';  

       grant create view on testdb.* to developer@'192.168.0.%';  

       grant show   view on testdb.* to developer@'192.168.0.%';

       grant create routine on testdb.* to developer@'192.168.0.%'; 

       grant alter  routine on testdb.* to developer@'192.168.0.%'; 

       grant execute        on testdb.* to developer@'192.168.0.%';

       grant all privileges on testdb to dba@'localhost' 
   

 

posted @ 2016-12-28 17:58  桃源仙居  阅读(310)  评论(0编辑  收藏  举报