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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

配置MySQL5.7基于keepalived的GTID的双主复制

發(fā)布時間:2025/3/14 数据库 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 配置MySQL5.7基于keepalived的GTID的双主复制 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

配置MySQL5.7基于GTID的雙主復(fù)制?

?

一、?????? 安裝前規(guī)劃

1.1 ??大體架構(gòu)規(guī)劃如下

?

?

Master1/Slave

192.168.9.71:3306

Master2/Slave

192.168.9.72:3306

?

?

?

數(shù)據(jù)庫搭建過程請參考《mysql5.7部署文檔》

?

二、?????? 雙主復(fù)制搭建

?

?

?

2.1 創(chuàng)建同步用戶repl(master上)

grant replication slave on *.* to 'repl'@'%' identified by '123456';

flush privileges;

?

注:其中'repl'@'%'部分可以修改為'repl'@'xxx.xxx.xxx.*'或'repl'@'xxx.xxx.xxx.xxx'的形式,出于安全目的用以限制網(wǎng)段或IP訪問(其中xxx為具體IP)

?

2.2 Master1 my.cnf添加同步參數(shù)

?

server_id=1

log-bin = /mysql/3306/binlog/bin.log #binlog存放路徑,需要根據(jù)實際情況修改

log_bin_index = /mysql/3306/binlog/bin.index

max-binlog-size=209715200

expire-logs-days = 7

sync-binlog = 1

binlog_format="ROW"

log_bin_trust_function_creators = 1

binlog_cache_size = 2097152

?

上訴參數(shù)數(shù)據(jù)庫搭建過程已經(jīng)配置。

# replication settings #

gtid_mode = on

enforce_gtid_consistency = 1

log_slave_updates

master_info_repository = TABLE

relay_log_info_repository = TABLE

relay_log = /mysql/3306/binlog/relay.log

relay_log_index = /mysql/3306/binlog/relay_log.index

relay_log_purge = 1

relay_log_recovery = 1

report-port = 3306

report-host = 192.168.9.71

slave-rows-search-algorithms = 'INDEX_SCAN,HASH_SCAN'

# new replication settings #

slave-parallel-type = LOGICAL_CLOCK

slave-parallel-workers = 8

slave_preserve_commit_order=1

slave_transaction_retries=128

# auto-increment #

auto-increment-increment = 2???

auto-increment-offset = 1

?

2.3 Master2 my.cnf添加同步參數(shù)

server_id=2

log-bin = /mysql/3306/binlog/bin.log #binlog存放路徑,需要根據(jù)實際情況修改

log_bin_index = /mysql/3306/binlog/bin.index

max-binlog-size=209715200

expire-logs-days = 7

sync-binlog = 1

binlog_format="ROW"

log_bin_trust_function_creators = 1

binlog_cache_size = 2097152

?

上訴參數(shù)數(shù)據(jù)庫搭建過程已經(jīng)配置。

?

# replication settings #

gtid_mode = on

enforce_gtid_consistency = 1

log_slave_updates

master_info_repository = TABLE

relay_log_info_repository = TABLE

relay_log = /mysql/3306/binlog/relay.log

relay_log_index = /mysql/3306/binlog/relay_log.index

relay_log_purge = 1

relay_log_recovery = 1

report-port = 3306

report-host = 192.168.9.72

slave-rows-search-algorithms = 'INDEX_SCAN,HASH_SCAN'

# new replication settings #

slave-parallel-type = LOGICAL_CLOCK

slave-parallel-workers = 8

slave_preserve_commit_order=1

slave_transaction_retries=128

# auto-increment #

auto-increment-increment = 2???

auto-increment-offset = 2

?

注:

1)?? 以上添加參數(shù)如已在my.cnf文件里則無需添加,其中主庫server_id的數(shù)字不能和備庫server_id相同,

一般一主一從架構(gòu)中,備庫server_id默認設(shè)為2即可

2)?? log-slave-updates參數(shù)用來控制slave庫是否把replication的event也寫進binlog,如果是M->S1->S2的級聯(lián)同步架構(gòu),則S1(第一個slave庫)需要打開binlog和把log-slave-updates參數(shù)設(shè)為1

3)?? auto_increment_increment 控制列中的值的增量值,也就是步長。?

4)?? auto_increment_offset 確定AUTO_INCREMENT列值的起點,也就是初始值。

?

在主主同步配置時,需要將兩臺服務(wù)器的:

auto_increment_offset??????? 分別配置為1和2。這是序號,第一臺從1開始,第二臺就是2,以此類推.....

