cyhgogogo 2019-12-15
master1IP地址:192.168.144.167 master2IP地址:192.168.144.151 slave1IP地址:192.168.144.168 slave2IP地址:192.168.144.145 monitorIP地址:192.168.144.164
[ ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo //获取源地址 [ ~]# yum -y install epel-release //安装epel源 [ ~]# yum clean all && yum makecache //yum缓存清空 [ ~]# yum -y install mariadb-server mariadb //安装mariadb数据库 [ ~]# systemctl stop firewalld.service //关闭防火墙 [ ~]# setenforce 0 [ ~]# systemctl start mariadb.service //开启数据库
[ ~]# vim /etc/my.cnf //进入编辑配置文件 [mysqld] log_error=/var/lib/mysql/mysql.err //错误日志文件 log=/var/lib/mysql/mysql_log.log //主从日志存放位置 log_slow_queries=/var/lib/mysql_slow_queris.log //man日志 binlog-ignore-db=mysql,information_schema //二进制文件 character_set_server=utf8 //字符集 log_bin=mysql_bin //二进制日志文件 server_id=11 //服务id(不能相同) log_slave_updates=true //允许从服务器更新 sync_binlog=1 //同步日志 auto_increment_increment=2 //自增列 auto_increment_offset=1 //起始点 [ ~]# systemctl restart mariadb.service //重启数据库
[ ~]# scp /etc/my.cnf :/etc/ (server-id=22) [ ~]# scp /etc/my.cnf :/etc/ (server-id=33) [ ~]# scp /etc/my.cnf :/etc/ (server-id=44) [ ~]# netstat -anpt | grep 3306 //查看端口3306 tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 4235/mysqld
[ ~]# mysql ##进入数据库 MariaDB [(none)]> 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) MariaDB [(none)]> grant replication slave on *.* to ‘replication‘@‘192.168.144.%‘ identified by ‘123456‘; //授权给144段的网段复制的权限用户名replication密码123456 Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> change master to master_host=‘192.168.144.167‘,master_user=‘replication‘,master_password=‘123456‘,master_log_file=‘mysql_bin.000001‘,master_log_pos=245; //在master2上同步master1服务器 Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> grant replication slave on *.* to ‘replication‘@‘192.168.144.%‘ identified by ‘123456‘; //在master2上授权复制权限 Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> show master status; //查看master2的服务器的状态信息 +------------------+----------+--------------+--------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+--------------------------+ | mysql_bin.000001 | 410 | | mysql,information_schema | +------------------+----------+--------------+--------------------------+ 1 row in set (0.00 sec) MariaDB [(none)]> flush privileges; //刷新权限 Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> change master to master_host=‘192.168.144.151‘,master_user=‘replication‘,master_password=‘123456‘,master_log_file=‘mysql_bin.000001‘,master_log_pos=410; //master1同步master2服务器数据库 Query OK, 0 rows affected (0.03 sec) MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> start slave; //开启同步 Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> show slave status\G; //查看同步状态信息 ... Slave_IO_Running: Yes Slave_SQL_Running: Yes ...
MariaDB [(none)]> change master to master_host=‘192.168.144.167‘,master_user=‘replication‘,master_password=‘123456‘,master_log_file=‘mysql_bin.000001‘,master_log_pos=245; //在从服务器上同步master1主服务器 Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> flush privileges; //刷新权限 Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> start slave; //开启同步 Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> show slave status\G; //查看同步的状态信息 ... Slave_IO_Running: Yes Slave_SQL_Running: Yes ...
MariaDB [(none)]> create database school; //创建数据库 Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> show databases; //查看数据库 +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | school | | test | +--------------------+ 5 rows in set (0.00 sec)
MariaDB [(none)]> show databases; //实现主主,主从同步 +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | school | | test | +--------------------+ 5 rows in set (0.00 sec)
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 mysql-mmm*
yum -y install mysql-mmm*
[ ~]# vim /etc/mysql-mmm/mmm_common.conf //所有主机上都要配置,直接复制多份 …… <host default> cluster_interface ens33 //修改网卡 … replication_user replication //修改用户名 replication_password 123456 //密码 agent_user mmm_agent agent_password 123456 //密码 <host db1> ip 192.168.144.167 //master1地址 mode master peer db2 </host> <host db2> ip 192.168.144.151 //master2地址 mode master peer db1 </host> <host db3> ip 192.168.144.168 //slave1地址 mode slave </host> <host db4> ip 192.168.144.145 //slave2地址 mode slave </host> <role writer> hosts db1, db2 ##写服务器虚拟ip ips 192.168.144.250 mode exclusive </role> <role reader> hosts db3, db4 ##读服务器虚拟ip ips 192.168.144.251, 192.168.144.252 mode balanced </role> ##复制到其他的服务器中 [ ~]# scp /etc/mysql-mmm/mmm_common.conf :/etc/mysql-mmm/ ‘s password: mmm_common.conf 100% 836 267.1KB/s 00:00 [ ~]# scp /etc/mysql-mmm/mmm_common.conf :/etc/mysql-mmm/ ‘s password: mmm_common.conf 100% 836 863.2KB/s 00:00 [ ~]# scp /etc/mysql-mmm/mmm_common.conf :/etc/mysql-mmm/ ‘s password: mmm_common.conf 100% 836 904.7KB/s 00:00 [ ~]# scp /etc/mysql-mmm/mmm_common.conf :/etc/mysql-mmm/
[ ~]# vim /etc/mysql-mmm/mmm_mon.conf <host default> monitor_user mmm_monitor monitor_password 123456 ##修改monitor的密码 </host>
MariaDB [(none)]> grant super, replication client, process on *.* to ‘mmm_agent‘@‘192.168.144.%‘ identified by ‘123456‘; //授权代理 Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> grant replication client on *.* to ‘mmm_monitor‘@‘192.168.144.%‘ identified by ‘123456‘; //授权监控 Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> flush privileges; //刷新权限 Query OK, 0 rows affected (0.00 sec)
[ ~]# vim /etc/mysql-mmm/mmm_agent.conf this db1 //根据规划进行逐一调整 [ ~]# vim /etc/mysql-mmm/mmm_agent.conf this db2 //根据规划进行逐一调整 [ ~]# vim /etc/mysql-mmm/mmm_agent.conf this db3 //根据规划进行逐一调整 [ ~]# vim /etc/mysql-mmm/mmm_agent.conf this db4 //根据规划进行逐一调整 ##所有数据库开启 [ ~]systemctl start mysql-mmm-agent.service //开启代理服务 [ ~]systemctl enable mysql-mmm-agent.service //加入开机自启动
[ ~]# vim /etc/mysql-mmm/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.167,192.168.144.151,192.168.144.168,192.168.144.145 //所有数据库服务器地址 auto_set_online 10 //自动在线时间 [ ~]# systemctl stop firewalld.service //关闭防火墙 [ ~]# setenforce 0 [ ~]# systemctl start mysql-mmm-monitor.service /开启监控服务 [ ~]# mmm_control show //查看主从的飘逸地址 db1(192.168.144.167) master/ONLINE. Roles: writer(192.168.144.250) db2(192.168.144.151) master/ONLINE. Roles: db3(192.168.144.168) slave/ONLINE. Roles: reader(192.168.144.252) db4(192.168.144.145) slave/ONLINE. Roles: reader(192.168.144.251)
[ ~]# systemctl stop mariadb.service //模拟停止master1服务器
[ ~]# mmm_control show db1(192.168.144.167) master/HARD_OFFLINE. Roles: db2(192.168.144.151) master/ONLINE. Roles: writer(192.168.144.250) db3(192.168.144.168) slave/ONLINE. Roles: reader(192.168.144.252) db4(192.168.144.145) slave/ONLINE. Roles: reader(192.168.144.251) (重启master1数据库服务,虚拟地址不会被抢占到master1) mmm_control checks all //需要各种OK mmm_control move_role writer db1 //可以切换虚拟地址
[ ~]# yum install mysql -y //master1服务器上授权monitor地址访问// MariaDB [(none)]> grant all on *.* to ‘testdba‘@‘192.168.144.164‘ identified by ‘123456‘; ##授权monitor地址访问 Query OK, 0 rows affected (0.01 sec) MariaDB [(none)]> flush privileges; ##刷新权限 Query OK, 0 rows affected (0.00 sec) [ ~]# mysql -utestdba -p -h 192.168.144.250 ##使用虚拟地址即可登录数据库 Enter password: MariaDB [(none)]>