MHA 一主两从搭建

Posted 长城之上是千亿的星空,星空之上是不灭的守望。

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MHA 一主两从搭建相关的知识,希望对你有一定的参考价值。

环境介绍:
主机名 IP MHA角色 mysql角色
node1 192.168.56.26 Node MySQL Master
node2 192.168.56.27 Node MySQL Master behind
node3 192.168.56.28 Node MySQL slave
node4 192.168.56.36 Manager None

所有主机 /etc/hosts 添加信息
192.168.56.26 node1
192.168.56.27 node2
192.168.56.28 node3
192.168.56.36 node4

关闭selinux
永久生效:
/etc/selinux/config 文件修改如下参数
SELINUX=disabled

临时生效:
setenforce 0


关闭防火墙
chkconfig iptables off
chkconfig ip6tables off

/etc/init.d/iptables stop


安装MySQL:

解压软件
mkdir /opt/mysql
mv mysql-5.6.34-linux-glibc2.5-x86_64.tar.gz /opt/mysql
cd /opt/mysql
tar -zxvf mysql-5.6.34-linux-glibc2.5-x86_64.tar.gz

创建软连接
ln -s /opt/mysql/mysql-5.6.34-linux-glibc2.5-x86_64 /usr/local/mysql

创建运行用户
groupadd mysql
useradd -g mysql -d /usr/local/mysql -s /sbin/nologin -M -n mysql

创建所需的目录
mkdir -p /data/mysql/3307/{data,logs,tmp}
chown -R mysql:mysql /data/mysql/3307/
chown -R mysql:mysql /usr/local/mysql

配置文件内容
#my.cnf
[client]
port = 3307
socket = /data/mysql/3307/tmp/3307.sock

[mysql]
#pager="less -i -n -S"
#tee=/opt/mysql/query.log
no-auto-rehash

[mysqld]
#misc
user = mysql
basedir = /usr/local/mysql
datadir = /data/mysql/3307/data
port = 3307
socket = /data/mysql/3307/tmp/3307.sock
event_scheduler = 0

tmpdir = /data/mysql/3307/tmp
#timeout
interactive_timeout = 300
wait_timeout = 300

#character set
character-set-server = utf8

open_files_limit = 65535
max_connections = 100
max_connect_errors = 100000
lower_case_table_names =1

#symi replication

#rpl_semi_sync_master_enabled=1
#rpl_semi_sync_master_timeout=1000 # 1 second
#rpl_semi_sync_slave_enabled=1

#logs
log-output=file
slow_query_log = 1
slow_query_log_file = slow.log
log-error = error.log
log_warnings = 2
pid-file = mysql.pid
long_query_time = 1
#log-slow-admin-statements = 1
#log-queries-not-using-indexes = 1
log-slow-slave-statements = 1

#binlog
#binlog_format = STATEMENT
binlog_format = row
server-id = 330728
log-bin = /data/mysql/3307/logs/mysql-bin
binlog_cache_size = 4M
max_binlog_size = 256M
max_binlog_cache_size = 1M
sync_binlog = 0
expire_logs_days = 10
#procedure
log_bin_trust_function_creators=1

#
#gtid-mode = on
#enforce-gtid-consistency=1


#relay log
skip_slave_start = 1
max_relay_log_size = 128M
relay_log_purge = 1
relay_log_recovery = 1
relay-log=relay-bin
relay-log-index=relay-bin.index
log_slave_updates
#slave-skip-errors=1032,1053,1062
#skip-grant-tables

#buffers & cache
table_open_cache = 2048
table_definition_cache = 2048
table_open_cache = 2048
max_heap_table_size = 96M
sort_buffer_size = 128K
join_buffer_size = 128K
thread_cache_size = 200
query_cache_size = 0
query_cache_type = 0
query_cache_limit = 256K
query_cache_min_res_unit = 512
thread_stack = 192K
tmp_table_size = 96M
key_buffer_size = 8M
read_buffer_size = 2M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 32M

