作为一名数据库运维人员,应该遇到过表记录被异常全表更新,这个时候该怎么办。作为Mysql数据库运维人员,第一反应,应该就是上报,然后联系开发人员,确认此表作用,然后再确定恢复方案。
在这里举一个配置表的例子,如果配置被异常更新,则需要先将被设置成只读,然后从备份里恢复出数据,或者看看产品是否能做出配置数据。
在这里就来详细说一下表是如何设置成只读。
1.设置表只读
[root@localhost] 10:46:02 [sbtest]>show tables;+------------------+| Tables_in_sbtest |+------------------+| sbtest1 || t_test |+------------------+2 rows in set (0.00 sec)[root@localhost] 10:46:03 [sbtest]>lock tables t_test read;Query OK, 0 rows affected (0.00 sec)
2.测试表只读是否生效
[tony@localhost] 10:47:03 [sbtest]>insert into t_test values(2000,93333,'efsdaf','fdsaf');[root@localhost] 10:47:26 [(none)]>show full processlist;+----+-----------------+-----------+--------+---------+--------+---------------------------------+--------------------------------------------------------+| Id | User | Host | db | Command | Time | State | Info |+----+-----------------+-----------+--------+---------+--------+---------------------------------+--------------------------------------------------------+| 5 | event_scheduler | localhost | NULL | Daemon | 418013 | Waiting on empty queue | NULL || 75 | root | localhost | sbtest | Sleep | 64 | | NULL || 76 | tony | localhost | sbtest | Query | 23 | Waiting for table metadata lock | insert into t_test values(2000,93333,'efsdaf','fdsaf') || 78 | root | localhost | NULL | Query | 0 | starting | show full processlist |+----+-----------------+-----------+--------+---------+--------+---------------------------------+--------------------------------------------------------+4 rows in set (0.00 sec)
从上面可以看到,insert插入操作已经被堵塞了,等待的是表元数据锁。
3.解锁表只读锁
当恢复sql准备好之后,就可以解锁表的只读锁。
[root@localhost] 10:50:20 [sbtest]>unlock tables;Query OK, 0 rows affected (0.00 sec)
解锁之后,就可以正常插入记录了。
[tony@localhost] 10:50:46 [sbtest]>insert into t_test values(2000,93333,'efsdaf','fdsaf');Query OK, 1 row affected (0.44 sec)