Linux learning -MySQL master-slave replication high-availability solution MHA

A, MHA Profile

1, MHA

 Master High Availability, to monitor the master node, enabling automatic failover to other nodes from; by a lift from the node to the new primary node, with the realization based on a master-slave replication to achieve, but also the client, MHA is currently a major support master multi-slave architecture to build MHA, requested a copy cluster must have at least three database servers, two from a master, that acts as a master, acts as a backup master, the other one acts as the library.

2, MHA cluster architecture

MHA cluster

3, the working principle of MHA

 (1) Save the binary event log (binlog events) down from the collapse of the master
 (2) identify the slave contains the latest updates
 Relay logs (3) Application of the difference (relay log) to other slave
 (4) Apply to save the event from the master binary log (binlog events)
 (5) lift a slave to the new master
 (6) connected to the other new master slave replication

4, MHA software components

 (1) Manager Toolkit
  Manager toolkit contains the following tools:    
   SSH configuration status masterha_check_ssh check the MHA
   masterha_check_repl check MySQL replication status
   masterha_manger start MHA         
   masterha_check_status detect the current running state of the MHA
   masterha_master_monitor detect whether the master is down
   masterha_master_switch failover (automatically or manually)
   masterha_conf_host add or remove server configuration information
 (2) Node Kit
  Node Kit :( These tools are usually triggered by MHA Manager scripts without manual operation) includes the following tools:
   binary log save_binary_logs save and copy the master
   event log events apply_diff_relay_logs relay identify differences and differences applied to other Slave
   filter_mysqlbinlog remove unnecessary ROLLBACK event (MHA no longer use this tool)
   purge_relay_logs Clear relay log (not will block the SQL thread)
  Note: The data in order to reduce as far as possible down the main library hardware damage caused by the loss, it is recommended that the MHA configured to configure MySQL 5.5 semi-synchronous replication
 (3) custom extensions
  secondary_check_script by a plurality of network routing detecting master availability
  master_ip_ailover_script Update Application uses masterip
  shutdown_script forcibly closed master node
   report_script send reports
  init_conf_load_script loads the initial configuration parameters
  master_ip_online_change_script update master node ip address
 (4) Profile:
  Global configuration: providing a default configuration for the application
  application configuration: for each master-slave replication cluster

 Second, the experimental environment

 Host: 4, a MHA Manager (192.168.214.17), three master-slave replication architecture (a master multi-slave), master (192.168.214.27), slave1 (192.168.214.37), slave2 (192.168.214.47)

 System: CentOS7.6

 Database: mariadb-server-5.5.60 (yum source disc)

 MHA 软件 packaging: mha4mysql-manager-0.56, mha4mysql-node-0.56

Third, the steps

 1, three master node from the principal point database installed mariadb, yum source using an optical disk

[root@centos7-27 ~]# yum install -y mariadb-server
[root@centos7-37 ~]# yum install -y mariadb-server
[root@centos7-47 ~]# yum install -y mariadb-server

 2, a package ready MHA, MHA Manager node needs mha4mysql-manager and mha4mysql-node, other nodes only need three mha4mysql-node

[root@centos7-17 ~]# ll mha4mysql*
-rw-r--r-- 1 root root 87119 Dec  9 10:32 mha4mysql-manager-0.56-0.el6.noarch.rpm
-rw-r--r-- 1 root root 36326 Dec  9 10:32 mha4mysql-node-0.56-0.el6.noarch.rpm
[root@centos7-27 ~]# ll mha4mysql*
-rw-r--r-- 1 root root 36326 Dec  9 10:32 mha4mysql-node-0.56-0.el6.noarch.rpm
[root@centos7-37 ~]# ll mha4mysql*
-rw-r--r-- 1 root root 36326 Dec  9 10:32 mha4mysql-node-0.56-0.el6.noarch.rpm
[root@centos7-47 ~]# ll mha4mysql*
-rw-r--r-- 1 root root 36326 Dec  9 10:32 mha4mysql-node-0.56-0.el6.noarch.rpm

 3, the installation package MHA, MHA Manager section relies epel source installation mha4mysql-manager, Ali used here epel source cloud

