search
HomeDatabaseMysql Tutorial史上最全的MySQL备份方法_MySQL

bitsCN.com 本人曾经
用过的备份方式有:mysqldump、mysqlhotcopy、BACKUP TABLE 、SELECT INTO
OUTFILE,又或者备份二进制日志(binlog),还可以是直接拷贝数据文件和相关的配置文件。MyISAM
表是保存成文件的形式,因此相对比较容易备份,上面提到的几种方法都可以使用。Innodb 所有的表都保存在同一个数据文件 ibdata1
中(也可能是多个文件,或者是独立的表空间文件),相对来说比较不好备份,免费的方案可以是拷贝数据文件、备份 binlog,或者用
mysqldump。

1.mysqldump备份

mysqldump 是采用SQL级别的备份机制,它将数据表导成 SQL 脚本文件,在不同的 MySQL 版本之间升级时相对比较合适,这也是最常用的备份方法。
示例:mysqldump -uroot -p database table > /home/jobs/back.sql
mysqldump也可做增量备份,mysqldump相关参数网上较多,就不在此一一赘述了

2.mysqlhotcopy备份

mysqlhotcopy 是一个 PERL 程序。它使用 LOCK TABLES、FLUSH
TABLES 和 cp 或 scp
来快速备份数据库。它是备份数据库或单个表的最快的途径,但它只能运行在数据库文件(包括数据表定义文件、数据文件、索引文件)所在的机器上。
mysqlhotcopy 只能用于备份 MyISAM,并且只能运行在 类Unix 和 NetWare 系统上。
mysqlhotcopy 支持一次性拷贝多个数据库,同时还支持正则表达。
示例: root#/usr/local/mysql/bin/mysqlhotcopy -h=localhost -u=root
-p=123456 database /tmp (把数据库目录 database 拷贝到 /tmp
下)root#/usr/local/mysql/bin/mysqlhotcopy -h=localhost -u=root -p=123456
db_name_1 ... db_name_n /tmproot#/usr/local/mysql/bin/mysqlhotcopy
-h=localhost -u=root -p=123456 db_name./regex/
/tmp更详细的使用方法请查看手册,或者调用下面的命令来查看 mysqlhotcopy 的帮助:
perldoc /usr/local/mysql/bin/mysqlhotcopy注意,想要使用 mysqlhotcopy,必须要有
SELECT、RELOAD(要执行 FLUSH TABLES) 权限,并且还必须要能够有读取 datadir/db_name 目录的权限。

还原mysqlhotcopy 备份出来的是整个数据库目录,使用时可以直接拷贝到 mysqld
指定的 datadir (在这里是 /usr/local/mysql/data/)目录下即可,同时要注意权限的问题,如下例: root#cp
-rf db_name /usr/local/mysql/data/root#chown -R nobody:nobody
/usr/local/mysql/data/ (将 db_name 目录的属主改成 mysqld 运行用户)

3.SQL 语法备份

3.1 备份BACKUP TABLE 语法其实和 mysqlhotcopy
的工作原理差不多,都是锁表,然后拷贝数据文件。它能实现在线备份,但是效果不理想,因此不推荐使用。它只拷贝表结构文件和数据文件,不同时拷贝索引文
件,因此恢复时比较慢。例子: BACK TABLE tbl_name TO '/tmp/db_name/';注意,必须要有 FILE
权限才能执行本SQL,并且目录 /tmp/db_name/ 必须能被 mysqld 用户可写,导出的文件不能覆盖已经存在的文件,以避免安全问题。
恢复用 BACKUP TABLE 方法备份出来的文件,可以运行 RESTORE TABLE 语句来恢复数据表。例子: RESTORE TABLE FROM '/tmp/db_name/';权限要求类似上面所述。

3.2 SELECT INTO OUTFILE 则是把数据导出来成为普通的文本文件,可以自定义字段间隔的方式,方便处理这些数据。例子:
SELECT INTO OUTFILE '/tmp/db_name/tbl_name.txt' FROM tbl_name;注意,必须要有
FILE 权限才能执行本SQL,并且文件 /tmp/db_name/tbl_name.txt 必须能被 mysqld
用户可写,导出的文件不能覆盖已经存在的文件,以避免安全问题。
用 SELECT INTO OUTFILE 方法备份出来的文件,可以运行 LOAD DATA INFILE 语句来恢复数据表。例子: LOAD
DATA INFILE '/tmp/db_name/tbl_name.txt' INTO TABLE
tbl_name;权限要求类似上面所述。倒入数据之前,数据表要已经存在才行。如果担心数据会发生重复,可以增加 REPLACE
关键字来替换已有记录或者用 IGNORE 关键字来忽略他们。

