Home Database Mysql Tutorial mysql备份的 三种方式

mysql备份的 三种方式

Jun 07, 2016 pm 05:39 PM
mysql backup Way

备份的本质就是将数据集另存一个副本,但是原数据会不停的发生变化,所以利用备份只能回复到数据变化之前的数据。那变化之后的呢?所以制定一个好的备份策略很重

1.1把需要备份的数据备份出来

mysql> use hellodb; //打开hellodb库 mysql> select * from students; 查看students的属性 mysql> select * from students where Age > 30 into outfile ‘/tmp/stud.txt’ ; //将年龄大于三十的同学的信息备份出来

[root@www ~]# mysqldump -uroot --single-transaction --master-data=2 --databases hellodb > /backup/hellodb_`date +%F`.sql --single-transaction: 基于此选项能实现热备InnoDB表;因此,不需要同时使用--lock-all-tables; --master-data=2 记录备份那一时刻的二进制日志的位置,并且注释掉,1是不注释的 --databases hellodb 指定备份的数据库 然后回到mysql服务器端,

2.4回到mysql服务器端更新数据

mysql> create table tb1(id int); 创建表 mysql> insert into tb1 values (1),(2),(3); 插入数据,这里只做演示,随便插入了几个数据

2.5先查看完全备份文件里边记录的位置:

[root@www backup]# cat hellodb_2013-09-08.sql | less -- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000013', MASTER_LOG_POS=15684; 记录了二进制日志的位置

2.6 在回到服务器端:

mysql> show master status; 显示此时的二进制日志的位置 从备份文件里边记录的位置到我们此时的位置,即为增量的部分 +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000004 | 15982 | | | +------------------+----------+--------------+------------------+

2.7做增量备份

[root@www backup]# mysqlbinlog --start-position=15694 --stop-position=15982 /mydata/data/mysql-bin.000013 > /backup/hellodb_`date +$F_%H`.sql

2.8再回到服务器

mysql> insert into tb1 values (4),(5); 在插入一些数值 mysql> drop database hellodb; 删除hellodb库

2.9导出这次得二进制日志:

[root@www backup]# mysqlbinlog --start-position=15982 /mydata/data/mysql-bin.000013 查看删除操作时二进制日志的位置 [root@www backup]# mysqlbinlog --start-position=15982 --stop-position=16176 /mydata/data/mysql-bin.000013 > /tmp/hellodb.sql //导出二进制日志

2.10先让mysql离线

回到服务器端: mysql> set sql_log_bin=0; 关闭二进制日志 mysql> flush logs; 滚动下日志

mysql> drop database hellodb;

2.12开始恢复数据:

[root@www ]# mysql 3.4 修改mysql主配置文件存放目录内的文件的权限与属主属组,并初始化mysql[root@www ~]# mkdir /mydata/data //创建数据目录 [root@www ~]# chown mysql:mysql /mydata/data //改属组属主 [root@www ~]# [root@www ~]# cd /usr/local/mysql/ //必须站在此目录下 [root@www mysql]# scripts/mysql_install_db --user=mysql --datadir=/mydata/data //初始化mysql

3.5修改配置文件:

vim /etc/my.cof datadir=/mydata/data 添加数据目录 sync_binlog = 1 开启此功能

3.6 启动服务

[root@www mysql]# service mysqld start mysql> set session sql_log_bin=0; 关闭二进制日志 mysql> source /backup/all_db_2013-09-08.sql 读取备份文件mysql> FLUSH TABLES WITH READ LOCK; 请求读锁 注:不要退出,另起一个终端: mysql> SHOW MASTER STATUS; 查看二进制文件的位置 +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000004 | 107 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) mysql> FLUSH LOGS; 建议滚动下日志。这样备份日志的时候就会很方便了

3.8导出二进制文件,创建个目录单独存放

[root@www ~]# mkdir /backup/limian [root@www ~]# mysql -e 'show master status;' > /backup/limian/binlog.txt [root@www ~]#[root@www ~]# lvcreate -L 100M -s -p r -n mysql_snap /dev/myvg/mydata

回到服务器端,,释放读锁

mysql> UNLOCK TABLES; [root@www ~]# mount /dev/myvg/mysql_snap /mnt/data [root@www data]# cp * /backup/limian/ [root@www data]#lvremove /dev/myvg/mylv_snap

mysql> create table limiantb (id int,name CHAR(10)); mysql> insert into limiantb values (1,'tom'); [root@www data]# mysqlbinlog --start-position=187 mysql-bin.000003 > /backup/limian/binlog.sql [root@www backup]# cd /mydata/data/ [root@www data]# rm -rf * [root@www ~]# cp -a /backup/limian/* /mydata/data/ [root@www data]# chown mysql:mysql *

启动服务

[root@www data]# service mysqld start [root@www data]# mysql 登陆测试 mysql> SHOW DATABASES; mysql> SET sql_log_bin=0 mysql> source/backup/limian/binlog.sql; #二进制恢复 mysql> SHOW TABLES; #查看恢复结果 mysql> SET sql_log_bin=1; #开启二进制日志

4.5回到mysql服务器端对数据进行更新操作

mysql> use hellodb; mysql> delete from students where StuID>=24;

4.6增量备份

innobackupex --user=root --password=mypass --incremental /innobackup/--incremental-basedir=/innobackup/2013-09-12_11-03-04/ --incremental 指定备份类型 --incremental-basedir= 指定这次增量备份是基于哪一次备份的,这里是完全备份文件,这样可以把增量备份的数据合并到完全备份中去
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

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks 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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL for Beginners: Getting Started with Database Management MySQL for Beginners: Getting Started with Database Management Apr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Apr 18, 2025 am 08:42 AM

When developing an e-commerce website using Thelia, I encountered a tricky problem: MySQL mode is not set properly, causing some features to not function properly. After some exploration, I found a module called TheliaMySQLModesChecker, which is able to automatically fix the MySQL pattern required by Thelia, completely solving my troubles.

See all articles