Home Database Mysql Tutorial 使用mysqldump工具备份还原Mysql数据库

使用mysqldump工具备份还原Mysql数据库

Jun 07, 2016 pm 04:16 PM
mysql use backup tool data reduction

我们在网站数据维护中经常会遇到备份数据库,还原数据库的情况,我们一般用一下两种方式来处理: 1.使用into outfile 和 load data infile导入导出备份数据 这种方法的好处是,导出的数据可以自己规定格式,并且导出的是纯数据,不存在建表信息,你可以直接

我们在网站数据维护中经常会遇到备份数据库,还原数据库的情况,我们一般用一下两种方式来处理:

1.使用into outfile 和 load data infile导入导出备份数据

这种方法的好处是,导出的数据可以自己规定格式,并且导出的是纯数据,不存在建表信息,你可以直接导入另外一个同数据库的不同表中,相对于mysqldump比较灵活机动。

我们来看下面的例子:

(1)下面的mysql命令是把select的mytable表中的数据导出到/home/db_bak2012文件。

select * from mytable where status!=0 and name!='' into outfile '/home/db_bak2012' fields terminated by '|' enclosed by '"' lines terminated by 'rn' ;

导入刚才备份的数据,可以使用load file方法,下面的mysql命令,把导出的数据导入了mytable_bak的表中:

load data infile '/home/db_bak2012' into table mytable_bak fields terminated by '|' enclosed by '"' lines terminated by 'rn' ;

2.使用mysqldump导出固定条件的数据库

我们来看几个常用用例:
(1)导出整个数据库

mysqldump -u 用户名 -p 数据库名 > 导出的文件名 mysqldump -u wcnc -p smgp_apps_wcnc > wcnc.sql

(2)导出一个表

mysqldump -u 用户名 -p 数据库名 表名> 导出的文件名 mysqldump -u wcnc -p smgp_apps_wcnc users> wcnc_users.sql

(3)导出一个数据库结构

mysqldump -u wcnc -p -d --add-drop-table smgp_apps_wcnc >d:wcnc_db.sql #-d 不导出数据只导出结构 --add-drop-table 在每个create语句之前增加一个drop table

(4)导入数据库,常用source 命令

#进入mysql数据库控制台, mysql -u root -p mysql>use 数据库 mysql>set names utf8; (先确认编码,如果不设置可能会出现乱码,注意不是UTF-8)  #然后使用source命令,后面参数为脚本文件(如这里用到的.sql) mysql>source d:wcnc_db.sql

上边的实例只是最基础的,有的时候我们可能需要批量导出多个库,我们就可以加上--databases 或者-B,如下语句:

mysqldump -uroot -p --databases test mysql #空格分隔

还有的时候我们可能需要把数据库内所有的库全部备份,我们就可以使用-all-databases,如下语句:

mysqldump -uroot -p -all-databases

可能我们还会有更多的需求,下面是我在网上找的感觉比较全的参数说明,贴出来供大家参考。

参数说明

--all-databases  , -A

导出全部数据库。

mysqldump  -uroot -p --all-databases

--all-tablespaces  , -Y

导出全部表空间。

mysqldump  -uroot -p --all-databases --all-tablespaces

--no-tablespaces  , -y

不导出任何表空间信息。

mysqldump  -uroot -p --all-databases --no-tablespaces

--add-drop-database

每个数据库创建之前添加drop数据库语句。

mysqldump  -uroot -p --all-databases --add-drop-database

--add-drop-table

每个数据表创建之前添加drop数据表语句。(默认为打开状态,使用--skip-add-drop-table取消选项)

mysqldump  -uroot -p --all-databases  (默认添加drop语句)

mysqldump  -uroot -p --all-databases –skip-add-drop-table  (取消drop语句)

--add-locks

在每个表导出之前增加LOCK TABLES并且之后UNLOCK  TABLE。(默认为打开状态,使用--skip-add-locks取消选项)

