mysqldump
bitsCN.com 最近这两天,因为之前设计数据库的时候没有注意到有的表是要建在另外一台服务器上面的(测试服务器数据库是分布式的,不同的表可能在不同服务器上)。现在里面已经有很多测试数据了,又不想重新添加一遍。所以就想能不能备份出来,再恢复到目标机器上去。然后昨天的话就折腾了一下,请教同事,上网查资料。这里把用法记录一下。
mysqldump命令:MySQL数据库备份还原
一、常用操作:
备份整个数据库
格式:
mysqldump -h主机名 -P端口 -u用户名 -p密码 (–database) 数据库名 > 文件名.sql
mysqldump -h{hostname} -P{port} -u{username} -p{password} {databasename} > {backupfile.sql}
例如:
代码如下:
1
mysqldump -hlocalhost -P3306 -uzhuchao -p123456 db_test > backfile1.sql
备份MySQL数据库为带删除表的格式
备份MySQL数据库为带删除表的格式,能够让该备份覆盖已有数据库而不需要手动删除原有数据库。
格式:mysqldump -–add-drop-table -u{username} -p{password} {databasename} > {backfile.sql}
例如:
代码如下:
1
mysqldump -–add-drop-table –uzhuchao -p123456 db_test > backfile2.sql
直接将MySQL数据库压缩备份
格式:mysqldump -h{hostname} -u{username} -p{password} {databasename} | gzip > {backfile.sql.gz}
例如:
代码如下:
1
mysqldump –hlocalhost –uzhuchao –p123456 db_test1 | gzip > backfile3.sql.gz
备份MySQL数据库某个(些)表
格式:mysqldump -h主机名 -P端口 -u用户名 -p密码 (–tables | –quick) 数据库名 表名1 (表名2 …) > 文件名.sql (括号中的可缺省)。
mysqldump -h{hostname} (-P{port}) -u{user} -p{password} (–tables | –quick) {databasename} {table1} {table2} > {backfile.sql}
例如:
代码如下:
1
2
3
4
mysqldump -hlocalhost -uzhuchao -p123456 db_test tbl_test > backfile4-1.sql
mysqldump -hlocalhost -P3306 -uzhuchao -p123456 db_test tbl_test > backfile4-2.sql
mysqldump -hlocalhost -P3306 -uzhuchao -p123456 --quick db_test tbl_test > backfile4-3.sql
mysqldump -hlocalhost -P3306 -uzhuchao -p123456 --tables db_test tbl_test1 tbl_test2 > backfile4-4.sql
同时备份多个MySQL数据库
格式:mysqldump -h{hostname} (-P{port}) -u{username} -p{password} –databases {databasename1} {databasename2} {databasename3} > multibackfile.sql
例如:
代码如下:
1
mysqldump -hlocalhost -uzhuchao -p123456 –databases db_test1 db_test2 db_test3 > multibackfile.sql
仅仅备份数据库结构
格式:mysqldump –no-data –databases {databasename1} {databasename2} > {structurebackfile.sql}
例如:
代码如下:
1
mysqldump –no-data –databases db_test1 db_test2 > structurebackfile.sql
备份服务器上所有数据库
格式:mysqldump –all-databases > allbackupfile.sql
========================================================================
还原MySQL数据库的命令
格式:mysql -h{hostname} -u{username} -p{password} {databasename} 例如:
代码如下:
1
mysql -hlocalhost -uroot -p123456 db_test4 还原压缩的MySQL数据库
格式:gunzip 例如:
代码如下:
1
gunzip 将数据库转移到新服务器
mysqldump –u{username} –p{password} {databasename} | mysql –host=*.*.*.* –C {databasename}
二、其他:
1、如果端口为默认的3306时,可省略 -P {端口号} 这一项。
2、命令行格式中 { } 中的内容都是变量
主机名 : {hostname}
端 口:{port} (一般默认3306,可缺省)
用户名:{user} {username} (如root)
密 码:{password}
数据库名 :{databasename}
表 名:{table} {table1} {table2}
文件名:{backfile.sql}
作者“石头博客”
bitsCN.com
Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

ToaddauserremotelytoMySQL,followthesesteps:1)ConnecttoMySQLasroot,2)Createanewuserwithremoteaccess,3)Grantnecessaryprivileges,and4)Flushprivileges.BecautiousofsecurityrisksbylimitingprivilegesandaccesstospecificIPs,ensuringstrongpasswords,andmonitori

TostorestringsefficientlyinMySQL,choosetherightdatatypebasedonyourneeds:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseTEXTforlong-formtextcontent.4)UseBLOBforbinarydatalikeimages.Considerstorageov

When selecting MySQL's BLOB and TEXT data types, BLOB is suitable for storing binary data, and TEXT is suitable for storing text data. 1) BLOB is suitable for binary data such as pictures and audio, 2) TEXT is suitable for text data such as articles and comments. When choosing, data properties and performance optimization must be considered.

No,youshouldnotusetherootuserinMySQLforyourproduct.Instead,createspecificuserswithlimitedprivilegestoenhancesecurityandperformance:1)Createanewuserwithastrongpassword,2)Grantonlynecessarypermissionstothisuser,3)Regularlyreviewandupdateuserpermissions

MySQLstringdatatypesshouldbechosenbasedondatacharacteristicsandusecases:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseBINARYorVARBINARYforbinarydatalikecryptographickeys.4)UseBLOBorTEXTforlargeuns


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
