Home Database Mysql Tutorial MySQL/MariaDB数据备份与数据恢复完整

MySQL/MariaDB数据备份与数据恢复完整

Jun 07, 2016 pm 04:20 PM
mariadb mysql whole data backup Data Recovery

MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可。开发这个分支的原因之一是:Oracle公司收购了MySQL后,有将MySQL闭源的潜在风险,因此社区采用分支的方式来避开这个风险。 数据对我们来说再重要不过了,那我们如何做到对

   MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可。开发这个分支的原因之一是:Oracle公司收购了MySQL后,有将MySQL闭源的潜在风险,因此社区采用分支的方式来避开这个风险。

  数据对我们来说再重要不过了,那我们如何做到对数据尽可能的安全呢,当我们的数据丢失了那又该怎么做呢,所以说数据备份对我们的数据安全性来说太重要了。

  数据对我们来说再熟悉不过了,也最平常不过了,我们每天都在接触各色各样的数据,数据记录了我们平常相关的业务信息,所以数据对于我们来说是很重要的, 这么重要的数据如果我们的数据丢失了那我们是不是相关的业务都没法进行了呢,这应该是个很麻烦的问题,那我们怎么保护我们的数据的安全呢,这就要用到我们 的数据备份了。

  如何执行备份恢复:备份与恢复在工作环境中是重中之重,为什么需要用到备份和恢复呢:

  1、一般是做灾难恢复的,比如说自然灾害等。

  2、可以做审计的,比如说某一数据在过去是什么样的。

  3、做测试的,比如说一个新的业务架构数据存储方式是否扛得着业务的访问

  备份的目的是用于恢复的,如果备份的数据用到时恢复不了数据怎么办,所以对备份数据做恢复测试是很有必要的。而且还要定期性的去做测试。

  备份类型:有很多种

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

  冷备份:cold backup,服务器要离线,意味着我们的读写操作都不可以进行了,这是最安全的备份方式,也是最不靠谱的方式。

  温备份:warm backup,全局施加共享锁,只可读,不可写的备份叫温备份

  热备份:hot backup,数据库不离线,读写操作都可以进行

  InnoDB记录数据时都会给数据一个序列号,所以在备份时基于MVCC(多版本并发控制)的机制自动加快照,每启动一个事务都会创建当前集的一个快 照,而后基于MVCC的机制把每一个序列号都给它记录一份下来,备份时只备份序列号或序列号之前的数据,往后发生的将不做备份,如果事务的隔离级别不是特 别高的话,它并不会影响事务的读写操作,而这样备份出来的数据一定是时间点一致的数据,所以要完成热备份,通常是基于事务的存储引擎才能够完成的。

  根据备份时的数据集进行分类:

  完全备份:full backup:指备份整个库,当下数据集的整个库的数据

  部分备份:partial backup:只备份某张表或某张表的一部份数据,有时备份单张表是有必要的。

  根据备份时的接口(直接备份数据文件还是通过mysql服务器导出数据)

  物理备份:直接复制(归档)数据文件的备份方式;跨平台能力没有逻辑备份好,physucal backup。大数据集用这个比较好。

  逻辑备份:把数据库中提出来保存为文本文件;通常使用的工具是mysqldump,logical backup,对于大容量数据不适用。恢复速度很慢,占据空间很大

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

  完全备份:full backup,跟备份数据集中的完全备份概念是相同的,也是备份整个库

  增量备份:incremental backup,上一次完全备份之后所改变的数据做备份的为增量备份,比如说周一做一次备份,周二做一次备份,周三又做一次,这样一天天累加上去的叫增量备份。比较节约空间。

  差异备份:differential backup,比如说周一做一次备份,到周二了就把周一和周二这两天的做一次备份,到周三就把周一周二周三的做一次备份,这就叫差异备份。比较容易恢复。

  备份策略:需要考虑到的问题

  1、选择备份方式,选择哪种方式根据我们的生产环境所需要来定;

  2、执行备份时间,选项一个访问最少的时间做备份是比较合理的;

  3、考虑到恢复成本:恢复时长;

  4、备份成本:考虑到锁时间、备份时长、备份负载;

  备份对象:我们备份需要备份什么呢

  1、备份数据库中的数据是最重要的;

  2、MySQL的配置文件,这个也是我们备份的对象

  3、MySQL的代码也是需要备份的:存储过程,存储函数,触发器

  4、OS相关的配置文件,如crontab配置计划及相关的脚本

  5、如果是在主从复制的场景中,跟复制相关的信息也要备份

  6、为了保证数据足够可靠,二进制日志文件也需要备份

  常用的备份工具:

  mysqldump:逻辑备份工具,是单线程备份工具,所以在某个服务器上做备份时它只能启动一个CPU启动一个线程进行备份,性能比较差。

  对InnoDB热备、对MyISAM只能做到温备、对Aria温备,备份和恢复过程较慢;

  mysqldumper:多线程的mysqldump,对多个库或多张表可以同时进行,提高性能;

  mysqldump、mysqldumper,这两个都是逻辑备份工具,通常情况也很难实现差异或增量备份,只能做完全备份,但可以做部分备份,比如只备份一张表是可以实现的;

  基于冷备份时:cp, 要基于lvm-snapshot逻辑卷快照进行备份的,接近于热备工具,因为要先请求全局锁,而后创建快照,并在创建快照完成后释放全局锁;而后使用 cp、tar等工具进行物理备份(因为复制的源数据文件),所以备份和恢复数据速度较快,缺点很难实现增量备份,并且请求全局锁需要等待一段时间,在繁忙 的服务器上尤其如此。

  SELECT clause INTO OUTFILE ‘/path/to/somefile’;把挑选出来的子句保存到某一个文件中,是个部分备份工具,不会备份关系定义,仅备份表中的数据,但这也是个逻辑 备份工具,在速度上也快于mysqldump,也没法实现增量备份。

  LOAD ADTA INFILE ‘/path/from/somefile’;表示从哪里读数据来恢复的;

  Innobase:提供了商业备份工具为Innobackup,可以实现InnoDB的热备支持增量备份;但是对于MyISAM不支持增量备份,只能实现完全备份,属于物理备份,速度比较快。

  Xtrabackup:由Percona组织提供的开源备份工具,物理备份,速度快;

  Mysqlhostcopy:几乎冷备,吹牛工具,不适用;

  mysqldump:常用的备份工具,也是个逻辑备份工具,用于小数据备份,一般都是在5G以下的小数据进行备份;可以使用文本进行二次处理;相当于MySQL的客户端工具

  使用格式:mysqldump [options] [db_name [tbl_name ...]]

  备份单个库时用这个工具:mysqldump[option] db_name

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

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

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)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

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 create a new connection to mysql in navicat How to create a new connection to mysql in navicat Apr 09, 2025 am 07:21 AM

You can create a new MySQL connection in Navicat by following the steps: Open the application and select New Connection (Ctrl N). Select "MySQL" as the connection type. Enter the hostname/IP address, port, username, and password. (Optional) Configure advanced options. Save the connection and enter the connection name.

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 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 and SQL: Essential Skills for Developers MySQL and SQL: Essential Skills for Developers Apr 10, 2025 am 09:30 AM

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

How to recover data after SQL deletes rows How to recover data after SQL deletes rows Apr 09, 2025 pm 12:21 PM

Recovering deleted rows directly from the database is usually impossible unless there is a backup or transaction rollback mechanism. Key point: Transaction rollback: Execute ROLLBACK before the transaction is committed to recover data. Backup: Regular backup of the database can be used to quickly restore data. Database snapshot: You can create a read-only copy of the database and restore the data after the data is deleted accidentally. Use DELETE statement with caution: Check the conditions carefully to avoid accidentally deleting data. Use the WHERE clause: explicitly specify the data to be deleted. Use the test environment: Test before performing a DELETE operation.

See all articles