Home Database Mysql Tutorial Mysql热备份-主从形式

Mysql热备份-主从形式

Jun 07, 2016 pm 04:25 PM
mysql backup

Mysql热备份---主从模式 注:转载请注明出处。 在上家公司四台服务器使用mysql的热备份机制,一台宕机其它还可以工作。防止以往总结如下: 1、本博文试验环境xp系统。利用热备份机制功能可以实现两个数据库同步,主从模式(A-B),互相备份模式(A=B)的功能。 my

Mysql热备份---主从模式
注:转载请注明出处。
  
    在上家公司四台服务器使用mysql的热备份机制,一台宕机其它还可以工作。防止以往总结如下:
1、本博文试验环境xp系统。利用热备份机制功能可以实现两个数据库同步,主从模式(A->B),互相备份模式(AB)的功能。
mysql 数据库同步复制功能的设置都在mysql的配置文件中体现
2、下面我们来看看如何配置数据同步(A->B):
假设数据库A为主机(将向B提供同步服务,即B中的数据来自A):
A机器:
IP = 192.168.1.101
B机器:
IP = 192.168.1.102
(1).在A机器中有数据库如下:
//数据库A
CREATE DATABASE backup_db;
USE backup_db;
CREATE TABLE `backup_table` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(20) character set utf8 NOT NULL,
`sex` varchar(2) character set utf8 NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
#A机器的my.cnf(或my.ini)中应该配置:
server-id=1
log-bin=c:\mysqlback #同步事件的日志记录文件
binlog-do-db=backup_db #提供数据同步服务的数据库
(2).在B机器中有数据库如下:
//数据库B
CREATE DATABASE backup_db;
USE backup_db;
CREATE TABLE `backup_table` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(20) character set utf8 NOT NULL,
`sex` varchar(2) character set utf8 NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
注:数据库A和B的数据库结构一定要相同,否则无法构成同步。
#B机器的my.cnf(或my.ini)中应该配置:
server-id=2
master-host=192.168.1.101 #主机A的地址
master-user=ym #主机A提供给B的用户,该用户中需要包括数据库backup_db的权限
master-password=ym #访问密码
master-port=3306 #端口,主机的MYSQL端口
master-connect-retry=60 #重试间隔60秒
replicate-do-db=backup_db #同步的数据库
(3).完成了以上配置之后,将A的mysql数据的权限给B。
A机器:
mysql>GRANT FILE ON *.* TO IDENTIFIEDBY ‘ym’;
(4).重启AB数据库,后:
B机器:
mysql>slave start;
查看同步配置情况
A机器:
mysql>show master status;
B机器:
mysql>show slave status;
(5).在A中的backup_db.backup_table表中插入一些数据,查看B中的backup_db.backup_table表是否同步了数据改动。如果没有看到同步数据结果,即同步不成功,请查看错误(如下)。
当有错误产生时*.err日志文件(可到mysql安装目录下找),同步的线程退出。当纠正错误后重复步骤(4)。
3、实现双向热备(AB):
将以上的(1)-(5)步骤按A-B双向配置即可。



1、异步复制基本原理

从MySQL3.23.15以后,MySQL支持单向的异步复制。也就是说,1台MySQL服务器充当Master(主库),1台或多台 MySQL服务器充当Slaves(从库),数据从Master向Slaves进行异步复制。注意,这种复制是异步的,有别于MySQL的同步复制实现 (这种实现称做MySQL集群,MySQL Cluster)。

当主库有更新的时候,主库会把更新操作的SQL写入二进制日志(Bin log),并维护一个二进制日志文件的索引,以便于日志文件轮回(Rotate)。在从库启动异步复制的时候,从库会开启两个I/O线程,其中一个线程连 接主库,要求主库把二进制日志的变化部分传给从库,并把传回的日志写入本地磁盘。另一个线程则负责读取本地写入的二进制日志,并在本地执行,以反映出这种 变化。较老的版本在复制的时候只启用一个I/O线程,实现这两部分的功能。

有几个关键性的地方需要注意:

- 主库必须启用Bin log,主库和从库必须有唯一的Server Id
- 从库必须清楚了解从主库的哪一个Bin log文件的哪一个偏移位置起开始复制
- 从库可以从主库只复制指定的数据库,或者数据库的某些数据表
- 主库和从库的数据库名称可以不一样,不过还是推荐使用一样的名称
- 主库和从库的MySQL版本需保持一致



MySQL数据库数据同步的方法
设置好新的master服务器IP.
首先,将slave这边的数据库表内容 phpmyadmin 或者直接 mysql命令进行truncate清空{删除那些bin-log文件,如果innodb则直接删掉数据库文件,然后重新创建表},之后,让 SLAVE START 或者重起MySQL服务,以便先同步一次,这样等会儿才不会出现 "Duplicate entry 重复记录" 这样的错误,
同步完成后,从master那边导出完整的.sql文件(InnoDB使用sql导出,MyISAM应当直接打包目录就可以了),将导出来的.sql导入slave这边应当就OK了
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 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
1654
14
PHP Tutorial
1252
29
C# Tutorial
1225
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.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

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.

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.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

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.

See all articles