查看linux mysql 收回权限,Linux学习笔记:MySQL读写分离实验【从库回收权限】

1)在从库上回收用户的写权限(UPDATE/DELETE/INSERT)

这种方法必须保证主库上对应的用户不会发生改动;如果真的发生了改动,该用户的授权又会同步到从库上去,导致回收失效。

实验拓扑图

请留意,Master上由于运行多实例,而本次测试用的是3308端口的实例。

67fc57ef2c6d6a6c9eca5c75562e2498.png

实验:

1)在vmtest上新建用户web,具备增删改权限mysql> grant select,update,insert,delete on mydb.student to 'web'@'192.168.5.%' identified by 'web123';

Query OK, 0 rows affected (0.00 sec)

mysql> show grants for 'web'@'192.168.5.%';

+--------------------------------------------------------------------------------------------------------------+

| Grants for web@192.168.5.%                                                                                   |

+--------------------------------------------------------------------------------------------------------------+

| GRANT USAGE ON *.* TO 'web'@'192.168.5.%' IDENTIFIED BY PASSWORD '*67138D0908E294A380CA501A1F1A48898426B13B' |

| GRANT SELECT, INSERT, UPDATE, DELETE ON `mydb`.`student` TO 'web'@'192.168.5.%'                              |

+--------------------------------------------------------------------------------------------------------------+

2 rows in set (0.00 sec)

2)配置主从同步[root@lab ~]# mysql -uroot -p -e 'show slave status\G'

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.5.103

Master_User: rep

Master_Port: 3308

Connect_Retry: 60

Master_Log_File: mysql-bin.000102

Read_Master_Log_Pos: 277

Relay_Log_File: mysqld-relay-bin.000039

Relay_Log_Pos: 422

Relay_Master_Log_File: mysql-bin.000102

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Replicate_Do_DB:

Replicate_Ignore_DB:

Replicate_Do_Table:

Replicate_Ignore_Table:

Replicate_Wild_Do_Table:

Replicate_Wild_Ignore_Table:

Last_Errno: 0

Last_Error:

Skip_Counter: 0

Exec_Master_Log_Pos: 277

Relay_Log_Space: 724

Until_Condition: None

Until_Log_File:

Until_Log_Pos: 0

Master_SSL_Allowed: No

Master_SSL_CA_File:

Master_SSL_CA_Path:

Master_SSL_Cert:

Master_SSL_Cipher:

Master_SSL_Key:

Seconds_Behind_Master: 0

Master_SSL_Verify_Server_Cert: No

Last_IO_Errno: 0

Last_IO_Error:

Last_SQL_Errno: 0

Last_SQL_Error:

[root@lab ~]# mysql -uroot -p -e 'show processlist\G'

*************************** 1. row ***************************

Id: 25

User: system user

Host:

db: NULL

Command: Connect

Time: 69971

State: Waiting for master to send event

Info: NULL

*************************** 2. row ***************************

Id: 26

User: system user

Host:

db: NULL

Command: Connect

Time: 2541

State: Has read all relay log; waiting for the slave I/O thread to update it

Info: NULL

以上两个show命令代表已经建立并持续运行同步

3)在从库Lab上回收增删改权限,只保留SELECT[root@lab ~]# mysql -uroot -p -e "revoke insert,update,delete on mydb.student from 'web'@'192.168.5.%';show grants for web@'192.168.5.%';"

+--------------------------------------------------------------------------------------------------------------+

| Grants for web@192.168.5.%                                                                                   |

+--------------------------------------------------------------------------------------------------------------+

| GRANT USAGE ON *.* TO 'web'@'192.168.5.%' IDENTIFIED BY PASSWORD '*67138D0908E294A380CA501A1F1A48898426B13B' |

| GRANT SELECT ON `mydb`.`student` TO 'web'@'192.168.5.%'                                                      |

+--------------------------------------------------------------------------------------------------------------+

4)在WebTest服务器上分别测试连接主从库

在主库vmtest(IP 192.168.5.103)上插入一条数据[root@WebTest ~]# mysql -uweb -pweb123 -h 192.168.5.103 -P 3308 -e "insert into mydb.student(name,score) values('webtest',88);"

在从库Lab(IP 192.168.5.41)上查询刚才插入的数据。[root@WebTest ~]# mysql -uweb -pweb123 -h 192.168.5.41 -e "select * from mydb.student where name='webtest';"

+----+---------+-------+------+------+------+

| id | name    | score | sex  | age  | qq   |

+----+---------+-------+------+------+------+

|  8 | webtest |    88 | NULL | NULL | NULL |

+----+---------+-------+------+------+------+

5)用web用户尝试在从库上修改和插入数据[root@WebTest ~]# mysql -uweb -pweb123 -h 192.168.5.41 -e "update mydb.student set score=98 where name='webtest';"

ERROR 1142 (42000) at line 1: UPDATE command denied to user 'web'@'192.168.5.141' for table 'student'

[root@WebTest ~]# mysql -uweb -pweb123 -h 192.168.5.41 -e "insert into mydb.student(name,score) values('hacker',100);"

ERROR 1142 (42000) at line 1: INSERT command denied to user 'web'@'192.168.5.141' for table 'student'

[root@WebTest ~]# mysql -uweb -pweb123 -h 192.168.5.41 -e "delete from mydb.student where name='webtest';"

ERROR 1142 (42000) at line 1: DELETE command denied to user 'web'@'192.168.5.141' for table 'student'

由于增删改权限被回收,web用户不能修改student表,但能读,那么就可以实现最基本的读写分离。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值