#myisam
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1

#innodb
innodb_buffer_pool_size = 100M
innodb_buffer_pool_instances = 1
innodb_data_file_path = ibdata1:100M:autoextend
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 8M
innodb_log_file_size = 100M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 50
innodb_file_per_table = 1
innodb_rollback_on_timeout
innodb_status_file = 1
innodb_io_capacity = 100
transaction_isolation = READ-COMMITTED
innodb_flush_method = O_DIRECT

修改my3307.cnf权限
chown mysql:mysql /etc/my.cnf

初始化数据库
/usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql/3307/data --user=mysql

设置环境变量
echo "export PATH=$PATH:/usr/local/mysql/bin">> /etc/profile
source /etc/profile

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

启动MySQL
/etc/init.d/mysqld start

账号处理
grant all privileges on *.* to ‘root‘@‘%‘ identified by ‘chengce243‘ with grant option;
delete from mysql.user where password =‘‘;
flush privileges;

搭建从库
主库创建复制账号
create user ‘repl‘@‘192.168.56.%‘ identified by ‘chengce243‘;
grant replication slave on *.* to ‘repl‘@‘192.168.56.%‘;
flush privileges;

两个从库分别执行如下语句
MASTER_LOG_FILE 和 MASTER_LOG_POS 值,从主库 show master status;查看

CHANGE MASTER TO MASTER_HOST=‘192.168.56.26‘,MASTER_USER=‘repl‘,MASTER_PASSWORD=‘chengce243‘,MASTER_PORT=3307,MASTER_LOG_FILE=‘mysql-bin.000003‘,MASTER_LOG_POS=1718;

两个从库分别开启复制
start slave;

主库上创建
create user ‘mhauser‘@‘192.168.56.%‘ identified by ‘chengce243‘;
grant all privileges on *.* to ‘mhauser‘@‘192.168.56.%‘;
flush privileges;

主库上
set global relay_log_purge=OFF;
set global read_only=OFF;

mysql> show variables like ‘relay_log_purge‘;
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| relay_log_purge | OFF |
+-----------------+-------+
1 row in set (0.00 sec)

mysql> show variables like ‘read_only‘;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| read_only | OFF |
+---------------+-------+
1 row in set (0.00 sec)

两个从库上
set global relay_log_purge=OFF;
set global read_only=ON;

mysql> show variables like ‘relay_log_purge‘;
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| relay_log_purge | OFF |
+-----------------+-------+
1 row in set (0.00 sec)

mysql> show variables like ‘read_only‘;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| read_only | ON |
+---------------+-------+
1 row in set (0.00 sec)


分别配置root用户和mysql用户互信

生成互信文件,输入如下命令,一路回车就行
ssh-keygen

cd ~/.ssh

cat id_rsa.pub > authorized_keys
chmod 600 *
cd ~/
scp -r .ssh 192.168.56.27:~/
scp -r .ssh 192.168.56.28:~/
scp -r .ssh 192.168.56.36:~/

最后每个节点都要验证
ssh node1 date
ssh node2 date
ssh node3 date
ssh node4 date


每个节点都要安装相关依赖包
yum install -y perl-devel
yum install -y perl-CPAN
yum install -y perl-Time-HiRes
yum install -y perl-DBD-MySQL
yum install -y perl-Params-Validate

yum install -y perl-Config-Tiny
yum install -y perl-Log-Dispatch
yum install -y perl-Parallel-ForkManager

MHA master节点,即node4安装
rpm -ivh mha4mysql-manager-0.56-0.el6.noarch.rpm
rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm


MHA node节点,即node1/node2/node3安装
rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm


为MHA的相关配置信息的存放规划目录结构(四台服务器均要操作):
mkdir -p /masterha_work/{conf,manager_workdir,rmt_mysql_binlog_workdir,script_dir,log}

