MySQL
1. 主數據庫配置
[mysqld]
server-id = 1 # 節點ID,確保唯一
# log config
log-bin = mysql-bin #開啟mysql的binlog日志功能
sync_binlog = 1 #控制數據庫的binlog刷到磁盤上去 , 0 不控制,性能最好,1每次事物提交都會刷到日志文件中,性能最差,最安全
binlog_format = mixed #binlog日志格式,mysql默認采用statement,建議使用mixed
expire_logs_days = 7 #binlog過期清理時間
max_binlog_size = 100m #binlog每個日志文件大小
binlog_cache_size = 4m #binlog緩存大小
max_binlog_cache_size= 512m #最大binlog緩存大
binlog-ignore-db=mysql #不生成日志文件的數據庫,多個忽略數據庫可以用逗號拼接,或者 復制這句話,寫多行
auto-increment-offset = 1 # 自增值的偏移量
auto-increment-increment = 1 # 自增值的自增量
slave-skip-errors = all #跳過從庫錯誤
2. 從數據庫配置加入
server-id=2
relay-log=slave-relay-bin
relay-log-index=slave-relay-bin.index
#replicate-do-db=test
#備注:
#server-id 服務器唯一標識,如果有多個從服務器,每個服務器的server-id不能重復,跟IP一樣是唯一標識,如果你沒設置server-id或者設置為0,則從服務器不會連接到主服務器。
#relay-log 啟動MySQL二進制日志,可以用來做數據備份和崩潰恢復,或主服務器掛掉了,將此從服務器作為其他從服務器的主服務器。
#replicate-do-db 指定同步的數據庫,如果復制多個數據庫,重復設置這個選項即可。若在master端不指定binlog-do-db,則在slave端可用replication-do-db來過濾。
#replicate-ignore-db 不需要同步的數據庫,如果有多個數據庫,重復設置這個選項即可。
3. 重啟主和從數據庫
service mysql restart
4. 主數據庫查看master狀態
show master status
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000005 120| | mysql | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
5. 從數據庫進入mysql配置
MariaDB [(none)]> CHANGE MASTER TO
MASTER_HOST = '172.17.0.3',
MASTER_USER = 'repl_user',
MASTER_PASSWORD = 'repl_passwd',
MASTER_PORT = 3307,
MASTER_LOG_FILE='mysql-bin.000005',
MASTER_LOG_POS=120,
MASTER_RETRY_COUNT = 60,
MASTER_HEARTBEAT_PERIOD = 10000;
# MASTER_USER = 'repl_user', # 主庫的賬號
# MASTER_PASSWORD = 'repl_passwd', # 主庫的密碼
# MASTER_LOG_FILE='mysql-bin.000005',#與主庫File 保持一致
# MASTER_LOG_POS=120 , #與主庫Position 保持一致
6. 從數據庫開啟slave進程
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.002 sec)
7. 查看同步狀態
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 120.78.176.77
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 154
Relay_Log_File: PC-202008301542-relay-bin.000002
Relay_Log_Pos: 422
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 154
Relay_Log_Space: 741
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Replicate_Do_Domain_Ids:
Replicate_Ignore_Domain_Ids:
Parallel_Mode: optimistic
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Slave_DDL_Groups: 0
Slave_Non_Transactional_Groups: 0
Slave_Transactional_Groups: 0
1 row in set (0.000 sec)
# 出現這兩個說明同步成功
# Slave_IO_Running: Yes
# Slave_SQL_Running: Yes
問題
ERROR 1201 (HY000): Could not initialize master info structure for ''; more error messages can be found in the MariaDB error log
如果在操作到遇到上面問題的報錯,就執行下面這句, 繼續從第5步開始
MariaDB [(none)]> reset slave;
Query OK, 0 rows affected (0.037 sec)