Table of Contents
Definition
Full backup and recovery demonstration
Home Database Mysql Tutorial How to implement full backup and incremental backup of MySQL database

How to implement full backup and incremental backup of MySQL database

May 27, 2023 am 11:40 AM
mysql

Definition

Full backup is to back up all the data and all objects in the database.

Since the data files in the MySQL server are disk-based text files, a full backup is to copy the database file, which is the simplest and fastest way.

However, the data files of the MySQL server are always open during the running of the server. In order to achieve a true full backup, the MySQL database server needs to be stopped first.

In order to ensure the integrity of the data, before stopping the MySQL server, you need to execute the flush tables statement to write all data to the data file. Students only need to know that this method is not feasible because it is not advisable to stop the database for backup in a production environment.

Use the mysqldump command to back up tables, databases, and database systems:

mysqldump [-h主机名] –u用户名 –p密码 --lock-all-tables --database [tables] > 文件名
Copy after login

-h host name, which can be omitted, indicates the local server, --lock-all-tables for the files to be backed up Read locks are applied to all tables in the database (during this process, the database is strictly in the read only state). You can add the table that needs to be backed up after --database. If the table name is not specified, it means backing up the entire database.

Full backup and recovery demonstration

Prepare a student table and build the table in the world database.

Create table:

CREATE DATABASE world;
USE world;
CREATE TABLE student(
    stuId INT(10) NOT NULL,
    stuName VARCHAR(10) NOT NULL,
    stuAge INT(10) NOT NULL,
    PRIMARY KEY(stuId)
    );
Copy after login

Insert data:

INSERT INTO student(stuId, stuName, stuAge) VALUES(1, 'zhangsan', 18), (2, 'lisi', 19),(3, 'wangwu', 18);
Copy after login

How to implement full backup and incremental backup of MySQL database

Use flush tables; statement writes all data to the data file:

FLUSH TABLES;
Copy after login

Exit the mysql environment and use the mysqldump command to fully back up the database world:

mysqldump -u root -p --lock-all-tables --databases world > /tmp/world.sql
Copy after login

Enter /tmp directory, view the backup file:

cd /tmp
ls
Copy after login

Now, we have fully backed up the world library and are not afraid of data loss.

Simulate the loss of the student table in the world database:

DROP TABLE student;
Copy after login

Confirm that the table is deleted

SHOW TABLES;
Copy after login
Copy after login

How to implement full backup and incremental backup of MySQL database

Use the mysql command to restore the database:

mysql -uroot -p < /tmp/world.sql
Copy after login

Enter the mysql environment and view the recovery results:

SHOW TABLES;
Copy after login
Copy after login

Output results:

How to implement full backup and incremental backup of MySQL database

Verify the data in the table:

SELECT * FROM student;
Copy after login

How to implement full backup and incremental backup of MySQL database

The incremental backup is the change since the last full backup or incremental backup Data backup relies on binary log files, and the binlog log of the database needs to be turned on. First perform a full backup of the database, and refresh the binlog log at the same time. All operations after this backup will be recorded in the new binlog log. We only need to back up the added binlog to realize the continuous increase of content. A perfect backup of the database. When an abnormality occurs in the database, we can first restore the most recent full backup, and then restore the incremental backup files one by one in sequence to achieve database recovery.

The above is the detailed content of How to implement full backup and incremental backup of MySQL database. 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 Article Tags

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)

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

PHP's big data structure processing skills

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

How to optimize MySQL query performance in PHP?

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

How to use MySQL backup and restore in PHP?

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into a MySQL table using PHP?

What are the application scenarios of Java enumeration types in databases? What are the application scenarios of Java enumeration types in databases? May 05, 2024 am 09:06 AM

What are the application scenarios of Java enumeration types in databases?

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

How to fix mysql_native_password not loaded errors on MySQL 8.4

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

How to use MySQL stored procedures in PHP?

Performance optimization strategies for PHP array paging Performance optimization strategies for PHP array paging May 02, 2024 am 09:27 AM

Performance optimization strategies for PHP array paging

See all articles