1 查看当前数据库内容并备份数据库 查看数据库信息: 备份数据库: [root@localhost ~]# mysqldump -u root -p t > /mnt/t.sqlEnter pass
查看数据库信息:
备份数据库:
[root@localhost ~]# mysqldump -u root -p t > /mnt/t.sqlEnter passWord: [root@localhost ~]# ll /mnt/t.sql -rw-r--r-- 1 root root 1771 Aug 25 11:56 /mnt/t.sql
首先查看数据库是否开启bin_log功能
mysql> show variables like "%log_bin%";
需要修改Mysql的配置文件,/etc/的my.cnf,添加一句log_bin = mysql_bin即可
mysql> insert into t1 values (3);Query OK, 1 row affected (0.00 sec)mysql> insert into t1 values (4);Query OK, 1 row affected (0.00 sec)mysql> insert into t1 values (5);Query OK, 1 row affected (0.00 sec)mysql> select * from t1;+------+| id |+------+| 1 || 2 || 5 || 4 || 3 |+------+5 rows in set (0.00 sec)mysql> flush logs;Query OK, 0 rows affected (0.00 sec)mysql> show master status;+------------------+----------+--------------+------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+--------------+------------------+| mysql_bin.000003 | 106 | | |+------------------+----------+--------------+------------------+1 row in set (0.00 sec)
删除数据:
mysql> truncate t1;Query OK, 0 rows affected (0.00 sec)mysql> select * from t1;Empty set (0.00 sec)
此时突然数据库损坏或者人为删除
mysql> drop table t1;Query OK, 0 rows affected (0.00 sec)mysql> show tables;Empty set (0.00 sec)
1 用已经备份的/mnt/t.sql来恢复数据
mysql> source /mnt/t.sql;Query OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected (0.00 sec)mysql> show tables;+-------------+| Tables_in_t |+-------------+| t1 |+-------------+1 row in set (0.00 sec)mysql> select * from t1;+------+| id |+------+| 1 || 2 |+------+2 rows in set (0.00 sec)
2 还有三条数据没有恢复,怎么办。只能用bin-log来恢复
[root@localhost ~]# mysqlbinlog --no-defaults /var/lib/mysql/mysql_bin.000002 | mysql -u root -p123.com t
mysql> use t;Reading table infORMation for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> select * from t1;+------+| id |+------+| 1 || 2 || 3 || 4 || 5 |+------+5 rows in set (0.00 sec)mysql>
备份数据
mysqldump -uroot -p123456 test -l -F '/tmp/test.sql'-l:读锁(只能读取,不能更新)-F:即flush logs,可以重新生成新的日志文件,当然包括log-bin日志
查看binlog日志
mysql>show master status;
导入之前备份数据
mysql -uroot -p t -v -f </mnt/t.sql-v查看导入的详细信息-f是当中间遇到错误时,可以skip过去,继续执行下面的语句
恢复binlog-file二进制日志文件
mysqlbinlog --no-defaults binlog-file | mysql -uroot -p t
从某一(367)点开始恢复
mysqlbinlog --no-defaults --stop-position="367" mysql-bin.000001| mysql -uroot -p t
先查好那一点,用more来查看
[root@localhost mysql]# /usr/bin/mysqlbinlog --no-defaults mysql-bin.000002 --start-position="794" --stop-position="1055" | more
然后恢复
[root@localhost mysql]# /usr/bin/mysqlbinlog --no-defaults mysql-bin.000002 --start-position="794" --stop-position="1055" | /usr/bin/mysql -uroot -p t
重置binlog日志
mysql> reset master;Query OK, 0 rows affected (0.01 sec)mysql> show master status;+------------------+----------+--------------+------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+--------------+------------------+| mysql-bin.000001 | 106 | | |+------------------+----------+--------------+------------------+
mysql> flush logs;#关闭当前的二进制日志文件并创建一个新文件,新的二进制日志文件的名字在当前的二进制文件的编号上加1。
来源地址:https://blog.csdn.net/qq_45228323/article/details/129977643
--结束END--
本文标题: 利用MySQL的binlog恢复误删的数据库
本文链接: https://lsjlt.com/news/416632.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0