auto_increment_increment???? 增長量都配置為2

2.4 ?Master1備份與Master2導(dǎo)入

2.4.1???????? mysqldump備份

Master1備份:

mysqldump -uroot -p --single-transaction --default-character-set=utf8 --master-data=2 --flush-logs --triggers --routines --events ?--all-databases > allDB.`hostname`_`date +"%Y%m%d%H%M"`.sql

Master2導(dǎo)入:

mysql> reset master;

Query OK, 0 rows affected (0.01 sec)

mysql -uroot -p < allDB.`hostname`_`date +"%Y%m%d%H%M"`.sql

?

?

2.4.2???????? 配置同步

Master1配置同步參數(shù)

?

CHANGE MASTER TO

?? MASTER_HOST = '192.168.9.72',

?? MASTER_PORT = 3306,

?? MASTER_USER = 'repl',

?? MASTER_PASSWORD = '123456',

?? MASTER_AUTO_POSITION = 1;

?

?

Master2配置同步參數(shù)

CHANGE MASTER TO

?? MASTER_HOST = '192.168.9.71',

?? MASTER_PORT = 3306,

?? MASTER_USER = 'repl',

?? MASTER_PASSWORD = '123456',

?? MASTER_AUTO_POSITION = 1;

?

注:

master_host參數(shù)配置主庫IP地址

master_port參數(shù)配置主庫端口號

master_user參數(shù)配置主庫的同步用戶名

master_password參數(shù)配置主庫同步用戶的密碼

?

2.4.3???????? 開啟同步并查看狀態(tài)(兩臺主機分別執(zhí)行)

mysql> start slave ;

Query OK, 0 rows affected (0.04 sec)

mysql> show slave status\G;

*************************** 1. row ***************************

?????????????? Slave_IO_State: Waiting for master to send event

????????????????? Master_Host: 192.168.9.71

????????????????? Master_User: repl

????????????????? Master_Port: 3306

??????????????? Connect_Retry: 60

????????????? Master_Log_File: bin.000004

????????? Read_Master_Log_Pos: 194

?????????????? Relay_Log_File: relay.000002

??????????????? Relay_Log_Pos: 355

??????? Relay_Master_Log_File: bin.000004

???????????? 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: 194

????????????? Relay_Log_Space: 552

????????????? 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: 1

????????????????? Master_UUID: 15c4b75c-cd03-11e9-be6b-080027364db6

???????????? Master_Info_File: mysql.slave_master_info

??????????????????? SQL_Delay: 0

????????? SQL_Remaining_Delay: NULL

????? Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates

?????????? Master_Retry_Count: 86400

????????????????? Master_Bind:

????? Last_IO_Error_Timestamp:

???? Last_SQL_Error_Timestamp:

?????????????? Master_SSL_Crl:

?????????? Master_SSL_Crlpath:

?????????? Retrieved_Gtid_Set:

??????????? Executed_Gtid_Set: 15c4b75c-cd03-11e9-be6b-080027364db6:1-231665

??????????????? Auto_Position: 1

???????? Replicate_Rewrite_DB:

???????????????? Channel_Name:

?????????? Master_TLS_Version:

1 row in set (0.00 sec)

?

ERROR:

No query specified

三、?????? 使用Keepalived實現(xiàn)故障轉(zhuǎn)移

3.1 Keepalived安裝

?

1、下載安裝包

http://www.keepalived.org/download.html

Keepalived for Linux - Version 2.0.18

2、編譯安裝Keepalived

tar -zxvf keepalived-2.0.18.tar.gz

cd keepalived-2.0.18

./configure --prefix=/usr/local/keepalived

make && make install

3、將Keepalived注冊為系統(tǒng)服務(wù)

cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

mkdir -p /etc/keepalived

cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf

cp /mysql/keepalived-2.0.18/keepalived/etc/init.d/keepalived /etc/init.d/keepalived

3.2 Keepalived配置,實現(xiàn)故障轉(zhuǎn)移

備注:Keepalived使用vip 192.168.9.73實現(xiàn)故障轉(zhuǎn)移。

Master1:/etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {?

???? notification_email {?

???? figo@live.cn?

???? }?

???? notification_email_from luwenju@live.cn?

???? smtp_server 127.0.0.1?

???? smtp_connect_timeout 30?

???? router_id MySQL-ha?

???? script_user root

???? enable_script_security

???? }?

?