4.启用二进制日志(binlog)

采用 binlog 的方法相对来说更灵活,省心省力,而且还可以支持增量备份。
启用 binlog 时必须要重启 mysqld。首先,关闭 mysqld,打开 my.cnf,加入以下几行:
server-id = 1
log-bin = binlog
log-bin-index = binlog.index
然后启动 mysqld 就可以了。运行过程中会产生 binlog.000001 以及 binlog.index,前面的文件是 mysqld
记录所有对数据的更新操作,后面的文件则是所有 binlog 的索引,都不能轻易删除。关于 binlog 的信息请查看手册。
需要备份时,可以先执行一下 SQL 语句,让 mysqld 终止对当前 binlog
的写入,就可以把文件直接备份,这样的话就能达到增量备份的目的了: FLUSH LOGS;如果是备份复制系统中的从服务器,还应该备份
master.info 和 relay-log.info 文件。
备份出来的 binlog 文件可以用 MySQL 提供的工具 mysqlbinlog 来查看,如:
/usr/local/mysql/bin/mysqlbinlog /tmp/binlog.000001该工具允许你显示指定的数据库下的所有
SQL 语句,并且还可以限定时间范围,相当的方便,详细的请查看手册。
恢复时,可以采用类似以下语句来做到: /usr/local/mysql/bin/mysqlbinlog /tmp/binlog.000001
| mysql -uyejr -pyejr db_name把 mysqlbinlog 输出的 SQL 语句直接作为输入来执行它。
如果你有空闲的机器,不妨采用这种方式来备份。由于作为 slave 的机器性能要求相对不是那么高,因此成本低,用低成本就能实现增量备份而且还能分担一部分数据查询压力,何乐而不为呢?

5.拷贝文件

直接备份数据文件相较前几种方法,备份数据文件最为直接、快速、方便,缺点是基本上不能实现增量备份。
为了保证数据的一致性,需要在靠背文件前,执行以下 SQL 语句: FLUSH TABLES WITH READ
LOCK;也就是把内存中的数据都刷新到磁盘中,同时锁定数据表,以保证拷贝过程中不会有新的数据写入。这种方法备份出来的数据恢复也很简单,直接拷贝回
原来的数据库目录下即可。
注意,对于 Innodb 类型表来说,还需要备份其日志文件,即 ib_logfile* 文件。因为当 Innodb 表损坏时,就可以依靠这些日志文件来恢复。

6.利用rsync备份
rsync作为同步工具也可以用来做备份,但要配置服务器端和客户端
示例rsync -vzrtopg --progress --delete root@192.168.1.3::root /tmp/
相关rsync配置可参考http://fanqiang.chinaunix.net/a6/b7/20010908/1305001258.html
缺点是rsync是根据文件修改时间做的增量备份,所以备份数据库都是全备,并且配置比较麻烦.

当然也可以参考下面的这篇文章
mysql 数据库备份和还原方法集锦bitsCN.com

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: 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.

What are common causes of poor MySQL query performance?What are common causes of poor MySQL query performance?Apr 12, 2025 am 12:11 AM

The main reasons for poor MySQL query performance include not using indexes, wrong execution plan selection by the query optimizer, unreasonable table design, excessive data volume and lock competition. 1. No index causes slow querying, and adding indexes can significantly improve performance. 2. Use the EXPLAIN command to analyze the query plan and find out the optimizer error. 3. Reconstructing the table structure and optimizing JOIN conditions can improve table design problems. 4. When the data volume is large, partitioning and table division strategies are adopted. 5. In a high concurrency environment, optimizing transactions and locking strategies can reduce lock competition.

When should you use a composite index versus multiple single-column indexes?When should you use a composite index versus multiple single-column indexes?Apr 11, 2025 am 12:06 AM

In database optimization, indexing strategies should be selected according to query requirements: 1. When the query involves multiple columns and the order of conditions is fixed, use composite indexes; 2. When the query involves multiple columns but the order of conditions is not fixed, use multiple single-column indexes. Composite indexes are suitable for optimizing multi-column queries, while single-column indexes are suitable for single-column queries.

How to identify and optimize slow queries in MySQL? (slow query log, performance_schema)How to identify and optimize slow queries in MySQL? (slow query log, performance_schema)Apr 10, 2025 am 09:36 AM

To optimize MySQL slow query, slowquerylog and performance_schema need to be used: 1. Enable slowquerylog and set thresholds to record slow query; 2. Use performance_schema to analyze query execution details, find out performance bottlenecks and optimize.

MySQL and SQL: Essential Skills for DevelopersMySQL and SQL: Essential Skills for DevelopersApr 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.

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
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment