欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MySQL 搭建雙主復(fù)制服務(wù)并通過 HAProxy 負(fù)載均衡的過程詳解

 更新時(shí)間:2024年03月19日 15:39:19   作者:小畢超  
在數(shù)據(jù)庫(kù)管理中,數(shù)據(jù)的備份和同步是至關(guān)重要的環(huán)節(jié),而雙主復(fù)制(Dual Master Replication)作為一種高可用性和數(shù)據(jù)同步的解決方案,本文將介紹MySQL雙主復(fù)制的配置過程并通過 HAProxy 負(fù)載均衡,感興趣的朋友一起看看吧

一、MySQL 搭建雙主復(fù)制高可用服務(wù)

在數(shù)據(jù)庫(kù)管理中,數(shù)據(jù)的備份和同步是至關(guān)重要的環(huán)節(jié),而雙主復(fù)制(Dual Master Replication)作為一種高可用性和數(shù)據(jù)同步的解決方案,通過讓兩個(gè)數(shù)據(jù)庫(kù)實(shí)例同時(shí)充當(dāng)主服務(wù)器和從服務(wù)器,MySQL雙主復(fù)制可以實(shí)現(xiàn)數(shù)據(jù)的雙向同步,為數(shù)據(jù)庫(kù)系統(tǒng)提供了更靈活和可靠的解決方案。即使其中一個(gè)主服務(wù)器發(fā)生故障,另一個(gè)主服務(wù)器仍然可以繼續(xù)提供服務(wù),確保系統(tǒng)的穩(wěn)定性和可用性。這種數(shù)據(jù)同步方式不僅可以加強(qiáng)數(shù)據(jù)的備份與恢復(fù)能力,還可以提高系統(tǒng)的擴(kuò)展性,適用于需要高度數(shù)據(jù)一致性和容錯(cuò)性的場(chǎng)景。

本文將介紹MySQL雙主復(fù)制的配置過程,整體實(shí)現(xiàn)架構(gòu)如下:

主機(jī)規(guī)劃:

ip規(guī)劃
172.19.222.20MySQL1
172.19.222.82MySQL2
172.19.222.84Haproxy

二、MySQL1 部署

mysql 的部署這里采用 docker + docker-compose 的方式快速實(shí)現(xiàn),主要關(guān)注點(diǎn)在于配置文件:

首先編寫 my.cnf 文件,寫入如下內(nèi)容:

[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
secure-file-priv= NULL
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# 主從同步
server-id = 1
log-bin = mysql-bin
sync_binlog = 1
binlog_checksum = none
binlog_format = mixed
auto-increment-increment = 2
auto-increment-offset = 1
slave-skip-errors = all
event_scheduler = 1
max_allowed_packet = 64M
# Custom config should go here
!includedir /etc/mysql/conf.d/

其中主從復(fù)制的參數(shù)解釋如下:

server-id = 1:指定MySQL實(shí)例的唯一標(biāo)識(shí)ID,用于在主從復(fù)制中區(qū)分不同的MySQL實(shí)例。

log-bin = mysql-bin:?jiǎn)⒂胋inlog日志,并指定binlog文件的前綴名為mysql-bin。binlog用于記錄所有的數(shù)據(jù)更改操作,以便主從服務(wù)器之間進(jìn)行數(shù)據(jù)同步。

sync_binlog = 1:表示每次事務(wù)提交時(shí)都將強(qiáng)制把binlog緩沖區(qū)的內(nèi)容寫入磁盤,確保binlog日志及時(shí)持久化,提高數(shù)據(jù)安全性。

binlog_checksum = none:設(shè)置binlog文件的校驗(yàn)方式為none,表示不對(duì)binlog文件進(jìn)行校驗(yàn)和驗(yàn)證。

binlog_format = mixed:指定binlog日志的格式為mixed,即混合模式,根據(jù)具體情況自動(dòng)選擇使用statement或row格式記錄數(shù)據(jù)變更。

auto-increment-increment = 2:設(shè)置自增長(zhǎng)字段的增量值為2,用于在主從復(fù)制中避免自增字段沖突。

