Centos7——mysql部署MHA

介绍:MHA集群是通过一主两从+第三方manager的形式存在,当主出现问题,两个slave会同时争主,
环境准备:
需要准备4台主机,三台主机均搭建mysql数据库,一台主机搭建manager,环境信息如下:
192.168.122.105 manger
192.168.122.102 master
192.168.122.103 node slave
192.168.122.104 node slave

详细操作步骤如下:
先检查防火墙是否关闭:

[root@kvm122105 mha4mysql]# systemctl stop firewalld
[root@kvm122105 mha4mysql]# systemctl disable firewalld
[root@kvm122105 mha4mysql]# vim /etc/selinux/config
SELINUX=disabled

manager安装:
1、将安装包复制到指定的环境下
[root@localhost ~]# scp -r mha4mysql/ 192.168.122.105:/root

2、安装manager相关服务[root@kvm122105 mha4mysql]# mount /dev/cdrom /mnt/
mount: /dev/sr0 is write-protected, mounting read-only
创建yum库:
[root@kvm122105 mha4mysql]# yum -y install createrepo[root@kvm122105 mha4mysql]# vim /etc/yum.repos.d/deve.repo
[mh]
name=mh
baseurl=file:///root/mha4mysql
enabled=1
gpgcheck=0

[root@kvm122105 ~]# cd mha4mysql[root@kvm122105 mha4mysql]# createrepo -v /root/mha4mysql
[root@kvm122105 mha4mysql]# yum -y localinstall mha4mysql-manager-0.57-0.el7.noarch.rpm

3、安装node,三台需要同时安装[root@localhost ~]# scp mha4mysql/mha4mysql-node-0.57-0.el7.noarch.rpm 192.168.122.102:/root
[root@localhost ~]# scp mha4mysql/mha4mysql-node-0.57-0.el7.noarch.rpm 192.168.122.103:/root
[root@localhost ~]# scp mha4mysql/mha4mysql-node-0.57-0.el7.noarch.rpm 192.168.122.104:/root

192.168.122.102安装node挂载
[root@kvm122102 ~]# mount /dev/cdrom /mnt/
安装
[root@kvm122102 ~]# yum -y localinstall mha4mysql-node-0.57-0.el7.noarch.rpm

192.168.122.103安装node
挂载
[root@kvm122103 ~]# mount /dev/cdrom /mnt/安装
[root@kvm122103 ~]# yum -y localinstall mha4mysql-node-0.57-0.el7.noarch.rpm

192.168.122.104安装node挂载
[root@kvm122103 ~]# mount /dev/cdrom /mnt/
安装
[root@kvm122104 ~]# yum -y localinstall mha4mysql-node-0.57-0.el7.noarch.rpm
4、所有主机要做ssh的免密通信,manager要跟三个node做通信,也就是说每台到另一台都是免密登陆,操作如下:

节点一(manager):192.168.122.105

[root@kvm122105 mha4mysql]# ssh-keygen
[root@kvm122105 mha4mysql]# ssh-copy-id 192.168.122.102 (输入登陆密码:admindl)[root@kvm122105 mha4mysql]# ssh-copy-id 192.168.122.103
[root@kvm122105 mha4mysql]# ssh-copy-id 192.168.122.104

节点二(master):192.168.122.102:
ssh-keygen执行时一直回车[root@kvm122102 ~]# ssh-keygen [root@kvm122102 ~]# ssh-copy-id 192.168.122.103
[root@kvm122102 ~]# ssh-copy-id 192.168.122.104

节点三(slave):192.168.122.103
ssh-keygen执行后,一直回车即可

[root@kvm122103 ~]# ssh-keygen
[root@kvm122103 ~]# ssh-copy-id 192.168.122.102
[root@kvm122103 ~]# ssh-copy-id 192.168.122.104

节点四(slave):192.168.122.104
ssh-keygen执行后,一直回车即可
[root@kvm122104 ~]# ssh-keygen
[root@kvm122104 ~]# ssh-copy-id 192.168.122.102
[root@kvm122104 ~]# ssh-copy-id 192.168.122.103

5、授权
master上:192.168.122.102
1)编辑/etc/my.cnf输入以下内容:
server-id=1
log-bin=/var/lib/mysql/mysql.log
2)创建目录及授权
[root@kvm122103 ~]# mkdir /var/lib/mysql
[root@kvm122103 ~]# chown -R mysql:mysql /var/lib/mysql