vrrp_instance VI_1 {?

???? state BACKUP?

???? interface eth1?

???? virtual_router_id 51?

???? priority 100??

???? advert_int 1?

???? nopreempt??

???? authentication {?

???? auth_type PASS?

???? auth_pass 1111?

???? }?

???? virtual_ipaddress {?

???? 192.168.9.73

???? }?

???? }?

?

virtual_server 192.168.9.73 3306 {?

???? delay_loop 2??

???? lb_algo wrr???

???? lb_kind DR??

???? persistence_timeout 60??

???? protocol TCP?

???? real_server 192.168.9.71 3306 {?

???? weight 3?

???? notify_down /usr/local/MySQL.sh?

???? TCP_CHECK {?

???? connect_timeout 10??

???? nb_get_retry 3??????

???? delay_before_retry 3??

???? connect_port 3306????

???? }?

???? }

}

Master2:/etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {?

???? notification_email {?

???? figo@live.cn?

???? }?

???? notification_email_from luwenju@live.cn?

???? smtp_server 127.0.0.1?

???? smtp_connect_timeout 30?

???? router_id MySQL-ha?

???? script_user root

???? enable_script_security

???? }?

?

vrrp_instance VI_1 {?

???? state BACKUP?

???? interface eth1?

???? virtual_router_id 51?

???? priority 90??

???? advert_int 1?

??? # nopreempt??

???? authentication {?

???? auth_type PASS?

???? auth_pass 1111?

???? }?

???? virtual_ipaddress {?

???? 192.168.9.73?

???? }?

???? }?

?

virtual_server 192.168.9.73 3306 {?

???? delay_loop 2??

???? lb_algo wrr???

???? lb_kind DR??

???? persistence_timeout 60??

???? protocol TCP?

???? real_server 192.168.9.72 3306 {?

???? weight 3?

???? notify_down /usr/local/MySQL.sh?

???? TCP_CHECK {?

???? connect_timeout 10??

???? nb_get_retry 3??????

???? delay_before_retry 3??

???? connect_port 3306 ????

???? }?

???? }

}

3.3 Keepalived依賴腳本

MySQL.sh 當(dāng)mysql服務(wù)down時,執(zhí)行此腳本,殺死keepalived實現(xiàn)切換

?

check_Mysql_KeepAlived.sh 是為了檢查mysqld進程是否存活的腳本,當(dāng)發(fā)現(xiàn)連接不上mysql,自動把keepalived進程殺死,讓VIP進行漂移。或者mysqld存活時自動啟動未啟動的keepalived進程

?

[root@oracle12c local]# cat /usr/local/MySQL.sh

#!/bin/sh?

pkill keepalived

[root@oracle12c local]# cat /usr/local/check_Mysql_KeepAlived.sh

#!/bin/bash

?

log_dir=/mysql/check_log

check_time=`date "+%Y-%m-%d %H:%M:%S"`

log_time=`date +%F`

log=$log_dir/check_${log_time}.log

?

?

. /root/.bash_profile

?

count=1

?

while true

do

?

mysql -uroot -p12345678 -S /var/lib/mysql/mysql.sock -e "show status;" > /dev/null 2>&1

i=$?

ps aux | grep mysqld | grep -v grep > /dev/null 2>&1

j=$?

if [ $i = 0 ] && [ $j = 0 ]

then

?? ps aux|grep keepalived|grep -v grep > /dev/null 2>&1

?? k=$?

?? if [ $k = 1 ]

?? then

?? /etc/init.d/keepalived start > /dev/null 2>&1

?? echo "$check_time,service mysqld is running,keepalived not active,start keepalived.">>$log

?? exit 0

?? else

?? echo "$check_time,services are running.">>$log

?? exit 0

?? fi

else

?? if [ $i = 1 ] && [ $j = 0 ]

?? then

?????? exit 0

?? else

??????? if [ $count -gt 5 ]

??????? then

??????????? ps aux|grep keepalived|grep -v grep > /dev/null 2>&1

??????????? rlk=$?

??????????? if [ $rlk = 0 ]

??????????? then

??????????????? pkill keepalived > /dev/null 2>&1

??????????????? echo "$check_time,service mysqld is not active,keepalived shoud be closed.">>$log

??????????????? break

??????????? else

?????????????????? echo "$check_time,services are not active.">>$log

?????????????????? break

???????? ??? fi

??????? fi

?? let count++

?? continue

?? fi

fi

?

done

?

?

腳本部署完畢后,修改部分內(nèi)容,賦執(zhí)行權(quán)限并配置定時任務(wù)

