一、概念
在企业中,数据库高可用一直是企业的重中之重,中小企业很多都是使用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(非抢占模式);
三、演示步骤
环境(主1服务器端IP:192.168.11.7;主2服务器端IP:192.168.11.8)
1、修改配置文件,配置服务器编号,开启bin-log
[root@bi7 ~]# vim /etc/my.cnf.d/server.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
server-id=7
log_bin=mysql-bin
binlog_format=row
relay-log=relay-mysql
relay-log-index=relay-mysql.index
log_slave_updates=on
auto-increment-increment=2
auto-increment-offset=1
[root@bi8 ~]# vim /etc/my.cnf.d/server.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
server-id=8
log_bin=mysql-bin
binlog_format=row
relay-log=relay-mysql
relay-log-index=relay-mysql.index
log_slave_updates=on
auto-increment-increment=2
auto-increment-offset=2
2、重启mysql服务(两台主机)
systemctl restart mariadb
ss -tnl |grep 3306
LISTEN 0 80 :::3306 :::*
3、创建复制用的用户(两台主机)
[root@bi8 ~]# mysql -uroot -proot
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.2.26-MariaDB-log MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the curbit input statement.
MariaDB [(none)]> grant replication slave on *.* to 'master'@'%' identified by '123';
Query OK, 0 rows affected (0.00 sec)
4、(若第一次同步可忽略此步骤)这里可以清空两台主机的master和slave日志(若之前有开启过同步的,需要先停止同步:stop slave;)
在mysql命令行中输入:
reset master;
reset slave;
5、查看二进制日志文件的位置
--主1
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 1087 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
--主2
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 520 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
6、为防止配置同步的同时又数据写入,可给数据库加读锁(两台主机):
MariaDB [(none)]> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
7、两台服务器互相连接
--主1连接主2
MariaDB [(none)]> change master to master_host='192.168.11.8',master_user='master',master_password='123',master_log_file='mysql-bin.000001',master_log_pos=520;
Query OK, 0 rows affected (0.01 sec)
--主2连接主1
MariaDB [(none)]> change master to master_host='192.168.11.7',master_user='master',master_password='123',master_log_file='mysql-bin.000001',master_log_pos=1087;
Query OK, 0 rows affected (0.05 sec)
8、启动slave(两台主机)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.01 sec)
9、检查连接状态(两台主机)
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Connecting to master
Master_Host: 192.168.11.7
Master_User: master
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1087
Relay_Log_File: relay-mysql.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Connecting
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 1087
Relay_Log_Space: 256
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 2003
Last_IO_Error: error connecting to master 'master@192.168.11.7:3306' - retry-time: 60 maximum-retries: 86400 message: Can't connect to MySQL server on '192.168.11.7' (113 "No route to host")
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 0
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Replicate_Do_Domain_Ids:
Replicate_Ignore_Domain_Ids:
Parallel_Mode: conservative
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
1 row in set (0.00 sec)
10、添加防火墙规则
[root@bi7 ~]# firewall-cmd --add-port=3306/tcp
success
[root@bi7 ~]# firewall-cmd --add-port=3306/tcp --permanent
success
[root@bi8 ~]# firewall-cmd --add-port=3306/tcp
success
[root@bi8 ~]# firewall-cmd --add-port=3306/tcp --permanent
success
11、再次检查连接状态(两台主机)
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.11.8
Master_User: master
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 520
Relay_Log_File: relay-mysql.000002
Relay_Log_Pos: 555
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 520
Relay_Log_Space: 860
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 8
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Replicate_Do_Domain_Ids:
Replicate_Ignore_Domain_Ids:
Parallel_Mode: conservative
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
1 row in set (0.00 sec)
12、测试
--解锁(两台主机)
MariaDB [(none)]> unlock tables;
Query OK, 0 rows affected (0.00 sec)
--查看主1的原本数据库
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| bi |
+--------------------+
4 rows in set (0.00 sec)
--在主1上创建新库yang
MariaDB [(none)]> create database if not exists yang default character set utf8;
Query OK, 1 row affected (0.00 sec)
--查看主2是否有新建的数据库
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| yang |
+--------------------+
4 rows in set (0.10 sec)
--使用yang数据库
MariaDB [(none)]> use yang;
Database changed
--在主2上创建新表hello,并插入一条数据
MariaDB [yang]> create table hello(id tinyint unsigned primary key auto_increment not null,name varchar(20));
Query OK, 0 rows affected (0.03 sec)
MariaDB [yang]> insert into hello values(0,'李国祥');
Query OK, 1 row affected (0.00 sec)
MariaDB [yang]> select * from hello;
+----+-----------+
| id | name |
+----+-----------+
| 2 | 李国祥 |
+----+-----------+
1 row in set (0.00 sec)
--查看主1是否新插入的数据
MariaDB [(none)]> use yang;
Database changed
MariaDB [yang]> select * from hello;
+----+-----------+
| id | name |
+----+-----------+
| 2 | 李国祥 |
+----+-----------+
1 row in set (0.01 sec)