3)重启
[root@kvm122102 ~]# service mysql restart
4)登陆mysql,进行授权[root@kvm122102 ~]# mysql -u root -p

grant replication slave on . to ‘slave’@‘192.168.122.103’ identified by ‘lifulai_000’;
grant replication slave on . to ‘slave’@‘192.168.122.104’ identified by ‘lifulai_000’;
flush privileges;

slave1上:192.168.122.103
1)编辑/etc/my.cnf里面,在[mysqld]下添加以下内容
server-id=2
log-bin=/var/lib/mysql/mysql.log
2)创建目录及授权
[root@kvm122103 ~]# mkdir /var/lib/mysql
[root@kvm122103 ~]# chown -R mysql:mysql /var/lib/mysql

3)重启
[root@kvm122103 ~]# service mysql restart
4)登陆mysql
[root@kvm122103 ~]# mysql -u root -p

change master to
master_host=‘192.168.122.102’,
master_user=‘slave’,
master_password=‘lifulai_000’,
master_log_file=‘mysql.000001’,
master_log_pos=902;

mysql> start slave;
mysql> show slave status \G;
mysql> grant replication slave on . to ‘slave’@‘192.168.122.102’ identified by ‘lifulai_000’;
mysql> grant replication slave on . to ‘slave’@‘192.168.122.104’ identified by ‘lifulai_000’;
mysql> flush privileges;

slave2上:192.168.122.104
1)编辑/etc/my.cnf
[mysqld]
server-id=3
log-bin=/var/lib/mysql/mysql.log
2)创建目录及授权
[root@kvm122103 ~]# mkdir /var/lib/mysql
[root@kvm122103 ~]# chown -R mysql:mysql /var/lib/mysql

3)重启
[root@kvm122104 ~]# service mysql restart
4)登陆
mysql -u root -p
change master to
master_host=‘192.168.122.102’,
master_user=‘slave’,
master_password=‘lifulai_000’,
master_log_file=‘mysql.000001’,
master_log_pos=902;

mysql> start slave;
mysql> show slave status \G;
mysql> grant replication slave on . to ‘slave’@‘192.168.122.102’ identified by ‘lifulai_000’;
mysql> grant replication slave on . to ‘slave’@‘192.168.122.103’ identified by ‘lifulai_000’;
mysql> flush privileges;

6、授权manager用户权限
master上:192.168.122.102
mysql> grant all privileges on . to ‘manager’@‘192.168.122.102’ identified by ‘lifulai_000’;
mysql> grant all privileges on . to ‘manager’@‘192.168.122.103’ identified by ‘lifulai_000’;
mysql> grant all privileges on . to ‘manager’@‘192.168.122.104’ identified by ‘lifulai_000’;
mysql> grant all privileges on . to ‘manager’@‘192.168.122.105’ identified by ‘lifulai_000’;
mysql> flush privileges;

slave1上:192.168.122.103grant all privileges on . to ‘manager’@‘192.168.122.102’ identified by ‘lifulai_000’;
grant all privileges on . to ‘manager’@‘192.168.122.103’ identified by ‘lifulai_000’;
grant all privileges on . to ‘manager’@‘192.168.122.104’ identified by ‘lifulai_000’;
grant all privileges on . to ‘manager’@‘192.168.122.105’ identified by ‘lifulai_000’;
flush privileges;

slave2上:192.168.122.104
grant all privileges on . to ‘manager’@‘192.168.122.102’ identified by ‘lifulai_000’;
grant all privileges on . to ‘manager’@‘192.168.122.103’ identified by ‘lifulai_000’;
grant all privileges on . to ‘manager’@‘192.168.122.104’ identified by ‘lifulai_000’;
grant all privileges on . to ‘manager’@‘192.168.122.105’ identified by ‘lifulai_000’;
flush privileges;

7、配置manager用户
在安装完的manager环境上,进行操作:192.168.122.105
1)创建manager工作目录:
[root@kvm122105 mha4mysql]# mkdir -p /masterha/app1
2)创建manager配置文件
[root@kvm122105 etc]# mkdir /etc/masterha
[root@kvm122105 etc]# vim /etc/masterha/app1.cnf
[server default]
manager_workdir=/masterha/app1
manager_log=/masterha/app1/manager.log
user=manager
password=lifulai_000
ssh_user=root
repl_user=slave
repl_password=lifulai_000
ping_interval=1
shutdown_script=""

[server1]
hostname=192.168.122.102
port=3306
master_binlog_dir="/var/lib/mysql"
candidate_master=1

