search
HomeDatabaseMysql TutorialMySQL数据备份与恢复_MySQL

一.备份数据的方式

MySQL参数说明:http://www.linuxidc.com/Linux/2014-04/99673.htm

MySQL管理之数据备份及恢复http://www.linuxidc.com/Linux/2014-04/99674.htm

1.物理备份 

    1.1冷备 cp tar等 

    1.2热备 mysqlhotcopy    只能备份存储引擎是myisam的表,依赖perl-DBD-MySQL

2.逻辑备份:用mysql自己提供的备份命令或第三放方软件提供的备份命令,备份创建库、表、和产生表记录时的sql语句。

二.数据备份策略:

1.完整备份        备份数据库服务器上的所有库所有表。

2.差异备份        备份自完整备份后所有产生的新数据。

3.增量备份        备份自上一次备份后所有产生的新数据

生产环境一般采用  完整备份+增量备份 或者 完整备份+差异备份

三.逻辑备份与恢复

1.mysqldump命令做数据的完整备份

命令格式:

mysqldump -h数据库服务器ip -u用户名 -p密码  数据库名 > 目录名/备份文件名

数据库名的表示方式:

--all-databases                    备份数据库服务器上的所有库所有表

数据库名                            对某个库做完整备份

数据库名  表                        对指定库里的指定表做完整备份

-B 数据库名1 数据库名2 数据库N      对某几个库做完整备份

#目录名 :若备份时不写目录名备份文件就存放在,执行备份命令时 所在的目录下

#备份文件名 :可以自己定义,要有标识性,约定俗成mysql的备份文件都以.sql 结尾

                      若文件重名,上一次备份会被当前备份覆盖。

# 备份用来连接数据库服务器的用户,要有被备份的目标库有相应的权限才可以。

2.恢复完整备份数据

命令格式:

mysql  -h数据库服务器ip  -u用户名  -p密码  数据库名 

mysql  -h数据库服务器ip  -u用户名  -p密码   

# 数据库名:可选项,当备份文件中有 create database,use  sql语句时,恢复数据时可以不指定数据库名。

# 用完整备份文件 恢复数据,只能把数据恢复到备份时的状态,完整备份之后产生的信息数据是无法恢复。

四.使用binlog日志做增量备份与数据恢复

1.mysql日志类型

mysql共有4种类型日志:

binlog日志(二进制日志)  记录除查询之外sql语句。

错误日志                  记录mysql服务在启动和运行过程中产生的错误。

查询日志                  记录执行的所有的sql操作。

慢查询日志                只记录超过指定时间,显示查询结果的sql语句。默认超时时间是10秒。

#默认情况下,mysql只开启错误日志。


#默认情况下日志都存放在数据库目录下。每种日志文件都默认命名方式。

2.启用binlog日志

vim /etc/my.cnf

[mysqld]

log_bin=/binlogdir/filename        //设置存放binlog目录和文件名,自定义目录必须让mysql用户有写入权限,目录不设置则默认在数据库目录/var/lib/mysql/下,文件名不设置则默认为:主机名-bin.000001                                   

max-binlog-size=100M                //设置binlog日志文件的大小,不设置则默认为500M自动生成第二个binlog日志文件

service  mysql  restart

# 对所有库操作的使用数据发送改变的sql语句都会写进当前编号最大的binlog日志文件里。

# localhost-bin.index  记录当前已有binlog日志文件

3.分析binlog日志文件内容。

binlog日志记录sql语句的方式:

① 时间方式

② 字符偏移量方式

mysqlbinlog  [选项]  binlog日志文件名

选项:

偏移量

--start-position=100        起始位置

--stop-position=1200        结束位置

时间点:

--start-datetime="YYYY-mm-dd  HH:MM:SS"  起始时间

--stop-datetime="YYYY-mm-dd  HH:MM:SS"    结束时间

# 不加任何选项则默认查看全部日志内容

4.手动生成新的binlog日志文件方式:

① mysql>flush logs;

② #mysql -h数据库服务器IP -u用户名 -p密码 -e "flush logs"

③ #mysqldump -h数据库服务器IP -u用户名 -p密码 --flush-logs 数据库名> /目录名/xxx.sql

