MariaDB之MHA杂记
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MariaDB之MHA杂记相关的知识,希望对你有一定的参考价值。
mysql复制及MHA
MHA
0、环境准备
master1:MHA Manager
master2:MariaDB master
master3:MariaDB slave
master4:MariaDB slave
各节点的/etc/hosts
10.201.106.131 master1 master1.com
10.201.106.132 master2 master2.com
10.201.106.133 master3 master3.com
10.201.106.134 master4 master4.com
1、配置主从
1.1 主节点配置
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
innodb_file_per_table = 1
skip_name_resolve = 1
log-bin = master-bin
relay-log = relay-bin
server_id = 1
[[email protected] ~]# systemctl start mariadb.service
创建一个拥有复制权限的账号:
MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO ‘repluser‘@‘10.201.106.%‘ IDENTIFIED BY ‘replpass‘;
MariaDB [(none)]> FLUSH PRIVILEGES;
查看二进制文件和位置
MariaDB [(none)]> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000001 | 498 | | |
|
1.2 从节点配置
从节点1配置:
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
innodb_file_per_table = 1
skip_name_resolve = 1
log-bin = master-bin
relay-log = relay-bin
server_id = 2
read_only = 1
relay_log_purge = 0
[[email protected] ~]# systemctl restart mariadb.service
拷贝节点1配置到节点2:
[[email protected] ~]# scp /etc/my.cnf master4:/etc/
[email protected]‘s password:
my.cnf 100% 713 0.7KB/s 00:00
[[email protected] ~]#
节点2修改server_id
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
server_id = 3
[[email protected] ~]# systemctl restart mariadb.service
1.3 从节点开启复制线程
[[email protected] mysql]#
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST=‘10.201.106.132‘,MASTER_USER=‘repluser‘,MASTER_PASSWORD=‘replpass‘,MASTER_LOG_FILE=‘master-bin.000001‘,MASTER_LOG_POS=245;
MariaDB [(none)]> START SLAVE;
查看状态:
MariaDB [(none)]> SHOW SLAVE STATUS\G
从节点2配置:
[[email protected] ~]# mysql
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST=‘10.201.106.132‘,MASTER_USER=‘repluser‘,MASTER_PASSWORD=‘replpass‘,MASTER_LOG_FILE=‘master-bin.000001‘,MASTER_LOG_POS=245;
MariaDB [(none)]> START SLAVE;
2、主节点:创建一个可以远程连接拥有管理权限的账号
[[email protected] ~]#
MariaDB [(none)]> GRANT ALL ON *.* TO ‘mhauser‘@‘10.201.106.%‘ IDENTIFIED BY ‘mhapass‘;
MariaDB [(none)]> FLUSH PRIVILEGES;
3、MHA 环境需要所有节点配置SSH密钥登录,互信通信
3.1 所有主机使用同一个私钥
创建覆盖密钥
[[email protected] ~]# ssh-keygen -t rsa -P ‘‘
将密钥内容覆盖到SSH密钥文件中
[[email protected] ~]# cat .ssh/id_rsa.pub > .ssh/authorized_keys
设置权限:
[[email protected] ~]# chmod 600 .ssh/authorized_keys
复制文件到另外三个节点
[[email protected] ~]# scp -p .ssh/id_rsa .ssh/authorized_keys master2:/root/.ssh
id_rsa 100% 1679 1.6KB/s 00:00
authorized_keys 100% 398 0.4KB/s 00:00
[[email protected] ~]# scp -p .ssh/id_rsa .ssh/authorized_keys master3:/root/.ssh
id_rsa 100% 1679 1.6KB/s 00:00
authorized_keys 100% 398 0.4KB/s 00:00
[[email protected] ~]# scp -p .ssh/id_rsa .ssh/authorized_keys master4:/root/.ssh
id_rsa 100% 1679 1.6KB/s 00:00
authorized_keys 100% 398 0.4KB/s 00:00
[[email protected] ~]#
测试:
[[email protected] ~]# ssh master2 ‘ifconfig ens33‘ | grep 13
inet 10.201.106.132 netmask 255.255.255.0 broadcast 10.201.106.255
[[email protected] ~]# ssh master3 ‘ifconfig ens33‘ | grep 13
inet 10.201.106.133 netmask 255.255.255.0 broadcast 10.201.106.255
[[email protected] ~]# ssh master4 ‘ifconfig ens33‘ | grep 13
inet 10.201.106.134 netmask 255.255.255.0 broadcast 10.201.106.255
4、MHS Manger安装
4.1 Manger节点安装manger和node
[[email protected] ~]# yum -y install mha4mysql-*
[[email protected] ~]# scp mha4mysql-node-0.57-0.el7.noarch.rpm master2:/root
mha4mysql-node-0.57-0.el7.noarch.rpm 100% 35KB 34.5KB/s 00:00
[[email protected] ~]# scp mha4mysql-node-0.57-0.el7.noarch.rpm master3:/root
mha4mysql-node-0.57-0.el7.noarch.rpm 100% 35KB 34.5KB/s 00:00
[[email protected] ~]# scp mha4mysql-node-0.57-0.el7.noarch.rpm master4:/root
mha4mysql-node-0.57-0.el7.noarch.rpm 100% 35KB 34.5KB/s 00:00
5、另外三个节点安装mha-node
[[email protected] ~]# yum -y install mha4mysql-node-0.57-0.el7.noarch.rpm
[[email protected] ~]# yum -y install mha4mysql-node-0.57-0.el7.noarch.rpm
[[email protected] ~]# yum -y install mha4mysql-node-0.57-0.el7.noarch.rpm
6、配置manger
6.1 查看manger文件
[[email protected] ~]# rpm -ql mha4mysql-manager
查看node文件
[[email protected] ~]# rpm -ql mha4mysql-node
6.2 创建配置manger文件
[[email protected] ~]# mkdir /etc/masterha
[[email protected] ~]# vim /etc/masterha/app1.cnf
[server default]
user=mhauser
password=mhapass
manager_workdir=/data/masterha/app1
manager_log=/data/masterha/app1/manager.log
remote_workdir=/data/masterha/app1
ssh_user=root
repl_user=repluser
repl_password=replpass
ping_interval=1
[server1]
hostname=10.201.106.132
[server2]
hostname=10.201.106.133
candidate_master=1
[server3]
hostname=10.201.106.134
7、检查
7.1 SSH检查
[[email protected] ~]# masterha_check_ssh --conf=/etc/masterha/app1.cnf
Wed Jan 18 11:29:21 2017 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Wed Jan 18 11:29:21 2017 - [info] Reading application default configuration from /etc/mangerha/app1.cnf..
Wed Jan 18 11:29:21 2017 - [info] Reading server configuration from /etc/mangerha/app1.cnf..
Wed Jan 18 11:29:21 2017 - [info] Starting SSH connection tests..
Wed Jan 18 11:29:22 2017 - [debug]
Wed Jan 18 11:29:21 2017 - [debug] Connecting via SSH from [email protected](10.201.106.132:22) to [email protected](10.201.106.133:22)..
Wed Jan 18 11:29:21 2017 - [debug] ok.
Wed Jan 18 11:29:21 2017 - [debug] Connecting via SSH from [email protected](10.201.106.132:22) to [email protected](10.201.106.134:22)..
Wed Jan 18 11:29:22 2017 - [debug] ok.
Wed Jan 18 11:29:23 2017 - [debug]
Wed Jan 18 11:29:21 2017 - [debug] Connecting via SSH from [email protected](10.201.106.133:22) to [email protected](10.201.106.132:22)..
Wed Jan 18 11:29:22 2017 - [debug] ok.
Wed Jan 18 11:29:22 2017 - [debug] Connecting via SSH from [email protected](10.201.106.133:22) to [email protected](10.201.106.134:22)..
Wed Jan 18 11:29:23 2017 - [debug] ok.
Wed Jan 18 11:29:24 2017 - [debug]
Wed Jan 18 11:29:22 2017 - [debug] Connecting via SSH from [email protected](10.201.106.134:22) to [email protected](10.201.106.132:22)..
Wed Jan 18 11:29:23 2017 - [debug] ok.
Wed Jan 18 11:29:23 2017 - [debug] Connecting via SSH from [email protected](10.201.106.134:22) to [email protected](10.201.106.133:22)..
Wed Jan 18 11:29:24 2017 - [debug] ok.
Wed Jan 18 11:29:24 2017 - [info] All SSH connection tests passed successfully.
7.2 检查主从复制环境
[[email protected] masterha]# masterha_check_repl --conf=/etc/masterha/app1.cnf
……
MySQL Replication Health is OK.
8、启动MHA服务
8.1 测试启动
[[email protected] masterha]# masterha_manager --conf=/etc/masterha/app1.cnf
Wed Jan 18 16:24:30 2017 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Wed Jan 18 16:24:30 2017 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Wed Jan 18 16:24:30 2017 - [info] Reading server configuration from /etc/masterha/app1.cnf..
8.2 切换测试
kill掉主mysql进程
[[email protected] mysql]# killall mysqld mysqld_safe
从节点2查看:
MariaDB [(none)]> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.201.106.133
从节点1的read_only已关掉:
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘read_only‘;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| read_only | OFF |
+---------------+-------+
怎么加从节点:
1、先在主节点进行备份,记住新的日志文件名字和POS;
2、把备份二进制导入到从节点,让从节点从指定位置开始复制;
3、
8.3 将原来的主节点重新上线
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
read_only = 1
relay_log_purge = 0
原来节点数据删除,重新做初始化。生产环境切勿操作:
[[email protected] ~]# rm -rf /var/lib/mysql/*
启动数据库:
[[email protected] ~]# systemctl start mariadb.service
从节点配置:
设置从头开始复制:
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST=‘10.201.106.133‘,MASTER_USER=‘repluser‘,MASTER_PASSWORD=‘replpass‘;
MariaDB [(none)]> GRANT ALL ON *.* TO ‘mhauser‘@‘10.201.106.%‘ IDENTIFIED BY ‘mhapass‘;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO ‘repluser‘@‘10.201.106.%‘ IDENTIFIED BY ‘replpass‘;
开启从节点线程:
MariaDB [(none)]> START SLAVE;
8.4 检测
[[email protected] masterha]# masterha_check_repl --conf=/etc/masterha/app1.cnf
MySQL Replication Health is OK.
8.5 检测状态
[[email protected] masterha]# masterha_manager --conf=/etc/masterha/app1.cnf
[[email protected] ~]# masterha_check_status --conf=/etc/masterha/app1.cnf
app1 (pid:31234) is running(0:PING_OK), master:10.201.106.133
8.6 停止服务
以上是关于MariaDB之MHA杂记的主要内容,如果未能解决你的问题,请参考以下文章