[server2]
hostname=192.168.122.103
port=3306
master_binlog_dir="/var/lib/mysql"
candidate_master=1

[server3]
hostname=192.168.122.104
port=3306
master_binlog_dir="/var/lib/mysql"
candidate_master=1

4)检查配置文件是否正确
[root@kvm122105 etc]# masterha_check_ssh --conf=/etc/masterha/app1.cnf

5)检查复制是否正常:
[root@kvm122105 masterha]# masterha_check_repl --conf=/etc/masterha/app1.cnf

8、开启manager
[root@kvm122105 mha4mysql]# masterha_manager --conf=/etc/masterha/app1.cnf
或者:

测试:
在master上做测试(192.168.122.102)
1)建库、建表、添加数据mysql> create database db2;
Query OK, 1 row affected (0.03 sec)

mysql> use db2
Database changed
mysql> create table t1 (id int,name varchar(20));
Query OK, 0 rows affected (0.22 sec)

mysql> insert into t1 values(1,‘lisi’);
Query OK, 1 row affected (0.05 sec)

mysql> select * from t1;
±-----±-----+
| id | name |
±-----±-----+
| 1 | lisi |
±-----±-----+
1 row in set (0.00 sec)

mysql>
2)在slave1上查看

mysql> show databases;
±-------------------+
| Database |
±-------------------+
| information_schema |
| db1 |
| db2 |
| mysql |
| performance_schema |
| sys |
±-------------------+
6 rows in set (0.00 sec)

mysql> use db2
Database changed
mysql> select * from t1;
±-----±-----+
| id | name |
±-----±-----+
| 1 | lisi |
±-----±-----+
1 row in set (0.00 sec)

mysql>

3)在slave2上查看

mysql> show databases;
±-------------------+
| Database |
±-------------------+
| information_schema |
| db1 |
| db2 |
| mysql |
| performance_schema |
| sys |
±-------------------+
6 rows in set (0.00 sec)

mysql> use db2
Database changed
mysql> select * from t1;
±-----±-----+
| id | name |
±-----±-----+
| 1 | lisi |
±-----±-----+
1 row in set (0.00 sec)

mysql>

3)关掉master的mysql服务

[root@kvm122102 data]# service mysql stop
Shutting down MySQL… SUCCESS!
[root@kvm122102 data]#

4)查看slave1、slave2的主从情况
根据查看,122.102变成了主,122.103变为从,可以做到同步

重新使用manager操作:
删除app1.failover.complete
mv /masterha/app1/app1.failover.complete /tmp
将停止服务的102设置为103的从机
[root@kvm122102 ~]# service mysql start
[root@kvm122102 ~]# mysql -u root -p
change master to
master_host=‘192.168.122.103’,
master_user=‘slave’,
master_password=‘lifulai_000’,
master_log_file=‘mysql.000001’,
master_log_pos=2503;
mysql> start slave;
mysql> show slave status \G;

错误解决:
1、
Sun Jun 17 12:03:11 2018 - [info] Connecting to [email protected](192.168.122.103:22)…
Can’t exec “mysqlbinlog”: No such file or directory at /usr/share/perl5/vendor_perl/MHA/BinlogManager.pm line 106.
mysqlbinlog version command failed with rc 1:0, please verify PATH, LD_LIBRARY_PATH, and client options
at /usr/bin/apply_diff_relay_logs line 493.

解决办法:
[root@data02 ~]# type mysqlbinlog
mysqlbinlog is/usr/local/mysql/bin/mysqlbinlog
[root@data02 ~]#
[root@data02 ~]# ln -s/usr/local/mysql/bin/mysqlbinlog /usr/bin/mysqlbinlog

2、
Checking slave recovery environment settings…
Opening /usr/local/mysql/data/relay-log.info … ok.
Relay log found at /usr/local/mysql/data, up to kvm122103-relay-bin.000003
Temporary relay log file is /usr/local/mysql/data/kvm122103-relay-bin.000003
Testing mysql connection and privileges…sh: mysql: command not found
mysql command failed with rc 127:0!
at /usr/bin/apply_diff_relay_logs line 375.
main::check() called at /usr/bin/apply_diff_relay_logs line 497
eval {…} called at /usr/bin/apply_diff_relay_logs line 475
main::main() called at /usr/bin/apply_diff_relay_logs line 120

找不到mysql命令,把你mysql安装的目录下的bin目录,做一个软链接就可以了
ln -s /usr/local/mysql/bin/mysql /usr/bin

你可能感兴趣的:(Linux学习)