日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql主主mycat_MySQL数据库主主复制并基于MyCAT实现高可用

發布時間:2025/3/20 数据库 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql主主mycat_MySQL数据库主主复制并基于MyCAT实现高可用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

簡單介紹

數據庫的高可用,首先需要多主多從的支持.那么多主之間怎么同步呢?

不妨先來看一下 MySQL 主從復制

看完主從復制,聰明的你是否已經想到怎么同步多主呢?

沒錯,那就是多主之間互為主從.

廢話不多說,開始配置.

環境描述操作系統:CentOS 7.4 1708

服務器:192.168.0.123

服務器:192.168.0.124

MySQL 主主復制

先將兩個服務器鎖表1

2mysql -u root -p

mysql> FLUSH TABLES WITH READ LOCK;

修改兩個服務器的數據庫配置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服務器配置:

$ vim /etc/my.cnf

[mysqld]

log-bin=mysql-bin

server-id=1 #服務器唯一ID,每臺服務器需不同

auto-increment-increment = 2 #有幾個服務器,這里就是幾

auto-increment-offset = 1

#做主主備份的時候,因為每臺數據庫服務器都可能在同一個表中插入數據,如果表有一個自動增長的主鍵,那么就會在多服務器上出現主鍵沖突。

#解決這個問題的辦法就是讓每個數據庫的自增主鍵不連續。上面兩項說的是,假設需要將來可能需要10臺服務器做備份,將auto-increment-increment設為10。而auto-increment-offset=1表示這臺服務器的序號。從1開始,不超過auto-increment-increment。

binlog-ignore-db = mysql #跳過mysql庫,避免主主復制的沖突

binlog-ignore-db = information_schema,performance_schema #同樣不需要復制

relay_log = mysql-relay-bin #開啟中繼日志,復制線程先把遠程的變化復制到中繼日志中,再執行。

slave-skip-errors = all #跳過所有的sql錯誤

log-slave-updates = 1 #中繼日志執行之后將變化寫入自己的二進制文件

124服務器配置:

$ vim /etc/my.cnf

[mysqld]

log-bin=mysql-bin

server-id=2 #服務器唯一ID,每臺服務器需不同

auto-increment-increment = 2 #有幾個服務器,這里就是幾

auto-increment-offset = 2

#做主主備份的時候,因為每臺數據庫服務器都可能在同一個表中插入數據,如果表有一個自動增長的主鍵,那么就會在多服務器上出現主鍵沖突。

#解決這個問題的辦法就是讓每個數據庫的自增主鍵不連續。上面兩項說的是,假設需要將來可能需要10臺服務器做備份,將auto-increment-increment設為10。而auto-increment-offset=1表示這臺服務器的序號。從1開始,不超過auto-increment-increment。

binlog-ignore-db = mysql #跳過mysql庫,避免主主復制的沖突

binlog-ignore-db = information_schema,performance_schema #同樣不需要復制

relay_log = mysql-relay-bin #開啟中繼日志,復制線程先把遠程的變化復制到中繼日志中,再執行。

slave-skip-errors = all #跳過所有的sql錯誤

log-slave-updates = 1 #中繼日志執行之后將變化寫入自己的二進制文件

重啟兩個服務器 MySQL1systemctl restart mysqld.service

建立賬戶

在 123 服務器上建立只能被 124 服務器帳戶并授權 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 服務器上建立只能被 123 服務器帳戶并授權 slave1

2

3mysql> MySQL -u root -p

mysql> GRANT REPLICATION SLAVE ON *.* to 'user'@'192.168.0.123' identified by 'password';

mysql> FLUSH PRIVILEGES;

查看數據庫 bin-log 狀態

123 服務器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 服務器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 已經要記下來,后面要用到。

指定同步位置

123 服務器執行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 服務器執行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;

啟動 slave 同步進程并查看狀態

123 服務器狀態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 服務器狀態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,才表明狀態正常。

驗證同步

分別創建數據庫

123 服務器1msyql> create database testa;

124 服務器1msyql> create database testb;

查看

123 服務器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 服務器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)

可以看到兩個服務器都有testa和testb數據庫

通過 MyCAT 中間件實現高可用+讀寫分離

MyCAT 介紹和安裝請參考,這里就不再多說。

之前的配置只能說實現了負載讀寫分離,這里主要說一下怎么配置 MyCAT 來實現高可用.

通過看MyCAT 的開發文檔做以下調整

配置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

總結

以上是生活随笔為你收集整理的mysql主主mycat_MySQL数据库主主复制并基于MyCAT实现高可用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。