mysqldump  -uroot -p --all-databases  (默认添加LOCK语句)

mysqldump  -uroot -p --all-databases –skip-add-locks   (取消LOCK语句)

--allow-keywords

允许创建是关键词的列名字。这由表名前缀于每个列名做到。

mysqldump  -uroot -p --all-databases --allow-keywords

--apply-slave-statements

在'CHANGE MASTER'前添加'STOP SLAVE',并且在导出的最后添加'START SLAVE'。

mysqldump  -uroot -p --all-databases --apply-slave-statements

--character-sets-dir

字符集文件的目录

mysqldump  -uroot -p --all-databases  --character-sets-dir=/usr/local/mysql/share/mysql/charsets

--comments

附加注释信息。默认为打开,可以用--skip-comments取消

mysqldump  -uroot -p --all-databases  (默认记录注释)

mysqldump  -uroot -p --all-databases --skip-comments   (取消注释)

--compatible

导出的数据将和其它数据库或旧版本的MySQL 相兼容。值可以为ansi、mysql323、mysql40、postgresql、oracle、mssql、db2、maxdb、no_key_options、no_tables_options、no_field_options等,

要使用几个值,用逗号将它们隔开。它并不保证能完全兼容,而是尽量兼容。

mysqldump  -uroot -p --all-databases --compatible=ansi

--compact

导出更少的输出信息(用于调试)。去掉注释和头尾等结构。可以使用选项:--skip-add-drop-table  --skip-add-locks --skip-comments --skip-disable-keys

mysqldump  -uroot -p --all-databases --compact

--complete-insert,  -c

使用完整的insert语句(包含列名称)。这么做能提高插入效率,但是可能会受到max_allowed_packet参数的影响而导致插入失败。

mysqldump  -uroot -p --all-databases --complete-insert

--compress, -C

在客户端和服务器之间启用压缩传递所有信息

mysqldump  -uroot -p --all-databases --compress

--create-options,  -a

在CREATE TABLE语句中包括所有MySQL特性选项。(默认为打开状态)

mysqldump  -uroot -p --all-databases

--databases,  -B

导出几个数据库。参数后面所有名字参量都被看作数据库名。

mysqldump  -uroot -p --databases test mysql

--debug

输出debug信息,用于调试。默认值为:d:t:o,/tmp/mysqldump.trace

mysqldump  -uroot -p --all-databases --debug

mysqldump  -uroot -p --all-databases --debug=” d:t:o,/tmp/debug.trace”

--debug-check

检查内存和打开文件使用说明并退出。

mysqldump  -uroot -p --all-databases --debug-check

--debug-info

输出调试信息并退出

mysqldump  -uroot -p --all-databases --debug-info

--default-character-set

设置默认字符集,默认值为utf8

mysqldump  -uroot -p --all-databases --default-character-set=latin1

--delayed-insert

采用延时插入方式(INSERT DELAYED)导出数据

mysqldump  -uroot -p --all-databases --delayed-insert

--delete-master-logs

master备份后删除日志. 这个参数将自动激活--master-data。

mysqldump  -uroot -p --all-databases --delete-master-logs

--disable-keys

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

How to use the chrono library in C? How to use the chrono library in C? Apr 28, 2025 pm 10:18 PM

Using the chrono library in C can allow you to control time and time intervals more accurately. Let's explore the charm of this library. C's chrono library is part of the standard library, which provides a modern way to deal with time and time intervals. For programmers who have suffered from time.h and ctime, chrono is undoubtedly a boon. It not only improves the readability and maintainability of the code, but also provides higher accuracy and flexibility. Let's start with the basics. The chrono library mainly includes the following key components: std::chrono::system_clock: represents the system clock, used to obtain the current time. std::chron

How to optimize code How to optimize code Apr 28, 2025 pm 10:27 PM