自定义规则:
conf,存放MHA的配置文件
log,存放于MHA有关的日志信息
script_dir,除了默认的存放脚本的位置外,另一个存放自定义的或者官方提供的脚本的位置
manager_workdir,Manager的工作目录
rmt_mysql_binlog_workdir,当发生切换的时候,MySQL的binlog的临时存放路径


修改配置目录权限:
chown -R mysql:mysql /masterha_work

[[email protected] conf]# cat /masterha_work/conf/mha_total.cnf

[server default]
manager_workdir=/masterha_work/manager_workdir
manager_log=/masterha_work/log/manager.log

master_binlog_dir=/data/mysql/3307/logs
user=mhauser
password=chengce243

#master_ip_failover_script=/usr/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=node1 --orig_master_ip=192.168.56.26 --orig_master_port=3307
master_ip_online_change_script=/usr/bin/master_ip_online_change

ping_interval=1

remote_workdir=/masterha_work/rmt_mysql_binlog_workdir

repl_user=repl
repl_password=chengce243
port=3307

report_script=/usr/bin/send_report

secondary_check_script=/usr/local/bin/masterha_secondary_check -s node2 -s node1 --user=mhame --master_host=node2 --master_ip=192.168.56.27 --master_port=3307
shutdown_script=""

ssh_user=root

[server1]
hostname=192.168.56.26
port=3307

[server2]
hostname=192.168.56.27
candidate_master=1
check_repl_delay=0
port=3307

[server3]
hostname=192.168.56.28
port=3307
no_master=1


测试SSH连通性:

[[email protected] conf]# masterha_check_ssh --conf=/masterha_work/conf/mha_total.cnf
Sun Oct 1 19:07:20 2017 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Sun Oct 1 19:07:20 2017 - [info] Reading application default configuration from /masterha_work/conf/mha_total.cnf..
Sun Oct 1 19:07:20 2017 - [info] Reading server configuration from /masterha_work/conf/mha_total.cnf..
Sun Oct 1 19:07:20 2017 - [info] Starting SSH connection tests..
Sun Oct 1 19:07:21 2017 - [debug]
Sun Oct 1 19:07:20 2017 - [debug] Connecting via SSH from [email protected](192.168.56.26:22) to [email protected](192.168.56.27:22)..
Sun Oct 1 19:07:20 2017 - [debug] ok.
Sun Oct 1 19:07:20 2017 - [debug] Connecting via SSH from [email protected](192.168.56.26:22) to [email protected](192.168.56.28:22)..
Sun Oct 1 19:07:21 2017 - [debug] ok.
Sun Oct 1 19:07:21 2017 - [debug]
Sun Oct 1 19:07:20 2017 - [debug] Connecting via SSH from [email protected](192.168.56.27:22) to [email protected](192.168.56.26:22)..
Sun Oct 1 19:07:21 2017 - [debug] ok.
Sun Oct 1 19:07:21 2017 - [debug] Connecting via SSH from [email protected](192.168.56.27:22) to [email protected](192.168.56.28:22)..
Sun Oct 1 19:07:21 2017 - [debug] ok.
Sun Oct 1 19:07:22 2017 - [debug]
Sun Oct 1 19:07:21 2017 - [debug] Connecting via SSH from [email protected](192.168.56.28:22) to [email protected](192.168.56.26:22)..
Sun Oct 1 19:07:21 2017 - [debug] ok.
Sun Oct 1 19:07:21 2017 - [debug] Connecting via SSH from [email protected](192.168.56.28:22) to [email protected](192.168.56.27:22)..
Sun Oct 1 19:07:22 2017 - [debug] ok.
Sun Oct 1 19:07:22 2017 - [info] All SSH connection tests passed successfully.


