Linux下mysql8.0.16MHA搭建(三)

链接: https://pan.baidu.com/s/1Gvom1VzBH5jAfheszIOO_g?pwd=m596 提取码: m596

链接: https://pan.baidu.com/s/1Gs1VjOliPRqx0LYOJmgDfg?pwd=3vy3 提取码: 3vy3

单节点、主从参考前两篇:

Linux下mysql8.0.16单节点的搭建(一)

mhaLinux下mysql8.0.16一主两从搭建(二)

 三台服务器之间的免密登录

master:192.168.200.57(node)

slave:192.168.200.58(manager)

slave:192.168.200.59(node)

 (优先设置三台服务器之间的免密登录)

1、进入mysql服务,为所有节点创建mha管理用户:

su - mysql
mysql -u root -p 输入密码
create user 'mhaadmin'@'192.168.200.%' identified with mysql_native_password by 'xxxxx';

 2、为所建用户赋权:

grant all on *.* to 'mhaadmin'@'192.168.200.%';

Linux下mysql8.0.16MHA搭建(三)_第1张图片

 3、上传安装包依赖包,三台服务器均上传node文件夹,选择一台服务器上传manager文件夹,每台服务器需另上传mha4mysql-manager-0.58-0.el7.centos.noarch.rpm和mha4mysql-node-0.58-0.el7.centos.noarch.rpm两个安装包。

(选择192.168.200.58位manager节点,57和59为node节点。)

跨服务器之间的复制,例:

复制文件:
scp /mysqlsoft/mha4mysql-* [email protected]:/mysqlsoft/
复制文件夹:
scp -r /mysqlsoft/mha4mysql-* [email protected]:/mysqlsoft/

4、进入相关目录,安装对应目录下的相关包(按照顺序执行):

rpm -ivh /mysqlsoft/node/perl-* (所有节点)
rpm -ivh /mysqlsoft/mha4mysql-node-0.58-0.el7.centos.noarch.rpm (所有节点)
rpm -ivh /mysqlsoft/manager/perl-* (slave节点(manager))
rpm -ivh /mysqlsoft/mha4mysql-manager-0.58-0.el7.centos.noarch.rpm (slave节点(manager))

5、修改配置文件vi /etc/my.cnf:

master节点:
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
server-id = 1
port = 3306
basedir = /mysqlsoft/mysql
datadir = /mysqlsoft/mysql/data
pid-file = /mysqlsoft/mysql/mysql.pid
socket = /tmp/mysql.sock
binlog_format = row
binlog_row_image=full
expire_logs_days=1 #修改binlog的保留时间
#skip-grant-tables #设置免密登录
max_connections=1000
max_connect_errors=1000
lower-case-table-names=1 #不区分大小
log_bin_trust_function_creators=1
sql_mode=only_full_group_by
character_set_server=utf8
collation_server=utf8_general_ci
wait_timeout = 1800 #阿里云值为7200
lock_wait_timeout = 3600 #阿里云值为31536000
#开启慢日志
slow_query_log = ON
slow_query_log_file =/mysqlsoft/mysql/slow-query.log
long_query_time = 1
binlog_expire_logs_seconds=2592000  #30天,binlog保存时间
log-bin=mysql-bin
skip_name_resolve
max_relay_log_size=1073741824
relay_log=mysql-relay-log
relay_log_purge=ON
relay_log_recovery=ON
relay_log_space_limit=10737418240
[mysqld_safe]
log-error=/mysqlsoft/mysql/log/error.log




slave节点:
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
server-id = 2
port = 3306
basedir = /mysqlsoft/mysql
datadir = /mysqlsoft/mysql/data
pid-file = /mysqlsoft/mysql/mysql.pid
socket = /tmp/mysql.sock
binlog_format = row
binlog_row_image=full
expire_logs_days=1 #修改binlog的保留时间
#skip-grant-tables #设置免密登录
max_connections=1000
max_connect_errors=1000
lower-case-table-names=1 #不区分大小
log_bin_trust_function_creators=1
sql_mode=only_full_group_by
#character_set_server=utf8
#collation_server=utf8_general_ci
wait_timeout = 1800 #阿里云值为7200
lock_wait_timeout = 3600 #阿里云值为31536000
#开启慢日志
slow_query_log = ON
slow_query_log_file =/mysqlsoft/mysql/slow-query.log
long_query_time = 1
binlog_expire_logs_seconds=2592000  #30天,binlog保存时间
log-bin=mysql-bin
read_only = ON
skip_name_resolve
log_slave_updates = 1
max_relay_log_size=1073741824
relay_log=mysql-relay-log
relay_log_purge=ON
relay_log_recovery=ON
relay_log_space_limit=10737418240
[mysqld_safe]
log-error=/mysqlsoft/mysql/log/error.log

6、创建mha相关目录:

#manager
mkdir -p /etc/mha/app1
mkdir -p /mysqlsoft/mha/app1
#node
mkdir -p /etc/mha

7、创建mha配置文件(manager)

[server default]
user=mhaadmin
password=mha123
port=3306
manager_workdir=/etc/mha/app1
manager_log=/etc/mha/manager.log
remote_workdir=/mysqlsoft/mha/app1
ssh_user=root
repl_user=user_slave
repl_password=123456
ping_interval=1
master_binlog_dir=/mysqlsoft/mysql/data
[server1]
hostname=192.168.200.59
ssh_port=22
candidate_master=1
check_repl_delay=0 #忽略relay logs日志的复制延迟
[server2]
hostname=192.168.200.57
ssh_port=22
#candidate_master=1 一共三台服务器,这个要注释掉,不注释有问题

8、软连接(三台服务器都执行)

ln -s /mysqlsoft/mysql/bin/mysqlbinlog  /usr/local/bin/mysqlbinlog

ln -s /mysqlsoft/mysql/bin/mysql  /usr/local/bin/mysql

9、检查ssh

masterha_check_ssh --conf=/etc/mha/mha.cnf

 10、检查mha状态:

masterha_check_repl -conf=/etc/mha/mha.cnf
[root@localhost ~]# masterha_check_repl -conf=/etc/mha/mha.cnf
Tue Aug 15 10:14:41 2023 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue Aug 15 10:14:41 2023 - [info] Reading application default configuration from /etc/mha/mha.cnf..
Tue Aug 15 10:14:41 2023 - [info] Reading server configuration from /etc/mha/mha.cnf..
Tue Aug 15 10:14:41 2023 - [info] MHA::MasterMonitor version 0.58.
Tue Aug 15 10:14:42 2023 - [info] GTID failover mode = 0
Tue Aug 15 10:14:42 2023 - [info] Dead Servers:
Tue Aug 15 10:14:42 2023 - [info] Alive Servers:
Tue Aug 15 10:14:42 2023 - [info]   192.168.200.59(192.168.200.59:3306)
Tue Aug 15 10:14:42 2023 - [info]   192.168.200.57(192.168.200.57:3306)
Tue Aug 15 10:14:42 2023 - [info] Alive Slaves:
Tue Aug 15 10:14:42 2023 - [info]   192.168.200.59(192.168.200.59:3306)  Version=8.0.16 (oldest major version between slaves) log-bin:enabled
Tue Aug 15 10:14:42 2023 - [info]     Replicating from 192.168.200.57(192.168.200.57:3306)
Tue Aug 15 10:14:42 2023 - [info]     Primary candidate for the new Master (candidate_master is set)
Tue Aug 15 10:14:42 2023 - [info] Current Alive Master: 192.168.200.57(192.168.200.57:3306)
Tue Aug 15 10:14:42 2023 - [info] Checking slave configurations..
Tue Aug 15 10:14:42 2023 - [warning]  relay_log_purge=0 is not set on slave 192.168.200.59(192.168.200.59:3306).
Tue Aug 15 10:14:42 2023 - [info] Checking replication filtering settings..
Tue Aug 15 10:14:42 2023 - [info]  binlog_do_db= , binlog_ignore_db=
Tue Aug 15 10:14:42 2023 - [info]  Replication filtering check ok.
Tue Aug 15 10:14:42 2023 - [info] GTID (with auto-pos) is not supported
Tue Aug 15 10:14:42 2023 - [info] Starting SSH connection tests..
Tue Aug 15 10:14:44 2023 - [info] All SSH connection tests passed successfully.
Tue Aug 15 10:14:44 2023 - [info] Checking MHA Node version..
Tue Aug 15 10:14:45 2023 - [info]  Version check ok.
Tue Aug 15 10:14:45 2023 - [info] Checking SSH publickey authentication settings on the current master..
Tue Aug 15 10:14:45 2023 - [info] HealthCheck: SSH to 192.168.200.57 is reachable.
Tue Aug 15 10:14:46 2023 - [info] Master MHA Node version is 0.58.
Tue Aug 15 10:14:46 2023 - [info] Checking recovery script configurations on 192.168.200.57(192.168.200.57:3306)..
Tue Aug 15 10:14:46 2023 - [info]   Executing command: save_binary_logs --command=test --start_pos=4 --binlog_dir=/mysqlsoft/mysql/data --output_file=/mysqlsoft/mha/app1/save_binary_logs_test --manager_version=0.58 --start_file=mysql-bin.000023
Tue Aug 15 10:14:46 2023 - [info]   Connecting to [email protected](192.168.200.57:22)..
  Creating /mysqlsoft/mha/app1 if not exists..    ok.
  Checking output directory is accessible or not..
   ok.
  Binlog found at /mysqlsoft/mysql/data, up to mysql-bin.000023
Tue Aug 15 10:14:47 2023 - [info] Binlog setting check done.
Tue Aug 15 10:14:47 2023 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..
Tue Aug 15 10:14:47 2023 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='mhaadmin' --slave_host=192.168.200.59 --slave_ip=192.168.200.59 --slave_port=3306 --workdir=/mysqlsoft/mha/app1 --target_version=8.0.16 --manager_version=0.58 --relay_dir=/mysqlsoft/mysql/data --current_relay_log=mysql-relay-log.000028  --slave_pass=xxx
Tue Aug 15 10:14:47 2023 - [info]   Connecting to [email protected](192.168.200.59:22)..
Creating directory /mysqlsoft/mha/app1.. done.
  Checking slave recovery environment settings..
    Relay log found at /mysqlsoft/mysql/data, up to mysql-relay-log.000028
    Temporary relay log file is /mysqlsoft/mysql/data/mysql-relay-log.000028
    Checking if super_read_only is defined and turned on.. not present or turned off, ignoring.
    Testing mysql connection and privileges..
mysql: [Warning] Using a password on the command line interface can be insecure.
 done.
    Testing mysqlbinlog output.. done.
    Cleaning up test file(s).. done.
Tue Aug 15 10:14:47 2023 - [info] Slaves settings check done.
Tue Aug 15 10:14:47 2023 - [info]
192.168.200.57(192.168.200.57:3306) (current master)
 +--192.168.200.59(192.168.200.59:3306)

Tue Aug 15 10:14:47 2023 - [info] Checking replication health on 192.168.200.59..
Tue Aug 15 10:14:47 2023 - [info]  ok.
Tue Aug 15 10:14:47 2023 - [warning] master_ip_failover_script is not defined.
Tue Aug 15 10:14:47 2023 - [warning] shutdown_script is not defined.
Tue Aug 15 10:14:47 2023 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.

11、无问题则启动并记录日志(只在manager节点操作)

nohup masterha_manager -conf=/etc/mha/mha.cnf &> /etc/mha/manager.log &

12、查看mha manager监控:

masterha_check_status --conf=/etc/mha/mha.cnf

13、手动测试切换(所有提示均选择”yes“):

