search
HomeDatabaseMysql TutorialMySQL数据库备份常识,不得不看

MySQL数据库备份常识,不得不看

Jun 07, 2016 pm 04:11 PM
mysqlmainbackupcommon sensedatabasearticle

以下的文章主要介绍的是MySQL数据库备份的常识,我们在对MySQL数据库进行操作时难免因为操作的失误而造成某些数据的丢失,不用急,MySQL数据库备份就可以帮你避免由于各种原因造成的数据丢失或着数据库的其他问题。 一、数据备份捷径 因为这个方法没有得到官

以下的文章主要介绍的是MySQL数据库备份的常识,我们在对MySQL数据库进行操作时难免因为操作的失误而造成某些数据的丢失,不用急,MySQL数据库备份就可以帮你避免由于各种原因造成的数据丢失或着数据库的其他问题。

一、数据备份捷径

因为这个方法没有得到官方正式文档的验证,我们暂称为试验吧。

目的:备份hostA主机中一个MySQL数据库备份TestA,并恢复到到hostB机中

试验环境:

操作系统:WinNT4.0,Mysql3.22.34,phpMyAdmin 2.1.0

在hostA中安装MySQL数据库备份并建立TestA数据库

hostB机安装MySQL数据库备份,没有TestA数据库

方法步骤:

启动phpMyAdmin察看HostA和HostB中的数据库列表,在HostB中没有TestA数据库

找到HostA中MySQL数据库备份的安装目录,并找到数据库目录data

在我的试验环境中,这个目录是C:\mysql\data

找到对应数据库名称的子目录C:\mysql\data\TestA

粘贴拷贝到HostB的Data目录下,是HostA同HostB MySQL数据库备份数据目录下的文件相同

刷新HostB的phpMyAdmin察看一下数据库列表,我们看到TestA已经出现,并且作查询修改等操作都正常,备份恢复恢复成功

试验结论:MySQL的数据库可以通过文件形式保存,备份,恢复只要将相应文件目录恢复即可,无需使用其它工具备份。

二、正规的方法(官方建议):

导出要用到MySQL数据库备份的mysqldump工具,基本用法是:

mysqldump [OPTIONS] database [tables]

如果你不给定任何表,整个数据库将被导出。

通过执行mysqldump help,你能得到你mysqldump的版本支持的选项表。

注意,如果你运行mysqldump没有quick或opt选项,mysqldump将在导出结果前装载整个结果集到内存中,如果你正在导出一个大的数据库,这将可能是一个问题。

mysqldump支持下列选项:

add-locks

在每个表导出之前增加LOCK TABLES并且之后UNLOCK TABLE。(为了使得更快地插入到MySQL数据库备份)。

add-drop-table

在每个create语句之前增加一个drop table。

allow-keywords

允许创建是关键词的列名字。这由在列名前面加表名的方法做到。

-c, complete-insert

使用完整的insert语句(用列名字)。

-C, compress

如果客户和服务器均支持压缩,压缩两者间所有的信息。

delayed

用INSERT DELAYED命令插入行。

-e, extended-insert

使用全新多行INSERT语法。(给出更紧缩并且更快的插入语句)

-#, debug[=option_string]

跟踪程序的使用(为了调试)。

help

显示一条帮助消息并且退出。

<ol class="dp-xml">
<li class="alt"><span><span class="attribute">fields-terminated-by</span><span>=...  </span></span></li>
<li>
<span class="attribute">fields-enclosed-by</span><span>=...  </span>
</li>
<li class="alt">
<span class="attribute">fields-optionally-enclosed-by</span><span>=...  </span>
</li>
<li>
<span class="attribute">fields-escaped-by</span><span>=...  </span>
</li>
<li class="alt">
<span class="attribute">fields-terminated-by</span><span>=...  </span>
</li>
</ol>

这些选择与-T选择一起使用,并且有相应的LOAD DATA INFILE子句相同的含义。

LOAD DATA INFILE语法。

-F, flush-logs

在开始导出前,洗掉在MySQL数据库备份服务器中的日志文件。

-f, force,

即使我们在一个表导出期间得到一个SQL错误,继续。

-h, host=..

从命名的主机上的MySQL数据库备份服务器导出数据。缺省主机是localhost。

-l, lock-tables.

为开始导出锁定所有表。

-t, no-create-info

不写入表创建信息(CREATE TABLE语句)

-d, no-data

不写入表的任何行信息。如果你只想得到一个表的结构的导出,这是很有用的!

opt

同:

quick add-drop-table add-locks extended-insert lock-tables

应该给你为读入一个MySQL数据库备份服务器的尽可能最快的导出。

-pyour_pass, password[=your_pass]

与服务器连接时使用的口令。如果你不指定“=your_pass”部分,mysqldump需要来自终端的口令。

-P port_num, port=port_num

与一台主机连接时使用的TCP/IP端口号。(这用于连接到localhost以外的主机,因为它使用 Unix套接字。)

-q, quick

不缓冲查询,直接导出至stdout;使用MySQL_use_result()做它。

-S /path/to/socket, socket=/path/to/socket

与localhost连接时(它是缺省主机)使用的套接字文件。

-T, tab=path-to-some-directory

对于每个给定的表,创建一个table_name.sql文件,它包含SQL CREATE 命令,和一个table_name.txt文件,它包含数据。 注意:这只有在mysqldump运行在mysqld守护进程运行的同一台机器上的时候才工作。.txt文件的格式根据fields-xxx和linesxxx选项来定。

-u user_name, user=user_name

与服务器连接时,MySQL使用的用户名。缺省值是你的Unix登录名。

-O var=option, set-variable var=option

设置一个变量的值。可能的变量被列在下面。

