search
HomeDatabaseMysql TutorialMysql实时备份实现方法_MySQL

Mysql实时备份实现方法_MySQL

May 23, 2018 pm 04:26 PM
mysqlreal time backup

目前成熟的实时备份为双机(master/slave),是基于同步日志事件来实现,那单机如何实现具有增量的备份呢?可以借用双机的原理,非常简单,实施步骤如下:
Mysql版本:mysql4.0+

1、vi my.cfg

代码如下:

[mysqld]
log-update=/home/backup/update #添加该行

2、service mysql restart

会在/home/backup/update00001文件,内容为数据库变化的所有SQL(没有select)

3、每天的全备,mysql4.0+最简单就是备份data目录。

代码如下:

service mysql stop
tar -czf data(日期).tar.gz mysql/data
service mysql start


当mysql启动时系统会自动在/home/backup/创建update0000*的文件,那我们可以用该文件作为当天全备的增量实时备份。

4、数据还原

代码如下:

service mysql stop
tar -zxvf data(日期).tar.gz mysql/
service mysql start
mysqladmin -u -p /home/backup/update0000*

如想还原昨天、前天的数据只需要找相应的update0000*来还原即可:)

以下是补充:

1、MYSQL数据库提供了一种主从备份的机制,其实就是把主数据库的所有的数据同时写到备份数据库里面,从而实现MYSQL数据库的实时备份。
2、版本要求,首先要保证主服务器和从服务器的MYSQL版本都高于3.2,另外,从数据库的版本可以高于主服务器,但不能低于主服务器。
3、主服务器设置:
A、先修改MY.INI中有关log-bin的设置,这是记录数据库更改的日志,由于MYSQL的复制机制,是基于日志的,所以主服务器必须要支持更改日志才可以。
接着设置要写入日志的数据库,或者不要写入日志的数据库,这是为了告诉MYSQL,那个库需要备份,哪个不需要。
下面是配置详情:

server-id=1 //数据库的id这个应该默认是1就不用改动
log-bin=log_name //日志文件的名称,这里可以制定日志到别的目录 如果没有设置则默认主机名的一个日志名称
binlog-do-db=db_name //记录日志的数据库
binlog-ignore-db=db_name //不记录日志的数据库

上面的binlog-do-db和binlog-ignore-db可以设置成多个数据库,每个数据库名称之间用“,”分割开。
下一步是设置同步数据库的用户账号
mysql> GRANT REPLICATION SLAVE ON *.*
-> TO ‘备份用户名'@'只能从这个IP登录' IDENTIFIED BY ‘备份用户密码';

设置好以后,重启一下数据库服务。
B、锁定现有的数据,并将数据备份
数据库锁定的命令是:
mysql> FLUSH TABLES WITH READ LOCK;
然后进入mysql的data目录,然后打包你需要备份的数据库目录。
C、现在可以查看主服务器的状态了:
命令如下:

mysql> show master status\G;

返回结果会是这样的

+—————+———-+————–+——————+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+—————+———-+————–+——————+
| mysql-bin.003 | 73 | test | manual,mysql |
+—————+———-+————–+——————+

当然,这个表,显示了你刚才在MY.INI中写入的配置。
然后解锁数据库:

mysql> UNLOCK TABLES;

4、从服务器设置
还是和刚才一样,修改数据库配置文件,即MY.INI
配置详情如下:

server-id=n //设置数据库id默认主服务器是1可以随便设置但是如果有多台从服务器则不能重复。
master-host=db-master.mycompany.com //主服务器的IP地址或者域名
master-port=3306 //主数据库的端口号
master-user=pertinax //同步数据库的用户
master-password=freitag //同步数据库的密码
master-connect-retry=60 //如果从服务器发现主服务器断掉,重新连接的时间差
report-host=db-slave.mycompany.com //报告错误的服务器

然后将你刚才打包的数据库文件拷贝到你的从数据库目录中。
重启从数据库服务器。
然后停止SLAVE的服务

mysql> slave stop; //停止slave的服务

停止之后,还是在mysql提示符下,设置主服务器的各种参数
命令如下:

mysql> CHANGE MASTER TO
-> MASTER_HOST='master_host_name', //主服务器的IP地址
-> MASTER_USER='replication_user_name', //同步数据库的用户
-> MASTER_PASSWORD='replication_password', //同步数据库的密码
-> MASTER_LOG_FILE='recorded_log_file_name', //主服务器二进制日志的文件名(前面要求记住的参数)
-> MASTER_LOG_POS=recorded_log_position; //日志文件的开始位置(前面要求记住的参数)

然后启动同步数据库的进程

mysql> slave start;

没有意外的话基本上到这一步,双库同步就已经实现了。

以上就是Mysql实时备份实现方法_MySQL的内容,更多相关内容请关注PHP中文网(www.php.cn)!


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 index cardinality affect query performance?How does MySQL index cardinality affect query performance?Apr 14, 2025 am 12:18 AM

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

MySQL: Resources and Tutorials for New UsersMySQL: Resources and Tutorials for New UsersApr 14, 2025 am 12:16 AM

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

Real-World MySQL: Examples and Use CasesReal-World MySQL: Examples and Use CasesApr 14, 2025 am 12:15 AM

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

SQL Commands in MySQL: Practical ExamplesSQL Commands in MySQL: Practical ExamplesApr 14, 2025 am 12:09 AM

SQL commands in MySQL can be divided into categories such as DDL, DML, DQL, DCL, etc., and are used to create, modify, delete databases and tables, insert, update, delete data, and perform complex query operations. 1. Basic usage includes CREATETABLE creation table, INSERTINTO insert data, and SELECT query data. 2. Advanced usage involves JOIN for table joins, subqueries and GROUPBY for data aggregation. 3. Common errors such as syntax errors, data type mismatch and permission problems can be debugged through syntax checking, data type conversion and permission management. 4. Performance optimization suggestions include using indexes, avoiding full table scanning, optimizing JOIN operations and using transactions to ensure data consistency.

How does InnoDB handle ACID compliance?How does InnoDB handle ACID compliance?Apr 14, 2025 am 12:03 AM

InnoDB achieves atomicity through undolog, consistency and isolation through locking mechanism and MVCC, and persistence through redolog. 1) Atomicity: Use undolog to record the original data to ensure that the transaction can be rolled back. 2) Consistency: Ensure the data consistency through row-level locking and MVCC. 3) Isolation: Supports multiple isolation levels, and REPEATABLEREAD is used by default. 4) Persistence: Use redolog to record modifications to ensure that data is saved for a long time.

MySQL's Place: Databases and ProgrammingMySQL's Place: Databases and ProgrammingApr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL: From Small Businesses to Large EnterprisesMySQL: From Small Businesses to Large EnterprisesApr 13, 2025 am 12:17 AM

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?Apr 13, 2025 am 12:16 AM

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.

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

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 agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft