MySQL 的主從配置原理是 slave 從 master 取得備份的資料庫檔案後匯入,並於 slave replication 功能啟動前,宣告 master 目前停留在 Binary log 中的 position,這樣 slave 才知道自己本地端的 Replay log 要同步對齊 master Binary log 的哪一句 SQL 語法。
作業系統:Rocky Linux 9.6x64MySQL version:8.0.41Virtual Machine:mysql-master、mysql-slave 共兩台
兩台機器皆安裝 MySQL Service
安裝 RockyLinux 9.6 預設的 MySQL-Server 版本,可以從安裝好之後的日誌看到,目前管理員的密碼為空dnf install --nogpgcheck mysql-server-8.0.41
systemctl enable mysqld
systemctl start mysqld
grep password /var/log/mysql/mysqld.log
2025-09-12T03:51:56.071676Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.無需輸入密碼、直接按 Enter 鍵登入 MySQL CLI 介面變更密碼
mysql -u root -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'password';
mysql> quit開啟防火牆
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reloadMaster 機器配置
注意!MySQL8 以後配置 mysqld 的路徑有變動,於/etc/my.cnf.d/mysql-server.cnf
設定檔添加參數[mysqld]
# 每組 replication 中只能設定一個不重複的值
server-id=1重啟 mysql 服務
systemctl restart mysqld查看目前的用戶並確認其連線驗證方式
mysql -u root -p
mysql> select user,host,plugin from mysql.user;
+------------------+----------------+-----------------------+
| user | host | plugin |
+------------------+----------------+-----------------------+
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session | localhost | caching_sha2_password |
| mysql.sys | localhost | caching_sha2_password |
| root | localhost | caching_sha2_password |
+------------------+----------------+-----------------------+
4 rows in set (0.00 sec)建立一個 mysql user account for replication(slaveip 請改為你的 slave 機器 ip)
mysql> CREATE USER 'account'@'slaveip' IDENTIFIED WITH mysql_native_password BY 'dbpassword';注意!MySQL8 以後,預設支持 caching_sha2_password 的連線驗證方式而不再支援 mysql_native_password,本例直接修改新增同步帳號的連線驗證模式以避免稍後 replication 出現驗證失敗的問題。
給予該用戶相關權限並使其生效
mysql> GRANT REPLICATION SLAVE ON *.* TO 'account'@'slaveip';
mysql> FLUSH PRIVILEGES;
mysql> quit匯出 master 目前的資料庫備份、傳送到 slave 機器上
mysqldump -uroot -p --all-databases > /tmp/master.sql
scp -rp /tmp/master.sql root@slaveip:/tmp/確認 master 角色的基本資料,特別記住 File 和 Position,稍後會用到
mysql> SHOW MASTER STATUS \G
*************************** 1. row ***************************
File: binlog.000009
Position: 1341
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)Slave 機器配置
還原 master 的資料庫備份到 slave 的 mysqlmysql -uroot -p < /tmp/master.sql/etc/my.cnf.d/mysql-server
設定檔添加參數[client-server]
server-id=2
# 開啟唯獨避免 slave db 被寫入
read-only=on重啟 mysql 服務
systemctl restart mysqld設定連線 master db 的相關資訊並啟動 MySQL replication
mysql -u root -p
mysql> STOP SLAVE;
mysql> RESET SLAVE;
mysql> RESET SLAVE ALL;
mysql> CHANGE MASTER TO MASTER_HOST='slaveip',
MASTER_PORT=3306,
MASTER_LOG_FILE='binlog.000009',
MASTER_LOG_POS=1341,
MASTER_USER='account',
MASTER_PASSWORD='dbpassword';
mysql> START SLAVE;確認 slave 狀態,只要能看到雙 Yes 就代表 replication 成功了
mysql> SHOW SLAVE STATUS \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: masterip
Master_User: account
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000009
Read_Master_Log_Pos: 1341
Relay_Log_File: mysql-slave-relay-bin.000002
Relay_Log_Pos: 323
Relay_Master_Log_File: binlog.000009
Slave_IO_Running: Yes
Slave_SQL_Running: Yes我們來驗證看看在 master 上建立一個空資料庫 db2,然後再去 slave 上看看是否憑空生出了 db2
mysql> CREATE DATABASE db2;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| db1 |
| db2 |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
6 rows in set (0.00 sec)Troubleshooting
確認 slave 狀態,發現Authentication plugin 'caching_sha2_password'
報錯mysql> SHOW SLAVE STATUS \G
Last_IO_Errno: 2061
Last_IO_Error: Error connecting to source 'replication@192.168.88.175:3306'. This was attempt 3/86400, with a delay of 60 seconds between attempts. Message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.網上有一些修改 my.cnf 參數的做法,於設定檔中添加「default_authentication_plugin=mysql_native_password」,然而這個參數在 v8.0.4 以後已經棄用,目前可行的做法除了本文實作直接給 replication 用戶設置「mysql_native_password」的 plugin,也可主動先讓欲同步的帳戶先對 MySQL master 進行一次連線,此時 slave 會從 master 取得 RSA 公鑰,以便未來 master 可透過預設的 caching_sha2_password 驗證方式來驗證連線的客戶端(slave)
我們回到 Slave 標題的部分往下走,調整的操作如下:
1. 保險起見,無論先前是否設定過 slave,先執行 slave 相關設定的移除
mysql -u root -p
mysql> STOP REPLICA;
mysql> RESET REPLICA;
mysql> RESET REPLICA ALL;
mysql> exit2. 匯入 master 的資料庫備份到 slave 的 mysql
mysql -uroot -p < /tmp/master.sql3. 讓 slave 欲進行 replication 的帳戶主動連線 master 以取得 RSA 公鑰,安全起見我們將機敏資訊放入 script 後執行
#!/bin/bash
DB_USER='account'
DB_PASSWD='dbpassword'
MASTER_IP='slaveip' # 輸入你自己的 MySQL slave host ip
MASTER_PORT=3306
mysql -u ${DB_USER} -p${DB_PASSWD} -h ${MASTER_IP} -P${MASTER_PORT} --get-server-public-keymysql> exit4. 接著以 root 身份登入 slave MySQL,繼續啟動 MySQL replication
mysql -u root -p
mysql> CHANGE REPLICATION SOURCE TO
SOURCE_HOST='192.168.88.175',
SOURCE_USER='account',
SOURCE_PASSWORD='dbpassword',
SOURCE_LOG_FILE='binlog.000009',
SOURCE_LOG_POS=1341;
mysql> START REPLICA;
mysql> SHOW REPLICA STATUS\G
*************************** 1. row ***************************
Replica_IO_State: Waiting for source to send event
Source_Host: masterip
Source_User: replication
Source_Port: 3306
Connect_Retry: 60
Source_Log_File: binlog.000009
Read_Source_Log_Pos: 1341
Relay_Log_File: mysql-slave-relay-bin.000002
Relay_Log_Pos: 323
Relay_Source_Log_File: binlog.000009
Replica_IO_Running: Yes
Replica_SQL_Running: Yes本文內容參閱以下連結:



0 Comments:
張貼留言