④ service  mysql  restart              //一般不用

5.增量备份数据恢复

原理:使用mysqlbinlog提取历史sql操作管道给mysql命令重做

命令格式:

mysqlbinlog  [选项]  binlog日志文件名  |  mysql  -h数据库服务器ip  -u用户名  -p密码  数据库名

批量恢复增量备份:

mysqlbinlog  `cat localhost-bin.index`| mysql -h数据库服务器ip -u用户名 -p密码  [数据库名]

6.清理binlog日志

删除早于指定编号的binlog日志文件:

mysql> PURGE  MASTER LOGS TO  'binlog日志文件名';

删除所有binlog日志文件,重建新日志:

mysql> RESET  MASTER;

更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2014-05/102266p2.htm

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 do you handle database upgrades in MySQL?How do you handle database upgrades in MySQL?Apr 30, 2025 am 12:28 AM

The steps for upgrading MySQL database include: 1. Backup the database, 2. Stop the current MySQL service, 3. Install the new version of MySQL, 4. Start the new version of MySQL service, 5. Recover the database. Compatibility issues are required during the upgrade process, and advanced tools such as PerconaToolkit can be used for testing and optimization.

What are the different backup strategies you can use for MySQL?What are the different backup strategies you can use for MySQL?Apr 30, 2025 am 12:28 AM

MySQL backup policies include logical backup, physical backup, incremental backup, replication-based backup, and cloud backup. 1. Logical backup uses mysqldump to export database structure and data, which is suitable for small databases and version migrations. 2. Physical backups are fast and comprehensive by copying data files, but require database consistency. 3. Incremental backup uses binary logging to record changes, which is suitable for large databases. 4. Replication-based backup reduces the impact on the production system by backing up from the server. 5. Cloud backups such as AmazonRDS provide automation solutions, but costs and control need to be considered. When selecting a policy, database size, downtime tolerance, recovery time, and recovery point goals should be considered.

What is MySQL clustering?What is MySQL clustering?Apr 30, 2025 am 12:28 AM

MySQLclusteringenhancesdatabaserobustnessandscalabilitybydistributingdataacrossmultiplenodes.ItusestheNDBenginefordatareplicationandfaulttolerance,ensuringhighavailability.Setupinvolvesconfiguringmanagement,data,andSQLnodes,withcarefulmonitoringandpe

How do you optimize database schema design for performance in MySQL?How do you optimize database schema design for performance in MySQL?Apr 30, 2025 am 12:27 AM

Optimizing database schema design in MySQL can improve performance through the following steps: 1. Index optimization: Create indexes on common query columns, balancing the overhead of query and inserting updates. 2. Table structure optimization: Reduce data redundancy through normalization or anti-normalization and improve access efficiency. 3. Data type selection: Use appropriate data types, such as INT instead of VARCHAR, to reduce storage space. 4. Partitioning and sub-table: For large data volumes, use partitioning and sub-table to disperse data to improve query and maintenance efficiency.

How can you optimize MySQL performance?How can you optimize MySQL performance?Apr 30, 2025 am 12:26 AM

TooptimizeMySQLperformance,followthesesteps:1)Implementproperindexingtospeedupqueries,2)UseEXPLAINtoanalyzeandoptimizequeryperformance,3)Adjustserverconfigurationsettingslikeinnodb_buffer_pool_sizeandmax_connections,4)Usepartitioningforlargetablestoi

How to use MySQL functions for data processing and calculationHow to use MySQL functions for data processing and calculationApr 29, 2025 pm 04:21 PM

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

An efficient way to batch insert data in MySQLAn efficient way to batch insert data in MySQLApr 29, 2025 pm 04:18 PM

Efficient methods for batch inserting data in MySQL include: 1. Using INSERTINTO...VALUES syntax, 2. Using LOADDATAINFILE command, 3. Using transaction processing, 4. Adjust batch size, 5. Disable indexing, 6. Using INSERTIGNORE or INSERT...ONDUPLICATEKEYUPDATE, these methods can significantly improve database operation efficiency.

Steps to add and delete fields to MySQL tablesSteps to add and delete fields to MySQL tablesApr 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

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

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.

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.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function