[root@centos7-17 ~]# cat /etc/yum.repos.d/base.repo
[development]
name=dvdbase repo
baseurl=file:///mnt/cdrom/
enabled=1
gpgcheck=1
gpgkey=file:///mnt/cdrom/RPM-GPG-KEY-CentOS-7

[aliyunEpel]
name=aliyun epel
baseurl=https://mirrors.aliyun.com/epel/$releasever/$basearch
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-$releasever
[root@centos7-17 ~]# yum install -y mha4mysql*
[root@centos7-27 ~]# yum install -y mha4mysql-node-0.56-0.el6.noarch.rpm
[root@centos7-37 ~]# yum install -y mha4mysql-node-0.56-0.el6.noarch.rpm
[root@centos7-47 ~]# yum install -y mha4mysql-node-0.56-0.el6.noarch.rpm

 4, the configuration from the master copy (from a multi-master)

  1). Master node

[root@centos7-27 ~]# vim /etc/my.cnf
[mysqld]
server-id=27    #添加此项
bin-log    #添加此项
skip_name_resolve=1    #添加此项
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d 
[root@centos7-27 ~]# systemctl start mariadb
[root@centos7-27 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show master logs;
+--------------------+-----------+
| Log_name           | File_size |
+--------------------+-----------+
| mariadb-bin.000001 |       245 |
+--------------------+-----------+
1 row in set (0.00 sec)

MariaDB [(none)]> grant replication slave on *.* to repluser@'192.168.214.%' identified by 'centos';    #添加主从复制用户
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)] > Grant All ON * @ * to mhauser. ' 192.168.214%. ' IDENTIFIED by ' mhauser ' ; # add mha manage user 
Query the OK, 0 rows affected ( 0.00 sec)

  2). Slave node, the two nodes arranged as to slave1 (192.168.214.37) Example

