MySQL 8.0 主從複寫設定筆記(GTID + SSL 支援)
MySQL 8.0 主從複寫設定筆記(GTID + SSL 支援)

MySQL 8.0 主從複寫設定筆記(GTID + SSL 支援)

MySQL 8.0 主從複寫設定筆記(GTID + SSL 支援)

這篇筆記記錄我在設定 MySQL 8.0 主從複寫(使用 GTID 與 SSL 加密傳輸)時的完整過程,包括憑證產生、my.cnf 設定、用戶管理與常見錯誤排解。


📌 系統環境

項目 主機 備註
Master 192.168.0.1 MySQL 8.0, Ubuntu
Slave 192.168.0.2 MySQL 8.0, Ubuntu

✅ 步驟一:產生 SSL 憑證(Master 與 Slave 共用)

mkdir -p /etc/mysql/ssl && cd /etc/mysql/ssl

# 產生 CA 憑證
openssl genrsa 2048 > ca-key.pem
openssl req -new -x509 -nodes -days 3650 \
    -key ca-key.pem -out ca.pem \
    -subj "/CN=MySQL CA"

# 產生 Server 憑證
openssl req -newkey rsa:2048 -days 3650 -nodes \
    -keyout server-key.pem -out server-req.pem \
    -subj "/CN=MySQL Server"
openssl x509 -req -in server-req.pem -days 3650 \
    -CA ca.pem -CAkey ca-key.pem -set_serial 01 \
    -out server-cert.pem

# 產生 Client 憑證
openssl req -newkey rsa:2048 -days 3650 -nodes \
    -keyout client-key.pem -out client-req.pem \
    -subj "/CN=MySQL Client"
openssl x509 -req -in client-req.pem -days 3650 \
    -CA ca.pem -CAkey ca-key.pem -set_serial 02 \
    -out client-cert.pem

# 設定權限
chown mysql:mysql /etc/mysql/ssl/*
chmod 600 /etc/mysql/ssl/*.pem

✅ 步驟二:設定 Master 的 my.cnf
在 /etc/mysql/my.cnf 或 /etc/mysql/mysql.conf.d/mysqld.cnf 加入:

[mysqld]
server-id = 1
log_bin = mysql-bin
binlog_format = ROW

gtid_mode = ON
enforce-gtid-consistency = ON

開啟 SSL

ssl-ca = /etc/mysql/ssl/ca.pem
ssl-cert = /etc/mysql/ssl/server-cert.pem
ssl-key = /etc/mysql/ssl/server-key.pem

bind-address = 0.0.0.0

重啟服務:

systemctl restart mysql


✅ 步驟三:建立複寫帳號(Master)

CREATE USER 'relay'@'%' IDENTIFIED BY 'xxxxxxx' REQUIRE SSL;
GRANT REPLICATION SLAVE ON . TO 'relay'@'%';
FLUSH PRIVILEGES;


✅ 步驟四:設定 Slave 的 my.cnf

[mysqld]
server-id = 2
log_bin = mysql-bin
relay-log = mysql-relay-bin

gtid_mode = ON
enforce-gtid-consistency = ON

可選

read_only = ON
super_read_only = ON

重啟服務:

systemctl restart mysql


✅ 步驟五:設定 Slave 複寫參數
進入 Slave 的 MySQL CLI:

STOP REPLICA;

CHANGE REPLICATION SOURCE TO
SOURCE_HOST = '192.168.0.1',
SOURCE_USER = 'relay',
SOURCE_PASSWORD = 'xxxxxxx',
SOURCE_AUTO_POSITION = 1,
SOURCE_SSL = 1,
SOURCE_SSL_CA = '/etc/mysql/ssl/ca.pem',
SOURCE_SSL_CERT = '/etc/mysql/ssl/client-cert.pem',
SOURCE_SSL_KEY = '/etc/mysql/ssl/client-key.pem';

START REPLICA;

✅ 驗證同步狀態

SHOW REPLICA STATUS\G


關鍵欄位應為:

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Last_IO_Error: (空白)

Last_SQL_Error: (空白)

🐞 常見錯誤與解法
❌ Authentication requires secure connection
原因: relayUser 使用 caching_sha2_password,但 Slave 無 SSL。

解法:

開啟 SSL

或改用 mysql_native_password

ALTER USER 'relay'@'%' IDENTIFIED WITH 'mysql_native_password' BY 'xxxxxxx';


❌ SSL connection error: SSL_CTX_set_default_verify_paths failed
原因: 沒有設定 SOURCE_SSL_CA 路徑

解法: 在 CHANGE REPLICATION SOURCE TO 加上:

SOURCE_SSL_CA='/etc/mysql/ssl/ca.pem'


❌ SSL is required but the server doesn't support it
原因: Master 的 have_ssl = DISABLED

解法:

確認已在 Master 設定 SSL 憑證

重啟 MySQL,並確認:

SHOW VARIABLES LIKE 'have_ssl'; -- 應為 YES


✅ 補充指令
驗證是否使用 SSL 連線

SHOW SESSION STATUS LIKE 'Ssl_version';


若有值如 TLSv1.3,代表 SSL 已使用。

🔐 建議安全強化措施
relayUser 設定為 REQUIRE SSL / REQUIRE X509

bind-address = 127.0.0.1 限制來源(若透過 VPN/內網)

防火牆開放特定來源 IP 的 3306

建議配合 failover 或 MGR 做高可用

✅ 總結
這套主從架構使用:

GTID:確保一致性與容錯

SSL:確保中間傳輸安全

備援:Slave 可隨時切主、做報表、備份

若你在設定過程遇到錯誤,可以用 SHOW REPLICA STATUS\G 找出具體錯誤資訊。