<ol class="dp-xml"><li class="alt"><span>-v, verbose </span></li></ol>

冗长模式。打印出程序所做的更多的信息。

<ol class="dp-xml"><li class="alt"><span>-V, version </span></li></ol>

打印版本信息并且退出。

<ol class="dp-xml"><li class="alt">
<span><span>-w, </span><span style="color: white; background-color: navy"><span class="attribute"><span id="1274150332687S" style="display: none"> </span></span></span></span><span ><span class="attribute">--</span></span><span><span class="attribute"><span id="1274150332563E" style="display: none"> </span>where</span><span>=</span><span class="attribute-value">'where-condition'</span><span> </span></span>
</li></ol>

只导出被选择了的记录;注意引号是强制的!

<ol class="dp-xml"><li class="alt"><span><span>"</span><span class="attribute">--where</span><span>=</span><span class="attribute-value">user</span><span>='jimf'" "-wuserid</span><span class="tag">></span><span>1" "-wuserid1" </span></span></li></ol>

最常见的mysqldump使用可能制作整个数据库的一个备份:

<ol class="dp-xml"><li class="alt"><span><span>mysqldump --opt database </span><span class="tag">></span><span> backup-file.sql </span></span></li></ol>

但是它对用来自于一个数据库的信息充实另外一个MySQL数据库备份也是有用的:

<ol class="dp-xml">
<li class="alt"><span><span>mysqldump --opt database  </span></span></li>
<li>
<span class="attribute">MySQL--host</span><span>=</span><span class="attribute-value">remote</span><span>-host -C database  </span>
</li>
</ol>

由于mysqldump导出的是完整的SQL语句,所以用MySQL数据库备份客户程序很容易就能把数据导入了:

<ol class="dp-xml">
<li class="alt"><span><span>mysqladmin create target_db_name  </span></span></li>
<li><span>MySQL target_db_name backup-file.sql  </span></li>
</ol>

MySQL数据库备份虽然大家在平时不经常遇到,但是多熟悉一门知识总不是坏事。MySQL数据库备份在使用的时候对于刚刚接触的人来说是保护数据库不会由于认为操作失误而导致数据丢失。


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
How does MySQL handle data replication?How does MySQL handle data replication?Apr 28, 2025 am 12:25 AM

MySQL processes data replication through three modes: asynchronous, semi-synchronous and group replication. 1) Asynchronous replication performance is high but data may be lost. 2) Semi-synchronous replication improves data security but increases latency. 3) Group replication supports multi-master replication and failover, suitable for high availability requirements.

How can you use the EXPLAIN statement to analyze query performance?How can you use the EXPLAIN statement to analyze query performance?Apr 28, 2025 am 12:24 AM

The EXPLAIN statement can be used to analyze and improve SQL query performance. 1. Execute the EXPLAIN statement to view the query plan. 2. Analyze the output results, pay attention to access type, index usage and JOIN order. 3. Create or adjust indexes based on the analysis results, optimize JOIN operations, and avoid full table scanning to improve query efficiency.

How do you back up and restore a MySQL database?How do you back up and restore a MySQL database?Apr 28, 2025 am 12:23 AM

Using mysqldump for logical backup and MySQLEnterpriseBackup for hot backup are effective ways to back up MySQL databases. 1. Use mysqldump to back up the database: mysqldump-uroot-pmydatabase>mydatabase_backup.sql. 2. Use MySQLEnterpriseBackup for hot backup: mysqlbackup--user=root-password=password--backup-dir=/path/to/backupbackup. When recovering, use the corresponding life

What are some common causes of slow queries in MySQL?What are some common causes of slow queries in MySQL?Apr 28, 2025 am 12:18 AM

The main reasons for slow MySQL query include missing or improper use of indexes, query complexity, excessive data volume and insufficient hardware resources. Optimization suggestions include: 1. Create appropriate indexes; 2. Optimize query statements; 3. Use table partitioning technology; 4. Appropriately upgrade hardware.

What are views in MySQL?What are views in MySQL?Apr 28, 2025 am 12:04 AM

MySQL view is a virtual table based on SQL query results and does not store data. 1) Views simplify complex queries, 2) Enhance data security, and 3) Maintain data consistency. Views are stored queries in databases that can be used like tables, but data is generated dynamically.

What are the differences in syntax between MySQL and other SQL dialects?What are the differences in syntax between MySQL and other SQL dialects?Apr 27, 2025 am 12:26 AM

MySQLdiffersfromotherSQLdialectsinsyntaxforLIMIT,auto-increment,stringcomparison,subqueries,andperformanceanalysis.1)MySQLusesLIMIT,whileSQLServerusesTOPandOracleusesROWNUM.2)MySQL'sAUTO_INCREMENTcontrastswithPostgreSQL'sSERIALandOracle'ssequenceandt

What is MySQL partitioning?What is MySQL partitioning?Apr 27, 2025 am 12:23 AM

MySQL partitioning improves performance and simplifies maintenance. 1) Divide large tables into small pieces by specific criteria (such as date ranges), 2) physically divide data into independent files, 3) MySQL can focus on related partitions when querying, 4) Query optimizer can skip unrelated partitions, 5) Choosing the right partition strategy and maintaining it regularly is key.

How do you grant and revoke privileges in MySQL?How do you grant and revoke privileges in MySQL?Apr 27, 2025 am 12:21 AM

How to grant and revoke permissions in MySQL? 1. Use the GRANT statement to grant permissions, such as GRANTALLPRIVILEGESONdatabase_name.TO'username'@'host'; 2. Use the REVOKE statement to revoke permissions, such as REVOKEALLPRIVILEGESONdatabase_name.FROM'username'@'host' to ensure timely communication of permission changes.

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 Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version