masterha_master_switch --conf=/etc/mha/mha.cnf --master_state=alive --new_master_host=192.168.200.59 --new_master_port=3306 --orig_master_is_new_slave
[root@localhost ~]# masterha_master_switch --conf=/etc/mha/mha.cnf --master_state=alive --new_master_host=192.168.200.59 --new_master_port=3306 --orig_master_is_new_slave
Tue Aug 15 10:28:19 2023 - [info] MHA::MasterRotate version 0.58.
Tue Aug 15 10:28:19 2023 - [info] Starting online master switch..
Tue Aug 15 10:28:19 2023 - [info]
Tue Aug 15 10:28:19 2023 - [info] * Phase 1: Configuration Check Phase..
Tue Aug 15 10:28:19 2023 - [info]
Tue Aug 15 10:28:19 2023 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue Aug 15 10:28:19 2023 - [info] Reading application default configuration from /etc/mha/mha.cnf..
Tue Aug 15 10:28:19 2023 - [info] Reading server configuration from /etc/mha/mha.cnf..
Tue Aug 15 10:28:20 2023 - [info] GTID failover mode = 0
Tue Aug 15 10:28:20 2023 - [info] Current Alive Master: 192.168.200.57(192.168.200.57:3306)
Tue Aug 15 10:28:20 2023 - [info] Alive Slaves:
Tue Aug 15 10:28:20 2023 - [info]   192.168.200.59(192.168.200.59:3306)  Version=8.0.16 (oldest major version between slaves) log-bin:enabled
Tue Aug 15 10:28:20 2023 - [info]     Replicating from 192.168.200.57(192.168.200.57:3306)
Tue Aug 15 10:28:20 2023 - [info]     Primary candidate for the new Master (candidate_master is set)

It is better to execute FLUSH NO_WRITE_TO_BINLOG TABLES on the master before switching. Is it ok to execute on 192.168.200.57(192.168.200.57:3306)? (YES/no): yes
Tue Aug 15 10:28:26 2023 - [info] Executing FLUSH NO_WRITE_TO_BINLOG TABLES. This may take long time..
Tue Aug 15 10:28:26 2023 - [info]  ok.
Tue Aug 15 10:28:26 2023 - [info] Checking MHA is not monitoring or doing failover..
Tue Aug 15 10:28:26 2023 - [info] Checking replication health on 192.168.200.59..
Tue Aug 15 10:28:26 2023 - [info]  ok.
Tue Aug 15 10:28:26 2023 - [info] 192.168.200.59 can be new master.
Tue Aug 15 10:28:26 2023 - [info]
From:
192.168.200.57(192.168.200.57:3306) (current master)
 +--192.168.200.59(192.168.200.59:3306)

To:
192.168.200.59(192.168.200.59:3306) (new master)
 +--192.168.200.57(192.168.200.57:3306)