C code optimization can be achieved through the following strategies: 1. Manually manage memory for optimization use; 2. Write code that complies with compiler optimization rules; 3. Select appropriate algorithms and data structures; 4. Use inline functions to reduce call overhead; 5. Apply template metaprogramming to optimize at compile time; 6. Avoid unnecessary copying, use moving semantics and reference parameters; 7. Use const correctly to help compiler optimization; 8. Select appropriate data structures, such as std::vector.

How to measure thread performance in C? How to measure thread performance in C? Apr 28, 2025 pm 10:21 PM

Measuring thread performance in C can use the timing tools, performance analysis tools, and custom timers in the standard library. 1. Use the library to measure execution time. 2. Use gprof for performance analysis. The steps include adding the -pg option during compilation, running the program to generate a gmon.out file, and generating a performance report. 3. Use Valgrind's Callgrind module to perform more detailed analysis. The steps include running the program to generate the callgrind.out file and viewing the results using kcachegrind. 4. Custom timers can flexibly measure the execution time of a specific code segment. These methods help to fully understand thread performance and optimize code.

What is real-time operating system programming in C? What is real-time operating system programming in C? Apr 28, 2025 pm 10:15 PM

C performs well in real-time operating system (RTOS) programming, providing efficient execution efficiency and precise time management. 1) C Meet the needs of RTOS through direct operation of hardware resources and efficient memory management. 2) Using object-oriented features, C can design a flexible task scheduling system. 3) C supports efficient interrupt processing, but dynamic memory allocation and exception processing must be avoided to ensure real-time. 4) Template programming and inline functions help in performance optimization. 5) In practical applications, C can be used to implement an efficient logging system.

How to understand DMA operations in C? How to understand DMA operations in C? Apr 28, 2025 pm 10:09 PM

DMA in C refers to DirectMemoryAccess, a direct memory access technology, allowing hardware devices to directly transmit data to memory without CPU intervention. 1) DMA operation is highly dependent on hardware devices and drivers, and the implementation method varies from system to system. 2) Direct access to memory may bring security risks, and the correctness and security of the code must be ensured. 3) DMA can improve performance, but improper use may lead to degradation of system performance. Through practice and learning, we can master the skills of using DMA and maximize its effectiveness in scenarios such as high-speed data transmission and real-time signal processing.

How to implement loosely coupled design in C? How to implement loosely coupled design in C? Apr 28, 2025 pm 09:42 PM

To implement loose coupling design in C, you can use the following methods: 1. Use interfaces, such as defining the Logger interface and implementing FileLogger and ConsoleLogger; 2. Dependency injection, such as the DataAccess class receives Database pointers through the constructor; 3. Observer mode, such as the Subject class notifies ConcreteObserver and AnotherObserver. Through these technologies, dependencies between modules can be reduced and code maintainability and flexibility can be improved.

MySQL: The Database, phpMyAdmin: The Management Interface MySQL: The Database, phpMyAdmin: The Management Interface Apr 29, 2025 am 12:44 AM

MySQL and phpMyAdmin can be effectively managed through the following steps: 1. Create and delete database: Just click in phpMyAdmin to complete. 2. Manage tables: You can create tables, modify structures, and add indexes. 3. Data operation: Supports inserting, updating, deleting data and executing SQL queries. 4. Import and export data: Supports SQL, CSV, XML and other formats. 5. Optimization and monitoring: Use the OPTIMIZETABLE command to optimize tables and use query analyzers and monitoring tools to solve performance problems.

Steps to add and delete fields to MySQL tables Steps to add and delete fields to MySQL tables Apr 29, 2025 pm 04:15 PM

In MySQL, add fields using ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column, delete fields using ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop. When adding fields, you need to specify a location to optimize query performance and data structure; before deleting fields, you need to confirm that the operation is irreversible; modifying table structure using online DDL, backup data, test environment, and low-load time periods is performance optimization and best practice.

See all articles