当前位置:网站首页>[DB Bao 45] MySQL highly available mgr+consult architecture deployment
[DB Bao 45] MySQL highly available mgr+consult architecture deployment
2022-06-24 17:16:00 【DBA collection of Wheat Seedlings】
toc
One 、MGR+Consul Architecture brief introduction
Based on the fact that there are a lot of MySQL Database single point of failure , Conventional MHA,PXC And so on VIP perhaps DNS The way of switching can be realized 、 Strong consistency of data based on database , use MGR colony , use consul Service registration discovery realizes the application side through dynamic DNS visit MGR colony , High availability of database , Automatic switching scheme .
of MGR I've published several articles before , Connect as follows :
【DB treasure 18】 stay Docker Installed in MySQL Highly available MGR
【DB treasure 35】 Use MySQL 8.0 clone (clone) Plug in quick add MGR node
This environment includes 7 Taiwan machine , among 3 Taiwan do MySQL Of MGR Data nodes ( It also needs to be installed Consul, As Consul Client), Single master mode , also 3 Taiwan do Consul Server colony , Realization Consul High availability , There's one left to do DNS The server , Used to resolve MGR Node domain name , The plan is shown in the table below :
Two 、 build MGR
2.1、 apply 3 platform MGR machine
-- Pull the mirror image docker pull lhrbest/lhrcentos76:8.0 -- Create a private network docker network create --subnet=192.168.68.0/16 mhalhr docker network inspect mhalhr -- Generate 3 platform CentOS Environment docker rm -f mysql8022mgr33060 mysql8022mgr33061 mysql8022mgr33062 docker run -d --name mysql8022mgr33060 -h lhrmgr60 \ --network mhalhr --ip 192.168.68.60 --privileged=true \ -p 33060:3306 \ -v /sys/fs/cgroup:/sys/fs/cgroup \ lhrbest/lhrcentos76:8.0 init docker run -d --name mysql8022mgr33061 -h lhrmgr61 \ --network mhalhr --ip 192.168.68.61 --privileged=true \ -p 33061:3306 \ -v /sys/fs/cgroup:/sys/fs/cgroup \ lhrbest/lhrcentos76:8.0 init docker run -d --name mysql8022mgr33062 -h lhrmgr62 \ --network mhalhr --ip 192.168.68.62 --privileged=true \ -p 33062:3306 \ -v /sys/fs/cgroup:/sys/fs/cgroup \ lhrbest/lhrcentos76:8.0 init -- Copy MySQL Install files to MySQL In container docker cp mysql-8.0.22-1.el7.x86_64.rpm-bundle.tar mysql8022mgr33060:/ docker cp mysql-8.0.22-1.el7.x86_64.rpm-bundle.tar mysql8022mgr33061:/ docker cp mysql-8.0.22-1.el7.x86_64.rpm-bundle.tar mysql8022mgr33062:/ docker network connect bridge mysql8022mgr33060 docker network connect bridge mysql8022mgr33061 docker network connect bridge mysql8022mgr33062 docker restart mysql8022mgr33060 mysql8022mgr33061 mysql8022mgr33062
2.2、3 Host installation MySQL Environmental Science
docker exec -it mysql8022mgr33060 bash docker exec -it mysql8022mgr33061 bash docker exec -it mysql8022mgr33062 bash tar -xvf mysql-8.0.22-1.el7.x86_64.rpm-bundle.tar yum localinstall -y mysql-community-*.rpm -- Remove the password verification policy mv /usr/lib64/mysql/plugin/validate_password.so /usr/lib64/mysql/plugin/validate_password.so_bk mv /usr/lib64/mysql/plugin/component_validate_password.so /usr/lib64/mysql/plugin/component_validate_password.so_bk -- start-up mysql systemctl start mysqld systemctl status mysqld -- Change Password grep 'temporary password' /var/log/mysqld.log mysql -uroot -p alter user [email protected]'localhost' identified with mysql_native_password by 'lhr'; grant all on *.* to [email protected]'localhost' with grant option; create user [email protected]'%' identified with mysql_native_password by 'lhr'; grant all on *.* to [email protected]'%' with grant option; flush privileges; select user,host,grant_priv,super_priv,authentication_string,password_last_changed from mysql.user;
2.3、 modify MySQL Parameters
-- node 1 cat > /etc/my.cnf <<"EOF" [mysqld] user=mysql port=3306 character_set_server=utf8mb4 secure_file_priv= server-id = 802233060 default-time-zone = '+8:00' log_timestamps = SYSTEM log-bin = binlog_format=row binlog_checksum=NONE skip-name-resolve log_slave_updates = 1 gtid-mode=ON enforce-gtid-consistency=on default_authentication_plugin=mysql_native_password max_allowed_packet = 500M master_info_repository=TABLE relay_log_info_repository=TABLE relay_log=lhrmgr60-relay-bin transaction_write_set_extraction=XXHASH64 loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa" loose-group_replication_start_on_boot=OFF loose-group_replication_local_address= "192.168.68.60:33061" loose-group_replication_group_seeds= "192.168.68.60:33061,192.168.68.61:33061,192.168.68.62:33061" loose-group_replication_bootstrap_group=OFF loose-group_replication_ip_whitelist="192.168.68.60,192.168.68.61,192.168.68.62" report_host=192.168.68.60 report_port=3306 EOF -- node 2 cat > /etc/my.cnf <<"EOF" [mysqld] user=mysql port=3306 character_set_server=utf8mb4 secure_file_priv= server-id = 802233061 default-time-zone = '+8:00' log_timestamps = SYSTEM log-bin = binlog_format=row binlog_checksum=NONE log_slave_updates = 1 gtid-mode=ON enforce-gtid-consistency=ON skip_name_resolve default_authentication_plugin=mysql_native_password max_allowed_packet = 500M master_info_repository=TABLE relay_log_info_repository=TABLE relay_log=lhrmgr61-relay-bin transaction_write_set_extraction=XXHASH64 loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa" loose-group_replication_start_on_boot=OFF loose-group_replication_local_address= "192.168.68.61:33061" loose-group_replication_group_seeds= "192.168.68.60:33061,192.168.68.61:33061,192.168.68.62:33061" loose-group_replication_bootstrap_group=OFF loose-group_replication_ip_whitelist="192.168.68.60,192.168.68.61,192.168.68.62" report_host=192.168.68.61 report_port=3306 EOF -- node 3 cat > /etc/my.cnf <<"EOF" [mysqld] user=mysql port=3306 character_set_server=utf8mb4 secure_file_priv= server-id = 802233062 default-time-zone = '+8:00' log_timestamps = SYSTEM log-bin = binlog_format=row binlog_checksum=NONE log_slave_updates = 1 gtid-mode=ON enforce-gtid-consistency=ON skip_name_resolve default_authentication_plugin=mysql_native_password max_allowed_packet = 500M master_info_repository=TABLE relay_log_info_repository=TABLE relay_log=lhrmgr62-relay-bin transaction_write_set_extraction=XXHASH64 loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa" loose-group_replication_start_on_boot=OFF loose-group_replication_local_address= "192.168.68.62:33061" loose-group_replication_group_seeds= "192.168.68.60:33061,192.168.68.61:33061,192.168.68.62:33061" loose-group_replication_bootstrap_group=OFF loose-group_replication_ip_whitelist="192.168.68.60,192.168.68.61,192.168.68.62" report_host=192.168.68.62 report_port=3306 EOF
2.4、 restart MySQL Environmental Science
-- restart MySQL systemctl restart mysqld -- Get into MySQL docker exec -it mysql8022mgr33060 bash docker exec -it mysql8022mgr33060 mysql -uroot -plhr -- Remote connection MySQL mysql -uroot -plhr -h192.168.66.35 -P33060 mysql -uroot -plhr -h192.168.66.35 -P33061 mysql -uroot -plhr -h192.168.66.35 -P33062 -- see MySQL journal docker logs -f --tail 10 mysql8022mgr33060 docker logs -f --tail 10 mysql8022mgr33061 docker logs -f --tail 10 mysql8022mgr33062 -- see MySQL The host name 、server_id and server_uuid mysql -uroot -plhr -h192.168.66.35 -P33060 -e "select @@hostname,@@server_id,@@server_uuid" mysql -uroot -plhr -h192.168.66.35 -P33061 -e "select @@hostname,@@server_id,@@server_uuid" mysql -uroot -plhr -h192.168.66.35 -P33062 -e "select @@hostname,@@server_id,@@server_uuid"
result :
C:\Users\lhrxxt>mysql -uroot -plhr -h192.168.66.35 -P33060 -e "select @@hostname,@@server_id,@@server_uuid" mysql: [Warning] Using a password on the command line interface can be insecure. +------------+-------------+--------------------------------------+ | @@hostname | @@server_id | @@server_uuid | +------------+-------------+--------------------------------------+ | lhrmgr60 | 802233060 | 44c693d8-80bb-11eb-b4bb-0242c0a8443c | +------------+-------------+--------------------------------------+ C:\Users\lhrxxt>mysql -uroot -plhr -h192.168.66.35 -P33061 -e "select @@hostname,@@server_id,@@server_uuid" mysql: [Warning] Using a password on the command line interface can be insecure. +------------+-------------+--------------------------------------+ | @@hostname | @@server_id | @@server_uuid | +------------+-------------+--------------------------------------+ | lhrmgr61 | 802233061 | 408acdb5-80bc-11eb-97a7-0242c0a8443d | +------------+-------------+--------------------------------------+ C:\Users\lhrxxt>mysql -uroot -plhr -h192.168.66.35 -P33062 -e "select @@hostname,@@server_id,@@server_uuid" mysql: [Warning] Using a password on the command line interface can be insecure. +------------+-------------+--------------------------------------+ | @@hostname | @@server_id | @@server_uuid | +------------+-------------+--------------------------------------+ | lhrmgr62 | 802233062 | 9d5c3e3e-80bc-11eb-a0b2-0242c0a8443e | +------------+-------------+--------------------------------------+
2.5、 install MGR plug-in unit ( All nodes execute )
mysql -uroot -plhr -h192.168.66.35 -P33060 INSTALL PLUGIN group_replication SONAME 'group_replication.so'; -- If MySQL The version is greater than 8.0.17, Then it is recommended to install clone plug-in unit INSTALL PLUGIN clone SONAME 'mysql_clone.so'; show plugins;
2.6、 Set up a copy account
-- In the main warehouse (192.168.68.60) On the implementation CREATE USER [email protected]'%' IDENTIFIED BY 'lhr'; GRANT REPLICATION SLAVE,BACKUP_ADMIN ON *.* TO [email protected]'%'; FLUSH PRIVILEGES; -- All nodes execute CHANGE MASTER TO MASTER_USER='repl', MASTER_PASSWORD='lhr' FOR CHANNEL 'group_replication_recovery';
2.7、 start-up MGR, In the main warehouse (192.168.68.60) On the implementation
SET GLOBAL group_replication_bootstrap_group=ON; START GROUP_REPLICATION; SET GLOBAL group_replication_bootstrap_group=OFF; -- see MGR Group information SELECT * FROM performance_schema.replication_group_members;
2.8、 Other nodes join MGR, From the library (192.168.68.61,192.168.68.62) On the implementation
reset master; START GROUP_REPLICATION; -- see MGR Group information SELECT * FROM performance_schema.replication_group_members;
Execution results :
MySQL [(none)]> SELECT * FROM performance_schema.replication_group_members; +---------------------------+--------------------------------------+---------------+-------------+--------------+-------------+----------------+ | CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION | +---------------------------+--------------------------------------+---------------+-------------+--------------+-------------+----------------+ | group_replication_applier | 408acdb5-80bc-11eb-97a7-0242c0a8443d | 192.168.68.61 | 3306 | ONLINE | SECONDARY | 8.0.22 | | group_replication_applier | 44c693d8-80bb-11eb-b4bb-0242c0a8443c | 192.168.68.60 | 3306 | ONLINE | PRIMARY | 8.0.22 | | group_replication_applier | 9d5c3e3e-80bc-11eb-a0b2-0242c0a8443e | 192.168.68.62 | 3306 | ONLINE | SECONDARY | 8.0.22 | +---------------------------+--------------------------------------+---------------+-------------+--------------+-------------+----------------+ 3 rows in set (0.05 sec)
You can see ,3 Node status is online, And the master node is 192.168.68.60, Only the master node can write , Other nodes are read-only ,MGR Single master mode built successfully .
2.9、 Test synchronization
Execute the following command on the master node , Then query on other nodes :
create database lhrdb; CREATE TABLE lhrdb.`tb1` ( `id` int(11) NOT NULL AUTO_INCREMENT, `hostname` varchar(100) DEFAULT NULL, `server_id` varchar(100) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; insert into lhrdb.tb1(hostname,server_id) select @@hostname,@@server_id; select * from lhrdb.tb1; -- 3 Query out the same value nodes MySQL [(none)]> select * from lhrdb.tb1; +----+----------+-----------+ | id | hostname | server_id | +----+----------+-----------+ | 1 | lhrmgr60 | 802233060 | +----+----------+-----------+ 1 row in set (0.05 sec)
3、 ... and 、 build Consul Server colony
3.1、 apply 3 platform Consul Server host
docker rm -f lhrconsulmgr66 lhrconsulmgr67 lhrconsulmgr68 docker run -d --name lhrconsulmgr66 -h lhrconsulmgr66 \ --net=mhalhr --ip 192.168.68.66 \ -p 8566:8500 \ -v /sys/fs/cgroup:/sys/fs/cgroup \ --privileged=true lhrbest/lhrcentos76:8.0 \ /usr/sbin/init docker run -d --name lhrconsulmgr67 -h lhrconsulmgr67 \ --net=mhalhr --ip 192.168.68.67 \ -p 8567:8500 \ --privileged=true lhrbest/lhrcentos76:8.0 \ /usr/sbin/init docker run -d --name lhrconsulmgr68 -h lhrconsulmgr68 \ --net=mhalhr --ip 192.168.68.68 \ -p 8568:8500 \ --privileged=true lhrbest/lhrcentos76:8.0 \ /usr/sbin/init docker cp consul_1.9.4_linux_amd64.zip lhrconsulmgr66:/ docker cp consul_1.9.4_linux_amd64.zip lhrconsulmgr67:/ docker cp consul_1.9.4_linux_amd64.zip lhrconsulmgr68:/ docker network connect bridge lhrconsulmgr66 docker network connect bridge lhrconsulmgr67 docker network connect bridge lhrconsulmgr68 docker restart lhrconsulmgr66 lhrconsulmgr67 lhrconsulmgr68
3.2、 install Consul Server
docker exec -it lhrconsulmgr66 bash docker exec -it lhrconsulmgr67 bash docker exec -it lhrconsulmgr68 bash unzip consul_1.9.4_linux_amd64.zip mv consul /usr/local/bin/ mkdir -p /consul/logs/ -- 66 node start-up nohup consul agent -server -bootstrap-expect 3 -bind=192.168.68.66 -client=0.0.0.0 -data-dir=/consul/data \ -node=n66 -ui >> /consul/logs/consul.log 2>&1 & -- 67 node start-up nohup consul agent -server -bootstrap-expect 3 -bind=192.168.68.67 -client=0.0.0.0 -data-dir=/consul/data \ -node=n67 -ui >> /consul/logs/consul.log 2>&1 & -- 68 node start-up nohup consul agent -server -bootstrap-expect 3 -bind=192.168.68.68 -client=0.0.0.0 -data-dir=/consul/data \ -node=n68 -ui >> /consul/logs/consul.log 2>&1 & -- 67 and 68 Nodes join the cluster consul join 192.168.68.66 -- Querying cluster status [[email protected] /]# consul members Node Address Status Type Build Protocol DC Segment n66 192.168.68.66:8301 alive server 1.9.4 2 dc1 <all> n67 192.168.68.67:8301 alive server 1.9.4 2 dc1 <all> n68 192.168.68.68:8301 alive server 1.9.4 2 dc1 <all> [[email protected] /]# consul operator raft list-peers Node ID Address State Voter RaftProtocol n66 719e2a32-5c4b-bd0f-35ef-9ac11a8e79e0 192.168.68.66:8300 leader true 3 n67 f80e2542-3d30-ca5a-af79-08879723c882 192.168.68.67:8300 follower true 3 n68 43520b16-1b65-7d22-4172-6c2822070a44 192.168.68.68:8300 follower true 3
For the convenience of subsequent startup , We use parameter files :
-- 66 node , Other nodes are modified node_name、advertise_addr and bind_addr that will do :
mkdir -p /etc/consul.d/
pkill -9 consul
cat > /etc/consul.d/server.json <<"EOF"
{
"data_dir": "/consul/data",
"datacenter": "dc1",
"node_name": "n66",
"enable_syslog": true,
"log_level": "INFO",
"server": true,
"advertise_addr":"192.168.68.66",
"bootstrap_expect": 3,
"bind_addr": "192.168.68.66",
"client_addr": "0.0.0.0",
"retry_join": ["192.168.68.66","192.168.68.67","192.168.68.68"],
"retry_interval": "10s",
"rejoin_after_leave": true,
"start_join": ["192.168.68.66","192.168.68.67","192.168.68.68"] ,
"ui": true
}
EOF
nohup consul agent -config-dir=/etc/consul.d > /consul/consul.log &3.3、web visit
http://192.168.66.35:8566
http://192.168.66.35:8567
http://192.168.66.35:8568
Four 、 stay MySQL Install on node Consul Client
4.1、 install Consul Client
docker cp consul_1.9.4_linux_amd64.zip mysql8022mgr33060:/
docker cp consul_1.9.4_linux_amd64.zip mysql8022mgr33061:/
docker cp consul_1.9.4_linux_amd64.zip mysql8022mgr33062:/
docker exec -it mysql8022mgr33060 bash
unzip consul_1.9.4_linux_amd64.zip
mv consul /usr/local/bin/
mkdir -p /consul/logs/
-- mgr60 The configuration file , Other nodes are modified bind_addr and node_name that will do
mkdir -p /etc/consul.d/
cat > /etc/consul.d/client.json <<"EOF"
{
"data_dir": "/data/consul",
"enable_script_checks": true,
"bind_addr": "192.168.68.60",
"retry_join": ["192.168.68.66","192.168.68.67","192.168.68.68"],
"retry_interval": "10s",
"rejoin_after_leave": true,
"start_join": ["192.168.68.66","192.168.68.67","192.168.68.68"] ,
"node_name": "MGR60"
}
EOF
nohup consul agent -config-dir=/etc/consul.d > /consul/consul.log &State of the query :
[[email protected] /]# consul members Node Address Status Type Build Protocol DC Segment n66 192.168.68.66:8301 alive server 1.9.4 2 dc1 <all> n67 192.168.68.67:8301 alive server 1.9.4 2 dc1 <all> n68 192.168.68.68:8301 alive server 1.9.4 2 dc1 <all> MGR60 192.168.68.60:8301 alive client 1.9.4 2 dc1 <default> MGR61 192.168.68.61:8301 alive client 1.9.4 2 dc1 <default> MGR62 192.168.68.62:8301 alive client 1.9.4 2 dc1 <default> [[email protected] /]# consul operator raft list-peers Node ID Address State Voter RaftProtocol n66 719e2a32-5c4b-bd0f-35ef-9ac11a8e79e0 192.168.68.66:8300 follower true 3 n67 f80e2542-3d30-ca5a-af79-08879723c882 192.168.68.67:8300 leader true 3 n68 43520b16-1b65-7d22-4172-6c2822070a44 192.168.68.68:8300 follower true 3
4.2、 stay Consul client Of 3 Write tests on nodes primay Scripting and detection slave Script
-- Test script 【master】
mkdir -p /data/consul/shell/
cat > /data/consul/shell/check_mysql_mgr_master.sh <<"EOF"
#!/bin/bash
port=3306
user="root"
passwod="lhr"
comm="/usr/bin/mysql -u$user -hlocalhost -P $port -p$passwod"
value=`$comm -Nse "select 1"`
primary_member=`$comm -Nse "select variable_value from performance_schema.global_status WHERE VARIABLE_NAME= 'group_replication_primary_member'"`
server_uuid=`$comm -Nse "select variable_value from performance_schema.global_variables where VARIABLE_NAME='server_uuid';"`
# Judge MySQL Survival
if [ -z $value ]
then
echo "mysql $port is down....."
exit 2
fi
# Judge node state , Survival
node_state=`$comm -Nse "select MEMBER_STATE from performance_schema.replication_group_members where MEMBER_ID='$server_uuid'"`
if [ $node_state != "ONLINE" ]
then
echo "MySQL $port state is not online...."
exit 2
fi
# Judge whether it is the master node
if [[ $server_uuid == $primary_member ]]
then
echo "MySQL $port Instance is master ........"
exit 0
else
echo "MySQL $port Instance is slave ........"
exit 2
fi
EOF
-- Test script 【slave】
cat > /data/consul/shell/check_mysql_mgr_slave.sh <<"EOF"
#!/bin/bash
port=3306
user="root"
passwod="lhr"
comm="/usr/bin/mysql -u$user -hlocalhost -P $port -p$passwod"
value=`$comm -Nse "select 1"`
primary_member=`$comm -Nse "select variable_value from performance_schema.global_status WHERE VARIABLE_NAME= 'group_replication_primary_member'"`
server_uuid=`$comm -Nse "select variable_value from performance_schema.global_variables where VARIABLE_NAME='server_uuid';"`
# Judge mysql Survival
if [ -z $value ]
then
echo "mysql $port is down....."
exit 2
fi
# Judge node state
node_state=`$comm -Nse "select MEMBER_STATE from performance_schema.replication_group_members where MEMBER_ID='$server_uuid'"`
if [ $node_state != "ONLINE" ]
then
echo "MySQL $port state is not online...."
exit 2
fi
# Judge whether it is the master node
if [[ $server_uuid != $primary_member ]]
then
echo "MySQL $port Instance is slave ........"
exit 0
else
node_num=`$comm -Nse "select count(*) from performance_schema.replication_group_members"`
# Judge if there are no slave nodes , The master node also registers the slave role service .
if [ $node_num -eq 1 ]
then
echo "MySQL $port Instance is slave ........"
exit 0
else
echo "MySQL $port Instance is master ........"
exit 2
fi
fi
EOFEmpowerment :
chmod +x /data/consul/shell/check_mysql_mgr_master.sh chmod +x /data/consul/shell/check_mysql_mgr_slave.sh
4.3、service To configure
Official website configuration reference :https://www.consul.io/docs/discovery/services
stay 3 platform MySQL All nodes need to be operated , Pay attention to revision address The address is the local address :
cat > /etc/consul.d/rw-mysql-mgr-server-lhr.json <<"EOF"
{
"service":
{
"name": "rw-mysql-mgr-server-lhr",
"tags": ["MGR-Master"],
"address": "192.168.68.60",
"port": 3306,
"check":
{
"args": ["/data/consul/shell/check_mysql_mgr_master.sh"],
"interval": "5s"
}
}
}
EOF
cat > /etc/consul.d/r-mysql-mgr-server-lhr.json <<"EOF"
{
"service": [
{
"name": "r-mysql-mgr-server-lhr",
"tags": ["MGR-Slave"],
"address": "192.168.68.60",
"port": 3306,
"check":
{
"args": ["/data/consul/shell/check_mysql_mgr_slave.sh"],
"interval": "5s"
}
}
]
}
EOF
-- Check whether the configuration file is normal
consul validate /etc/consul.d/
-- Reload the configuration file
consul reload
-- Check the configuration results
[[email protected] ~]# consul catalog services
consul
r-mysql-mgr-server-lhr
rw-mysql-mgr-server-lhrBe careful : Because each one mysql server both master、slave Test script 、 and mysql server Can only be master perhaps slave、 So there are failed tests ,master Only one test succeeded ,slave There was only one failure of the test .
All services :
Main library connection :
Connect from library :
4.4、service test
yum install -y bind-utils bind bind-chroot dnsmasq dig @192.168.68.66 -p 8600 r-mysql-mgr-server-lhr.service.consul a dig @192.168.68.66 -p 8600 rw-mysql-mgr-server-lhr.service.consul a dig @127.0.0.1 -p 8600 MGR60.node.consul dig @127.0.0.1 -p 8600 MGR61.node.consul dig @127.0.0.1 -p 8600 MGR62.node.consul
test result :
[[email protected] ~]# dig @192.168.68.66 -p 8600 r-mysql-mgr-server-lhr.service.consul a ; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.4 <<>> @192.168.68.66 -p 8600 r-mysql-mgr-server-lhr.service.consul a ; (1 server found) ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 7862 ;; flags: qr aa rd; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1 ;; WARNING: recursion requested but not available ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;r-mysql-mgr-server-lhr.service.consul. IN A ;; ANSWER SECTION: r-mysql-mgr-server-lhr.service.consul. 0 IN A 192.168.68.62 r-mysql-mgr-server-lhr.service.consul. 0 IN A 192.168.68.61 ;; Query time: 0 msec ;; SERVER: 192.168.68.66#8600(192.168.68.66) ;; WHEN: Wed Mar 10 10:52:12 CST 2021 ;; MSG SIZE rcvd: 98 [[email protected] ~]# dig @192.168.68.66 -p 8600 rw-mysql-mgr-server-lhr.service.consul A ; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.4 <<>> @192.168.68.66 -p 8600 rw-mysql-mgr-server-lhr.service.consul a ; (1 server found) ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 13129 ;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1 ;; WARNING: recursion requested but not available ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;rw-mysql-mgr-server-lhr.service.consul. IN A ;; ANSWER SECTION: rw-mysql-mgr-server-lhr.service.consul. 0 IN A 192.168.68.60 ;; Query time: 0 msec ;; SERVER: 192.168.68.66#8600(192.168.68.66) ;; WHEN: Wed Mar 10 10:52:17 CST 2021 ;; MSG SIZE rcvd: 83
You can see ,r-mysql-mgr-server-lhr.service.consul The resolved address is 192.168.68.61 and 192.168.68.62, namely 2 A slave ; and rw-mysql-mgr-server-lhr.service.consul The resolved address is 192.168.68.60, The main library .
5、 ... and 、 To configure DNS Domain name resolution
App Configure domain name server to resolve consul Suffix domain name ,DNS Analysis and jump , There are several options :
Original Intranet dns The server , Do domain name forwarding ,consul Suffix , All go to consul server On dns All jump to consul DNS Server , Not consul Suffix , Use recursors Property jump to original DNS Server dnsmaq turn : server=/consul/192.168.X.X#8600 analysis consul Suffix Use BIND To configure DNS The server
Here are just a few dnsmasq This program , Please refer to the official document for more methods :https://learn.hashicorp.com/tutorials/consul/dns-forwarding
5.1、 Use dnsmasq To configure the DNS analysis
stay 192.168.66.69 On the configuration dnsmasq.
docker rm -f lhrconsuldns69 docker run -d --name lhrconsuldns69 -h lhrconsuldns69 \ --net=mhalhr --ip 192.168.68.69 \ -p 53:53 \ -v /sys/fs/cgroup:/sys/fs/cgroup \ --privileged=true lhrbest/lhrcentos76:8.0 \ /usr/sbin/init docker network connect bridge lhrconsuldns69 docker exec -it lhrconsuldns69 bash yum install -y bind-utils bind bind-chroot dnsmasq echo " server=/consul/192.168.68.66#8600 server=/consul/192.168.68.67#8600 server=/consul/192.168.68.68#8600 " > /etc/dnsmasq.d/10-consul echo " server=114.114.114.114 server=8.8.8.8 server=223.5.5.5 " >> /etc/dnsmasq.conf -- Add to all machines , Including the machine 、3 individual client、3 individual server End etc. echo "nameserver 192.168.68.69" > /etc/resolv.conf systemctl enable dnsmasq systemctl restart dnsmasq systemctl status dnsmasq dig @192.168.68.69 -p 53 r-mysql-mgr-server-lhr.service.consul a dig @192.168.68.69 -p 53 rw-mysql-mgr-server-lhr.service.consul a nslookup r-mysql-mgr-server-lhr.service.consul nslookup rw-mysql-mgr-server-lhr.service.consul ping r-mysql-mgr-server-lhr.service.consul -c 4 ping rw-mysql-mgr-server-lhr.service.consul -c 4
result :
[[email protected] /]# nslookup r-mysql-mgr-server-lhr.service.consul nslookup rw-mysql-mgr-server-lhr.service.consulServer: 192.168.68.69 Address: 192.168.68.69#53 Name: r-mysql-mgr-server-lhr.service.consul Address: 192.168.68.62 Name: r-mysql-mgr-server-lhr.service.consul Address: 192.168.68.61 [[email protected] /]# nslookup rw-mysql-mgr-server-lhr.service.consul Server: 192.168.68.69 Address: 192.168.68.69#53 Name: rw-mysql-mgr-server-lhr.service.consul Address: 192.168.68.60 [[email protected] /]# [[email protected] /]# ping r-mysql-mgr-server-lhr.service.consul -c 4 PING r-mysql-mgr-server-lhr.service.consul (192.168.68.61) 56(84) bytes of data. 64 bytes from 192.168.68.61 (192.168.68.61): icmp_seq=1 ttl=64 time=0.083 ms 64 bytes from 192.168.68.61 (192.168.68.61): icmp_seq=2 ttl=64 time=0.065 ms 64 bytes from 192.168.68.61 (192.168.68.61): icmp_seq=3 ttl=64 time=0.108 ms 64 bytes from 192.168.68.61 (192.168.68.61): icmp_seq=4 ttl=64 time=0.084 ms --- r-mysql-mgr-server-lhr.service.consul ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 7022ms rtt min/avg/max/mdev = 0.065/0.085/0.108/0.015 ms [[email protected] /]# [[email protected] /]# ping rw-mysql-mgr-server-lhr.service.consul -c 4 PING rw-mysql-mgr-server-lhr.service.consul (192.168.68.60) 56(84) bytes of data. 64 bytes from lhrmgr60 (192.168.68.60): icmp_seq=1 ttl=64 time=0.058 ms 64 bytes from lhrmgr60 (192.168.68.60): icmp_seq=2 ttl=64 time=0.070 ms 64 bytes from lhrmgr60 (192.168.68.60): icmp_seq=3 ttl=64 time=0.091 ms 64 bytes from lhrmgr60 (192.168.68.60): icmp_seq=4 ttl=64 time=0.071 ms --- rw-mysql-mgr-server-lhr.service.consul ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 2999ms rtt min/avg/max/mdev = 0.058/0.072/0.091/0.014 ms [[email protected] /]# [[email protected] /]# dig @192.168.68.69 -p 53 r-mysql-mgr-server-lhr.service.consul a ; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.4 <<>> @192.168.68.69 -p 53 r-mysql-mgr-server-lhr.service.consul a ; (1 server found) ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 48770 ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;r-mysql-mgr-server-lhr.service.consul. IN A ;; ANSWER SECTION: r-mysql-mgr-server-lhr.service.consul. 0 IN A 192.168.68.62 r-mysql-mgr-server-lhr.service.consul. 0 IN A 192.168.68.61 ;; Query time: 1 msec ;; SERVER: 192.168.68.69#53(192.168.68.69) ;; WHEN: Thu Mar 11 10:30:03 CST 2021 ;; MSG SIZE rcvd: 98 [[email protected] /]# [[email protected] /]# dig @192.168.68.69 -p 53 rw-mysql-mgr-server-lhr.service.consul a ; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.4 <<>> @192.168.68.69 -p 53 rw-mysql-mgr-server-lhr.service.consul a ; (1 server found) ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 47378 ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;rw-mysql-mgr-server-lhr.service.consul. IN A ;; ANSWER SECTION: rw-mysql-mgr-server-lhr.service.consul. 0 IN A 192.168.68.60 ;; Query time: 1 msec ;; SERVER: 192.168.68.69#53(192.168.68.69) ;; WHEN: Thu Mar 11 10:30:13 CST 2021 ;; MSG SIZE rcvd: 83
5.2、MySQL Connect through a domain name
[[email protected] /]# mysql -uroot -plhr -hrw-mysql-mgr-server-lhr.service.consul mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 53753 Server version: 8.0.22 MySQL Community Server - GPL Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> SELECT * FROM performance_schema.replication_group_members; +---------------------------+--------------------------------------+---------------+-------------+--------------+-------------+----------------+ | CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION | +---------------------------+--------------------------------------+---------------+-------------+--------------+-------------+----------------+ | group_replication_applier | 408acdb5-80bc-11eb-97a7-0242c0a8443d | 192.168.68.61 | 3306 | ONLINE | SECONDARY | 8.0.22 | | group_replication_applier | 44c693d8-80bb-11eb-b4bb-0242c0a8443c | 192.168.68.60 | 3306 | ONLINE | PRIMARY | 8.0.22 | | group_replication_applier | 9d5c3e3e-80bc-11eb-a0b2-0242c0a8443e | 192.168.68.62 | 3306 | ONLINE | SECONDARY | 8.0.22 | +---------------------------+--------------------------------------+---------------+-------------+--------------+-------------+----------------+ 3 rows in set (0.00 sec) mysql> select @@hostname,@@report_host; +------------+---------------+ | @@hostname | @@report_host | +------------+---------------+ | lhrmgr60 | 192.168.68.60 | +------------+---------------+ 1 row in set (0.00 sec) mysql> exit Bye [[email protected] /]# mysql -uroot -plhr -hr-mysql-mgr-server-lhr.service.consul mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 47616 Server version: 8.0.22 MySQL Community Server - GPL Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> SELECT * FROM performance_schema.replication_group_members; +---------------------------+--------------------------------------+---------------+-------------+--------------+-------------+----------------+ | CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION | +---------------------------+--------------------------------------+---------------+-------------+--------------+-------------+----------------+ | group_replication_applier | 408acdb5-80bc-11eb-97a7-0242c0a8443d | 192.168.68.61 | 3306 | ONLINE | SECONDARY | 8.0.22 | | group_replication_applier | 44c693d8-80bb-11eb-b4bb-0242c0a8443c | 192.168.68.60 | 3306 | ONLINE | PRIMARY | 8.0.22 | | group_replication_applier | 9d5c3e3e-80bc-11eb-a0b2-0242c0a8443e | 192.168.68.62 | 3306 | ONLINE | SECONDARY | 8.0.22 | +---------------------------+--------------------------------------+---------------+-------------+--------------+-------------+----------------+ 3 rows in set (0.00 sec) mysql> create database lhrdb2; ERROR 1290 (HY000): The MySQL server is running with the --super-read-only option so it cannot execute this statement
6、 ... and 、 Test high availability
6.1、 test Consul Server High availability
As shown in the figure below , at present Consul Server Yes 3 Taiwan machine , Form a cluster , If any of these machines goes down , Can't affect service Parsing :
The current state :
[[email protected] /]# consul members Node Address Status Type Build Protocol DC Segment n66 192.168.68.66:8301 alive server 1.9.4 2 dc1 <all> n67 192.168.68.67:8301 alive server 1.9.4 2 dc1 <all> n68 192.168.68.68:8301 alive server 1.9.4 2 dc1 <all> MGR60 192.168.68.60:8301 alive client 1.9.4 2 dc1 <default> MGR61 192.168.68.61:8301 alive client 1.9.4 2 dc1 <default> MGR62 192.168.68.62:8301 alive client 1.9.4 2 dc1 <default> [[email protected] /]# consul operator raft list-peers Node ID Address State Voter RaftProtocol n68 43520b16-1b65-7d22-4172-6c2822070a44 192.168.68.68:8300 follower true 3 n66 ca78760f-2f7e-f350-4e47-2db17e8719fe 192.168.68.66:8300 leader true 3 n67 f80e2542-3d30-ca5a-af79-08879723c882 192.168.68.67:8300 follower true 3
Next , hold n66、n67 Shut down the machine :
docker stop lhrconsulmgr66 lhrconsulmgr67
Querying cluster status :
[[email protected] /]# consul members Node Address Status Type Build Protocol DC Segment n66 192.168.68.66:8301 failed server 1.9.4 2 dc1 <all> n67 192.168.68.67:8301 failed server 1.9.4 2 dc1 <all> n68 192.168.68.68:8301 alive server 1.9.4 2 dc1 <all> MGR60 192.168.68.60:8301 alive client 1.9.4 2 dc1 <default> MGR61 192.168.68.61:8301 alive client 1.9.4 2 dc1 <default> MGR62 192.168.68.62:8301 alive client 1.9.4 2 dc1 <default> [[email protected] /]# consul operator raft list-peers Error getting peers: Failed to retrieve raft configuration: Unexpected response code: 500 (rpc error making call: No cluster leader) [[email protected] /]# consul catalog services Error listing services: Unexpected response code: 500 (rpc error making call: No cluster leader)
Check whether the domain name is in normal use :
dig @192.168.68.69 -p 53 r-mysql-mgr-server-lhr.service.consul a dig @192.168.68.69 -p 53 rw-mysql-mgr-server-lhr.service.consul a nslookup r-mysql-mgr-server-lhr.service.consul nslookup rw-mysql-mgr-server-lhr.service.consul ping r-mysql-mgr-server-lhr.service.consul -c 4 ping rw-mysql-mgr-server-lhr.service.consul -c 4
Verification results :
[[email protected] /]# nslookup r-mysql-mgr-server-lhr.service.consul Server: 192.168.68.69 Address: 192.168.68.69#53 Name: r-mysql-mgr-server-lhr.service.consul Address: 192.168.68.61 Name: r-mysql-mgr-server-lhr.service.consul Address: 192.168.68.62 [[email protected] /]# [[email protected] /]# nslookup rw-mysql-mgr-server-lhr.service.consul Server: 192.168.68.69 Address: 192.168.68.69#53 Name: rw-mysql-mgr-server-lhr.service.consul Address: 192.168.68.60
You can see ,Consul Server The high availability of the cluster is working properly .
6.2、 test MySQL Load balancing and read-write separation of
First test read load balancing :
[[email protected] /]# for i in $(seq 1 10); do mysql -uroot -plhr -hr-mysql-mgr-server-lhr.service.consul -P3306 \ > -e 'select @@server_id;'; done | egrep '[0-9]' mysql: [Warning] Using a password on the command line interface can be insecure. 802233061 mysql: [Warning] Using a password on the command line interface can be insecure. 802233061 mysql: [Warning] Using a password on the command line interface can be insecure. 802233062 mysql: [Warning] Using a password on the command line interface can be insecure. 802233062 mysql: [Warning] Using a password on the command line interface can be insecure. 802233062 mysql: [Warning] Using a password on the command line interface can be insecure. 802233061 mysql: [Warning] Using a password on the command line interface can be insecure. 802233061 mysql: [Warning] Using a password on the command line interface can be insecure. 802233061 mysql: [Warning] Using a password on the command line interface can be insecure. 802233062 mysql: [Warning] Using a password on the command line interface can be insecure. 802233061
You can see , Read operations are assigned to 61 and 62 On a host .
Read write separation is connected through different host domain names , It's been tested before , therefore , No more testing here .
6.3、 test MGR High availability
at present 192.168.68.60 Give priority to the library ,192.168.68.61 and 192.168.68.62 For from the library , therefore , We put 192.168.68.60 To turn it off , And then connect through the domain name , Check whether the connection is normal .
[[email protected] /]# mysql -uroot -plhr -hrw-mysql-mgr-server-lhr.service.consul -P3306 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 54447 Server version: 8.0.22 MySQL Community Server - GPL Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> select @@hostname,@@report_host; +------------+---------------+ | @@hostname | @@report_host | +------------+---------------+ | lhrmgr60 | 192.168.68.60 | +------------+---------------+ 1 row in set (0.00 sec) mysql> SELECT * FROM performance_schema.replication_group_members; +---------------------------+--------------------------------------+---------------+-------------+--------------+-------------+----------------+ | CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION | +---------------------------+--------------------------------------+---------------+-------------+--------------+-------------+----------------+ | group_replication_applier | 408acdb5-80bc-11eb-97a7-0242c0a8443d | 192.168.68.61 | 3306 | ONLINE | SECONDARY | 8.0.22 | | group_replication_applier | 44c693d8-80bb-11eb-b4bb-0242c0a8443c | 192.168.68.60 | 3306 | ONLINE | PRIMARY | 8.0.22 | | group_replication_applier | 9d5c3e3e-80bc-11eb-a0b2-0242c0a8443e | 192.168.68.62 | 3306 | ONLINE | SECONDARY | 8.0.22 | +---------------------------+--------------------------------------+---------------+-------------+--------------+-------------+----------------+ 3 rows in set (0.01 sec) mysql> shutdown ; Query OK, 0 rows affected (0.00 sec) mysql> select @@hostname,@@report_host; ERROR 2013 (HY000): Lost connection to MySQL server during query mysql> select @@hostname,@@report_host; +------------+---------------+ | @@hostname | @@report_host | +------------+---------------+ | lhrmgr61 | 192.168.68.61 | +------------+---------------+ 1 row in set (0.00 sec) mysql> SELECT * FROM performance_schema.replication_group_members; +---------------------------+--------------------------------------+---------------+-------------+--------------+-------------+----------------+ | CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION | +---------------------------+--------------------------------------+---------------+-------------+--------------+-------------+----------------+ | group_replication_applier | 408acdb5-80bc-11eb-97a7-0242c0a8443d | 192.168.68.61 | 3306 | ONLINE | PRIMARY | 8.0.22 | | group_replication_applier | 9d5c3e3e-80bc-11eb-a0b2-0242c0a8443e | 192.168.68.62 | 3306 | ONLINE | SECONDARY | 8.0.22 | +---------------------------+--------------------------------------+---------------+-------------+--------------+-------------+----------------+ 2 rows in set (0.00 sec) [[email protected] /]# nslookup rw-mysql-mgr-server-lhr.service.consul Server: 192.168.68.69 Address: 192.168.68.69#53 Name: rw-mysql-mgr-server-lhr.service.consul Address: 192.168.68.61 [[email protected] /]# nslookup r-mysql-mgr-server-lhr.service.consul Server: 192.168.68.69 Address: 192.168.68.69#53 Name: r-mysql-mgr-server-lhr.service.consul Address: 192.168.68.62
You can see ,MGR The high availability of is also running normally .
About Me
● The author of this article : Wheat seedling , Some content is organized from the network , In case of infringement, please contact wheat seedling to delete
边栏推荐
- 主链系统发展解析
- A tutorial on how the zblog system obtains user related information based on user ID
- ClassNotFoundException v/s NoClassDefFoundError
- As for IOT safety, 20 CSOs from major manufacturers say
- The RTSP video image intelligent analysis platform easynvr cascades to the superior platform through the national standard for playback optimization
- Is CICC securities reliable? Is it legal? Is it safe to open a stock account?
- Bypass kernel function pointer integrity check
- The problem is as big as the middle stage
- ## Kubernetes集群中流量暴露的几种方案 Kubernetes集群中流量暴露的几种方案
- The TKE cluster node reports an error when executing kubectl
猜你喜欢

MySQL learning -- table structure of SQL test questions

Why do you develop middleware when you are young? "You can choose your own way"
![[leetcode108] convert an ordered array into a binary search tree (medium order traversal)](/img/e1/0fac59a531040d74fd7531e2840eb5.jpg)
[leetcode108] convert an ordered array into a binary search tree (medium order traversal)

Daily algorithm & interview questions, 28 days of special training in large factories - the 15th day (string)
随机推荐
[play with Tencent cloud] & lt; trtc-room&gt; Applet component usage
liver failure! My friend made a programming navigation website!
The TKE cluster node reports an error when executing kubectl
Cloud development environment to create a five-star development experience
[play with Tencent cloud] check 9 popular Tencent cloud products
zblog判断某个插件是否安装启用的内置函数代码
[tke] enable CPU static management strategy
Five steps to effectively monitor network traffic
中金证券靠谱吗?是否合法?开股票账户安全吗?
Bypass kernel function pointer integrity check
未来银行需要用明天的思维,来思考今天架构
Automatically refresh CDN cache in real time based on cos log
Easycvr, an urban intelligent video monitoring image analysis platform, plays national standard equipment videos and captures unstable packets for troubleshooting
Cloud native monitoring via blackbox_ Exporter monitoring website
Low education without food? As an old Android rookie in the past six years, I was the most difficult one
Daily algorithm & interview questions, 28 days of special training in large factories - the 15th day (string)
[web] what happens after entering the URL from the address bar?
网站SEO排名越做越差是什么原因造成的?
Data acquisition and transmission instrument reservoir dam safety monitoring
Kubernetes 1.20.5 helm installation Jenkins