首頁 > 軟體

Mysql一主多從部署的實現步驟

2022-05-20 13:12:07

1.下載地址

https://dev.mysql.com/downloads/mysql/

2.下載tar.gz包

選擇redhat版本

3.安裝

1.linux系統上建立mysql1使用者

useradd mysql1

2.將tar.gz包上傳到伺服器上並且解壓

tar -zxvf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz

3.將mysql-5.7.31-el7-x86_64目錄下的檔案mv到 /home/mysql1下

4.mkdir -p /home/mysql1/data 檔案儲存mysql1資料目錄

mkdir -p /home/mysql1/data

5.將/etc/my.cfg檔案複製到mysql家目錄下

cp /etc/my.cnf /home/mysql1/

6.對/home/mysql1下賦權

chmod -R 755 /home/mysq1l
chown mysql:mysql -R /home/mysql1/data
chmod 777 /home/mysql1/data

7.修改mysql組態檔

vi /home/mysql1/my.cnf

[mysqld]
user                            = 
port                            = 3307
basedir                         = /home/mysql1
datadir                         = /home/mysql1/data
socket                          = /home/mysql1/mysql.sock
pid_file                        = /home/mysql1/mysql.pid
log_error                       = /home/mysql1/data/error.log
#binlog紀錄檔檔案
log_bin                         = /home/mysql1/data/mysql-bin
relay_log                       = /home/mysql1/data/relay-bin
slow_query_log_file             = /home/mysql1/data/slow.log
#binlog過期清理時間
expire_logs_days                = 15
log-slave-updates               = 1
log_bin_trust_function_creators = 1
lower_case_table_names          = 1
max_connections                 = 3000
max_connect_errors              = 1000000 
# 每個範例的id都設定成不一樣的,比如a主機,3307 b主機設定成23307,c主機設定成33307,後面1主2從會用到
server-id                       = 3307                              
autocommit                      = 1
# pool_size根據實際情況進行更新
innodb_buffer_pool_size         = 40G
innodb_buffer_pool_instances    = 8
innodb_write_io_threads         = 16
innodb_read_io_threads          = 16
#mysql複製主要有三種方式:基於SQL語句的複製(statement-based replication, SBR),基於行的複製(row-based replication, RBR),混合模式複製(mixed-based replication, MBR)。對應的,binlog的格式也有三種:STATEMENT,ROW,MIXED
binlog_format                   = ROW
gtid_mode                       = on
enforce_gtid_consistency        = 1
innodb_flush_log_at_trx_commit  = 1  
#add 2                          
innodb_thread_concurrency       = 20
innodb_print_all_deadlocks      = 1
innodb_flush_method             = O_DIRECT
innodb_io_capacity              = 8000
innodb_io_capacity_max          = 15000
enforce_gtid_consistency        = 1
binlog_rows_query_log_events    = 1
character_set_server            = utf8mb4
default-storage-engine          = INNODB
transaction_isolation           = READ-COMMITTED
max_allowed_packet              = 67108864
event_scheduler                 = 1
slow_query_log                  = on
explicit_defaults_for_timestamp = 1   
master_info_repository          = TABLE
relay_log_info_repository       = TABLE
relay_log_recovery              = 1
relay_log_purge                 = 0  
slave_rows_search_algorithms    = 'INDEX_SCAN,HASH_SCAN'
slave_parallel_type             = LOGICAL_CLOCK
slave_parallel_workers          = 16
slave_preserve_commit_order     = 1
slave_transaction_retries       = 64
sync_relay_log                  = 0
sync_relay_log_info             = 0
sync_master_info                = 0
sync_binlog                     = 1
collation_server                = utf8mb4_bin
skip_name_resolve               = 1 
plugin-load                     = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
rpl-semi-sync-master-enabled    = 1
rpl-semi-sync-slave-enabled     = 1

8.資料檔案初始化

1.初始化

./mysqld --defaults-file=/home/mysql1/my.cnf --initialize --basedir=/home/mysql1 --datadir=/home/mysql1/data --user=mysql1

2.啟動

./mysqld_safe --defaults-file=/home/mysql1/my.cnf --user=mysql1 &

檢視mysql程序已經起來,並且監聽3307介面

3.本機登陸,root的初始密碼可以在errorlog中找到

cat error.log | grep "temporary password"

4.本機登入指定sock檔案以及指定埠及輸入臨時密碼

./mysql -uroot -P3307 -S /home/mysql1/mysql.sock -p

5. 登入後修改root密碼

set password=password('XXXXXXXX'); 修改密碼
flush privileges;

6. 賦予許可權

grant all privileges on *.* to 'root'@'%' identified by 'XXXXXXX' with grant option;
flush privileges;

7. 停止服務

./mysqladmin shutdown -uroot -p******** -S /home/mysql1/mysql.sock

4.使用mysql使用者端datagrip連線mysql成功

5.通過如上相同操作安裝從庫,只修改組態檔相關資訊,並且初始化,並且使用者端連線

6.在master資料庫上執行

1.建立repl使用者並且賦予同步許可權

CREATE USER 'repl' IDENTIFIED BY 'slavepass';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%' identified by 'slavepass';
flush privileges;

2.檢視master狀態

SHOW MASTER STATUSG;

7.在slave上面執行

1.設定slave從節點的master節點以及binlog偏移位置等

CHANGE MASTER TO MASTER_HOST='master地址',
MASTER_USER='repl',
MASTER_PASSWORD='slavepass',
MASTER_LOG_FILE='mysql-bin.000003', #為master的log_file
MASTER_LOG_POS=1597,       #master的POS
MASTER_PORT=3307;

2.啟動slave同步程序以及檢視slave狀態

start slave;  #啟動slave同步程序
show slave statusG;

8.測試

在mysql1中建立testdb,使用datagrip在mysql2中即可檢視從mysql1中同步的新增的庫以及資料,不過datagrip需要重新整理下使用者端。

9.總結主從部署

注意主從mysql的my.cnf組態檔的server-id必須設定不一樣

到此這篇關於Mysql一主多從部署的實現步驟的文章就介紹到這了,更多相關Mysql一主多從內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com