[centos7- the root @ 37 [ ~] # CAT / etc / the my.cnf 
[mysqld] 
Server - ID = 37 [     # add this value into the slave2 remember 47 
log_bin # Add this 
Read - only # Add this 
skip_name_resolve = 1     # add this 
relay_log_purge = 0     # add this, whether to clear the relay logs, enabled by default, to pull it off here, because of the need to use relay logs 
... the following omitted 
[root @ centos7-37 ~] # systemctl MariaDB Start [centos7 the root @
- 37 [ ~ ] # MySQL available for purchase to the Commands The End Monitor with MariaDB; or \ G.. Your MariaDB Connection ID IS . 6 Server Version: 5.5 . 60 - MariaDB MariaDB Server Copyright (c) 2000 , 2018 ., The Oracle, MariaDB Corporation Ab and Others Type ' Help; ' or ' \ H ' for . Help Type ' \ c ' to the Clear at The Current the INPUT of Statement . MariaDB [(none)] > information CHANGE mASTER tO # configure the connection to the master node -> MASTER_HOST = ' 192.168.214.27 ' , the master node # the IP -> MASTER_USER = ' repluser ' , account for replication # -> = MASTER_PASSWORD ' CentOS ' , # Password -> of MASTER_LOG_FILE = ' MariaDB-bin.000001 ' , to be copied binary log # -> MASTER_LOG_POS = 245 ; start position of the binary log copy # Query the OK, 0 rows affected ( 0.01 sec) MariaDB [(none)] > Start Slave; # copy open threaded Query the OK, 0 rows affected ( 0.00 sec) MariaDB [(none)] > Show Slave status \ G; # can be used to view the on-state

  5, MHA Manager to configure the host and the other three hosts of ssh key based authentication service

[root@centos7-17 ~]# ssh-keygen  #生成密钥
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:F8VxSwWdphlQLd1J9csOHEW932/[email protected] ZIhi6agC2BxDJ89A 
of The Key ' S randomart Image IS: 
+ --- [the RSA 2048 ] ---- + 
. | OO O = oOBO | 
. | .. O + E = B = | 
| + .B.. O | 
| + ..oo .. |. 
|. S O + OO |. 
|. O OO .. | 
|... + O |. 
|.... .. + | 
| ...... O | 
+ ---- [the SHA256] ----- + 
[centos7 the root @ - . 17 ~] # LS . -a SSH 
. id_rsa id_rsa.pub the known_hosts .. 
# copied to the local public key, then the local. ssh folder to the other three host / root / directory 
# can be realized before the four host machine can interoperate with each other without having to log in lost password  
other three # configuration if only MHA Manage landing, just use the following command
# ssh-copy-id -i  /root/.ssh/id_rsa.pub root@192.168.214.27 
[root@centos7-17 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.214.17  
[root@centos7-17 ~]# scp -r .ssh root@192.168.214.27:/root/
[root@centos7-17 ~]# scp -r .ssh root@192.168.214.37:/root/
[root@centos7-17 ~]# scp -r .ssh root@192.168.214.47:/root/

 6, configuration profiles MHA Manager node

[@ centos7- the root . 17 ~] # mkdir / etc / MHA / 
[the root centos7 @ - . 17 ~] # Vim / etc / MHA / app1.cnf 
[Server default] 
User = mhauser # administrative user 
password = mhauser # password 
manager_workdir = / Data / mastermha / App1 / 
manager_log = / Data / mastermha / App1 / manager.log 
remote_workdir = / Data / mastermha / App1 /  
ssh_user = the root user # ssh 
repl_user = repluser # copy user 
repl_password = CentOS # password 
ping_interval = . 1     # monitoring master of time 1s

[server1] 
hostname = 192.168 . 214.27 
candidate_master = . 1     # This option indicates this master node precedence 
[Server2] 
hostname = 192.168 . 214.37 
candidate_master = . 1 
[Server3] 
hostname = 192.168 . 214.47

 7, validation and startup

[@ centos7- the root . 17 ~] # = masterha_check_ssh --conf / etc / MHA / app1.cnf # authentication SSH 
[centos7 the root @ - . 17 ~] # = masterha_check_repl --conf / etc / MHA / app1.cnf primary verification # from the copy 
[the root centos7 @ - . 17 ~] # = masterha_manager --conf / etc / MHA / # start app1.cnf

  Note: masterha_manager --conf = / etc / mha / app1.cnf this is a one-time start (after the completion of a switch dropped out), and only in the current session; background is recommended to start running: nohup masterha_manager --conf = / etc / mha / app1.cnf> mha.log

 8, when the mater node test OFF, switching from the main

[root@centos7-27 ~]# systemctl stop mariadb  #关闭主节点数据库服务
[root@centos7-37 ~]# mysql  
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 21
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to Clear The Current INPUT Statement. 

MariaDB [(none)] > Show Slave Status \ G; # query is empty, indicating that the node has been promoted to the primary node slave1 
Empty SET ( 0.00 sec) 
MariaDB [(none)] > Show Variables like ' READ_ONLY ' ; Read-the # slave1 node only option has been closed
 + --------------- + ------- + 
| variable_name | Value | 
+ ------ + ------- + --------- 
| READ_ONLY | OFF | 
+ --------------- + ------- + 1 Row in SET ( 0.00 sec) 
[@ centos7 the root - 47 ~ ] # MySQL 
MariaDB [(none)] >

 show slave status\G;   #在slave2上查看
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.214.37  #当节点变为了slave1
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.000001
          Read_Master_Log_Pos: 245
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 531
        Relay_Master_Log_File: mariadb-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
...以下省略

 

Guess you like

Origin www.cnblogs.com/hovin/p/12009708.html