search
HomeDatabaseMysql Tutorialmysql备份与还原_MySQL

1.mysqldump

1.1.备份方法:

(1)导出整个数据库并指定字符集(包括数据库中的数据)

mysqldump -u root -p --default-character-set=utf8 dbname > 存放路径 

(2)导出数据库结构(不含数据)

mysqldump -u username -p -d dbname > dbname.sql    

(3)导出数据库中的某张数据表(包含数据)

mysqldump -u username -p dbname tablename > tablename.sql    

(4)导出数据库中的某张数据表的表结构(不含数据)

mysqldump -u username -p -d dbname tablename > tablename.sql

1.2.mysqldump常用参数

–compatible=name 它告诉 mysqldump,导出的数据将和哪种数据库或哪个旧版本的 MySQL 服务器相兼容。值可以为 ansi、mysql323、mysql40、postgresql、oracle、mssql、db2、maxdb、no_key_options、no_tables_options、no_field_options
等,要使用几个值,用逗号将它们隔开。当然了,它并不保证能完全兼容,而是尽量兼容。


–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语句)


–comments附加注释信息。默认为打开,可以用–skip-comments取消mysqldump -uroot -p
–all-databases (默认记录注释)mysqldump -uroot -p –all-databases
–skip-comments (取消注释)


–compact导出更少的输出信息(用于调试)。去掉注释和头尾等结构。可以使用选项:–skip-add-drop-table
–skip-add-locks –skip-comments –skip-disable-keysmysqldump -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


–databases, -B导出几个数据库。参数后面所有名字参量都被看作数据库名。mysqldump -uroot -p
–databases test mysql


–debug输出debug信息,用于调试。默认值为:d:t:o,/tmp/mysqldump.tracemysqldump -uroot
-p –all-databases –debugmysqldump -uroot -p –all-databases –debug=” d:t:o,/tmp/debug.trace”


–debug-info输出调试信息并退出mysqldump -uroot -p –all-databases –debug-info


–default-character-set设置默认字符集,默认值为utf8mysqldump -uroot -p
–all-databases –default-character-set=latin1


–delayed-insert采用延时插入方式(INSERT DELAYED)导出数据mysqldump -uroot -p
–all-databases –delayed-insert


–events, -E导出事件。mysqldump -uroot -p –all-databases –events


–flush-logs开始导出之前刷新日志。请注意:假如一次导出多个数据库(使用选项–databases或者–all-databases),将会逐个数据库刷新日志。除使用–lock-all-tables或者–master-data外。在这种情况下,日志将会被刷新一次,相应的所以表同时被锁定。因此,如果打算同时导出和刷新日志应该使用–lock-all-tables
或者–master-data 和–flush-logs。mysqldump -uroot -p –all-databases
–flush-logs


–flush-privileges在导出mysql数据库之后,发出一条FLUSH PRIVILEGES
语句。为了正确恢复,该选项应该用于导出mysql数据库和依赖mysql数据库数据的任何时候。mysqldump -uroot -p
–all-databases –flush-privileges


–force在导出过程中忽略出现的SQL错误。mysqldump -uroot -p –all-databases –force


–host, -h需要导出的主机信息mysqldump -uroot -p –host=localhost –all-databases


–ignore-table不导出指定表。指定忽略多个表时,需要重复多次,每次一个表。每个表必须同时指定数据库和表名。例如:–ignore-table=database.table1
–ignore-table=database.table2 ……mysqldump -uroot -p –host=localhost
–all-databases –ignore-table=mysql.user


–lock-all-tables,
-x提交请求锁定所有数据库中的所有表,以保证数据的一致性。这是一个全局读锁,并且自动关闭–single-transaction 和–lock-tables 选项。mysqldump -uroot -p –host=localhost –all-databases
–lock-all-tables


–lock-tables, -l开始导出前,锁定所有表。用READ
LOCAL锁定表以允许MyISAM表并行插入。对于支持事务的表例如InnoDB和BDB,–single-transaction是一个更好的选择,因为它根本不需要锁定表。请注意当导出多个数据库时,–lock-tables分别为每个数据库锁定表。因此,该选项不能保证导出文件中的表在数据库之间的逻辑一致性。不同数据库表的导出状态可以完全不同。mysqldump
-uroot -p –host=localhost –all-databases –lock-tables


–no-create-db, -n只导出数据,而不添加CREATE DATABASE 语句。mysqldump -uroot -p
–host=localhost –all-databases –no-create-db


–no-create-info, -t只导出数据,而不添加CREATE TABLE 语句。mysqldump -uroot -p
–host=localhost –all-databases –no-create-info


–no-data, -d不导出任何数据,只导出数据库表结构。mysqldump -uroot -p –host=localhost
–all-databases –no-data


–opt 这只是一个快捷选项,等同于同时添加 –add-drop-tables –add-locking –create-option –disable-keys –extended-insert –lock-tables –quick –set-charset 选项。本选项能让 mysqldump 很快的导出数据,并且导出的数据能很快导回。该选项默认开启,但可以用 –skip-opt 禁用。注意,如果运行 mysqldump 没有指定 –quick 或 –opt 选项,则会将整个结果集放在内存中。如果导出大数据库的话可能会出现问题

1.3.还原

转载或分享请标明地址:http://blog.csdn.net/w19981220
mysqldump -u用户名 -p密码 数据库 mysql -uroot -p1234 数据库名称

还可以使用这一种方法
首先先use 那一个数据库

<code class="hljs bash">mysql> source sql文件路径</code>
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
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

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

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),