MySQL case combat--cold backup of MySQL database

Preface

This environment is based on Centos 7.8 system to build MySQL-5.7.14 for
specific construction, please refer to MySQL-5.7.14 environment construction

Backup is the most important thing to consider in the system. Although they account for less than 1% of the entire planning, development and testing process of the system, seemingly unimportant and obscure work can only be truly reflected when it is restored. The importance of this is that any loss of data and any attempt to see the data down machine cannot be received. Database backup is also important. Next, we will introduce the MySQL database strategy.


1. Database backup type

Classification of backups

1. Hot backup, warm backup, cold backup (according to server status)

  • Hot backup: read and write are not affected;
  • Warm backup: only read operations can be performed;
  • Cold backup: offline backup; read and write operations are aborted;

2. Physical backup and logical backup (divided from object)

  • Physical backup: copy data files;
  • Logical backup: export data to a text file;

3. Full backup, incremental backup, differential backup (divided from data collection)

  • Full backup: backup all data;
  • Incremental backup: only back up the data that has changed since the last full backup or incremental backup;
  • Differential backup: only back up the data that has changed since the last full backup

Comparison of advantages and disadvantages of backup strategies
Insert picture description here

Insert picture description here

Advantages and disadvantages of logical backup

1. Advantages of logical backup:

  • In terms of backup speed, the two types of backups depend on different storage engines
  • The restore speed of the physical backup is very fast. But the minimum granularity of physical backup can only be done for tables
  • The structure of logical backup is usually pure ASCII, so we can use text processing tools to process
  • Logical backup has very strong compatibility, while physical backup has very high version requirements
  • Logical backup also guarantees the security of data

2. Disadvantages of logical backup:

  • Logical backup puts extra pressure on RDBMS, while bare backup has no pressure
  • The result of the logical backup may be larger than the source file. So many people compress the content of the backup
  • Logical backup may lose the precision information of floating point numbers

Second, the content of the database backup

  • data files
  • Log files (such as transaction logs, binary logs)
  • Stored procedures, stored functions, triggers
  • Configuration file (very important, each configuration file must be backed up)
  • Scripts used to implement database backup, Crontab to clean up the database itself, etc...

3. Cold backup of MySQL database

1. Environmental preparation

Two MySQL-7.5.14 servers
node01 and node02 based on Centos system

2. Backup requirements

Requirement: Back up all data of the MySQL database of node01 to the database server of node02, and ensure the availability of the backup data on the server of node02

3. Backup process

View node01 node server database information

mysql> use db1;
Database changed
mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| fruits        |
+---------------+
1 row in set (0.00 sec)

mysql> select * from fruits;
+------+------+------------+---------+
| f_id | s_id | f_name     | f_price |
+------+------+------------+---------+
| a1   |  101 | apple      |    5.20 |
| a2   |  103 | apricot    |    2.20 |
| b1   |  101 | blackberry |   10.20 |
| b2   |  104 | berry      |    7.60 |
| b5   |  107 | xxxx       |    3.60 |
| bs1  |  102 | orange     |   11.20 |
| bs2  |  105 | melon      |    8.20 |
| c0   |  101 | cherry     |    3.20 |
| l2   |  104 | lemon      |    6.40 |
| m1   |  106 | mango      |   15.60 |
| m2   |  105 | xbabay     |    2.60 |
| m3   |  105 | xxtt       |   11.60 |
| o2   |  103 | coconut    |    9.20 |
| t1   |  102 | banana     |   10.30 |
| t2   |  102 | grape      |    5.30 |
| t4   |  107 | xbababa    |    3.60 |
+------+------+------------+---------+
16 rows in set (0.00 sec)

mysql> 

node01 node, start to pack database backup files

# 停止mysqld服务
[root@mysql-yum ~]# systemctl stop mysqld
# 打包文件
[root@mysql-yum ~]# cd  /var/lib/mysql
[root@mysql-yum mysql]# ll
total 110660
-rw-r-----. 1 mysql mysql       56 Jan 14 13:38 auto.cnf
-rw-------. 1 mysql mysql     1679 Jan 14 13:38 ca-key.pem
-rw-r--r--. 1 mysql mysql     1074 Jan 14 13:38 ca.pem
-rw-r--r--. 1 mysql mysql     1078 Jan 14 13:38 client-cert.pem
-rw-------. 1 mysql mysql     1679 Jan 14 13:38 client-key.pem
drwxr-x---  2 mysql mysql       56 Jan 16 17:30 db1
-rw-r-----  1 mysql mysql      309 Jan 16 17:34 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 Jan 16 17:34 ibdata1
-rw-r-----. 1 mysql mysql 50331648 Jan 16 17:34 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 Jan 14 13:38 ib_logfile1
drwxr-x---. 2 mysql mysql     4096 Jan 14 13:38 mysql
drwxr-x---. 2 mysql mysql     8192 Jan 14 13:38 performance_schema
-rw-------. 1 mysql mysql     1679 Jan 14 13:38 private_key.pem
-rw-r--r--. 1 mysql mysql      451 Jan 14 13:38 public_key.pem
-rw-r--r--. 1 mysql mysql     1078 Jan 14 13:38 server-cert.pem
-rw-------. 1 mysql mysql     1675 Jan 14 13:38 server-key.pem
drwxr-x---. 2 mysql mysql     8192 Jan 14 13:38 sys
[root@mysql-yum mysql]# tar czf /opt/mysql_datafile.bak.tar.gz *
[root@mysql-yum mysql]# ll /opt/
total 1348
-rw-r--r-- 1 root root 1378773 Jan 16 17:42 mysql_datafile.bak.tar.gz
# 拷贝文件
[root@mysql-yum mysql]# scp /opt/mysql_datafile.bak.tar.gz 192.168.5.12:/opt/

