当前位置:网站首页>MySQL highly available cluster – MHA
MySQL highly available cluster – MHA
2022-06-29 17:08:00 【Full stack programmer webmaster】
What is? MHA
MHA(Master High Availability) It's automatic master Fail over and slave Upgraded packages . It's based on standards MySQL Copy ( asynchronous / Half a synchronous ). MHA There are two parts :MHA Manager( The management node ) and MHA Node( Data nodes ). MHA Manager It can be deployed on a single machine to manage multiple master-slave colony , It can also be deployed in one slave On .MHA Manager Detecting the cluster's node node , If I found master In case of failure , It can automatically send slave Upgrade to a new master, And then all the others slave Point to the new master. The entire fail over process is transparent to the application . MHA node Run on each MySQL Server (master/slave/manager), It has parsing and cleaning through monitoring logs Function to speed up the failover process . As a precondition , It should be configured first MySQL Copy , And set up SSH Public key password free login . Let's say CentOS For example , It's better to install first EPEL(http://fedoraproject.org/wiki/EPEL), Otherwise YUM Some packages may not be found .MHA from Node and Manager form ,Node Run on every one MySQL Server , in other words , Whether it's MySQL master server , still MySQL From the server , All have to be installed Node, and Manager It usually runs on a separate server , But if hardware resources are tight , You can also use one MySQL From the server Part-time job Manager Role .
MHA Working principle and tool kit
working principle
From the crash of master Save binary log events (binlog events) - Identify those with the latest updates slave Apply differential relay logs (relay log) To the others slave Application from master Saved binary log events (binlog events) - Upgrade one slave As new master Make others slave Connect new master replicate
Manager Tools
masterha_check_ssh : Check MHA Of SSH To configure masterha_check_repl : Check MySQL Copy masterha_manager : start-up MHA masterha_check_status : Detect current MHA Running state masterha_master_monitor : monitoring master Is it down? masterha_master_switch : Control failover ( Automatic or manual ) masterha_conf_host : Add or remove configured server Information
Node Tools ( These tools are usually made of MHA Manager The script triggers , No need to operate by hand )
save_binary_logs : Save and copy master Binary log apply_diff_relay_logs : Identify different relay log events and apply them to other slave filter_mysqlbinlog : Remove unnecessary ROLLBACK event (MHA This tool is no longer used ) purge_relay_logs : Clear relay logs ( It won't block SQL Threads )
MHA advantage
a、 Automatic failover fast , Seconds to complete the failover b、 Can be combined with semi synchronous replication , Ensure the consistency of master-slave data c、manager Nodes can manage multiple MHA The cluster system d、 At run time ,manager The node just sends periodically ICMP package , Very low impact on performance e、 as long as mysql Replication technology support engine ,MHA It supports f.MHA Enhanced data security g.MHA Data loss is less likely to occur during failover
Deploy Mysql One master and two slaves ( Do a good job of master-slave can be ignored )
Deploy MYSQL A master from
Environmental preparation
IP | edition | service |
|---|---|---|
192.168.1.40 | CentOS Linux release 7.6.1810 (Core) | MHA-manager |
192.168.1.10(master) | CentOS Linux release 7.6.1810 (Core) | mysql( Lord )、MHA-node |
192.168.1.20(slave1) | CentOS Linux release 7.6.1810 (Core) | mysql( from )、MHA-node |
192.168.1.30(slave2) | CentOS Linux release 7.6.1810 (Core) | mysql( from )、MHA-node |
This experiment centos Minimize installation Four hosts install some commands And shut down the firewall
systemctl stop firewalld && yum -y install net-tools vim wget git make cmake gcc gcc-c++Modify the host name and log in without secret
master operation
[[email protected] ~]# vim /etc/hosts
# add to
192.168.1.10 master
192.168.1.20 slave1
192.168.1.30 slave2
[[email protected] ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
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:lU9My4J5QGSrEV8wvMfN61Fxu9jIyCX6+WdxjYTykp8 [email protected]
The key's randomart image is:
+---[RSA 2048]----+
| .+B.. . |
| +.B = .. .|
| . =o=o=. o.|
| o.o+=+.o. |
| . S.o+*++.o|
| .oo++.+o|
| .+.o o|
| oE o |
| ..o |
+----[SHA256]-----+
[[email protected] ~]# ssh-copy-id -i slave1
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'slave1 (192.168.1.20)' can't be established.
ECDSA key fingerprint is SHA256:dnnDcAA2qVnA31i7mtr9LYJmH2veu2+r4t+19qUSqqw.
ECDSA key fingerprint is MD5:0e:f3:c1:3c:dc:5f:12:66:ae:c9:01:51:66:db:bb:02.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'slave1'"
and check to make sure that only the key(s) you wanted were added.
[[email protected] ~]# ssh-copy-id -i slave2
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'slave2 (192.168.1.30)' can't be established.
ECDSA key fingerprint is SHA256:xltTmad7PHB4XORvNjSpTaA4hdjQmix+o3VHfYgwuRE.
ECDSA key fingerprint is MD5:52:f6:2c:cc:d4:75:ce:06:c6:c0:1e:18:7c:4e:2c:73.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'slave2'"
and check to make sure that only the key(s) you wanted were added.
[[email protected] ~]# hostname master
[[email protected] ~]# bash
[[email protected] ~]#
[[email protected] ~]# scp /etc/hosts slave1:/etc/hosts
hosts 100% 218 174.0KB/s 00:00
[[email protected] ~]# scp /etc/hosts slave2:/etc/hosts
hosts 100% 218 117.7KB/s 00:00 slave1 operation
[[email protected] ~]# hostname slave1
[[email protected] ~]# bash
[[email protected] ~]# ssh-copy-id -i master
[[email protected] ~]# ssh-copy-id -i slave2slave2 operation
[[email protected] ~]# hostname slave2
[[email protected] ~]# bash
[[email protected] ~]# ssh-copy-id -i master
[[email protected] ~]# ssh-copy-id -i slave1 Four hosts Turn off the firewall / Sandbox
systemctl stop firewalld && setenforce 0Install first MYSQL 1. install MySQL Of yum Source
wget http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpmyum -y install mysql57-community-release-el7-11.noarch.rpm2. installation is complete , Check whether the installation is successful
yum repolist enabled | grep mysql.*3. It can be installed mysql 了
yum install mysql-community-server4. After the installation is complete, you can start mysql 了
systemctl start mysqld.service5. Get random password
grep 'temporary password' /var/log/mysqld.log6. If you want to set your own password easily , It comes with a password checking mechanism that doesn't allow you to set it up easily sql Enter Settings
mysql -uroot -p
password: The random password just obtained
Changing the default password is complicated and difficult
mysql>set global validate_password_policy=0;# Set the password check policy to 0
mysql>set global validate_password_length=1;# Set the password length to 17. Change the database password
set password for user name @localhost = password(' New password ');8. thus MySQL Installation completed
Modify the configuration file for master-slave replication
To configure Master Main library machine
Turn on binlog Set up mysql The unique number of (mysql5.7 That is, new parameters are added in the later version ), And on binlog journal Input vi /etc/my.cnf Go to configuration file , Press Insert Key to enter edit mode , Add the following parameters server-id=1 ( Unique identification of the machine ) log-bin=/var/lib/mysql/mysql-bin ( Turn on binlog)
vim /etc/my.cnfConduct mysql restart
systemctl restart mysqldGet into mysql Catalog view binlog Open successfully or not
Get into MySQL Create users and authorize
mysql> grant replication slave on *.* to 'slave'@'192.168.1.%' identified by '123.com';
Query OK, 0 rows affected, 1 warning (0.00 sec) To configure Slave From the library machine
slave1slave2restart MySQL service
systemctl restart mysqld Check the main server status
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000006 | 1049 | | | |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec) Configure the slave database to synchronize with the master database slave1、salve2
mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.10',
-> MASTER_USER='slave',
-> MASTER_PASSWORD='123.com',
-> MASTER_LOG_FILE='master-bin.000006',
-> MASTER_LOG_POS=1049;
Query OK, 0 rows affected, 2 warnings (0.01 sec)Start the synchronization
start slave;see slave state , These two items must be yes
show slave status\G;Test for synchronization :
The main database is used to create the database ;
mysql> create database test;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.00 sec)see slave1、slave2 Slave Library ;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.00 sec)One master and two slaves are successfully set up !!!
Deploy MHA
The required MHA Install package Links
link :https://pan.baidu.com/s/1QOgJrkHItlCjwMX60h6s4A
Extraction code :e9bi
Copy this content after opening baidu network disk mobile phone App, It is more convenient to operate First step
build MySQL Master-slave ( Do well and skip )
One thing to pay attention to :mysql The authorized users of master-slave replication also need all three , Because once one of the main machines goes down , There is one that will replace the master , So you still need a user who can copy binary logs , The three user names must be the same
MySQL Master my.cnf The configuration file
server-id=1
log-bin=master-bin
log-slave-updates=true
relay-log = relay-log-binMySQL From my.cnf The configuration file
Why does the configuration file of the slave have the same as that of the master log-bin, The same reason for authorization , If after the primary outage , From to main , You also need binary log files
a server-id by 2, a server-id by 3
server-id=2
log-bin=master-bin
log-slave-updates=true
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index
read-only=1 # Start read-only properties
relay-log-purge=0 # High availability for switching , When ascending from master , Keep previous relay logs Database authorization mha user , This user will be used as the management side mysql Database user used when
Three databases log in to the database client respectively , Execute the following command to authorize
grant all on *.* to 'mha'@'192.168.1.%' identified by '123.com';Then authorize the master and slave to copy users ( All three databases do )
grant replication slave on *.* to 'slave'@'192.168.1.%' identified by '123.com';see MySQL Lord (192.168.1.10) The offset , Used to copy and use from the server
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 | 1021 | | | |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)It's on two MySQL(192.168.1.20/192.168.1.30) From the following
change master to master_host='192.168.1.10',master_user='slave',master_password='123.comm',master_log_file='master-bin.000001',master_log_pos=1021;
start slave;
show slave status\G # Success occurs when the following occurs
Slave_IO_Running: Yes
Slave_SQL_Running: YesThe second step
For the convenience of management , Do password free login between the four servers
MHA The management end 192.168.1.40 node1、node2、node3 Again ( Have done something that can be ignored )
ssh-keygen
ssh-copy-id -i 192.168.1.10
ssh-copy-id -i 192.168.1.20
ssh-copy-id -i 192.168.1.30The third step
Install managed nodes on each server
The operation of the four machines is consistent
[[email protected] ~]# rpm -ivh epel-release-latest-7.noarch.rpmUse the Internet yum Installation dependencies , You need to use the network yum
yum -y install perl-DBD-mysql perl-DBI[[email protected] ~]# rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpmStep four # Please delete the following comments when copying the following code ( So as not to report a mistake ) stay MHA The management end (192.168.1.40) Install Management Pack
In the absence of mysql Install the following packages on the host of
yum -y install perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager
rpm -ivh mha4mysql-manager-0.56-0.el6.noarch.rpmWriting configuration files
[[email protected] ~]# vim /etc/masterha_default.cnf
[server default] # Global configuration
user=mha # The management user authorized by the database to the management node
password=123.com # Password of the management user authorized by the database to the management node
repl_user=slave # Users used by master-slave replication
repl_password=123.com # The user password used by master-slave replication
ssh_user=root # Manage the... Used between nodes ssh user
master_binlog_dir=/var/lib/mysql # mysql The directory where the binary logs are stored
remote_workdir=/data/login # When you get mysql The primary binary log is stored in this directory of the local computer
ping_interval=2 # Manage nodes and mysql Main communication time ( second ), Over time , Think mysql The main outage
shutdown_script="" # When mysql Scripts executed when the main machine goes down , Can be null Write the configuration file of the management node
[[email protected] ~]# mkdir /etc/mha
[[email protected] ~]# vim /etc/mha/app1.cnf
[server default] # Global configuration
manager_workdir=/var/lib/manager # working directory
manager_log=/var/log/manager/manager.log # Log directory ,manager Need to create
[server1] # Node configuration
hostname=192.168.1.10
port=3306
ssh_port=22
[server2]
hostname=192.168.1.20
port=3306
ssh_port=22
candidate_master=1 # Express mysql After the main outage , Yes server2 To replace the master
[server3]
hostname=192.168.1.30
port=3306
ssh_port=22Create directory
mkdir /var/log/managertest manager And database nodes ssh Whether the communication is normal
[[email protected] ~]# masterha_check_ssh --global-conf=/etc/masterha_default.cnf --conf=/etc/mha/app1.cnf
# Finally, it shows okCheck whether the master-slave replication between nodes is normal
[[email protected] ~]# masterha_check_repl --global-conf=/etc/masterha_default.cnf --conf=/etc/mha/app1.cnf
# Finally, it shows okstart-up MHA The management end , In a monitored state
[[email protected] ~]# masterha_manager --global-conf=/etc/masterha_default.cnf --conf=/etc/mha/app1.cnf
# Blocked state verification :
MySQL Lord 192.168.1.10
systemctl stop mysqldWill the Lord mysql After the service simulates the failure , The monitoring management side will use mha The user starts electing another master , To succeed ,down The fallen Master goes online again , It's independent mysql
Create a library on the slave that becomes the master library to verify , Whether others are copied from the library to
Friendship connection
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/101934.html Link to the original text :https://javaforall.cn
边栏推荐
猜你喜欢

