Let's briefly introduce mysql two-way hot standby: mysql provides database replication function since version 3.23.15. Using this function, two databases can be synchronized, master-slave mode (A->B), and mutual backup mode (A<=>B).
The actual description of the operation of the two-way hot standby of the mysql database:
1. The settings of the synchronous replication function of the mysql database are reflected in the configuration file of mysql. The configuration file in the linux environment is generally in /etc/mysql/my.cnf or my.cnf in the home directory of the mysql user, the author's my.cnf is in /etc/my.cnf; in the windows environment, you can go to Find my.ini in the mysql installation path.
2. Configure data synchronization (A->B) (take MySQL version 5.0.26 as an example):
Suppose database A is the host:
A machine:
IP = 192.168.1.101
B machine:
IP = 192.168.1.102
(1) There is a database in machine A as follows:
//database 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 machine's my.cnf (or my.ini) should be configured:
- server-id=1
- log - bin = c :\ mysqlback #Logging file for synchronization events
- binlog - do - db = backup_db #Database that provides data synchronization services
(2) There is a database in machine B as follows:
//database 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机器:
(4)重启AB数据库,后:
B机器:
查看同步配置情况
A机器:
B机器:
(5)在A中的backup_db.backup_table表中插入一些数据,查看B中的backup_db.backup_table表是否同步了数据改动。如果没有看到同步数据结果,即同步不成功,请查看错误(如下)。
当有错误产生时*.err日志文件(可到mysql安装目录下找),同步的线程退出。当纠正错误后重复步骤(4)。
3、实现双向热备(A<=>B):
将以上的(1)-(5)步骤按A-B双向配置即可。
总结一下:
主要是两边建立同样的数据库,然后在数据库配置文件里加入更新的语句即可。
相互开通互有权限的用户,然后这条命令就是同步频率和同步数据库: