一、概念
在企业中,数据库高可用一直是企业的重中之重,中小企业很多都是使用mysql主从方案,一主多从,读写分离等,但是单主存在单点故障,从库切换成主库需要作改动。因此,如果是双主或者多主,就会增加mysql入口,增加高可用。不过多主需要考虑自增长ID问题,这个需要特别设置配置文件,比如双主,可以使用奇偶,总之,主之间设置自增长ID相互不冲突就能完美解决自增长ID冲突问题。
单点故障解决方案:
主主架构:
互为主备,互相监控对方二进制日志文件进行同步
note:当两个sql语句发生冲突的时候主主架构有可能出现数据不一致的现象;
MHA(master high availability):
HMA可以有多个配置文件,一个配置文件监控一个主从架构
二、主主架构思路
1、两台mysql都可读可写,互为主备,默认只使用一台(masterA)负责数据的写入,另一台(masterB)备用;
2、masterA是masterB的主库,masterB又是masterA的主库,它们互为主从;
3、两台主库之间做高可用,可以采用keepalived等方案(使用VIP对外提供服务);
4、所有提供服务的从服务器与masterB进行主从同步(双主多从);
5、建议采用高可用策略的时候,masterA或masterB均不因宕机恢复后而抢占VIP(非抢占模式);
可以增加这两个参数,使两个主库可以异步写入且能区分
auto_increment_offset=2 <
auto_increment_increment=2 <
三、演示步骤
环境(主1服务器端IP:10.1.1.11;主2服务器端IP:10.0.0.12)
yum源:
[mysql57-community]
name=MySQL 5.7Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1gpgcheck=0
yum -y remove mariadb*
yum -y install mysql-community-server
mysql-p`cat /var/log/mysqld.log |grep password |head -1 |awk '{print $NF}'`
alter user'root'@'localhost' identified by 'Ryz0304/1';
vim /root/.my.cnf
[mysql]
user=root
password='Ryz0304/1'
主1(10.0.0.11)
1、修改主1配置文件,配置服务器编号,开启bin-log
vim /etc/my.cnf
##############
server-id=1
log-bin=binlog##############
2、重启主1mysql服务
systemctl restart mysqld
systemctl enable mysqld
firewall-cmd --add-service=mysql
firewall-cmd --add-service=mysql --permanent
3、创建复制用的用户(主1)
mysql> grant replication slave on *.* to 'repl'@'10.0.0.12' identified by'Repl0304/1';
Query OK,0 rows affected, 1 warning (0.00sec)
mysql>flush privileges;
Query OK,0 rows affected (0.00 sec)
4、(若第一次同步可忽略此步骤)这里可以清空两台主机的master和slave日志(若之前有开启过同步的,需要先停止同步:stop slave;)
在mysql命令行中输入:
reset master;
reset slave;
5、查看主1二进制日志文件的位置
mysql>show master status\G*************************** 1. row ***************************File: binlog.000001Position:154Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:1 row in set (0.00 sec)
主2(10.0.0.12)
1、修改主2配置文件,配置服务器编号,开启bin-log
[mysqld]
server-id=2log-bin=binlog
2、重启主2mysql服务
systemctl restart mysqld
systemctl enable mysqld
firewall-cmd --add-service=mysql
firewall-cmd --add-service=mysql --permanent
3、(若第一次同步可忽略此步骤)这里可以清空两台主机的master和slave日志(若之前有开启过同步的,需要先停止同步:stop slave;)
在mysql命令行中输入:
reset master;
reset slave;
4、连接到主1服务器上面
mysql> change master to master_host='10.0.0.11',master_user='repl',master_password='Repl0304/1',master_port=3306,master_log_file='binlog.000001',master_log_pos=154;
Query OK,0 rows affected, 2 warnings (0.01sec)
mysql>start slave;
Query OK,0 rows affected (0.01sec)
mysql>show slave status\G*************************** 1. row ***************************Slave_IO_State: Waitingformaster to send event
Master_Host:10.0.0.11Master_User: repl
Master_Port:3306Connect_Retry:60Master_Log_File: binlog.000001Read_Master_Log_Pos:154Relay_Log_File: s2-relay-bin.000002Relay_Log_Pos:317Relay_Master_Log_File: binlog.000001Slave_IO_Running: Yes
Slave_SQL_Running: Yes
5、查看主2二进制日志文件的位置
mysql>show master status\G*************************** 1. row ***************************File: binlog.000001Position:154Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:1 row in set (0.00 sec)
6、创建复制用的用户(主2)
mysql> grant replication slave on *.* to 'repl'@'10.0.0.11' identified by'Repl0304/1';
Query OK,0 rows affected, 1 warning (0.00sec)
mysql>flush privileges;
Query OK,0 rows affected (0.00 sec)
主1(10.0.0.11)
连接到主2服务器上
mysql> change master to master_host='10.0.0.12',master_user='repl',masterr_password='Repl0304/1',master_port=3306,master_log_file='binlog.000001',,master_log_pos=154;
Query OK,0 rows affected, 2 warnings (0.01sec)
mysql>start slave;
Query OK,0 rows affected (0.01sec)
mysql>show slave status\G*************************** 1. row ***************************Slave_IO_State: Waitingformaster to send event
Master_Host:10.0.0.12Master_User: repl
Master_Port:3306Connect_Retry:60Master_Log_File: binlog.000001Read_Master_Log_Pos:597Relay_Log_File: s1-relay-bin.000002Relay_Log_Pos:760Relay_Master_Log_File: binlog.000001Slave_IO_Running: Yes <
Slave_SQL_Running: Yes <
测试
1、在主2服务器上创建库,创建表并插入数据
mysql>create database ren;
Query OK,1 row affected (0.00sec)
mysql>use ren;
Database changed
mysql> create table tb1(id int primary key auto_increment,name char(30));
Query OK,0 rows affected (0.01sec)
mysql> insert into tb1(name) values('rr'),('ryz'),('dad');
Query OK,3 rows affected (0.13sec)
Records:3 Duplicates: 0 Warnings: 0mysql> select *from tb1;+----+------+
| id | name |
+----+------+
| 1 | rr |
| 2 | ryz |
| 3 | dad |
+----+------+
3 rows in set (0.00 sec)
2、登录主2服务器并检查是否已经有主1上面的数据
mysql>show databases;+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| ren |
| sys |
+--------------------+
5 rows in set (0.07sec)
mysql> select *from ren.tb1;+----+------+
| id | name |
+----+------+
| 1 | rr |
| 2 | ryz |
| 3 | dad |
+----+------+
3 rows in set (0.00 sec)