auto-increment-offset = 1:設(shè)置自增長(zhǎng)字段的偏移量為1,用于在主從復(fù)制中避免自增字段沖突。

slave-skip-errors = all:當(dāng)從服務(wù)器在執(zhí)行SQL線程時(shí)發(fā)生錯(cuò)誤時(shí),跳過所有錯(cuò)誤繼續(xù)執(zhí)行,這可能會(huì)導(dǎo)致數(shù)據(jù)不一致,謹(jǐn)慎使用。

event_scheduler = 1:?jiǎn)⒂肊vent Scheduler,用于執(zhí)行預(yù)定的事件任務(wù)。

max_allowed_packet = 64M:設(shè)置最大允許的數(shù)據(jù)包大小為64MB,用于控制單個(gè)數(shù)據(jù)庫(kù)請(qǐng)求或查詢的數(shù)據(jù)包大小限制。

編寫 docker-compose.yml 文件:

version: '2.0'
services:         
  mysql:
    restart: always
    image: mysql:8.0.20
    container_name: mysql
    volumes:
      - ./data:/var/lib/mysql
      - ./my.cnf:/etc/mysql/my.cnf
    command:
      --lower_case_table_names=1
      --character-set-server=utf8
      --sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
    ports:
      - "3306:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=root123
      - TZ=Asia/Shanghai 

啟動(dòng)服務(wù):

docker-compose up -d

下面使用客戶端工具連接該數(shù)據(jù)庫(kù):

查看當(dāng)前 master 狀態(tài):

show master status;

注意這里查詢出來(lái)的 FilePosition 下面主從復(fù)制時(shí)會(huì)用到。

創(chuàng)建用于主從復(fù)制的用戶:

CREATE USER 'replica'@'%' IDENTIFIED WITH mysql_native_password BY 'replica123';
GRANT REPLICATION SLAVE ON *.* TO 'replica'@'%';
FLUSH PRIVILEGES;

三、MySQL2 部署

同樣編寫 my.cnf 文件,寫入如下內(nèi)容:

