|
目录
一、环境搭建
涉及主机
主机名IP地址db01(master)192.168.112.40db02(slave1)192.168.112.50db03(slave2)192.168.112.601、关闭防火墙firewalld,selinux
- systemctl stop firewalld
- systemctl disable firewalld
- setenforce 0
- sed -i 's/SELINUX=enforcing /SELINUX=disabled/g' /etc/selinux/config
复制代码 2、每台主机安装MySQL
- #二进制安装
- wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.6.40-linux-glibc2.12-x86_64.tar.gz
- tar xzvf mysql-5.6.40-linux-glibc2.12-x86_64.tar.gz
- mkdir /application
- mv mysql-5.6.40-linux-glibc2.12-x86_64 /application/mysql-5.6.40
- ln -s /application/mysql-5.6.40/ /application/mysql
- cd /application/mysql/support-files/
- \cp my-default.cnf /etc/my.cnf
- cp mysql.server /etc/init.d/mysqld
- cd /application/mysql/scripts
- useradd mysql -s /sbin/nologin -M
- yum -y install autoconf
- cd /application/mysql/scripts/
- ./mysql_install_db --user=mysql --basedir=/application/mysql --data=/application/mysql/data
- echo 'export PATH="/application/mysql/bin:$PATH"' >> /etc/profile.d/mysql.sh
- source /etc/profile
- sed -i 's#/usr/local#/application#g' /etc/init.d/mysqld /application/mysql/bin/mysqld_safe
- #指定MySQL安装根目录以及数据目录
- vim /etc/my.cnf
- basedir = /application/mysql/
- datadir = /application/mysql/data
- #设置密码
- mysqladmin -uroot password '123'
复制代码 二、基于GTID的主从复制
1、修改/etc/my.cnf文件
主库
- vim /etc/my.cnf
- [mysqld] #在mysqld标签下配置
- server_id=1
- log_bin=mysql-bin #开启binlog日志
- skip-name-resolv #跳过域名解析
- gtid_mode=ON
- log_slave_updates #开启slave binlog同步
- enforce_gtid_consistency #不允许任何违反GTID一致性
- [root@db01 ~]# /etc/init.d/mysqld restart #重启MySQL
复制代码所有主机
- #创建主从复制用户
- mysql -uroot -p123 #登录数据库
- grant replication slave on *.* to slave@'192.168.112.%' identified by '123'; #创建slave用户
复制代码从库
- [root@db02 ~]# vim /etc/my.cnf
- [mysqld] #在mysqld标签下配置
- server_id=2 #主库server-id为1,从库必须大于1
- log_bin=mysql-bin #开启binlog日志
- gtid_mode=ON
- log_slave_updates #开启slave binlog同步
- enforce_gtid_consistency #不允许任何违反GTID一致性
- [root@db02 ~]# /etc/init.d/mysqld restart #重启MySQL
- [root@db03 ~]# vim /etc/my.cnf
- [mysqld] #在mysqld标签下配置
- server_id=3 #主库server-id为1,从库必须大于1
- log_bin=mysql-bin #开启binlog日志
- gtid_mode=ON
- log_slave_updates #开启slave binlog同步
- enforce_gtid_consistency #不允许任何违反GTID一致性
- [root@db02 ~]# /etc/init.d/mysqld restart #重启MySQL
复制代码 2、检查GTID状态
主库上查看
- show global variables like '%gtid%';
复制代码
3、配置主从复制
从库配置
- mysql> change master to
- -> master_host='192.168.112.40',
- -> master_user='slave',
- -> master_password='123',
- -> master_auto_position=1;
- mysql> start slave;
- mysql> show slave status\G; #确保从库的IO和SQL线程开启Yes
复制代码
4、从库设置
- mysql> set global relay_log_purge = 0; #禁用自动删除relay log 功能
- mysql> set global read_only=1; #设置从库只读
复制代码- [root@mysql-db02 ~]# vim /etc/my.cnf
- #编辑配置文件
- [mysqld]
- #在mysqld标签下添加
- relay_log_purge = 0
- #禁用自动删除relay log 永久生效
复制代码 三、部署MHA
1、准备环境(所有节点)
- #下载工具包
- cd && wget https://download.s21i.faiusr.com/23126342/0/0/ABUIABBPGAAg3OHUiAYolpPt7AQ.zip?f=mysql-master-ha.zip&v=1628778716
复制代码- #安装依赖包
- yum install -y perl-DBD-MySQL
- yum install -y unzip
- mv ABUIABBPGAAg3OHUiAYolpPt7AQ.zip\?f\=mysql-master-ha.zip master-ha.zip
- unzip master-ha.zip
- cd mysql-master-ha/
- #安装node包
- rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
- #登录数据库
- mysql -uroot -p123
- #添加MHA管理账号
- mysql> grant all privileges on *.* to mha@'192.168.112.%' identified by 'mha';
- #查看账号是否添加成功
- mysql> select user,host,password from mysql.user;
复制代码- #创建软链接
- ln -s /application/mysql/bin/mysqlbinlog /usr/bin/mysqlbinlog
- ln -s /application/mysql/bin/mysql /usr/bin/mysql
复制代码 2、部署管理节点(可以部署在任何机器上)
这里选择db03作为管理节点
- [root@db03 ~]# yum install -y epel-release
- #安装manager依赖包
- [root@db03 ~]# yum install -y perl-Config-Tiny epel-release perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes
- #安装manager包
- [root@db03 ~]# cd ~/mysql-master-ha/ && rpm -ivh mha4mysql-manager-0.56-0.el6.noarch.rpm
复制代码编辑manager节点配置文件
- [root@db03 ~]# mkdir -p /etc/mha
- [root@db03 ~]# mkdir -p /var/log/mha/app1
- [root@db03 ~]# vim /etc/mha/app1.cnf
- [server default]
- manager_log=/var/log/mha/app1/manager.log
- manager_workdir=/var/log/mha/app1
- master_binlog_dir=/application/mysql/data
- user=mha
- password=mha
- ping_interval=2
- repl_password=123
- repl_user=slave
- ssh_user=root
- [server1]
- hostname=192.168.112.40
- port=3306
- [server2]
- hostname=192.168.112.50
- port=3306
- candidate_master=1
- check_repl_delay=0
- [server3]
- hostname=192.168.112.60
- port=3306
复制代码 3、配置ssh信任
- #创建密钥对
- [root@db01 ~]# ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa >/dev/null 2>&1
- #发送密钥,包括自己
- [root@db01 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.112.40
- [root@db01 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.112.50
- [root@db01 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.112.60
复制代码 4、启动测试(manage节点)
- #测试ssh
- [root@db03 ~]# masterha_check_ssh --conf=/etc/mha/app1.cnf
复制代码- #测试复制
- [root@db03 ~]# masterha_check_repl --conf=/etc/mha/app1.cnf
复制代码如果遇到MySQL Replication Health is NOT OK!
可以尝试在manage节点的/etc/my.cnf配置文件里添加skip-name-resolv跳过解析域名
四、启动MHA
- #启动
- [root@db03 ~]# nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1 &
- [root@db03 ~]# jobs
- [1]+ 运行中 nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1 &
- #检测状态
- [root@db03 ~]# masterha_check_status --conf=/etc/mha/app1.cnf
- app1 (pid:19754) is running(0:PING_OK), master:192.168.112.40
复制代码 五、切换master测试
1、检查两从复制情况
2、停掉主库
- #停掉主库
- [root@db01 ~]# /etc/init.d/mysqld stop
- Shutting down MySQL...... SUCCESS!
- #查看db02的slave状态
- mysql> show slave status\G;
- Empty set (0.00 sec)
- ERROR:
- No query specified
- #查看db03的slave状态
- mysql> show slave status\G;
- *************************** 1. row ***************************
- Slave_IO_State: Waiting for master to send event
- Master_Host: 192.168.112.50
- Master_User: slave
- Master_Port: 3306
- Connect_Retry: 60
- Master_Log_File: mysql-bin.000002
- Read_Master_Log_Pos: 659
- Relay_Log_File: db03-relay-bin.000002
- Relay_Log_Pos: 662
- Relay_Master_Log_File: mysql-bin.000002
- Slave_IO_Running: Yes
- Slave_SQL_Running: Yes
- Replicate_Do_DB:
- Replicate_Ignore_DB:
- Replicate_Do_Table:
- Replicate_Ignore_Table:
- Replicate_Wild_Do_Table:
- Replicate_Wild_Ignore_Table:
- Last_Errno: 0
- Last_Error:
- Skip_Counter: 0
- Exec_Master_Log_Pos: 659
- Relay_Log_Space: 865
- Until_Condition: None
- Until_Log_File:
- Until_Log_Pos: 0
- Master_SSL_Allowed: No
- Master_SSL_CA_File:
- Master_SSL_CA_Path:
- Master_SSL_Cert:
- Master_SSL_Cipher:
- Master_SSL_Key:
- Seconds_Behind_Master: 0
- Master_SSL_Verify_Server_Cert: No
- Last_IO_Errno: 0
- Last_IO_Error:
- Last_SQL_Errno: 0
- Last_SQL_Error:
- Replicate_Ignore_Server_Ids:
- Master_Server_Id: 2
- Master_UUID: e5c86268-ed04-11ee-8716-000c2936523e
- Master_Info_File: /application/mysql-5.6.40/data/master.info
- SQL_Delay: 0
- SQL_Remaining_Delay: NULL
- Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
- Master_Retry_Count: 86400
- Master_Bind:
- Last_IO_Error_Timestamp:
- Last_SQL_Error_Timestamp:
- Master_SSL_Crl:
- Master_SSL_Crlpath:
- Retrieved_Gtid_Set: e5c86268-ed04-11ee-8716-000c2936523e:1
- Executed_Gtid_Set: 15c89969-ed05-11ee-8717-000c291d4def:1,
- 51fc215b-eab2-11ee-b7f0-000c29c4dc96:1,
- e5c86268-ed04-11ee-8716-000c2936523e:1
- Auto_Position: 1
- 1 row in set (0.00 sec)
- ERROR:
- No query specified
复制代码可以清晰的看到db02已经切换为master
- #manage节点的状态检测也停止了
- [root@db03 ~]# masterha_check_status --conf=/etc/mha/app1.cnf
- app1 is stopped(2:NOT_RUNNING).
- #对应的[server1]也被删除了
- [root@db03 ~]# cat /etc/mha/app1.cnf
- [server default]
- manager_log=/var/log/mha/app1/manager.log
- manager_workdir=/var/log/mha/app1
- master_binlog_dir=/application/mysql/data
- password=mha
- ping_interval=2
- repl_password=123
- repl_user=slave
- ssh_user=root
- user=mha
- [server2]
- candidate_master=1
- check_repl_delay=0
- hostname=192.168.112.50
- port=3306
- [server3]
- hostname=192.168.112.60
- port=3306
复制代码 3、手动将主库以slave身份加回去
即使重新开启主库,主库也不会自动加回去了,只能以slave的身份手动加回去
- mysql> change master to
- -> master_host='192.168.112.50',
- -> master_user='slave',
- -> master_password='123',
- -> master_auto_position=1;
- Query OK, 0 rows affected, 2 warnings (0.01 sec)
- mysql> start slave;
- Query OK, 0 rows affected (0.00 sec)
- mysql> show slave status\G;
- *************************** 1. row ***************************
- Slave_IO_State: Connecting to master
- Master_Host: 192.168.112.50
- Master_User: slave
- Master_Port: 3306
- Connect_Retry: 60
- Master_Log_File:
- Read_Master_Log_Pos: 4
- Relay_Log_File: db01-relay-bin.000001
- Relay_Log_Pos: 4
- Relay_Master_Log_File:
- Slave_IO_Running: Connecting
- Slave_SQL_Running: Yes
- Replicate_Do_DB:
- Replicate_Ignore_DB:
- Replicate_Do_Table:
- Replicate_Ignore_Table:
- Replicate_Wild_Do_Table:
- Replicate_Wild_Ignore_Table:
- Last_Errno: 0
- Last_Error:
- Skip_Counter: 0
- Exec_Master_Log_Pos: 0
- Relay_Log_Space: 151
- Until_Condition: None
- Until_Log_File:
- Until_Log_Pos: 0
- Master_SSL_Allowed: No
- Master_SSL_CA_File:
- Master_SSL_CA_Path:
- Master_SSL_Cert:
- Master_SSL_Cipher:
- Master_SSL_Key:
- Seconds_Behind_Master: 0
- Master_SSL_Verify_Server_Cert: No
- Last_IO_Errno: 0
- Last_IO_Error:
- Last_SQL_Errno: 0
- Last_SQL_Error:
- Replicate_Ignore_Server_Ids:
- Master_Server_Id: 0
- Master_UUID:
- Master_Info_File: /application/mysql/data/master.info
- SQL_Delay: 0
- SQL_Remaining_Delay: NULL
- Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
- Master_Retry_Count: 86400
- Master_Bind:
- Last_IO_Error_Timestamp:
- Last_SQL_Error_Timestamp:
- Master_SSL_Crl:
- Master_SSL_Crlpath:
- Retrieved_Gtid_Set:
- Executed_Gtid_Set: 51fc215b-eab2-11ee-b7f0-000c29c4dc96:1
- Auto_Position: 1
- 1 row in set (0.00 sec)
- ERROR:
- No query specified
复制代码 六、配置VIP漂移
在MHA(MySQL Master High Availability)高可用架构中,虚拟IP(VIP)漂移是用于确保服务不间断的一种策略,当主数据库发生故障时,VIP会从原主节点迁移到新的主节点,这样应用程序和服务始终可以通过VIP访问到当前的主数据库,而无需修改任何指向数据库的实际IP地址
- VIP漂移的两种方式
- 通过keepalived的方式,管理虚拟IP的漂移
- 通过MHA自带脚本方式,管理虚拟IP的漂移
1、编辑配置文件
- #manage节点
- [root@db03 ~]# vim /etc/mha/app1.cnf
- [server default]
- master_ip_failover_script=/etc/mha/master_ip_failover #在[server default]标签下添加
- #随便把之前删除的[server1]加回来
- [server1]
- hostname=192.168.112.40
- port=3306
复制代码 2、编辑MHA自带的脚本
- [root@db03 ~]# vim /etc/mha/master_ip_failover
- #!/usr/bin/env perl
- use strict;
- use warnings FATAL => 'all';
- use Getopt::Long;
- my (
- $command, $ssh_user, $orig_master_host, $orig_master_ip,
- $orig_master_port, $new_master_host, $new_master_ip, $new_master_port
- );
- my $vip = '192.168.112.66/24';
- my $key = '0';
- my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";
- my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down";
- GetOptions(
- 'command=s' => \$command,
- 'ssh_user=s' => \$ssh_user,
- 'orig_master_host=s' => \$orig_master_host,
- 'orig_master_ip=s' => \$orig_master_ip,
- 'orig_master_port=i' => \$orig_master_port,
- 'new_master_host=s' => \$new_master_host,
- 'new_master_ip=s' => \$new_master_ip,
- 'new_master_port=i' => \$new_master_port,
- );
- exit &main();
- sub main {
- print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";
- if ( $command eq "stop" || $command eq "stopssh" ) {
- my $exit_code = 1;
- eval {
- print "Disabling the VIP on old master: $orig_master_host \n";
- &stop_vip();
- $exit_code = 0;
- };
- if ($@) {
- warn "Got Error: $@\n";
- exit $exit_code;
- }
- exit $exit_code;
- }
- elsif ( $command eq "start" ) {
- my $exit_code = 10;
- eval {
- print "Enabling the VIP - $vip on the new master - $new_master_host \n";
- &start_vip();
- $exit_code = 0;
- };
- if ($@) {
- warn $@;
- exit $exit_code;
- }
- exit $exit_code;
- }
- elsif ( $command eq "status" ) {
- print "Checking the Status of the script.. OK \n";
- exit 0;
- }
- else {
- &usage();
- exit 1;
- }
- }
- sub start_vip() {
- `ssh $ssh_user\@$new_master_host " $ssh_start_vip "`;
- }
- sub stop_vip() {
- return 0 unless ($ssh_user);
- `ssh $ssh_user\@$orig_master_host " $ssh_stop_vip "`;
- }
- sub usage {
- print
- "Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n";
- }
复制代码这里面需要依据个人修改的是
my $vip = '192.168.112.66/24';
然后添加执行权限
- [root@db03 ~]# chmod a+x /etc/mha/master_ip_failover
复制代码 3、手动绑定VIP(master节点)
- #所有主机,主要是ifconfig命令
- yum install -y net-tools
- #绑定vip
- [root@db02 ~]# ifconfig ens33:0 192.168.112.66/24
- [root@db02 ~]# ip a | grep ens33
- 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
- inet 192.168.112.50/24 brd 192.168.112.255 scope global noprefixroute ens33
- inet 192.168.112.66/24 brd 192.168.112.255 scope global secondary ens33:0
复制代码 4、启动测试(manage节点)
- masterha_check_ssh --conf=/etc/mha/app1.cnf
- masterha_check_repl --conf=/etc/mha/app1.cnf
复制代码 5、重启MHA(manage节点)
- #启动
- [root@db03 ~]# nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1 &
- [1] 21063
- [root@db03 ~]# jobs
- [1]+ 运行中 nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1 &
- #检测状态,识别出master
- [root@db03 ~]# masterha_check_status --conf=/etc/mha/app1.cnf
- app1 (pid:21063) is running(0:PING_OK), master:192.168.112.50
复制代码 6、测试IP漂移
- #在db01上查看vip信息
- [root@db01 ~]# ip a |grep ens33
- #在db02上查看vip信息
- [root@db02 ~]# ip a |grep ens33
- 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
- inet 192.168.112.40/24 brd 192.168.112.255 scope global noprefixroute ens33
- inet 192.168.112.66/24 brd 192.168.112.255 scope global secondary ens33:0
复制代码 来源:https://www.cnblogs.com/misakivv/p/18102949
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作! |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
|