MySQL8.0.18配置多主一從
1. 現(xiàn)實(shí)背景
現(xiàn)有 4 臺主機(jī),均能夠自動(dòng)地采集數(shù)據(jù),并存入其 MySQL 數(shù)據(jù)庫中,另有 1 臺專門用于處理數(shù)據(jù)的高配置主服務(wù)器。這 5 臺機(jī)器經(jīng)常不在同一個(gè)網(wǎng)段下,但希望,一旦處于同一個(gè)網(wǎng)段下時(shí),4 臺用于采集數(shù)據(jù)的主機(jī)能夠自動(dòng)地向主服務(wù)器匯集數(shù)據(jù),為此配置環(huán)境。
2. 約定
- slave,主服務(wù)器
- master1, 用于采集數(shù)據(jù)的某一臺主機(jī)
- master2, 用于采集數(shù)據(jù)的某一臺主機(jī)
- master3, 用于采集數(shù)據(jù)的某一臺主機(jī)
- master4, 用于采集數(shù)據(jù)的某一臺主機(jī)
3. 配置 master
3.1. 配置啟動(dòng)參數(shù)
多臺 master 只需確保 server-id 不一致即可,其他根據(jù)自身需求配置。
[mysqld] # 服務(wù)器標(biāo)識符, 確保每臺服務(wù)器標(biāo)識符都不一樣 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)限:
flush privileges;
4. 配置 slave 服務(wù)器
4.1. 配置啟動(dòng)參數(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ò)超時(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:
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 | | +------------------+----------+--------------+-------------------------------------------------+-------------------+
說明:啟動(dòng) MySQL 會(huì)強(qiáng)制生成新的 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)備表
啟動(dòng)前,在三臺機(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';
show slave status\G;
注意 Slave_IO_Running 和 Slave_Slave_Running 需要均顯示為 Yes,才表示成功,否則留意錯(cuò)誤提示。
到此這篇關(guān)于MySQL8.0.18配置多主一從 的文章就介紹到這了,更多相關(guān)MySQL 多主一從 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MySQL中NULLIF?、IFNULL、IF的用法和區(qū)別舉例詳解
這篇文章主要給大家介紹了關(guān)于MySQL中NULLIF?、IFNULL、IF的用法和區(qū)別的相關(guān)資料,nullif和ifnull都是MySQL中用于處理NULL值的函數(shù),但它們的用法和作用略有不同,下面給大家詳細(xì)介紹下,需要的朋友可以參考下2024-05-05關(guān)于JDBC與MySQL臨時(shí)表空間的深入解析
這篇文章主要給大家介紹了關(guān)于JDBC與MySQL臨時(shí)表空間的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09ubuntu系統(tǒng)中Mysql ERROR 1045 (28000): Acces
這篇文章主要介紹了ubuntu系統(tǒng)安裝mysql登陸提示 解決Mysql ERROR 1045 (28000): Access denied for user root@ localhost問題,需要的朋友可以參考下2017-05-05使用Python的Django框架中的壓縮組件Django Compressor
這篇文章主要介紹了使用Python的Django框架中的壓縮組件Django Compressor,這個(gè)工具主要用于實(shí)現(xiàn)js/css的壓縮,需要的朋友可以參考下2015-05-05mysql中的四大運(yùn)算符種類實(shí)例匯總(20多項(xiàng))?
這篇文章主要介紹了mysql中的四大運(yùn)算符種類匯總,運(yùn)算符連接表達(dá)式中的各個(gè)操作數(shù),他的作用是用來指明對數(shù)據(jù)表中的操作數(shù)所進(jìn)行的運(yùn)算2022-07-07在MySQL中解析JSON或?qū)⒈碇凶侄沃岛喜镴SON問題
這篇文章主要介紹了在MySQL中解析JSON或?qū)⒈碇凶侄沃岛喜镴SON問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04