测试MySQL复制的情况:
[[email protected] ~]# masterha_check_repl --conf=/masterha_work/conf/mha_total.cnf
Fri Oct 6 13:28:22 2017 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Fri Oct 6 13:28:22 2017 - [info] Reading application default configuration from /masterha_work/conf/mha_total.cnf..
Fri Oct 6 13:28:22 2017 - [info] Reading server configuration from /masterha_work/conf/mha_total.cnf..
Fri Oct 6 13:28:22 2017 - [info] MHA::MasterMonitor version 0.56.
Fri Oct 6 13:28:22 2017 - [info] GTID failover mode = 0
Fri Oct 6 13:28:22 2017 - [info] Dead Servers:
Fri Oct 6 13:28:22 2017 - [info] Alive Servers:
Fri Oct 6 13:28:22 2017 - [info] 192.168.56.26(192.168.56.26:3307)
Fri Oct 6 13:28:22 2017 - [info] 192.168.56.27(192.168.56.27:3307)
Fri Oct 6 13:28:22 2017 - [info] 192.168.56.28(192.168.56.28:3307)
Fri Oct 6 13:28:22 2017 - [info] Alive Slaves:
Fri Oct 6 13:28:22 2017 - [info] 192.168.56.27(192.168.56.27:3307) Version=5.6.34-log (oldest major version between slaves) log-bin:enabled
Fri Oct 6 13:28:22 2017 - [info] Replicating from 192.168.56.26(192.168.56.26:3307)
Fri Oct 6 13:28:22 2017 - [info] Primary candidate for the new Master (candidate_master is set)
Fri Oct 6 13:28:22 2017 - [info] 192.168.56.28(192.168.56.28:3307) Version=5.6.34-log (oldest major version between slaves) log-bin:enabled
Fri Oct 6 13:28:22 2017 - [info] Replicating from 192.168.56.26(192.168.56.26:3307)
Fri Oct 6 13:28:22 2017 - [info] Not candidate for the new Master (no_master is set)
Fri Oct 6 13:28:22 2017 - [info] Current Alive Master: 192.168.56.26(192.168.56.26:3307)
Fri Oct 6 13:28:22 2017 - [info] Checking slave configurations..
Fri Oct 6 13:28:22 2017 - [info] read_only=1 is not set on slave 192.168.56.27(192.168.56.27:3307).
Fri Oct 6 13:28:22 2017 - [warning] relay_log_purge=0 is not set on slave 192.168.56.27(192.168.56.27:3307).
Fri Oct 6 13:28:22 2017 - [info] read_only=1 is not set on slave 192.168.56.28(192.168.56.28:3307).
Fri Oct 6 13:28:22 2017 - [warning] relay_log_purge=0 is not set on slave 192.168.56.28(192.168.56.28:3307).
Fri Oct 6 13:28:22 2017 - [info] Checking replication filtering settings..
Fri Oct 6 13:28:22 2017 - [info] binlog_do_db= , binlog_ignore_db=
Fri Oct 6 13:28:22 2017 - [info] Replication filtering check ok.
Fri Oct 6 13:28:22 2017 - [info] GTID (with auto-pos) is not supported
Fri Oct 6 13:28:22 2017 - [info] Starting SSH connection tests..
Fri Oct 6 13:28:25 2017 - [info] All SSH connection tests passed successfully.
Fri Oct 6 13:28:25 2017 - [info] Checking MHA Node version..
Fri Oct 6 13:28:25 2017 - [info] Version check ok.
Fri Oct 6 13:28:25 2017 - [info] Checking SSH publickey authentication settings on the current master..
Fri Oct 6 13:28:25 2017 - [info] HealthCheck: SSH to 192.168.56.26 is reachable.
Fri Oct 6 13:28:26 2017 - [info] Master MHA Node version is 0.56.
Fri Oct 6 13:28:26 2017 - [info] Checking recovery script configurations on 192.168.56.26(192.168.56.26:3307)..
Fri Oct 6 13:28:26 2017 - [info] Executing command: save_binary_logs --command=test --start_pos=4 --binlog_dir=/data/mysql/3307/logs --output_file=/masterha_work/rmt_mysql_binlog_workdir/save_binary_logs_test --manager_version=0.56 --start_file=mysql-bin.000003
Fri Oct 6 13:28:26 2017 - [info] Connecting to [email protected](192.168.56.26:22)..
Creating /masterha_work/rmt_mysql_binlog_workdir if not exists.. ok.
Checking output directory is accessible or not..
ok.
Binlog found at /data/mysql/3307/logs, up to mysql-bin.000003
Fri Oct 6 13:28:26 2017 - [info] Binlog setting check done.
Fri Oct 6 13:28:26 2017 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..
Fri Oct 6 13:28:26 2017 - [info] Executing command : apply_diff_relay_logs --command=test --slave_user=‘mhauser‘ --slave_host=192.168.56.27 --slave_ip=192.168.56.27 --slave_port=3307 --workdir=/masterha_work/rmt_mysql_binlog_workdir --target_version=5.6.34-log --manager_version=0.56 --relay_log_info=/data/mysql/3307/data/relay-log.info --relay_dir=/data/mysql/3307/data/ --slave_pass=xxx
Fri Oct 6 13:28:26 2017 - [info] Connecting to [email protected](192.168.56.27:22)..
Checking slave recovery environment settings..
Opening /data/mysql/3307/data/relay-log.info ... ok.
Relay log found at /data/mysql/3307/data, up to relay-bin.000002
Temporary relay log file is /data/mysql/3307/data/relay-bin.000002
Testing mysql connection and privileges..Warning: Using a password on the command line interface can be insecure.
done.
Testing mysqlbinlog output.. done.
Cleaning up test file(s).. done.
Fri Oct 6 13:28:26 2017 - [info] Executing command : apply_diff_relay_logs --command=test --slave_user=‘mhauser‘ --slave_host=192.168.56.28 --slave_ip=192.168.56.28 --slave_port=3307 --workdir=/masterha_work/rmt_mysql_binlog_workdir --target_version=5.6.34-log --manager_version=0.56 --relay_log_info=/data/mysql/3307/data/relay-log.info --relay_dir=/data/mysql/3307/data/ --slave_pass=xxx
Fri Oct 6 13:28:26 2017 - [info] Connecting to [email protected](192.168.56.28:22)..
Checking slave recovery environment settings..
Opening /data/mysql/3307/data/relay-log.info ... ok.
Relay log found at /data/mysql/3307/data, up to relay-bin.000002
Temporary relay log file is /data/mysql/3307/data/relay-bin.000002
Testing mysql connection and privileges..Warning: Using a password on the command line interface can be insecure.
done.
Testing mysqlbinlog output.. done.
Cleaning up test file(s).. done.
Fri Oct 6 13:28:27 2017 - [info] Slaves settings check done.
Fri Oct 6 13:28:27 2017 - [info]
192.168.56.26(192.168.56.26:3307) (current master)
+--192.168.56.27(192.168.56.27:3307)
+--192.168.56.28(192.168.56.28:3307)

Fri Oct 6 13:28:27 2017 - [info] Checking replication health on 192.168.56.27..
Fri Oct 6 13:28:27 2017 - [info] ok.
Fri Oct 6 13:28:27 2017 - [info] Checking replication health on 192.168.56.28..
Fri Oct 6 13:28:27 2017 - [info] ok.
Fri Oct 6 13:28:27 2017 - [warning] master_ip_failover_script is not defined.
Fri Oct 6 13:28:27 2017 - [warning] shutdown_script is not defined.
Fri Oct 6 13:28:27 2017 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.

 

以上是关于MHA 一主两从搭建的主要内容,如果未能解决你的问题,请参考以下文章

redis一主两从搭建

MySQL——MHA搭建

docker-compose搭建redis集群一主两从三哨兵

MySQL之MHA

Docker搭建redis主从+哨兵

MySQL之 MHA 高可用集群部署