MySQL8.0.18配置多主一從
現(xiàn)有 4 臺主機(jī),均能夠自動地采集數(shù)據(jù),并存入其 MySQL 數(shù)據(jù)庫中,另有 1 臺專門用于處理數(shù)據(jù)的高配置主服務(wù)器。這 5 臺機(jī)器經(jīng)常不在同一個網(wǎng)段下,但希望,一旦處于同一個網(wǎng)段下時,4 臺用于采集數(shù)據(jù)的主機(jī)能夠自動地向主服務(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. 配置 master3.1. 配置啟動參數(shù)多臺 master 只需確保 server-id 不一致即可,其他根據(jù)自身需求配置。
[mysqld]# 服務(wù)器標(biāo)識符, 確保每臺服務(wù)器標(biāo)識符都不一樣server-id = 1000# master 機(jī)必須開啟 log_bin# mysql-bin 為自定義名字,會生成諸如 mysql-bin.index、mysql-bin.000001 等文件log_bin=mysql-bin# 二進(jìn)制日志過期時間(單位:天),默認(rèn)值為 0,即不過期expire_logs_days = 0# 錯誤日志log-error=/var/lib/mysql/mysql-error.log# 單個 log_bin 文件最大值,達(dá)到最大值之后新建文件后綴自增,如 mysql-bin.000002max_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_schemabinlog_ignore_db=performance_schemabinlog_ignore_db=sysbinlog_ignore_db=mysql# 二進(jìn)制日志保存模式binlog_format=MIXED# blob 類型的最大存儲值(單位:字節(jié)、B)# 1048576 B = 1MBmax_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=1innodb_flush_log_at_trx_commit=0[mysql]default-character-set=utf8mb4[client]default-character-set=utf8mb43.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:mysqldata# slave 復(fù)制的數(shù)據(jù)庫replicate_do_db=test# slave 忽略的數(shù)據(jù)庫replicate_ignore_db=information_schemareplicate_ignore_db=performance_schemareplicate_ignore_db=mysqlreplicate_ignore_db=sys# slave 網(wǎng)絡(luò)超時重連間隔(單位:秒)slave_net_timeout=60[mysql]default-character-set=utf8[client]default-character-set=utf84.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 會強(qiáng)制生成新的 log-bin,因此位置均為 155。
5.2. 配置 slave 與 master 的關(guān)聯(lián)查看是否有其他殘余的配置:
show slave statusG;
停止 slave,清除殘余配置:
stop slave;
reset slave all;
根據(jù) master1 的 file,position 配置 replication 通道“master1”
change master tomaster_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 = 0for channel ’master1’;
根據(jù) master2 的 file,position 配置 replication 通道“master2”
change master tomaster_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 = 0for channel ’master2’; master_connect_retry:連接失敗,重試間隔(單位:秒) master_retry_count:連接失敗重試次數(shù),0 為無限次5.3. 準(zhǔn)備表
啟動前,在三臺機(jī)器的數(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 statusG;
注意 Slave_IO_Running 和 Slave_Slave_Running 需要均顯示為 Yes,才表示成功,否則留意錯誤提示。
到此這篇關(guān)于MySQL8.0.18配置多主一從 的文章就介紹到這了,更多相關(guān)MySQL 多主一從 內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. SQL Server數(shù)據(jù)庫的三種創(chuàng)建方法匯總2. 關(guān)于mongoose連接mongodb重復(fù)訪問報錯的解決辦法3. python之sqlalchemy創(chuàng)建表的實(shí)例詳解4. DB2 自動遞增字段實(shí)現(xiàn)方法5. SQLite教程(四):內(nèi)置函數(shù)6. sql語句LEFT JOIN拼接表詳解7. ubuntu下使用SQLite3的基本命令8. 國內(nèi)學(xué)院派專家對DB2 9新產(chǎn)品贊不絕口9. SQLite數(shù)據(jù)庫安裝及基本操作指南10. 目前學(xué)習(xí)到的常用命令之Mariadb
