重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
其中,
角色 | Hosts | IP地址 | 应用属性 | VIP |
---|---|---|---|---|
master1 | master | 192.168.142.135 | write | 192.168.142.188 |
master2 | backup | 192.168.142.132 | write|read | 192.168.142.188 |
slave1 | slave | 192.168.142.136 | read | 192.168.142.200, 192.168.142.210 |
slave2 | slave | 192.168.142.137 | read | 192.168.142.200, 192.168.142.210 |
monitor | monitor | 192.168.142.143 | moitor | 无VIP |
配置ALI云源(若已安装可跳过)
这里以master1作示范,实际五台均需要安装
[root@master1 ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@master1 ~]# yum -y install epel-release
[root@master1 ~]# yum clean all && yum makecache
给所有主从服务器安装MySQL,并配置
[root@master1 ~]# yum install mysql -y
[root@master1 ~]# vim /etc/my.cnf #修改配置文件
##server id不能一样
server-id =
log-bin = zhu-bin
log-slave-updates = ture ##开启主从同步
sync_binlog = 1 ##二进制文件立即写入
auto_increment_increment=2 ###增量为2
auto_increment_offset=1 ###### 起始值为1
[root@master1 ~]# systemctl restart mysqld
[root@master1 ~]# systemctl enable mysqld
m1给m2从权限,m2给m1从权限
[root@master1 ~]# mysql -u root -p
grant replication slave on *.* to 'myslave'@'192.168.142.%' identified by 'asd123';
#允许从服务器使用myslave账户在主服务器上进行复制操作(两台主均要授权)
show master status;
+------------------+----------+--------------+--------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql_bin.000001 | 245 | | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
1 row in set (0.00 sec)
#查看作为主服务器的信息,关键点为position号码
change master to master_host='主服务器地址',master_user='myslave',master_password='asd123',master_log_file='File',master_log_pos=position号码;
##授予权限(m1为m2的主,m2为m1的主)
start slave;
show slave status\G
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
#查看作为从服务器的信息,是否同步开启
配置两台从服务器指向master1
[root@slave1 ~]# vim /etc/my.cnf
server-id = 6 #不能有相同的
log-bin=mysql-bin
log-slave-updates=ture
sync_binlog = 1
[root@slave1 ~]# systemctl restart mysqld
[root@slave1 ~]# netstat -atnp | grep 3306
tcp6 0 0 :::3306 :::* LISTEN 38831/mysqld
#提权,指向master1(两台从操作相同!!!!!)
[root@slave1 ~]# mysql -uroot -p
change master to master_host='主服务器地址',master_user='myslave',master_password='asd123',master_log_file='主服务器查看到的信息',master_log_pos=号码;
##授予权限(slave1、slave2为master1的从)
start slave;
show slave status\G
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
#查看作为从服务器的信息,是否同步开启
修改mmm_common配置文件
[root@master1 ~]# yum -y install mysql-mmm*
#修改MMM配置文件
[root@master1 ~]# vim /etc/mysql-mmm/mmm_common.conf
cluster_interface ens33
......
replication_user myslave #从服务器访问主服务器账户
replication_password asd123
agent_user mmm_user #MMM用户账户
agent_password 123123
ip 192.168.142.135 #master1的IP地址
mode master
peer db2
ip 192.168.142.132 #master2的IP地址
mode master
peer db1
ip 192.168.142.136 #slave1的IP地址
mode slave
ip 192.168.142.137 #slave2的IP地址
mode slave
hosts db1, db2
ips 192.168.142.188 ###VIP虚拟IP地址
mode exclusive
hosts db3, db4
ips 192.168.142.200, 192.168.142.210 ###VIP虚拟IP地址
mode balanced
#scp远程复制,将配置远程推送
[root@master1 ~]# scp /etc/mysql-mmm/mmm_common.conf root@192.168.142.132:/etc/mysql-mmm/
[root@master1 ~]# scp /etc/mysql-mmm/mmm_common.conf root@192.168.142.136:/etc/mysql-mmm/
[root@master1 ~]# scp /etc/mysql-mmm/mmm_common.conf root@192.168.142.137:/etc/mysql-mmm/
[root@master1 ~]# scp /etc/mysql-mmm/mmm_common.conf root@192.168.142.143:/etc/mysql-mmm/
修改mmm_monitor配置文件(仅monitor监听端)
[root@localhost ~]# vim /etc/mysql-mmm/mmm_mon.conf
......
ping_ips 192.168.142.135,192.168.142.132,192.168.142.136,192.168.142.137 #所有主从服务器IP地址
auto_set_online 5
#等待上线时间
monitor_user mmm_monitor ##创建监控用户
monitor_password 123123
在所有主从数据库中(MMM用户、监控用户)进行提权
[root@master1 ~]# mysql -u root -p
grant super,replication client,process on *.* to 'mmm_user'@'192.168.142.%' identified by '123123';
grant replication client on *.* to 'mmm_monitor'@'192.168.142.%' identified by '123123';
flush privileges
##每台主从都要提权
修改agant配置文件(每台主从都要修改)
位置:/etc/mysql-mmm/mmm_agent.conf
[root@master1 ~]# vim /etc/mysql-mmm/mmm_agent.conf
this db1(对应的角色)
所有主从服务器开启mmm_agant服务
[root@master1 ~]# systemctl start mysql-mmm-agent.service
[root@master1 ~]# systemctl enable mysql-mmm-agent.service
监控端开启mmm_monitor服务
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl start mysql-mmm-monitor.service
[root@localhost ~]# mmm_control show ##查看监控状态
[root@localhost mysql-mmm]# mmm_control show
db1(192.168.142.135) master/ONLINE. Roles: writer(192.168.18.250)
db2(192.168.142.132) master/ONLINE. Roles:
db3(192.168.142.136) slave/ONLINE. Roles: reader(192.168.18.251)
db4(192.168.142.137) slave/ONLINE. Roles: reader(192.168.18.252)
//利用命令调整虚拟IP切换至master2:
[root@localhost mysql-mmm]# mmm_control move_role writer db2
OK: Role 'writer' has been moved from 'db1' to 'db2'. Now you can wait some time and check new roles info!
[root@localhost mysql-mmm]# mmm_control show
db1(192.168.142.135) master/ONLINE. Roles:
db2(192.168.142.132) master/ONLINE. Roles: writer(192.168.18.250)
db3(192.168.142.136) slave/ONLINE. Roles: reader(192.168.18.251)
db4(192.168.142.137) slave/ONLINE. Roles: reader(192.168.18.252)
//检测所有状态是否都正常:
[root@localhost mysql-mmm]# mmm_control checks all
db4 ping [last change: 2019/11/25 15:25:54] OK
db4 mysql [last change: 2019/11/25 15:25:54] OK
db4 rep_threads [last change: 2019/11/25 15:25:54] OK
db4 rep_backlog [last change: 2019/11/25 15:25:54] OK: Backlog is null
db2 ping [last change: 2019/11/25 15:25:54] OK
db2 mysql [last change: 2019/11/25 15:25:54] OK
db2 rep_threads [last change: 2019/11/25 15:25:54] OK
db2 rep_backlog [last change: 2019/11/25 15:25:54] OK: Backlog is null
db3 ping [last change: 2019/11/25 15:25:54] OK
db3 mysql [last change: 2019/11/25 15:25:54] OK
db3 rep_threads [last change: 2019/11/25 15:25:54] OK
db3 rep_backlog [last change: 2019/11/25 15:25:54] OK: Backlog is null
db1 ping [last change: 2019/11/25 15:25:54] OK
db1 mysql [last change: 2019/11/25 15:25:54] OK
db1 rep_threads [last change: 2019/11/25 15:25:54] OK
db1 rep_backlog [last change: 2019/11/25 15:25:54] OK: Backlog is null
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。