目錄
- 1. 現(xiàn)實(shí)背景
- 2. 約定
- 3. 配置 master
- 3.1. 配置啟動(dòng)參數(shù)
- 3.2. 重啟服務(wù)使參數(shù)生效
- 3.3. 以 root 身份登錄,創(chuàng)建用戶,賦予密碼,授權(quán),刷新權(quán)限
- 4. 配置 slave 服務(wù)器
- 4.1. 配置啟動(dòng)參數(shù)
- 4.2. 重啟服務(wù)使參數(shù)生效
- 5. 配置多主一從
- 5.1. 查看 master 狀態(tài)
- 5.2. 配置 slave 與 master 的關(guān)聯(lián)
- 5.3. 準(zhǔn)備表
- 5.4. 啟動(dòng) slave,查看 slave 狀態(tài)
1. 現(xiàn)實(shí)背景
現(xiàn)有 4 臺(tái)主機(jī),均能夠自動(dòng)地采集數(shù)據(jù),并存入其 MySQL 數(shù)據(jù)庫中,另有 1 臺(tái)專門用于處理數(shù)據(jù)的高配置主服務(wù)器。這 5 臺(tái)機(jī)器經(jīng)常不在同一個(gè)網(wǎng)段下,但希望,一旦處于同一個(gè)網(wǎng)段下時(shí),4 臺(tái)用于采集數(shù)據(jù)的主機(jī)能夠自動(dòng)地向主服務(wù)器匯集數(shù)據(jù),為此配置環(huán)境。
2. 約定
- slave,主服務(wù)器
- master1, 用于采集數(shù)據(jù)的某一臺(tái)主機(jī)
- master2, 用于采集數(shù)據(jù)的某一臺(tái)主機(jī)
- master3, 用于采集數(shù)據(jù)的某一臺(tái)主機(jī)
- master4, 用于采集數(shù)據(jù)的某一臺(tái)主機(jī)
3. 配置 master
3.1. 配置啟動(dòng)參數(shù)
多臺(tái) master 只需確保 server-id 不一致即可,其他根據(jù)自身需求配置。
[mysqld]
# 服務(wù)器標(biāo)識(shí)符, 確保每臺(tái)服務(wù)器標(biāo)識(shí)符都不一樣
server-id = 1000
# master 機(jī)必須開啟 log_bin
# mysql-bin 為自定義名字,會(huì)生成諸如 mysql-bin.index、mysql-bin.000001 等文件
log_bin=mysql-bin
# 二進(jìn)制日志過期時(shí)間(單位:天),默認(rèn)值為 0,即不過期
expire_logs_days = 0
# 錯(cuò)誤日志
log-error=/var/lib/mysql/mysql-error.log
# 單個(gè) log_bin 文件最大值,達(dá)到最大值之后新建文件后綴自增,如 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
# 二進(jìn)制日志保存模式
binlog_format=MIXED
# blob 類型的最大存儲(chǔ)值(單位:字節(jié)、B)
# 1048576 B = 1MB
max_allowed_packet=1048576
# 密碼復(fù)雜度配置,需要插件
# 密碼長度至少為 0
# validate_password_length=8
# 大小寫同時(shí)存在的最少數(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,同時(shí)賦予密碼:
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)限:
4. 配置 slave 服務(wù)器
4.1. 配置啟動(dòng)參數(shù)
[mysqld]
# 服務(wù)器標(biāo)識(shí)符, 確保每臺(tái)服務(wù)器標(biāo)識(shí)符都不一樣
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ò)超時(shí)重連間隔(單位:秒)
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:
+------------------+----------+--------------+-------------------------------------------------+-------------------+
| 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:
+------------------+----------+--------------+-------------------------------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+-------------------------------------------------+-------------------+
| mysql-bin.000005 | 155 | test | information_schema,performance_schema,sys,mysql | |
+------------------+----------+--------------+-------------------------------------------------+-------------------+
說明:啟動(dòng) MySQL 會(huì)強(qiáng)制生成新的 log-bin,因此位置均為 155。
5.2. 配置 slave 與 master 的關(guān)聯(lián)
查看是否有其他殘余的配置:
停止 slave,清除殘余配置:
根據(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)備表
啟動(dòng)前,在三臺(tái)機(jī)器的數(shù)據(jù)庫中使用 DDL 語句定義好表結(jié)構(gòu),且表結(jié)構(gòu)保持一致,確保主從復(fù)制前的一致性,否則會(huì)出錯(cuò)!
5.4. 啟動(dòng) slave,查看 slave 狀態(tài)
start slave for channel 'master1';
start slave for channel 'master2';
注意 Slave_IO_Running 和 Slave_Slave_Running 需要均顯示為 Yes,才表示成功,否則留意錯(cuò)誤提示。
到此這篇關(guān)于MySQL8.0.18配置多主一從 的文章就介紹到這了,更多相關(guān)MySQL 多主一從 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- MySQL主從搭建(多主一從)的實(shí)現(xiàn)思路與步驟
- Mysql多主一從數(shù)據(jù)備份的方法教程
- Centos7 Mysql 5.6 多主一從 解決方案與詳細(xì)配置