Home Database Mysql Tutorial Mysql实时备份实现方法_MySQL

Mysql实时备份实现方法_MySQL

May 23, 2018 pm 04:26 PM
mysql real time backup

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

1、vi my.cfg

代码如下:

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

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
Copy after login


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

4、数据还原

代码如下:

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

如想还原昨天、前天的数据只需要找相应的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;
Copy after login

返回结果会是这样的

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

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

mysql> UNLOCK TABLES;
Copy after login

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 //报告错误的服务器
Copy after login

然后将你刚才打包的数据库文件拷贝到你的从数据库目录中。
重启从数据库服务器。
然后停止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; //日志文件的开始位置(前面要求记住的参数)
Copy after login

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

mysql> slave start;
Copy after login

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

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


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)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 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.

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.

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 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 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 recover data after SQL deletes rows How to recover data after SQL deletes rows Apr 09, 2025 pm 12:21 PM

Recovering deleted rows directly from the database is usually impossible unless there is a backup or transaction rollback mechanism. Key point: Transaction rollback: Execute ROLLBACK before the transaction is committed to recover data. Backup: Regular backup of the database can be used to quickly restore data. Database snapshot: You can create a read-only copy of the database and restore the data after the data is deleted accidentally. Use DELETE statement with caution: Check the conditions carefully to avoid accidentally deleting data. Use the WHERE clause: explicitly specify the data to be deleted. Use the test environment: Test before performing a DELETE operation.

How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

See all articles