node02 node starts, reply data

# 查看备份数据包
[root@mysql-rpm ~]# ll /opt/mysql_datafile.bak.tar.gz 
-rw-r--r-- 1 root root 1378773 Jan 16 17:43 /opt/mysql_datafile.bak.tar.gz
# 停止mysqld服务
[root@mysql-rpm ~]# systemctl stop mysqld
# 删除原有的mysqld服务数据存放目录的文件
[root@mysql-rpm ~]# cd /var/lib/mysql
[root@mysql-rpm mysql]# rm -rf ./*
# 解压备份数据到mysqld服务数据存放目录
[root@mysql-rpm mysql]# tar xf /opt/mysql_datafile.bak.tar.gz -C /var/lib/mysql
[root@mysql-rpm mysql]# ll
total 110660
-rw-r----- 1 mysql mysql       56 Jan 14 13:38 auto.cnf
-rw------- 1 mysql mysql     1679 Jan 14 13:38 ca-key.pem
-rw-r--r-- 1 mysql mysql     1074 Jan 14 13:38 ca.pem
-rw-r--r-- 1 mysql mysql     1078 Jan 14 13:38 client-cert.pem
-rw------- 1 mysql mysql     1679 Jan 14 13:38 client-key.pem
drwxr-x--- 2 mysql mysql       56 Jan 16 17:30 db1
-rw-r----- 1 mysql mysql      309 Jan 16 17:34 ib_buffer_pool
-rw-r----- 1 mysql mysql 12582912 Jan 16 17:34 ibdata1
-rw-r----- 1 mysql mysql 50331648 Jan 16 17:34 ib_logfile0
-rw-r----- 1 mysql mysql 50331648 Jan 14 13:38 ib_logfile1
drwxr-x--- 2 mysql mysql     4096 Jan 14 13:38 mysql
drwxr-x--- 2 mysql mysql     8192 Jan 14 13:38 performance_schema
-rw------- 1 mysql mysql     1679 Jan 14 13:38 private_key.pem
-rw-r--r-- 1 mysql mysql      451 Jan 14 13:38 public_key.pem
-rw-r--r-- 1 mysql mysql     1078 Jan 14 13:38 server-cert.pem
-rw------- 1 mysql mysql     1675 Jan 14 13:38 server-key.pem
drwxr-x--- 2 mysql mysql     8192 Jan 14 13:38 sys
# 启动mysqld服务
[root@mysql-rpm mysql]# systemctl start mysqld
[root@mysql-rpm mysql]# netstat -lnutp | grep mysqld
tcp6       0      0 :::3306                 :::*                    LISTEN      2135/mysqld 

node02 node verification backup effect

[root@mysql-rpm mysql]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.14 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use db1;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| fruits        |
+---------------+
1 row in set (0.00 sec)

mysql> select * from fruits;
+------+------+------------+---------+
| f_id | s_id | f_name     | f_price |
+------+------+------------+---------+
| a1   |  101 | apple      |    5.20 |
| a2   |  103 | apricot    |    2.20 |
| b1   |  101 | blackberry |   10.20 |
| b2   |  104 | berry      |    7.60 |
| b5   |  107 | xxxx       |    3.60 |
| bs1  |  102 | orange     |   11.20 |
| bs2  |  105 | melon      |    8.20 |
| c0   |  101 | cherry     |    3.20 |
| l2   |  104 | lemon      |    6.40 |
| m1   |  106 | mango      |   15.60 |
| m2   |  105 | xbabay     |    2.60 |
| m3   |  105 | xxtt       |   11.60 |
| o2   |  103 | coconut    |    9.20 |
| t1   |  102 | banana     |   10.30 |
| t2   |  102 | grape      |    5.30 |
| t4   |  107 | xbababa    |    3.60 |
+------+------+------------+---------+
16 rows in set (0.00 sec)

mysql> 

Data backup is complete! ! !

Guess you like

Origin blog.csdn.net/XY0918ZWQ/article/details/112716143