《Mysql学习详解MySQL误操作后怎样进行数据恢复》要点:
本文介绍了Mysql学习详解MySQL误操作后怎样进行数据恢复,希望对您有用。如果有疑问,可以联系我们。
MYSQL入门一、开启binlog.
MYSQL入门首先查看binlog是否开启
MYSQL入门
mysql> show variables like "log_bin";
+---------------+-------+
|Variable_name | Value
+---------------+-------+
| log_bin OFF
+---------------+-------+
1 row in set (0.00 sec)
MYSQL入门值为OFF,需开启,开启binlog方式如下:
MYSQL入门
#vim /etc/my.cnf
MYSQL入门在[mysqld]中加入
MYSQL入门
log-bin = mysql-bin
log-bin = /usr/local/mysql/log/mysql-bin.log
MYSQL入门重启mysql服务
MYSQL入门
#service mysqld stop
#service mysqld start
MYSQL入门二、模拟数据写入
MYSQL入门建库
MYSQL入门
create database backup;
MYSQL入门建表
MYSQL入门
CREATE TABLE `number` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '编号',
`updatetime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
MYSQL入门写入数据
MYSQL入门程序2-1
MYSQL入门
#coding:utf8
#python2.7
import MySQLdb
import time
def connect_mysql(db_host="192.168.11.169",user="martin",passwd="martin",db="backup",charset="utf8"):
conn = MySQLdb.connect(host=db_host,user=user,passwd=passwd,db=db,charset=charset)
conn.autocommit(True)
return conn.cursor()
#数据插入
for i in range(0,10):
#time=time.strftime("%Y-%m-%d %H:%M:%S")
sql = 'insert into number(updatetime) values(%s)'
values = [(time.strftime("%Y-%m-%d %H:%M:%S"))]
db1 = connect_mysql()
print db1.executemany(sql,values)
MYSQL入门查询数据
MYSQL入门
mysql> select * from number;
+-------+------------------------+
| id | updatetime
+--------------------------------+
| 1 | 2016-06-29 23:27:15 |
| 2 | 2016-06-29 23:27:15 |
| 3 | 2016-06-29 23:27:15 |
| 4 | 2016-06-29 23:27:15 |
| 5 | 2016-06-29 23:27:15 |
| 6 | 2016-06-29 23:27:15 |
| 7 | 2016-06-29 23:27:15 |
| 8 | 2016-06-29 23:27:15 |
| 9 | 2016-06-29 23:27:15 |
| 10 | 2016-06-29 23:27:15 |
+-------+------------------------+
10 rows in set (0.00 sec)
MYSQL入门三、全量备份
MYSQL入门
mysqldump -uroot -p -F --master-data=2 backup |gzip> /martin/data/backup_$(date +%F).sql.gz
MYSQL入门注:加-F能刷新binlog,方便恢复时操作.
MYSQL入门四、模拟写入增量数据
MYSQL入门继续执行程序2-1.
MYSQL入门查询数据
MYSQL入门
mysql> select * from number;
+----+---------------------------+
| id | updatetime |
+----+---------------------------+
| 1 | 2016-06-29 23:27:15 |
| 2 | 2016-06-29 23:27:15 |
| 3 | 2016-06-29 23:27:15 |
| 4 | 2016-06-29 23:27:15 |
| 5 | 2016-06-29 23:27:15 |
| 6 | 2016-06-29 23:27:15 |
| 7 | 2016-06-29 23:27:15 |
| 8 | 2016-06-29 23:27:15 |
| 9 | 2016-06-29 23:27:15 |
| 10 | 2016-06-29 23:27:15 |
| 11 | 2016-06-29 23:31:03 |
| 12 | 2016-06-29 23:31:03 |
| 13 | 2016-06-29 23:31:03 |
| 14 | 2016-06-29 23:31:03 |
| 15 | 2016-06-29 23:31:03 |
| 16 | 2016-06-29 23:31:03 |
| 17 | 2016-06-29 23:31:03 |
| 18 | 2016-06-29 23:31:03 |
| 19 | 2016-06-29 23:31:03 |
| 20 | 2016-06-29 23:31:03 |
+-------+---------------------+
20 rows in set (0.00 sec)
MYSQL入门五、增量备份
MYSQL入门保留mysql-bin.000002及之后的binlog即可.
MYSQL入门六、模拟误操作
MYSQL入门
delete from number;
MYSQL入门七、再次写入增量数据
MYSQL入门执行程序2-1
MYSQL入门select * from bumber;
MYSQL入门
+------+------------------------+
| id | updatetime |
+------+------------------------+
| 21 | 2016-06-29 23:41:06 |
| 22 | 2016-06-29 23:41:06 |
| 23 | 2016-06-29 23:41:06 |
| 24 | 2016-06-29 23:41:06 |
| 25 | 2016-06-29 23:41:06 |
| 26 | 2016-06-29 23:41:06 |
| 27 | 2016-06-29 23:41:06 |
| 28 | 2016-06-29 23:41:06 |
| 29 | 2016-06-29 23:41:06 |
| 30 | 2016-06-29 23:41:06 |
+------+------------------------+
10 rows in set (0.00 sec)
MYSQL入门八、恢复
MYSQL入门此时发现之前的delete操作为误操作,急需恢复,恢复过程如下
MYSQL入门给该表加上读锁
MYSQL入门
lock table number read;
MYSQL入门将全量备份的数据导入
MYSQL入门
#cd /martin/data/
#gzip -d number_2016-06-29.sql.gz
#grep -i "change" *.sql
-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=107;
MYSQL入门刷新日志
MYSQL入门
#mysqladmin -uroot -p'martin' flush-logs
#cd /usr/local/mysql/log
#ls|grep mysql-bin|grep -v index
mysql-bin.000001
mysql-bin.000002
mysql-bin.000003
MYSQL入门可确定mysql-bin.000002为增量数据binlog
MYSQL入门导入全量备份
MYSQL入门
#cd /martin/data/
#mysql -uroot -p backup < number_2016-06-29.sql
#cp /usr/local/mysql/log/mysql-bin.000002 /martin/data/
#mysqlbinlog mysql-bin.000002 >bin.sql
#vim bin.sql
MYSQL入门在bin.sql找到之前的delete语句,删除
MYSQL入门
mysql -uroot -p <bin.sql
MYSQL入门九、确认已恢复数据
MYSQL入门登录mysql
MYSQL入门
#mysql -uroot -p'martin' backup
select * from number;
MYSQL入门
+----+---------------------+
| id | updatetime |
+----+---------------------+
| 1 | 2016-06-29 23:27:15 |
| 2 | 2016-06-29 23:27:15 |
| 3 | 2016-06-29 23:27:15 |
| 4 | 2016-06-29 23:27:15 |
| 5 | 2016-06-29 23:27:15 |
| 6 | 2016-06-29 23:27:15 |
| 7 | 2016-06-29 23:27:15 |
| 8 | 2016-06-29 23:27:15 |
| 9 | 2016-06-29 23:27:15 |
| 10 | 2016-06-29 23:27:15 |
| 11 | 2016-06-29 23:31:03 |
| 12 | 2016-06-29 23:31:03 |
| 13 | 2016-06-29 23:31:03 |
| 14 | 2016-06-29 23:31:03 |
| 15 | 2016-06-29 23:31:03 |
| 16 | 2016-06-29 23:31:03 |
| 17 | 2016-06-29 23:31:03 |
| 18 | 2016-06-29 23:31:03 |
| 19 | 2016-06-29 23:31:03 |
| 20 | 2016-06-29 23:31:03 |
| 21 | 2016-06-29 23:41:06 |
| 22 | 2016-06-29 23:41:06 |
| 23 | 2016-06-29 23:41:06 |
| 24 | 2016-06-29 23:41:06 |
| 25 | 2016-06-29 23:41:06 |
| 26 | 2016-06-29 23:41:06 |
| 27 | 2016-06-29 23:41:06 |
| 28 | 2016-06-29 23:41:06 |
| 29 | 2016-06-29 23:41:06 |
| 30 | 2016-06-29 23:41:06 |
+----+---------------------+
30 rows in set (0.00 sec)
MYSQL入门恢复完成!以上就是本文的全部内容,在操作数据库时候要多加小心尽量避免误操作,如果万一遇到了,希望本文能够帮助大家.
转载请注明本页网址:
http://www.vephp.com/jiaocheng/5409.html