Home Database Mysql Tutorial MySQL(四):备份恢复详解

MySQL(四):备份恢复详解

Jun 07, 2016 pm 05:26 PM
backup mysql recover

一、备份的类型随着互联网的迅速发展;数据的重要性不需要多说;相信大家都知道。所以数据备份就显得格外重要了;并且选择一个合适的备份方法能提高性能。下面就

一、备份的类型

随着互联网的迅速发展;数据的重要性不需要多说;相信大家都知道。所以数据备份就显得格外重要了;并且选择一个合适的备份方法能提高性能。下面就备份类型来分类:

  • 根据备份时;数据库服务器是否在线:

  •    冷备:cold backup

       温备:warm backup

       热备:hot backup 基于事务的存储引擎(InnoDB)


  • 根据备份时的数据集:

  •    完全备份:full backup

       部分备份:partial backup


  • 根据备份时的接口(直接备份数据文件还是通过mysql服务器导出数据):

  •    物理备份(physical backup):直接复制(归档)数据文件的备份方式;

       1、不需要额外工具

       2、跨平台性能差

       3、表空间占据大


       逻辑备份(logical backup):把数据从库中提取出来保存为文本文件;

       1、可以使用编辑器处理数据文件

       2、可以使用批处理或网络恢复数据;可以更换数据引擎

       3、有助于避免数据损坏等

       缺陷:恢复慢;无法保证浮点数的精度;还原数据后需要重建索引,消耗时间和资源


  • 根据备份时数据变化(整个数据|变化的数据):

  •    1、完全备份:full backup

       2、增量备份:incremental backup

       3、差异备份:differential backup


    二、基本的备份工具

    1、mysqldump:逻辑备份工具

    对InnoDB热备、MyISAM温备、Aria温备

    备份和恢复过程较慢


    2、mysqldumper:多线程的mysqldump

    很难实现差异或增量备份;


    3、基于lvm-snapshot:

    接近于热备的工具;因为要先请求全局锁;而后创建快照;并在创建完成后释放全局锁

    cp、tar等工具进行物理备份:仅限于冷备

    备份和恢复速度较快;

    很难实现增量备份;并且请求全局需要等待一段时间;在繁忙的服务器上尤其如此。


    4、部分备份工具;不会备份关系定义;仅备份表中的数据;逻辑备份工具

    select clause into outfile '/path/to/somefile'

    load data infile '/path/to/somefile'


    5、Xtrabackup:由percona提供的开源备份工具

    InnoDB热备;增量备份

    MyISAM温备;不支持增量

    物理备份;速度快


    三、基于mydump进行备份

    #使用格式 [root@localhost ~]# mysqldump Usage: mysqldump [OPTIONS] database [tables] OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...] OR mysqldump [OPTIONS] --all-databases [OPTIONS] For more options, use mysqldump --help #详细可以--help查看;man也可以查看详细说明
  • 备份单个库

  • #备份单个库 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | hellodb | | mysql | | performance_schema | | test | | ultrax | +--------------------+ 6 rows in set (0.00 sec) #假设备份上述中hellodb数据库 [root@localhost ~]# mysqldump -uroot -p --databases hellodb --lock-all-tables > /tmp/hdb.sql Enter password: [root@localhost ~]# # #删除、恢复 mysql> drop database hellodb; Query OK, 7 rows affected (0.20 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | ultrax | +--------------------+ 5 rows in set (0.00 sec) #恢复 mysql> source /tmp/hdb.sql Query OK, 0 rows affected (0.00 sec) ... mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | hellodb | | mysql | | performance_schema | | test | | ultrax | +--------------------+ 6 rows in set (0.00 sec) #测试恢复成功
  • 完全备份以及利用二进制日志进行时间点恢复

  • [root@localhost ~]# mysqldump -uroot -p --lock-all-tables --all-databases --flush-logs --master-data=2 --events >/tmp/all.sql Enter password: #--lock-all-tables 请求锁;不让其他用户此刻做修改 #--all-databases 备份所有数据库 #--flush-logs 滚动日志 #--master-data=2 记录二进制日志的位置 #--events 导出事件 [root@localhost ~]# vim /tmp/all.sql -- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000007', MASTER_LOG_POS=107; #这行记录的就是二进制日志的位置;后续需要依赖进行时间点恢复 # # # #进行测试删除和恢复 mysql> create database soul; Query OK, 1 row affected (0.00 sec) mysql> use soul; Database changed mysql> create table t1 (ID int); Query OK, 0 rows affected (0.06 sec) mysql> insert into t1 values (1),(2),(3); Query OK, 3 rows affected (0.04 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> drop database soul; Query OK, 1 row affected (0.05 sec) mysql> drop database hellodb; Query OK, 7 rows affected (0.00 sec) #利用备份恢复 mysql> set session sql_log_bin=0; #关闭记录二进制日志;因为恢复过程不需要记录 mysql> source /tmp/all.sql mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | hellodb | | mysql | | performance_schema | | test | | ultrax | +--------------------+ 6 rows in set (0.00 sec) #时间点之后的还没有 [root@localhost ~]# mysqlbinlog --start-position=107 /data/mydata/mysql-bin.000007 # at 547 #位置 #140324 1:04:00 server id 1 end_log_pos 628 Query thread_id=33 exec_time=0 error_code=0 SET TIMESTAMP=1395594240/*!*/; drop database soul #删除的库 /*!*/; # at 628 #位置 #140324 1:04:20 server id 1 end_log_pos 715 Query thread_id=33 exec_time=0 error_code=0 SET TIMESTAMP=1395594260/*!*/; drop database hellodb #删除的库 /*!*/; #能查看完全备份之后的操作;也能看到删除的操作;所以在此截取后来恢复 [root@localhost ~]# mysqlbinlog --start-position=107 --stop-position=547 /data/mydata/mysql-bin.000007 >/tmp/bin.sql mysql> source /tmp/bin.sql ... mysql> use soul; Database changed mysql> show tables -> ; +----------------+ | Tables_in_soul | +----------------+ | t1 | +----------------+ 1 row in set (0.00 sec) mysql> select * from t1; +------+ | ID | +------+ | 1 | | 2 | | 3 | +------+ 3 rows in set (0.15 sec) mysql> set session sql_log_bin=1; Query OK, 0 rows affected (0.00 sec) #在恢复的过程中;应该限制其他用户的任何操作;可以通过iptables来关闭端口;或其他策略 #备份除了数据文件还有二进制文件、配置文件等
    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

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. How to Fix Audio if You Can't Hear Anyone
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    WWE 2K25: How To Unlock Everything In MyRise
    1 months ago By 尊渡假赌尊渡假赌尊渡假赌

    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)

    MySQL: The Ease of Data Management for Beginners MySQL: The Ease of Data Management for Beginners Apr 09, 2025 am 12:07 AM

    MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

    Can I retrieve the database password in Navicat? Can I retrieve the database password in Navicat? Apr 08, 2025 pm 09:51 PM

    Navicat itself does not store the database password, and can only retrieve the encrypted password. Solution: 1. Check the password manager; 2. Check Navicat's "Remember Password" function; 3. Reset the database password; 4. Contact the database administrator.

    How to create navicat premium How to create navicat premium Apr 09, 2025 am 07:09 AM

    Create a database using Navicat Premium: Connect to the database server and enter the connection parameters. Right-click on the server and select Create Database. Enter the name of the new database and the specified character set and collation. Connect to the new database and create the table in the Object Browser. Right-click on the table and select Insert Data to insert the data.

    MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

    MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

    MySQL and SQL: Essential Skills for Developers MySQL and SQL: Essential Skills for Developers Apr 10, 2025 am 09:30 AM

    MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

    How to create a new connection to mysql in navicat How to create a new connection to mysql in navicat Apr 09, 2025 am 07:21 AM

    You can create a new MySQL connection in Navicat by following the steps: Open the application and select New Connection (Ctrl N). Select "MySQL" as the connection type. Enter the hostname/IP address, port, username, and password. (Optional) Configure advanced options. Save the connection and enter the connection name.

    How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

    You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

    How to execute sql in navicat How to execute sql in navicat Apr 08, 2025 pm 11:42 PM

    Steps to perform SQL in Navicat: Connect to the database. Create a SQL Editor window. Write SQL queries or scripts. Click the Run button to execute a query or script. View the results (if the query is executed).

    See all articles