search
HomeDatabaseMysql Tutorial mariadb常用备份与还原工具介绍

一、备份的意义从数据安全的角度来说,数据库服务器磁盘都会做RAID,Mariadb本身也有主从等容灾机制,但它们都无法完全取代备份。容灾和高可用能帮我们有效的应

二、备份类型

1、根据备份时,数据库服务器是否在线:

2、根据备份的数据集:

4、根据备份时是备份整个数据还是仅备份变化的数据:

5、备份策略:

6、备份对象

三、常见的备份工具

mariadb本身为我们提供了mysqldump、mysqlbinlog备份工具,percona也为我们提供了强大的Xtrabackup,加上开源的mydumper,还有基于主从同步的延迟备份、从库冷备等方式,以及基于文件系统快照的备份,其实将这些方式合理搭配已经能够满足我们的需要了。而备份本身是为了恢复,所以能够让我们在出现故障后迅速、准确恢复的备份方式,就是最适合我们的,当然,同时能够省钱、省事,那就非常完美。下面就几种备份工具进行一些比较,探讨下它们各自的适用场景,及简单的使用做一下说明

1、mysqldump

⑴、mysqldump优缺点

mysqldump是最简单的逻辑备份方式(工作方式单线程)。在备份myisam表的时候,如果要得到一致的数据,就需要锁表,简单而粗暴。而在备份innodb表的时候,加上–master-data=2 –single-transaction 选项,在事务开始时刻,记录下binlog-pos点,然后利用mvcc(多版本并发控制)来获取一致的数据,由于是一个大事务,在写入和更新量很大的数据库上,将产生非常多的undo,显著影响性能,所以要慎用。

wKioL1NJY1PwpftQAAF4qal76Cc359.jpg


优点:简单,可针对单表备份,在完全导出表结构的时候尤其有用。可以做到对不同的存储引擎进行备份(InnoDB热备、MyISAM温备、Aria温备)

缺点:简单粗暴,单线程,备份慢而且恢复,不支持差异或增量备份,如果要进行差异或增量备份要结合binlog日志文件

mydumper是mysqldump的加强版。相比mysqldump:

内置支持压缩,可以节省2-4倍的存储空间。

支持并行备份和恢复,因此速度比mysqldump快很多,但是由于是逻辑备份,仍不是很快,如果要进行差异或增量备份要结合binlog日志文件

部分备份工具

SELECT clause INTO OUTFILE '/path/to/somefile'

LOAD DATA INFILE '/path/from/somefile'

不会备份关系定义,仅备份表中的数据;

逻辑备份工具,快于mysqldump。

⑵、mysqldump命令介绍及简单使用

①命令介绍

mysqldump [options] [db_name [tbl_name ...]]

备份单个库:

mysqldump [options] db_name

恢复时:如果目标库不存在,需要事先手动创建

options说明

--all-databases: 备份所有库

--databases db1 db2 ...: 备份指定的多个库

注意:备份前要加锁

--lock-all-tables:请求锁定所有表之后再备份,对MyISAM、InnoDB、Aria做温备

--single-transaction: 能够对InnoDB存储引擎实现热备;

备份代码:

--events: 备份事件调度器代码

--routines: 备份存储过程和存储函数

--triggers:备份触发器

备份时滚动日志:

--flush-logs: 备份前、请求到锁之后滚动日志;

复制时的同步位置标记:

--master-data=[0|1|2]

0: 不记录

1:记录为CHANGE MASTER语句

2:记录为注释的CHANGE MASTER语句

注意

使用mysqldump备份

请求锁:--lock-all-tables或使用--singe-transaction进行innodb热备;

滚动日志:--flush-logs

选定要备份的库:--databases

记录二进制日志文件及位置:--master-data=

恢复

建议:关闭二进制日志,关闭其它用户连接;

建议备份策略:基于mysqldump

备份部分

mysqldump+二进制日志文件;

恢复

完全备份+各二进制日志文件中至此刻的事件,对MySQL配置文件,以及与MySQL相关的OS配置文件在每次修改后都应该直接进行备份;

②、实例

要求如下:

