1. 程式人生 > >在docker-compose搭建mysql主從複製中遇到的幾個問題

在docker-compose搭建mysql主從複製中遇到的幾個問題

簡單記錄下配置過程:

1、 配置主庫my.cnf檔案

[mysqld]
lower_case_table_names=1
server-id=248 # 任意數字,一般填寫伺服器IP後幾位
log_bin=/tmp/mysql/mysql-bin
binlog-do-db=whalephp #指定要同步的資料庫,不指定則同步全部
2、 配置從庫my.cnf檔案
[mysqld]
lower_case_table_names=1
server-id=148
log_bin=/tmp/mysql/s1-bin
sync_binlog=1

3、主庫配置

# 建立用於主從複製的使用者:master_repl_user
GRANT REPLICATION SLAVE ON *.* TO 'master_repl_user'@'%' IDENTIFIED BY 'repl_001';

# 重新整理主庫使用者許可權
FLUSH PRIVILEGES;

# 檢視主庫master狀態
show master status\G


4、從庫配置

#從伺服器執行
stop slave;
change master to master_host='192.168.0.1',master_port=8090,master_user='master_repl_user',master_password='repl_001',master_log_file='mysql-bin.000016',master_log_pos=601;
start slave;

# 檢視從庫slave狀態
show slave status\G


當Slave_IO_Running、Slave_SQL_Running均為YES的時候表示主從配置成功

======================================

在此過程中遇到幾個問題:

1、docker-compose up過程中db配置未生效

原因:配置路徑未找到

解決:在conf.d同級目錄增加my.cnf配置檔案並寫入相關配置

[mysqld]
lower_case_table_names=1
server-id=1
log_bin=/tmp/mysql/mysql-bin  
2、兩主機搭建MySQL主從複製後,show slave status顯示:Last_IO_Error: error connecting to master ……

原因:授權密碼、IP或帳號不對

排查方法:

mysql -urepl -h m1 -prepl -P8090

解決:

flush PRIVILEGES;
create user repl;
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%' IDENTIFIED BY 'repl';