Home Database Mysql Tutorial Tell an example of mysql database recovery

Tell an example of mysql database recovery

May 03, 2017 pm 05:22 PM

Some time ago, due to accidentally unplugging the power of the xen server server (~~!!), the virtual machine zabbix database on the xen server caused an error. Although it is not very important (used to monitor the company's intranet gateway server), monitoring One year's worth of data cannot be lost just like that. Thinking of the backup sql files and mysql binary log files in the zabbix database, I tried to restore the mysql database. It was successful and the recovery process was very simple!

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

#详细步骤

  

#1 记住误删除表(或者出错)的时间 例如 Oct 17 08:30:27 CST 2011

#2 先 drop database zabbix ; 

   再 create database zabbix;(或者跳过直接执行第3步)

#3 导入备份的sql文件 

   mysql -uroot -pdong zabbix < zabbix20111016.sql

#4 如果有主从,停掉从服务器 

   例如 mysql>stop slave; 

#5 从二进制日志文件得到sql语句

#20111017

   mysqlbinlog --database=zabbix --stop-datetime="2011-10-17 08:30:00" mysql-bin.000158 > test.sql

#6 恢复sql 文件

#20110425

   mysql -root -pdong zabbix < test.sql

#20111017 

   mysql -root -pdong zabbix < test.sql 

#7 关于二进制日志文件

正常是每天17:59生成一个二进制日志文件,8:34是掉电后重启服务器生成的二进制日志文件

-rw-rw---- 1 mysql mysql  269983630 Oct 14 17:59 mysql-bin.000155

-rw-rw---- 1 mysql mysql  269744900 Oct 15 17:59 mysql-bin.000156

-rw-rw---- 1 mysql mysql  270254094 Oct 16 17:59 mysql-bin.000157

-rw-rw---- 1 mysql mysql  164263912 Oct 17 08:34 mysql-bin.000158

 

#8 关于从库数据恢复(可选)

-rw-rw---- 1 mysql mysql  270254094 Oct 16 17:59 mysql-bin.000157

-rw-rw---- 1 mysql mysql  164263912 Oct 17 08:34 mysql-bin.000158

-rw-rw---- 1 mysql mysql     953434 Oct 17 10:07 mysql-bin.000159

-rw-rw---- 1 mysql mysql 1073749046 Oct 17 11:21 mysql-bin.000160

-rw-rw---- 1 mysql mysql  414747331 Oct 17 12:00 mysql-bin.000161

-rw-rw---- 1 mysql mysql   24960747 Oct 17 13:42 mysql-bin.000162

-rw-rw---- 1 mysql mysql       6042 Oct 17 12:00 mysql-bin.index

  

主库,关闭了数据库以前旧的二进制文件全部删除掉,重启后将生成新的二进制日志文件,从000001开始

/etc/init.d/mysqld stop

tar czvf 20111017.tar.gz zabbix/

mkdir tmp

mv mysql-bin.* tmp/

scp 20111017.tar.gz root@192.168.57.82:/root/

/etc/init.d/mysqld start

ll

-rw-r--r-- 1 root  root  362462977 Oct 17 14:42 20111017.tar.gz

-rw-rw---- 1 mysql root     812700 Oct 17 14:45 cl3.test.com.err

-rw-rw---- 1 mysql mysql         6 Oct 17 14:45 cl3.test.com.pid

-rw-rw---- 1 mysql mysql  17638261 Oct 17 14:46 lowquery.log

drwx------ 2 mysql root       4096 May  4 10:35 mysql

-rw-rw---- 1 mysql mysql   7046279 Oct 17 15:17 mysql-bin.000001

-rw-rw---- 1 mysql mysql        38 Oct 17 14:45 mysql-bin.index

drwxr-xr-x 2 root  root       4096 Oct 17 14:42 tmp

drwx------ 2 mysql mysql     12288 Oct 17 11:23 zabbix

  

  

从库,重置了slave从库信息,重新建立从库信息,由于主库二进制日志从新开始,所以 master_log_file=&#39;mysql-bin.000001&#39;,master_log_pos=1;

  

/etc/init.d/mysqld stop

mv /usr/local/mysql/var/zabbix /usr/local/mysql/var/zabbix_tmp

tar zxvf 20111017.tar.gz -C /usr/local/mysql/var/

/etc/init.d/mysqld start

mysql -uroot -p

mysql> reset slave;

mysql> change master to master_host=&#39;192.168.6.53&#39;,master_user=&#39;dongnan&#39;,master_password=&#39;123456&#39;,master_log_file=&#39;mysql-bin.000001&#39;,master_log_pos=1;

Query OK, 0 rows affected (0.01 sec)

  

mysql> slave start;

Query OK, 0 rows affected (0.00 sec)

  

mysql> show slave status \G;

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

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.6.53

                  Master_User: dongnan

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000001

          Read_Master_Log_Pos: 3249706

               Relay_Log_File: zabbix-slave-relay-bin.000002

                Relay_Log_Pos: 3249851

        Relay_Master_Log_File: mysql-bin.000001

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

  

  

  

#mysqlbinlog 其它

#找到 2011-10-16 17:00:00 到 2011-10-16 17:58:59 这期间的sql语句

mysqlbinlog --database=zabbix --start-datetime="2011-10-16 17:00:00" --stop-datetime="2011-10-16 17:58:59" mysql-bin.000157 > test2.sql

  

du -sh test2.sql

16M test2.sql

  

head test2.sql

/*!40019 SET @@session.max_insert_delayed_threads=0*/;

/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;

DELIMITER /*!*/;

# at 4

#111015 17:59:02 server id 1  end_log_pos 106   Start: binlog v 4, server v 5.1.55-log created 111015 17:59:02

BINLOG &#39;

ZlmZTg8BAAAAZgAAAGoAAAAAAAQANS4xLjU1LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAUwAEGggAAAAICAgC

&#39;/*!*/;

# at 259091243

  

tail test2.sql

/*!*/;

# at 270239069

#111016 17:58:56 server id 1  end_log_pos 270239321     Query   thread_id=120   exec_time=0 error_code=0

SET TIMESTAMP=1318759136/*!*/;

insert into history_uint (itemid,clock,value) values (18532,1318759132,25427968),(18533,1318759133,43491328),(18534,1318759134,4308992),(18504,1318759134,15250739200),(18535,1318759135,0)

/*!*/;

DELIMITER ;

# End of log file

ROLLBACK /* added by mysqlbinlog */;

/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;

  

mysql -uroot -pdong zabbix < test2.sql

  

#/usr/local/mysql/bin/mysqlbinlog --database=zabbix mysql-bin.000013 > test.txt

#head -n 10 test.txt 

#/usr/local/mysql/bin/mysqlbinlog --start-datetime="2011-04-20 15:33:51" mysql-bin.000013 | /usr/local/mysql/bin/mysql -uroot -p

Copy after login

End

The entire recovery process is nothing more than importing the sql file backed up the previous day, then getting the desired sql statement from the binary log by time or position, and importing it into the database again. ! If there is a slave library, just export a copy of the data to the slave library!

The above is the detailed content of Tell an example of mysql database recovery. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Centos install mysql Centos install mysql Apr 14, 2025 pm 08:09 PM

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

See all articles