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 一主兩從搭建