日期:2014-05-16  浏览次数:20732 次

mysql用户授权操作

mysql 对用户授权的操作
?
? grant? 权限? on? 数据库名.表名 to 用户名@'可以访问的地址' identified by "密码"
??
?
权限:? all? 所有权限??? usage 无权限?? select,update,insert,delete,等权限
数据库.表名:? *.*?????????? 所有数据库的所有表
????????????????????????????? 数据库名.*???? 单个数据库的所有表
???????????????????????????? 数据库名.表名???? 单个数据库的某个表
用户名: 授权的用户名
可以访问的地址:?? % 所有地址,但是localhost不能访问
??????????????????????????????????????? localhost?? 只有localhost可以访问
?????????????????????????????????????? 192.168.1.0/24 可以访问网段地址
?????????????????????????????????????? 192.168.1.1 只能某个地址访问
?实验部分:
?
??? 1、给zhaoyun用户授予在localhost登陆,对zhaoyun数据库可以进行 create,select,update,insert,delete操作,但是不设置密码就可以登陆;????
mysql> grant create,select,update,insert,delete on zhaoyun.* to zhaoyun@'localhost';
?
登陆测试
[root@zhaoyun ~]# mysql -uzhaoyun
mysql> use mysql ;?? #zhaoyun用户对mysql数据库没有任何权限。
ERROR 1044 (42000): Access denied for user 'zhaoyun'@'localhost' to database 'mysql'
mysql>
mysql> use zhaoyun ;? #可以使用zhaoyun数据库
Database changed
mysql> show grants ;??? #查看当前用户拥有的权限
+--------------------------------------------------+
| Grants for zhaoyun@localhost???????????????????????????????????????????????????????? |
+--------------------------------------------------+
| GRANT USAGE ON *.* TO 'zhaoyun'@'localhost'????????????????????????????????????????? |
| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE ON `zhaoyun`.* TO 'zhaoyun'@'localhost' |
+--------------------------------------------------+
2 rows in set (0.00 sec)
mysql> create table test(user char(3));???? #建表测试
Query OK, 0 rows affected (0.00 sec)
mysql> select * from test ;??? #查询测试
Empty set (0.00 sec)
mysql> insert into test values('zhaoyun');?? #插入数据测试
Query OK, 1 row affected, 1 warning (0.01 sec)

?
2、给zhaoyun用户授予从192.168.0.7的机器登陆,并有相应的权限。
mysql> grant create,select,insert,update on zhaoyun.* to zhaoyun@'192.168.0.7' i
dentified by "zhaoyun";
Query OK, 0 rows affected (0.00 sec)
客户端登陆测试
[root@zhaoyun ~]# mysql -h192.168.0.55 -uzhaoyun -pzhaoyun;
Welcome to the MySQL monitor.? Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.1.32-community MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> show grants ;
+------------------------------------------+
| Grants for zhaoyun@192.168.0.7?????????????????????????????????????????????????????????????????????????????????? |
+------------------------------------------+
| GRANT USAGE ON *.* TO 'zhaoyun'@'192.168.0.7' IDENTIFIED BY PASSWORD '*875232B4D3487BBF724E4A0B22DB6A8DFC489C11' |
| GRANT SELECT, INSERT, UPDATE, CREATE ON `zhaoyun`.* TO 'zhaoyun'@'192.168.0.7'?????????????????????????????????? |
+------------------------------------------+
2 rows in set (0.01 sec)
把ip地址改成192.168.0.8测试,就 不行了
[root@zhaoyun ~]# ifconfig eth1 192.168.0.8
[root@bogon red hat 5]# ifconfig eth1 |grep addr
????????? inet addr:192.168.0.8? Bcast:192.168.0.255? Mask:255.255.255.0
?? [root@bogon red hat 5]# mysql -h192.168.0.55 -uzhaoyun -pzhaoyun
ERROR 1130 (00000): Host '192.168.0.8' is not allowed to connect to this MySQL server
再改回来:就可以了。
[root@bogon red hat 5]# ifconfig eth1 192.168.0.7
[root@bogon red hat 5]# mysql -h192.168.0.55 -uzhaoyun -pzhaoyun
Welcome to the MySQL monitor.? Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.1.32-community MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
上面授予的权限没有delete权限,测试下
有问题了吧
mysql> delete from t1 ;
ERROR 1142 (42000): DELETE command denied to user 'zhaoyun'@'BOGON' for table 't1'

?
3、给用户授予可以把自己权限再授给其他人的权限。
mysql> grant create on zhaoyun.t1 to zhaoyun@'192.168.0.7' identified by "z