Mycat+Mysql master-slave replication to achieve dual-machine hot backup

1. Introduction to the principle

       Master-slave replication principle: The built-in replication function of Mysql is the basis for building large-scale, high-performance applications. The Mysql data is distributed to multiple systems. This distribution mechanism is realized by copying the data of a Mysql host to other hosts (slaves) and re-executing it.
       Dual-system hot standby concept: It means to keep the state of two databases automatically synchronized. Operations on any one database are automatically applied to the other database, and the data in the two databases is always kept consistent. There are many benefits to doing so. 1. It can be used for disaster recovery. One of them can be switched to the other if it fails. 2. Load balancing can be done, and requests can be allocated to any one of them to improve website throughput.
       Schematic diagram of master-slave replication:
 
     Perform master-slave replication in three steps:
          ①master records changes to the binary log (Binary log)
          ②Slave accesses the Master to copy the Master's Bibary log record to the Slave's relay log (Relay log)
          ③Slave's SQL thread thread executes the event of Relay log, executes the change once, and synchronizes it to the Slave's database

2. Mysql builds one-way one master and one slave

     
     1. Server preparation: prepare two servers or two virtual machines
     2. Refer to the previous blog post <<Install mysql in linux environment>> , install Mysql on the two servers respectively, the username and password of mysql on the two servers are both root
     3. Our following configurations are called uniformly, MasterA and SlaveB
           ①Authorize SlaveB on the MasterA server
           Use the command to connect to mysql:
           mysql -uroot -proot
           Execute the authorization command: grant replication slave on *.* to 'root'@'SlaveB's ip ' identified by 'root';
           Parameter Description:
           Username: root
           Password: root
           This configuration means: Allow SlaveB to use the username root and the password root to access the Binary log of MasterA
          ②Open the binary log of the MasterA server
           Open the my.cnf configuration file, add the configuration, and use the command to edit the my.cnf configuration file: vi /etc/my.cnf
           
          Parameter Description:
          datadir: data storage path
          socket: mysql has two connection methods socket and TCP/IP
          server-id: cannot be repeated when building master-slave replication
          log-bin: the name of the binary log
          expire_logs_days: the expiration time of the binlog log, which will be automatically cleaned up after the set time is reached
          binlog-do-db: Set which library operation logs are recorded in the binary log
      4. Create the database
       If a brand new library needs to be created on MasterA and SlaveB, it is enough to create a database directly on the two mysqls. If the database for master-slave replication is in use, we need to lock the database first, and then unlock it after the operation is completed. , in order to avoid that the data of the two databases is not the same initial state, if the initial state of the database to be master-slave replication on the two servers is not the same, it is meaningless.
       锁定数据库命令:FLUSH TABLES WITH READ LOCK;
       将数据库从MasterA上导出,将导出的数据库导入到SlaveB的mysql上
       查看MasterA数据库的binary位置,用于配置SlaveB:
       
      参数说明:           
             File:日志名称
             Position:日志偏移量
             Binlog_Do_DB:记录日志的库
      将刚才锁定的数据库解锁:UNLOCK TABLES;
      5.配置SlaveB的mysql
      使用命令vi /etc/my.cnf编辑配置文件,添加以下配置:
      
      6.开启SlaveB的slave
      
     7.查看SlaveB的slave线程是否开启
      
     Slave_IO_Running为读取master的binaryLog的线程
     Slave_SQL_Running为执行SQL的线程
     这两个线程必须都为YES才可以实现主从复制。

三、Mycat+Mysql单向主从结果验证

     1.使用navicat连接上两个mysql
     2.在MasterA的mysql的库中添加一条数据,查看SlaveB的mysql库中是否同步过来,结果是同步了
     3.在MasterA这台服务器上安装一个mycat,我们配置一下mycat的schema和server两个配置文件
     schema:

     server.xml添加以下配置:
<user name="root">
		<property name="password">root</property>
		<property name="schemas">itoo</property>		
</user>
      4.通过mycat连接schema
      mysql -uroot -proot -h127.0.0.1 -P8066
     
      5.插入一条数据,结果验证是两个库中的数据会同步
      6.将Master的mysql停掉,依旧通过mycat插入一条数据,数据会插入到Slave的mysql中,将Master的mysql启动,通过mycat插入一条数据到数据库,发现两个数据库之间不会同步.

四、配置两个mysql互为主从

     在以上配置的基础上,将Master机器作为slave,slave的机器作为master.

         1.在Slave(相对的Master)机器上连接mysql,为Master(相对的Slave)授权
         grant replication slave on *.* to 'root'@'192.168.22.xxx' identified by 'root';
         其中192.168.22.xxx是Master机的ip
         2.查看Slave机的binarylog
         
        3.开启Master的同步
       
       如果出现上图的错误就先将slave停掉,再操作一遍,使用命令:STOP SLAVE,(此处命令必须为大写)开启完同步之后需要打开slave,使用命令:START SLAVE(此处命令必须为大写)。
       4.在Master上查看slave的状态
        

五、验证互为主从搭建结果

       1.navicat连接mysql之后,在数据库中直接添加一条数据,分别在两个数据库中添加一条数据,发现数据在两个数据库中同步。
       2.mycat的配置不用修改,用mycat连接mysql,命令为:mysql -uroot -proot -h127.0.0.1 -P8066
       3.插入一条数据,发现数据库也能同步
       4.将其中一个数据库停掉,在启动着的数据库中添加一条数据,然后启动刚才停掉的数据库,发现数据同样同步到了数据库中。

六、总结

      通过一步步的实践,让系统更加的稳定,之后数据量大了之后还要研究分库分表,下一篇博客,小编将会写一下mycat-web的可视化监控界面,实时监测mycat的性能。

Guess you like

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