为什么要使用MySQL高可用群集
优点: 高可用性,扩展性好,出现故障自动切换,对于主主同步,在同一时间只提供一台数据库写操作,保证的数据的一致性。
服务器 | 操作系统 | IP地址 | 所需软件 |
---|---|---|---|
master1 | centos7.3 x86_64 | 192.168.144.128 | mariadb、MySQL-MMM |
master2 | centos7.3 x86_64 | 192.168.144.145 | mariadb、MySQL-MMM |
slave1 | centos7.3 x86_64 | 192.168.144.141 | mariadb、MySQL-MMM |
slave2 | centos7.3 x86_64 | 192.168.144.129 | mariadb、MySQL-MMM |
monitor | centos7.3 x86_64 | 192.168.144.136 | MySQL-MMM |
这里需要注意的是我们选择Mariadb替代MySQL。
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum -y install epel-release
yum clean all && yum makecache
yum -y install mariadb-server mariadb
systemctl disable firewalld.service
systemctl stop firewalld.service
setenforce 0
vim /etc/my.cnf
[mysqld]log_error=/var/lib/mysql/mysql.errlog=/var/lib/mysql/mysql_log.loglog_slow_queries=/var/lib/mysql_slow_queris.logbinlog-ignore-db=mysql,information_schemacharacter_set_server=utf8log_bin=mysql_binserver_id=1 //这里需要区分每台服务器ID需不同log_slave_updates=truesync_binlog=1auto_increment_increment=2auto_increment_offset=1
mysql>show master status;
MariaDB [(none)]> show master status;+------------------+----------+--------------+--------------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+--------------+--------------------------+| mysql_bin.000001 | 577 | | mysql,information_schema |+------------------+----------+--------------+--------------------------+1 row in set (0.00 sec)
mysql>grant replication slave on . to ‘replication‘@‘192.168.144.%‘ identified by ‘123456‘; //两台主都执行,从不需要
mysql>change master to master_host=‘192.168.144.145‘,master_user=‘replication‘,master_password=‘123456‘,master_log_file=‘mysql_bin.000001‘,master_log_pos=577;
mysql>start slave; //主主之间开启相互slave模式
mysql>flush privileges;
mysql>show slave status\G; //查看状态
mysql>change master to master_host=‘192.168.144.128‘,master_user=‘replication‘,master_password=‘123456‘,master_log_file=‘mysql_bin.000001‘,master_log_pos=577;
mysql>start slave;
mysql>show slave status\G;
yum -y install mysql-mmm*
cd /etc/mysql-mmm/
vi mmm_common.conf //所有主机上都要配置
<host default> cluster_interface ens33 pid_path /run/mysql-mmm-agent.pid bin_path /usr/libexec/mysql-mmm/ replication_user replication //MMM服务器切换验证 replication_password 123456 agent_user mmm_agent //MMM访问数据库代理验证 agent_password 123456</host><host db1> //数据库地址池的设定 ip 192.168.144.128 mode master peer db2</host><host db2> ip 192.168.144.145 mode master peer db1</host><host db3> ip 192.168.144.141 mode slave</host><host db4> ip 192.168.144.129 mode slave</host><role writer> //设置仅可在主服务器上写入 hosts db1, db2 ips 192.168.144.200 //设置主服务器虚拟IP mode exclusive</role><role reader> //设置仅可在从服务器上读取 hosts db3, db4 ips 192.168.144.201, 192.168.144.202 //设置从服务器虚拟IP mode balanced</role>
vi /etc/mysql-mmm/mmm_agent.conf
this db1 //根据规划进行逐一调整
mysql>grant super, replication client, process on . to ‘mmm_agent‘@‘192.168.144.%‘ identified by ‘123456‘;
mysql>grant replication client on . to ‘mmm_monitor‘@‘192.168.144.%‘ identified by ‘123456‘;
mysql>flush privileges;
systemctl start mysql-mmm-agent.service
systemctl enable mysql-mmm-agent.service 加入开机自启动
cd /etc/mysql-mmm/ //改密码
vi mmm_mon.conf
<monitor> ip 127.0.0.1 pid_path /run/mysql-mmm-monitor.pid bin_path /usr/libexec/mysql-mmm status_path /var/lib/mysql-mmm/mmm_mond.status ping_ips 192.168.144.128,192.168.144.145,192.168.144.141,192.168.144.129 //设置监控地址池 auto_set_online 10 //设置自动上线时间10S······<host default> monitor_user mmm_monitor monitor_password 123456</host>
systemctl start mysql-mmm-monitor.service
mmm_control show
[root@bogon mysql-mmm]# mmm_control showdb1(192.168.144.128) master/ONLINE. Roles: writer(192.168.144.200)db2(192.168.144.145) master/ONLINE. Roles: db3(192.168.144.141) slave/ONLINE. Roles: reader(192.168.144.202)db4(192.168.144.129) slave/ONLINE. Roles: reader(192.168.144.201)
mmm_control show db1(192.168.144.128) master/HARD_OFFLINE. Roles: db2(192.168.144.145) master/ONLINE. Roles: writer(192.168.144.200)
mysql>grant all on . to ‘testdba‘@‘192.168.144.136‘ identified by ‘123456‘;
flush privileges;
mysql -utestdba -p -h 192.168.144.200 //虚拟地址