GNN notes: message propagation model

controller、service、dao之间的关系

机器学习7-支持向量机

Graduates are confused and middle-aged people are anxious. How can the career path become wider and wider?

LSB hidden items of stream carrier based on assembly implementation

疫情居家外包项目之协作开发| 社区征文

iNFTnews | Meta在元宇宙中的后续计划会是什么?

AI and creativity

解题元宇宙,网络游戏中的多元通信方案

深圳内推 | 深圳计算科学研究院招聘机器学习助理工程师(校招)
随机推荐
InheritableThreadLocal 在线程池中进行父子线程间消息传递出现消息丢失的解析
In depth analysis of Monai (I) data and transforms
Which is better and safer, GF e-gold or Dongfang fortune
Inheritablethreadlocal resolves message loss during message transmission between parent and child threads in the thread pool
Why does selenium become the first choice for web automated testing? (source code attached)
An error is reported in the Flink SQL rownumber. Who has met him? How to solve it?
XAMPP Apache安装时问题总结
PCB板框的绘制——AD19
疫情居家外包项目之协作开发| 社区征文
固态存储厂商忆联加入龙蜥社区,共建开源新生态
NAACL 2022 | 机器翻译SOTA模型的蒸馏
深圳内推 | 深圳计算科学研究院招聘机器学习助理工程师(校招)
A user level thread library based on C language
Tencent cloud released the upgraded version of CDW Clickhouse to provide a fast experience for massive data real-time analysis scenarios
Comment configurer logback? 30 minutes pour apprendre à coder et à frapper tard.
How to configure logback? 30 minutes for you to thoroughly learn the code to stay up late and knock
SLAM中的子图
函数计算异步任务能力介绍 - 任务触发去重
Paper notes: e (n) equivariant graph neural networks
What are the financial products suitable for the poor in 2022?