备份所有数据库,要每周日凌晨自动执行;

③解决方案(此方法不唯一)

备份阶段

第一步,先远程登录到数据库上,事先看一下现有的数据库。

wKiom1NJaHbjzl5BAAH03wdWkVI320.jpg

由上图可见,除了hellodb数据库,其它的数据库都是系统自带的,看一下hellodb中有那些表,及当前binlog日志的记录的位置。

wKioL1NL446AziAXAAIAEWNmgcs930.jpg

由于当前数据库只有一个用户数据库,所以我们在乎其表的存储引擎来判断使用什么方式来备份。(是申请锁或是单事务)

wKioL1NJaebTRB0wAAN7xg3NH6I301.jpg

查看表状态发现hellodb中的表全部都是MyISAM的存储引擎,那么就可以使用申请全局锁来备份了。

# mysqldump -uroot -p --all-databases --lock-all-tables --flush-logs --master-data=2 >/tmp/all.sql

wKiom1NJa7aiKc0oAAEZ8d6UEkE727.jpg

这时数据库全部备份完毕(此方法只适用于数据量不是很大,挑选一个相对并发的写请求不是特别多的时间或午夜备份。如果数据量特别大,此方法将不做参考范围)。

假如在这之后我们又在原来的库中建立新的表或插入数据,而在某一次我误操作删除了此数据库。将如何恢复?

wKiom1NL5PvhDZJ6AAEIzh4vRzk068.jpg

由上图可见,原来的hellodb数据库中多了一张表,为不演示恢复效果我将hellodb删除,看怎么样恢复

wKioL1NL5XuCCG1mAAE_TpkyEQM706.jpg

现在hellodb己经删除,看如何恢复。

恢复部分

第一步,将服务器离线,导出现在正在使用的binlog日志,还原之前的完全备份

查看完全备份中binlog日志的起始位置。

#vim /tmp/all.sql -- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=365;
Statement
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
MySQL's Place: Databases and ProgrammingMySQL's Place: Databases and ProgrammingApr 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

MySQL: From Small Businesses to Large EnterprisesMySQL: From Small Businesses to Large EnterprisesApr 13, 2025 am 12:17 AM

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?Apr 13, 2025 am 12:16 AM

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.

MySQL: Not a Programming Language, But...MySQL: Not a Programming Language, But...Apr 13, 2025 am 12:03 AM

MySQL is not a programming language, but its query language SQL has the characteristics of a programming language: 1. SQL supports conditional judgment, loops and variable operations; 2. Through stored procedures, triggers and functions, users can perform complex logical operations in the database.

MySQL: An Introduction to the World's Most Popular DatabaseMySQL: An Introduction to the World's Most Popular DatabaseApr 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.

The Importance of MySQL: Data Storage and ManagementThe Importance of MySQL: Data Storage and ManagementApr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system suitable for data storage, management, query and security. 1. It supports a variety of operating systems and is widely used in Web applications and other fields. 2. Through the client-server architecture and different storage engines, MySQL processes data efficiently. 3. Basic usage includes creating databases and tables, inserting, querying and updating data. 4. Advanced usage involves complex queries and stored procedures. 5. Common errors can be debugged through the EXPLAIN statement. 6. Performance optimization includes the rational use of indexes and optimized query statements.

Why Use MySQL? Benefits and AdvantagesWhy Use MySQL? Benefits and AdvantagesApr 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.

Describe InnoDB locking mechanisms (shared locks, exclusive locks, intention locks, record locks, gap locks, next-key locks).Describe InnoDB locking mechanisms (shared locks, exclusive locks, intention locks, record locks, gap locks, next-key locks).Apr 12, 2025 am 12:16 AM

InnoDB's lock mechanisms include shared locks, exclusive locks, intention locks, record locks, gap locks and next key locks. 1. Shared lock allows transactions to read data without preventing other transactions from reading. 2. Exclusive lock prevents other transactions from reading and modifying data. 3. Intention lock optimizes lock efficiency. 4. Record lock lock index record. 5. Gap lock locks index recording gap. 6. The next key lock is a combination of record lock and gap lock to ensure data consistency.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)