How does Mysql do dual-machine hot backup and load balancing (method 2)

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

  1. CREATE DATABASE backup_db;
  2. USE backup_db;
  3. CREATE TABLE `backup_table`(
  4. `id`int(11) NOT NULL auto_increment,
  5. `name` varchar(20) character set utf8 NOT NULL,
  6. `sex` varchar(2) character set utf8 NOT NULL,
  7. PRIMARY KEY (`id`)
  8. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
 
 

A machine's my.cnf (or my.ini) should be configured:

  1. server-id=1
  2. log - bin = c :\ mysqlback #Logging  file for synchronization events
  3. binlog - do - db = backup_db #Database  that provides data synchronization services
 

(2) There is a database in machine B as follows:

//database B

  1. CREATE DATABASE backup_db;
  2. USE backup_db;
  3. CREATE TABLE `backup_table`(
  4. `id`int(11) NOT NULL auto_increment,
  5. `name` varchar(20) character set utf8 NOT NULL,
  6. `sex` varchar(2) character set utf8 NOT NULL,
  7. PRIMARY KEY (`id`)
  8. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

注:数据库A和B的数据库结构一定要相同,否则无法构成同步。

B机器的my.cnf(或my.ini)中应该配置:

  1. server-id=2
  2. master-host=192.168.1.101#主机A的地址
  3. master-user=ym #主机A提供给B的用户,该用户中需要包括数据库backup_db的权限
  4. master-password=ym #访问密码
  5. master-port=3306#端口,主机的MYSQL端口
  6. master-connect-retry=60#重试间隔60秒
  7. replicate-do-db=backup_db #同步的数据库
 

(3)完成了以上配置之后,将A的mysql数据的权限给B。

A机器:

  1. mysql>GRANT FILE ON *.* TO ym@192.168.1.102 IDENTIFIEDBY ym’;
 

(4)重启AB数据库,后:

B机器:

  1. mysql>slave start;
 

查看同步配置情况

A机器:

  1. mysql>show master status;
 

B机器:

  1. mysql>show slave status;
 

(5)在A中的backup_db.backup_table表中插入一些数据,查看B中的backup_db.backup_table表是否同步了数据改动。如果没有看到同步数据结果,即同步不成功,请查看错误(如下)。

当有错误产生时*.err日志文件(可到mysql安装目录下找),同步的线程退出。当纠正错误后重复步骤(4)。

3、实现双向热备(A<=>B):

将以上的(1)-(5)步骤按A-B双向配置即可。

总结一下:

主要是两边建立同样的数据库,然后在数据库配置文件里加入更新的语句即可。
相互开通互有权限的用户,然后这条命令就是同步频率和同步数据库:

  1. master - connect - retry = 60 #Retry interval 60 seconds
  2. replicate - do - db = backup_db #synchronized  database

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326192459&siteId=291194637