佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > MySQL8.0.18配置多主一從

MySQL8.0.18配置多主一從

熱門標(biāo)簽:Linux服務(wù)器 百度競價排名 網(wǎng)站排名優(yōu)化 地方門戶網(wǎng)站 呼叫中心市場需求 鐵路電話系統(tǒng) AI電銷 服務(wù)外包

1. 現(xiàn)實背景

現(xiàn)有 4 臺主機,均能夠自動地采集數(shù)據(jù),并存入其 MySQL 數(shù)據(jù)庫中,另有 1 臺專門用于處理數(shù)據(jù)的高配置主服務(wù)器。這 5 臺機器經(jīng)常不在同一個網(wǎng)段下,但希望,一旦處于同一個網(wǎng)段下時,4 臺用于采集數(shù)據(jù)的主機能夠自動地向主服務(wù)器匯集數(shù)據(jù),為此配置環(huán)境。

2. 約定

  • slave,主服務(wù)器
  • master1, 用于采集數(shù)據(jù)的某一臺主機
  • master2, 用于采集數(shù)據(jù)的某一臺主機
  • master3, 用于采集數(shù)據(jù)的某一臺主機
  • master4, 用于采集數(shù)據(jù)的某一臺主機

3. 配置 master

3.1. 配置啟動參數(shù)

多臺 master 只需確保 server-id 不一致即可,其他根據(jù)自身需求配置。

[mysqld]
# 服務(wù)器標(biāo)識符, 確保每臺服務(wù)器標(biāo)識符都不一樣
server-id = 1000

# master 機必須開啟 log_bin
# mysql-bin 為自定義名字,會生成諸如 mysql-bin.index、mysql-bin.000001 等文件
log_bin=mysql-bin

# 二進制日志過期時間(單位:天),默認值為 0,即不過期
expire_logs_days = 0

# 錯誤日志
log-error=/var/lib/mysql/mysql-error.log

# 單個 log_bin 文件最大值,達到最大值之后新建文件后綴自增,如 mysql-bin.000002
max_binlog_size = 100M

# mysql 安裝路徑
basedir=/var/lib/mysql

# mysql 數(shù)據(jù)路徑
datadir=/var/lib/mysql

# master 記錄操作的數(shù)據(jù)庫
binlog_do_db=replication

# master 忽略的數(shù)據(jù)庫
binlog_ignore_db=information_schema
binlog_ignore_db=performance_schema
binlog_ignore_db=sys
binlog_ignore_db=mysql

# 二進制日志保存模式
binlog_format=MIXED

# blob 類型的最大存儲值(單位:字節(jié)、B)
# 1048576 B = 1MB
max_allowed_packet=1048576


# 密碼復(fù)雜度配置,需要插件
# 密碼長度至少為 0
# validate_password_length=8

# 大小寫同時存在的最少數(shù)目
# validate_password_mixed_case_count=1

# 密碼至少存在的數(shù)字?jǐn)?shù)目
# validate_password_number_count=1

# 密碼至少存在的特殊字符數(shù)目
# validate_password_special_char_count=1

innodb_flush_log_at_trx_commit=0

[mysql]
default-character-set=utf8mb4

[client]
default-character-set=utf8mb4

3.2. 重啟服務(wù)使參數(shù)生效

3.3. 以 root 身份登錄,創(chuàng)建用戶,賦予密碼,授權(quán),刷新權(quán)限

創(chuàng)建用戶 replication,同時賦予密碼:

create user 'replication'@'%' identified with mysql_native_password by 'JINGhuaSHUIyue123,.';

如果創(chuàng)建用戶失敗,可能已經(jīng)存在用戶,不緊要的話可以刪除該用戶:

drop user 'replication'@'%';

如果不希望刪除重建用戶,只希望修改密碼:

alter user 'replication'@'%' identified with mysql_native_password by 'JINGhuaSHUIyue123,.';

賦予用戶 replication slave 權(quán)限:

grant replication slave on *.* to 'replication'@'%';

保證 replication slave 權(quán)限立即生效,刷新權(quán)限:

flush privileges;

4. 配置 slave 服務(wù)器

4.1. 配置啟動參數(shù)

[mysqld]
# 服務(wù)器標(biāo)識符, 確保每臺服務(wù)器標(biāo)識符都不一樣
server-id = 2000

# mysql 安裝路徑
basedir=D:\mysql

