mysql主主mycat_MySQL数据库主主复制并基于MyCAT实现高可用
簡(jiǎn)單介紹
數(shù)據(jù)庫的高可用,首先需要多主多從的支持.那么多主之間怎么同步呢?
不妨先來看一下 MySQL 主從復(fù)制
看完主從復(fù)制,聰明的你是否已經(jīng)想到怎么同步多主呢?
沒錯(cuò),那就是多主之間互為主從.
廢話不多說,開始配置.
環(huán)境描述操作系統(tǒng):CentOS 7.4 1708
服務(wù)器:192.168.0.123
服務(wù)器:192.168.0.124
MySQL 主主復(fù)制
先將兩個(gè)服務(wù)器鎖表1
2mysql -u root -p
mysql> FLUSH TABLES WITH READ LOCK;
修改兩個(gè)服務(wù)器的數(shù)據(jù)庫配置1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30123服務(wù)器配置:
$ vim /etc/my.cnf
[mysqld]
log-bin=mysql-bin
server-id=1 #服務(wù)器唯一ID,每臺(tái)服務(wù)器需不同
auto-increment-increment = 2 #有幾個(gè)服務(wù)器,這里就是幾
auto-increment-offset = 1
#做主主備份的時(shí)候,因?yàn)槊颗_(tái)數(shù)據(jù)庫服務(wù)器都可能在同一個(gè)表中插入數(shù)據(jù),如果表有一個(gè)自動(dòng)增長(zhǎng)的主鍵,那么就會(huì)在多服務(wù)器上出現(xiàn)主鍵沖突。
#解決這個(gè)問題的辦法就是讓每個(gè)數(shù)據(jù)庫的自增主鍵不連續(xù)。上面兩項(xiàng)說的是,假設(shè)需要將來可能需要10臺(tái)服務(wù)器做備份,將auto-increment-increment設(shè)為10。而auto-increment-offset=1表示這臺(tái)服務(wù)器的序號(hào)。從1開始,不超過auto-increment-increment。
binlog-ignore-db = mysql #跳過mysql庫,避免主主復(fù)制的沖突
binlog-ignore-db = information_schema,performance_schema #同樣不需要復(fù)制
relay_log = mysql-relay-bin #開啟中繼日志,復(fù)制線程先把遠(yuǎn)程的變化復(fù)制到中繼日志中,再執(zhí)行。
slave-skip-errors = all #跳過所有的sql錯(cuò)誤
log-slave-updates = 1 #中繼日志執(zhí)行之后將變化寫入自己的二進(jìn)制文件
124服務(wù)器配置:
$ vim /etc/my.cnf
[mysqld]
log-bin=mysql-bin
server-id=2 #服務(wù)器唯一ID,每臺(tái)服務(wù)器需不同
auto-increment-increment = 2 #有幾個(gè)服務(wù)器,這里就是幾
auto-increment-offset = 2
#做主主備份的時(shí)候,因?yàn)槊颗_(tái)數(shù)據(jù)庫服務(wù)器都可能在同一個(gè)表中插入數(shù)據(jù),如果表有一個(gè)自動(dòng)增長(zhǎng)的主鍵,那么就會(huì)在多服務(wù)器上出現(xiàn)主鍵沖突。
#解決這個(gè)問題的辦法就是讓每個(gè)數(shù)據(jù)庫的自增主鍵不連續(xù)。上面兩項(xiàng)說的是,假設(shè)需要將來可能需要10臺(tái)服務(wù)器做備份,將auto-increment-increment設(shè)為10。而auto-increment-offset=1表示這臺(tái)服務(wù)器的序號(hào)。從1開始,不超過auto-increment-increment。
binlog-ignore-db = mysql #跳過mysql庫,避免主主復(fù)制的沖突
binlog-ignore-db = information_schema,performance_schema #同樣不需要復(fù)制
relay_log = mysql-relay-bin #開啟中繼日志,復(fù)制線程先把遠(yuǎn)程的變化復(fù)制到中繼日志中,再執(zhí)行。
slave-skip-errors = all #跳過所有的sql錯(cuò)誤
log-slave-updates = 1 #中繼日志執(zhí)行之后將變化寫入自己的二進(jìn)制文件
重啟兩個(gè)服務(wù)器 MySQL1systemctl restart mysqld.service
建立賬戶
在 123 服務(wù)器上建立只能被 124 服務(wù)器帳戶并授權(quán) slave1
2
3mysql> MySQL -u root -p
mysql> GRANT REPLICATION SLAVE ON *.* to 'user'@'192.168.0.124' identified by 'password';
mysql> FLUSH PRIVILEGES;
在 124 服務(wù)器上建立只能被 123 服務(wù)器帳戶并授權(quán) slave1
2
3mysql> MySQL -u root -p
mysql> GRANT REPLICATION SLAVE ON *.* to 'user'@'192.168.0.123' identified by 'password';
mysql> FLUSH PRIVILEGES;
查看數(shù)據(jù)庫 bin-log 狀態(tài)
123 服務(wù)器1
2
3
4
5
6
7mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 | 120 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.02 sec)
124 服務(wù)器1
2
3
4
5
6
7mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 120 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.02 sec)
File 和 Position 已經(jīng)要記下來,后面要用到。
指定同步位置
123 服務(wù)器執(zhí)行1
2
3
4
5
6mysql> change master to
-> master_host='192.168.0.124',
-> master_user='user',
-> master_password='password',
-> master_log_file='mysql-bin.000002',
-> master_log_pos=120;
124 服務(wù)器執(zhí)行1
2
3
4
5
6mysql> change master to
-> master_host='192.168.0.123',
-> master_user='user',
-> master_password='password',
-> master_log_file='mysql-bin.000004',
-> master_log_pos=120;
啟動(dòng) slave 同步進(jìn)程并查看狀態(tài)
123 服務(wù)器狀態(tài)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16mysql> start slave;
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.169.0.124
Master_User: user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 120
Relay_Log_File: mysql-relay-bin.000292
Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
...
124 服務(wù)器狀態(tài)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16mysql> start slave;
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.169.0.123
Master_User: user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 120
Relay_Log_File: mysql-relay-bin.000292
Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
...
其中 Slave_IO_Running 與 Slave_SQL_Running 的值都必須為 YES,才表明狀態(tài)正常。
驗(yàn)證同步
分別創(chuàng)建數(shù)據(jù)庫
123 服務(wù)器1msyql> create database testa;
124 服務(wù)器1msyql> create database testb;
查看
123 服務(wù)器1
2
3
4
5
6
7
8
9
10
11msyql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| testa |
| testb |
+--------------------+
5 rows in set (0.01 sec)
124 服務(wù)器1
2
3
4
5
6
7
8
9
10
11msyql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| testa |
| testb |
+--------------------+
5 rows in set (0.01 sec)
可以看到兩個(gè)服務(wù)器都有testa和testb數(shù)據(jù)庫
通過 MyCAT 中間件實(shí)現(xiàn)高可用+讀寫分離
MyCAT 介紹和安裝請(qǐng)參考,這里就不再多說。
之前的配置只能說實(shí)現(xiàn)了負(fù)載讀寫分離,這里主要說一下怎么配置 MyCAT 來實(shí)現(xiàn)高可用.
通過看MyCAT 的開發(fā)文檔做以下調(diào)整
配置1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24<?xml version="1.0"?>
writeType="0" dbType="mysql" dbDriver="native" switchType="2" slaveThreshold="100">
show slave status
重啟 MyCAT1$ mycat restart
完
總結(jié)
以上是生活随笔為你收集整理的mysql主主mycat_MySQL数据库主主复制并基于MyCAT实现高可用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 错误 未找到引用源_你不理解的EXCEL
- 下一篇: github mysql 数据恢复_记一