Starting master switch from 192.168.200.57(192.168.200.57:3306) to 192.168.200.59(192.168.200.59:3306)? (yes/NO): yes
Tue Aug 15 10:28:28 2023 - [info] Checking whether 192.168.200.59(192.168.200.59:3306) is ok for the new master..
Tue Aug 15 10:28:28 2023 - [info]  ok.
Tue Aug 15 10:28:28 2023 - [info] 192.168.200.57(192.168.200.57:3306): SHOW SLAVE STATUS returned empty result. To check replication filtering rules, temporarily executing CHANGE MASTER to a dummy host.
Tue Aug 15 10:28:28 2023 - [info] 192.168.200.57(192.168.200.57:3306): Resetting slave pointing to the dummy host.
Tue Aug 15 10:28:28 2023 - [info] ** Phase 1: Configuration Check Phase completed.
Tue Aug 15 10:28:28 2023 - [info]
Tue Aug 15 10:28:28 2023 - [info] * Phase 2: Rejecting updates Phase..
Tue Aug 15 10:28:28 2023 - [info]
master_ip_online_change_script is not defined. If you do not disable writes on the current master manually, applications keep writing on the current master. Is it ok to proceed? (yes/NO):yes
Tue Aug 15 10:28:31 2023 - [info] Locking all tables on the orig master to reject updates from everybody (including root):
Tue Aug 15 10:28:31 2023 - [info] Executing FLUSH TABLES WITH READ LOCK..
Tue Aug 15 10:28:31 2023 - [info]  ok.
Tue Aug 15 10:28:31 2023 - [info] Orig master binlog:pos is mysql-bin.000023:1242.
Tue Aug 15 10:28:31 2023 - [info]  Waiting to execute all relay logs on 192.168.200.59(192.168.200.59:3306)..
Tue Aug 15 10:28:31 2023 - [info]  master_pos_wait(mysql-bin.000023:1242) completed on 192.168.200.59(192.168.200.59:3306). Executed 0 events.
Tue Aug 15 10:28:31 2023 - [info]   done.
Tue Aug 15 10:28:31 2023 - [info] Getting new master's binlog name and position..
Tue Aug 15 10:28:31 2023 - [info]  mysql-bin.000013:5562
Tue Aug 15 10:28:31 2023 - [info]  All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='192.168.200.59', MASTER_PORT=3306, MASTER_LOG_FILE='mysql-bin.000013', MASTER_LOG_POS=5562, MASTER_USER='user_slave', MASTER_PASSWORD='xxx';
Tue Aug 15 10:28:31 2023 - [info] Setting read_only=0 on 192.168.200.59(192.168.200.59:3306)..
Tue Aug 15 10:28:31 2023 - [info]  ok.
Tue Aug 15 10:28:31 2023 - [info]
Tue Aug 15 10:28:31 2023 - [info] * Switching slaves in parallel..
Tue Aug 15 10:28:31 2023 - [info]
Tue Aug 15 10:28:31 2023 - [info] Unlocking all tables on the orig master:
Tue Aug 15 10:28:31 2023 - [info] Executing UNLOCK TABLES..
Tue Aug 15 10:28:31 2023 - [info]  ok.
Tue Aug 15 10:28:31 2023 - [info] Starting orig master as a new slave..
Tue Aug 15 10:28:31 2023 - [info]  Resetting slave 192.168.200.57(192.168.200.57:3306) and starting replication from the new master 192.168.200.59(192.168.200.59:3306)..
Tue Aug 15 10:28:31 2023 - [info]  Executed CHANGE MASTER.
Tue Aug 15 10:28:31 2023 - [info]  Slave started.
Tue Aug 15 10:28:31 2023 - [info] All new slave servers switched successfully.
Tue Aug 15 10:28:31 2023 - [info]
Tue Aug 15 10:28:31 2023 - [info] * Phase 5: New master cleanup phase..
Tue Aug 15 10:28:31 2023 - [info]
Tue Aug 15 10:28:31 2023 - [info]  192.168.200.59: Resetting slave info succeeded.
Tue Aug 15 10:28:31 2023 - [info] Switching master to 192.168.200.59(192.168.200.59:3306) completed successfully.

登录数据查看主从状态:

Linux下mysql8.0.16MHA搭建(三)_第2张图片

 Linux下mysql8.0.16MHA搭建(三)_第3张图片

 (这里感觉有点问题,新的master是59,但58还是指向57,但后面的主从没有问题,不知道为啥出现这样的情况,猜测是没有设置自动切换ip的脚本的原因)

Linux下mysql8.0.16MHA搭建(三)_第4张图片

 在新主库59上创建新的数据库,查看主从同步:

Linux下mysql8.0.16MHA搭建(三)_第5张图片Linux下mysql8.0.16MHA搭建(三)_第6张图片

 12、模拟故障:

此时59为新的master,在59上停止mysql服务:

 Linux下mysql8.0.16MHA搭建(三)_第7张图片

 

你可能感兴趣的:(linux,运维,服务器,mysql)