# mysql 數(shù)據(jù)路徑
datadir=D:\mysql\data

# slave 復(fù)制的數(shù)據(jù)庫
replicate_do_db=test

# slave 忽略的數(shù)據(jù)庫
replicate_ignore_db=information_schema
replicate_ignore_db=performance_schema
replicate_ignore_db=mysql
replicate_ignore_db=sys

# slave 網(wǎng)絡(luò)超時重連間隔(單位:秒)
slave_net_timeout=60

[mysql]

default-character-set=utf8

[client]

default-character-set=utf8

4.2. 重啟服務(wù)使參數(shù)生效

5. 配置多主一從

5.1. 查看 master 狀態(tài)

以 root 身份登陸 master1,需要留意其中的 file、position:

show master status;
+------------------+----------+--------------+-------------------------------------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB                                | Executed_Gtid_Set |
+------------------+----------+--------------+-------------------------------------------------+-------------------+
| mysql-bin.000006 |      155 | test         | information_schema,performance_schema,sys,mysql |                   |
+------------------+----------+--------------+-------------------------------------------------+-------------------+

以 root 身份登陸 master1,需要留意其中的 file、position:

show master status;
+------------------+----------+--------------+-------------------------------------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB                                | Executed_Gtid_Set |
+------------------+----------+--------------+-------------------------------------------------+-------------------+
| mysql-bin.000005 |      155 | test         | information_schema,performance_schema,sys,mysql |                   |
+------------------+----------+--------------+-------------------------------------------------+-------------------+

說明:啟動 MySQL 會強制生成新的 log-bin,因此位置均為 155。

5.2. 配置 slave 與 master 的關(guān)聯(lián)

查看是否有其他殘余的配置:

show slave status\G;

停止 slave,清除殘余配置:

stop slave;
reset slave all;

根據(jù) master1 的 file,position 配置 replication 通道“master1”

change master to
master_host = '112.124.1.100',
master_user = 'replication',
master_port = 3306,
master_password = 'replication',
master_log_file = 'mysql-bin.000006',
master_log_pos = 155,
master_connect_retry = 15,
master_retry_count = 0
for channel 'master1';

根據(jù) master2 的 file,position 配置 replication 通道“master2”

change master to
master_host = '192.168.1.139',
master_user = 'replication',
master_port = 3306,
master_password = 'JINGhuaSHUIyue123,.',
master_log_file = 'mysql-bin.000005',
master_log_pos = 155,
master_connect_retry = 15,
master_retry_count = 0
for channel 'master2';
  • master_connect_retry:連接失敗,重試間隔(單位:秒)
  • master_retry_count:連接失敗重試次數(shù),0 為無限次

5.3. 準(zhǔn)備表

啟動前,在三臺機器的數(shù)據(jù)庫中使用 DDL 語句定義好表結(jié)構(gòu),且表結(jié)構(gòu)保持一致,確保主從復(fù)制前的一致性,否則會出錯!

5.4. 啟動 slave,查看 slave 狀態(tài)

start slave for channel 'master1';
start slave for channel 'master2';
show slave status\G;

注意 Slave_IO_Running 和 Slave_Slave_Running 需要均顯示為 Yes,才表示成功,否則留意錯誤提示。

到此這篇關(guān)于MySQL8.0.18配置多主一從 的文章就介紹到這了,更多相關(guān)MySQL 多主一從 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • MySQL主從搭建(多主一從)的實現(xiàn)思路與步驟
  • Mysql多主一從數(shù)據(jù)備份的方法教程
  • Centos7 Mysql 5.6 多主一從 解決方案與詳細配置

標(biāo)簽:蘭州 湘潭 黃山 銅川 崇左 衡水 湖南 仙桃

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MySQL8.0.18配置多主一從》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    福建省| 和顺县| 潍坊市| 大足县| 梧州市| 岳阳市| 奉化市| 满洲里市| 丰原市| 中山市| 西华县| 边坝县| 保德县| 东平县| 霍邱县| 隆子县| 四会市| 平昌县| 万荣县| 东海县| 汕尾市| 中牟县| 尼玛县| 阿瓦提县| 尤溪县| 丰镇市| 武安市| 房产| 自治县| 资源县| 德阳市| 会东县| 凌源市| 同心县| 当涂县| 台前县| 漾濞| 延寿县| 桂阳县| 遂宁市| 龙陵县|