search
HomeDatabaseMysql TutorialMYSQL-数据同步,双向热备(转)

MYSQL----数据同步,双向热备(转) 原文地址:http://www.iteye.com/topic/153875 ? 1、简要介绍:mysql从3.23.15版本以后提供数据库复制功能。利用该功能可以实现两个数据库同步,主从模式(A-B),互相备份模式(A=B)的功能。 mysql数据库同步复制功能的设置都

MYSQL----数据同步,双向热备(转)

原文地址:http://www.iteye.com/topic/153875

?

1、简要介绍:mysql从3.23.15版本以后提供数据库复制功能。利用该功能可以实现两个数据库同步,主从模式(A->B),互相备份模式(AB)的功能。
mysql数据库同步复制功能的设置都在mysql的配置文件中体现。在linux环境下的配置文件一般在/etc/mysql/my.cnf或者在mysql用户的home目录下的my.cnf,笔者的my.cnf则在/etc/my.cnf;windows环境下则可到mysql安装路径下找到my.ini。

?

2、下面我们来看看如何配置数据同步(A->B):
(笔者mysql版本 5.0.26)
假设数据库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 ym@'192.168.1.102' 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双向配置即可。

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 handle data replication?How does MySQL handle data replication?Apr 28, 2025 am 12:25 AM

MySQL processes data replication through three modes: asynchronous, semi-synchronous and group replication. 1) Asynchronous replication performance is high but data may be lost. 2) Semi-synchronous replication improves data security but increases latency. 3) Group replication supports multi-master replication and failover, suitable for high availability requirements.

How can you use the EXPLAIN statement to analyze query performance?How can you use the EXPLAIN statement to analyze query performance?Apr 28, 2025 am 12:24 AM

The EXPLAIN statement can be used to analyze and improve SQL query performance. 1. Execute the EXPLAIN statement to view the query plan. 2. Analyze the output results, pay attention to access type, index usage and JOIN order. 3. Create or adjust indexes based on the analysis results, optimize JOIN operations, and avoid full table scanning to improve query efficiency.

How do you back up and restore a MySQL database?How do you back up and restore a MySQL database?Apr 28, 2025 am 12:23 AM

Using mysqldump for logical backup and MySQLEnterpriseBackup for hot backup are effective ways to back up MySQL databases. 1. Use mysqldump to back up the database: mysqldump-uroot-pmydatabase>mydatabase_backup.sql. 2. Use MySQLEnterpriseBackup for hot backup: mysqlbackup--user=root-password=password--backup-dir=/path/to/backupbackup. When recovering, use the corresponding life

What are some common causes of slow queries in MySQL?What are some common causes of slow queries in MySQL?Apr 28, 2025 am 12:18 AM

The main reasons for slow MySQL query include missing or improper use of indexes, query complexity, excessive data volume and insufficient hardware resources. Optimization suggestions include: 1. Create appropriate indexes; 2. Optimize query statements; 3. Use table partitioning technology; 4. Appropriately upgrade hardware.

What are views in MySQL?What are views in MySQL?Apr 28, 2025 am 12:04 AM

MySQL view is a virtual table based on SQL query results and does not store data. 1) Views simplify complex queries, 2) Enhance data security, and 3) Maintain data consistency. Views are stored queries in databases that can be used like tables, but data is generated dynamically.

What are the differences in syntax between MySQL and other SQL dialects?What are the differences in syntax between MySQL and other SQL dialects?Apr 27, 2025 am 12:26 AM

MySQLdiffersfromotherSQLdialectsinsyntaxforLIMIT,auto-increment,stringcomparison,subqueries,andperformanceanalysis.1)MySQLusesLIMIT,whileSQLServerusesTOPandOracleusesROWNUM.2)MySQL'sAUTO_INCREMENTcontrastswithPostgreSQL'sSERIALandOracle'ssequenceandt

What is MySQL partitioning?What is MySQL partitioning?Apr 27, 2025 am 12:23 AM

MySQL partitioning improves performance and simplifies maintenance. 1) Divide large tables into small pieces by specific criteria (such as date ranges), 2) physically divide data into independent files, 3) MySQL can focus on related partitions when querying, 4) Query optimizer can skip unrelated partitions, 5) Choosing the right partition strategy and maintaining it regularly is key.

How do you grant and revoke privileges in MySQL?How do you grant and revoke privileges in MySQL?Apr 27, 2025 am 12:21 AM

How to grant and revoke permissions in MySQL? 1. Use the GRANT statement to grant permissions, such as GRANTALLPRIVILEGESONdatabase_name.TO'username'@'host'; 2. Use the REVOKE statement to revoke permissions, such as REVOKEALLPRIVILEGESONdatabase_name.FROM'username'@'host' to ensure timely communication of permission changes.

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

Video Face Swap

Video Face Swap

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

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!