chmod +x /usr/local/check_Mysql_KeepAlived.sh

chmod +x /usr/local/MySQL.sh

crontab -e

*/1 * * * * /usr/local/check_Mysql_KeepAlived.sh

?

3.4 故障轉(zhuǎn)移測試

開啟Keepslave進程

service keepalived start

service keepalived status

檢查狀態(tài)

tail -f /var/log/messages

ip a

?

先在服務(wù)器端授權(quán)遠程連接:

mysql> grant all privileges on *.* to 'root'@'%' identified by '123456';

mysql> flush privileges;

?

然后在第三臺服務(wù)器上用vip連接數(shù)據(jù)庫:

mysql -uroot -p -h 192.168.9.73

mysql> show variables like '%host%';

+-------------------------------+--------+

| Variable_name???????????????? | Value? |

+-------------------------------+--------+

| host_cache_size?????????????? | 653??? |

| hostname????????????????????? | mysql4 |

| performance_schema_hosts_size | 100??? |

| report_host?????????????????? |??????? |

+-------------------------------+--------+

?

如果停止當(dāng)前連接節(jié)點mysql4 mysql服務(wù):

service mysqld stop

Shutting down MySQL.............[? OK? ]

?

過一會兒查看目標(biāo)連接:

mysql> show variables like '%host%';

+-------------------------------+--------+

| Variable_name???????????????? | Value? |

+-------------------------------+--------+

| host_cache_size?????????????? | 653??? |

| hostname????????????????????? | mysql3 |

| performance_schema_hosts_size | 100??? |

| report_host?????????????????? |??????? |

+-------------------------------+--------+

4 rows in set (0.00 sec)

?

此時再開啟mysql4上的mysql服務(wù):

service mysqld start

Starting MySQL...[? OK? ]

?

再次在客戶端查看連接:

mysql> show variables like '%host%';

+-------------------------------+--------+

| Variable_name???????????????? | Value? |

+-------------------------------+--------+

| host_cache_size?????????????? | 653??? |

| hostname????????????????????? | mysql3 |

| performance_schema_hosts_size | 100??? |

| report_host?????????????????? |??????? |

+-------------------------------+--------+

4 rows in set (0.00 sec)

四、?????? 附錄

4.1???????????? keepalived.conf參數(shù)詳解

global_defs {?

???? notification_email {?

???? figo@live.cn?

???? }?

???? notification_email_from? figo@live.cn?

???? smtp_server 127.0.0.1?

???? smtp_connect_timeout 30?

???? router_id MySQL-ha? #標(biāo)識,雙主相同

???? }?

?

vrrp_instance VI_1 {?

???? state BACKUP? #兩臺都設(shè)置BACKUP

???? interface eth0?

???? virtual_router_id 51?? #主備相同

???? priority 100??? #優(yōu)先級,backup設(shè)置90

???? advert_int 1?

???? nopreempt??? #不主動搶占資源,只在master這臺優(yōu)先級高的設(shè)置,backup不設(shè)置

? ???authentication {?

???? auth_type PASS?

???? auth_pass 1111?

???? }?

???? virtual_ipaddress {?

???? 192.168.9.57?

???? }?

???? }?

?

virtual_server 192.168.9.57 3306 {?

???? delay_loop 2??

???? lb_algo wrr??? #LVS算法

???? lb_kind DR???? #LVS模式

? ???persistence_timeout 60?? #同一IP的連接60秒內(nèi)被分配到同一臺真實服務(wù)器

???? protocol TCP?

???? real_server 192.168.9.55 3306 {? #檢測本地mysql,backup也要寫檢測本地mysql

???? weight 3?

???? notify_down /usr/local/MySQL.sh?? #當(dāng)mysq服down時,執(zhí)行此腳本,殺死keepalived實現(xiàn)切換

???? TCP_CHECK {?

???? connect_timeout 10??

???? nb_get_retry 3???????? #連接超時

???? delay_before_retry 3?? #重試次數(shù)

???? connect_port 3306????? #重試間隔時間

???? }?

???? }

}

?

4.2???????????? 遺留問題

1、? Keepalived腦裂問題解決思路。

2、? 開啟GTID雙主同步后備份恢復(fù)注意事項。

轉(zhuǎn)載于:https://www.cnblogs.com/JcLevy/p/11462746.html

總結(jié)

以上是生活随笔為你收集整理的配置MySQL5.7基于keepalived的GTID的双主复制的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。