[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
secure-file-priv= NULL
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# 主從同步
server-id = 2
log-bin = mysql-bin
sync_binlog = 1
binlog_checksum = none
binlog_format = mixed
auto-increment-increment = 2
auto-increment-offset = 2
slave-skip-errors = all
event_scheduler = 1
max_allowed_packet = 64M
# Custom config should go here
!includedir /etc/mysql/conf.d/

配置和MySQL1差不多,主要注意 server-idauto-increment-offset 的區(qū)別。

編寫 docker-compose.yml 文件:

version: '2.0'
services:         
  mysql:
    restart: always
    image: mysql:8.0.20
    container_name: mysql
    volumes:
      - ./data:/var/lib/mysql
      - ./my.cnf:/etc/mysql/my.cnf
    command:
      --lower_case_table_names=1
      --character-set-server=utf8
      --sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
    ports:
      - "3306:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=root123
      - TZ=Asia/Shanghai 

啟動(dòng)服務(wù):

docker-compose up -d

下面使用客戶端工具連接該數(shù)據(jù)庫(kù):

查看當(dāng)前 master 狀態(tài):

show master status;

同樣這里查詢出來(lái)的 FilePosition 下面主從復(fù)制時(shí)會(huì)用到。

創(chuàng)建用于主從復(fù)制的用戶:

CREATE USER 'replica'@'%' IDENTIFIED WITH mysql_native_password BY 'replica123';
GRANT REPLICATION SLAVE ON *.* TO 'replica'@'%';
FLUSH PRIVILEGES;

四、配置主主復(fù)制

MySQL 1 同步 MySQL 2

MySQL1 中執(zhí)行 :

change master to master_host='172.19.222.82',master_user='replica',master_password='replica123',master_log_file='mysql-bin.000003',master_log_pos=152;

注意:其中 master_log_filemaster_log_pos 是上面部署 MySQL 2 時(shí) show master status; 查詢的結(jié)果。

啟動(dòng)同步進(jìn)程:

start slave;

查看同步狀態(tài):

show slave status\G

看到 Slave_IO_RunningSlave_SQL_Running 都為 Yes 則表示啟動(dòng)成功。

MySQL 2 同步 MySQL 1

MySQL 2 中執(zhí)行 :

change master to master_host='172.19.222.20',master_user='replica',master_password='replica123',master_log_file='mysql-bin.000003',master_log_pos=811;

其中 master_log_filemaster_log_pos 是上面部署 MySQL 1 時(shí) show master status; 查詢的結(jié)果。

啟動(dòng)同步進(jìn)程:

start slave;

查看同步狀態(tài):

show slave status\G

同樣觀察 Slave_IO_RunningSlave_SQL_Running 都為 Yes 則表示啟動(dòng)成功。

五、測(cè)試主主復(fù)制

首先在 MySQL1 中創(chuàng)建數(shù)據(jù)庫(kù) testdb:

create database testdb;

然后在 MySQL 2 中查看數(shù)據(jù)庫(kù):

show databases;

可以正常查到 MySQL 1 中創(chuàng)建的數(shù)據(jù)庫(kù)。

然后在 MySQL 2 中創(chuàng)建測(cè)試表:

use testdb;
create table `test` (
  `id` int not null auto_increment,
  `name` varchar(255) default null,
  primary key (`id`)
) engine=InnoDB default charset=utf8mb4 collate=utf8mb4_0900_ai_ci;

接著在 MySQL 1 中查看表:

use testdb;
show tables;

可以正??吹?MySQL 2 創(chuàng)建的表。

然后在 MySQL 1 中寫入一條測(cè)試數(shù)據(jù):

insert into test(name) values('小明');

然后到 MySQL 2 中查看表數(shù)據(jù):

select * from test;

可以正常查到 MySQL 1 寫入的數(shù)據(jù)。

六、配置 HAProxy 負(fù)載 MySQL 服務(wù)

經(jīng)過上面測(cè)試已經(jīng)證明主主復(fù)制功能正常,數(shù)據(jù)無(wú)論寫在哪個(gè)數(shù)據(jù)庫(kù)上都能同步給另外一臺(tái),下面通過 HAProxy 負(fù)載均衡代理 MySQL 1MySQL 2 ,對(duì)外提供統(tǒng)一的入口。

下載 HAProxy

yum install -y haproxy

覆蓋修改 /etc/haproxy/haproxy.cfg 配制文件,注意修改 backend mysql-apiserver 下的兩個(gè) MySQLip

cat > /etc/haproxy/haproxy.cfg << EOF
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    log         127.0.0.1 local2
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon 
    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------  
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000
#---------------------------------------------------------------------
# mysql apiserver frontend which proxys to the backends
#--------------------------------------------------------------------- 
frontend mysql-apiserver
    mode                 tcp
    bind                 *:3306
    option               tcplog
    default_backend      mysql-apiserver    
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend mysql-apiserver
    mode        tcp
    balance     roundrobin
    server      master1   172.19.222.20:3306 check
    server      master2   172.19.222.82:3306 check
#---------------------------------------------------------------------
# collection haproxy statistics message
#---------------------------------------------------------------------
listen stats
    bind                 *:1080
    stats auth           admin:awesomePassword
    stats refresh        5s
    stats realm          HAProxy\ Statistics
    stats uri            /admin?stats
EOF

啟動(dòng)haproxy

systemctl start haproxy

設(shè)置開機(jī)自啟:

systemctl enable haproxy

查看啟動(dòng)狀態(tài):

systemctl status haproxy

測(cè)試

通過 84 服務(wù)器連接 MySQL

查看前面創(chuàng)建的數(shù)據(jù)庫(kù)、表、數(shù)據(jù):

寫入一條數(shù)據(jù):

insert into test(name) values("小紅");

分別到 MySQL 1MySQL 2查詢數(shù)據(jù):

都可以查詢到數(shù)據(jù),到此 MySQL 搭建雙主復(fù)制服務(wù) 并 通過 HAProxy 負(fù)載均衡就結(jié)束了。

到此這篇關(guān)于MySQL 搭建雙主復(fù)制服務(wù) 并 通過 HAProxy 負(fù)載均衡的文章就介紹到這了,更多相關(guān)MySQL 雙主復(fù)制服務(wù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • MySQL中explain語(yǔ)句的基本使用教程

    MySQL中explain語(yǔ)句的基本使用教程

    這篇文章主要給大家介紹了關(guān)于MySQL中explain語(yǔ)句的基本使用教程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Mac上安裝Mysql的詳細(xì)步驟及配置

    Mac上安裝Mysql的詳細(xì)步驟及配置

    這篇文章主要給大家介紹了關(guān)于Mac上安裝Mysql的詳細(xì)步驟及配置,文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2023-02-02
  • MySQL UNION操作符基礎(chǔ)知識(shí)點(diǎn)

    MySQL UNION操作符基礎(chǔ)知識(shí)點(diǎn)

    在本文里小編給大家整理了關(guān)于MySQL UNION操作符的相關(guān)知識(shí)點(diǎn)內(nèi)容,需要的朋友們跟著學(xué)習(xí)下。
    2019-02-02
  • MySQL權(quán)限USAGE和ALL PRIVILEGES的用法

    MySQL權(quán)限USAGE和ALL PRIVILEGES的用法

    本文主要介紹了MySQL權(quán)限USAGE和ALL PRIVILEGES的用法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-09-09
  • Mysql去重的幾種方式分步講解

    Mysql去重的幾種方式分步講解

    SQL去重是數(shù)據(jù)分析工作中比較常見的一個(gè)場(chǎng)景,下面這篇文章主要給大家介紹了關(guān)于SQL去重的3種實(shí)用方法的相關(guān)資料,文中通過圖文以及實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-12-12
  • MySQL報(bào)1366錯(cuò)誤的原因及解決

    MySQL報(bào)1366錯(cuò)誤的原因及解決

    SQL Error1366是一個(gè)常見的 MySQL 錯(cuò)誤,主要成因是數(shù)據(jù)類型不匹配或數(shù)據(jù)超出了范圍,本文主要介紹了MySQL報(bào)1366錯(cuò)誤的原因及解決,感興趣的可以了解一下
    2024-02-02
  • 深入淺析Mysql聯(lián)合索引最左匹配原則

    深入淺析Mysql聯(lián)合索引最左匹配原則

    這篇文章主要介紹了Mysql聯(lián)合索引最左匹配原則,使用聯(lián)合索引的好處多多,具體內(nèi)容詳情大家跟隨腳本之家小編一起學(xué)習(xí)吧
    2018-06-06
  • 深入理解Mysql事務(wù)隔離級(jí)別與鎖機(jī)制問題

    深入理解Mysql事務(wù)隔離級(jí)別與鎖機(jī)制問題

    MySQL默認(rèn)的事務(wù)隔離級(jí)別是可重復(fù)讀,用Spring開發(fā)程序時(shí),如果不設(shè)置隔離級(jí)別默認(rèn)用MySQL設(shè)置的隔離級(jí)別,如果Spring設(shè)置了就用已設(shè)置的隔離級(jí)別,本文重點(diǎn)給大家介紹Mysql事務(wù)隔離級(jí)別與鎖機(jī)制的相關(guān)知識(shí),一起看看吧
    2021-09-09
  • MySQL數(shù)據(jù)庫(kù)服務(wù)器端核心參數(shù)詳解和推薦配置

    MySQL數(shù)據(jù)庫(kù)服務(wù)器端核心參數(shù)詳解和推薦配置

    MySQL手冊(cè)上也有服務(wù)器端參數(shù)的解釋,以及參數(shù)值的相關(guān)說明信息,現(xiàn)針對(duì)我們大家重點(diǎn)需要注意、需要修改或影響性能 的服務(wù)器端參數(shù),作其用處的解釋和如何配置參數(shù)值的推薦,此事情拖了不少時(shí)間,為方便大家?guī)兔m錯(cuò)
    2011-12-12
  • mysql中json_extract的具體使用

    mysql中json_extract的具體使用

    mysql5.7版本開始支持JSON類型字段,本文主要介紹了mysql中json_extract的具體使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-05-05

最新評(píng)論