一、主(master)數據庫配置
1. my.cnf 添加配置
[mariadb]
log-bin
server_id=1
log-basename=master1
binlog-format=mixed
max_binlog_size=200M
expire_logs_days=7
server_id 必須唯一。
log-basename 是指定binlog 的命名規則, binlog 會以它為前綴生成日志,如 master1-bin.000001。
max_binlog_size=200M 生成的log最大值,到達最大值,會重新創建一個,如 master1-bin.000002。
expire_logs_days binlog 過期天數。
然后重啟數據庫即生效。
2. 創建執行同步的數據庫用戶
CREATE USER 'replication_user'@'%' IDENTIFIED BY 'bigs3cret';
GRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'%';
用戶名: replication_user, 密碼: bigs3cret,可以修改成你想要的。
3. 鎖住數據庫
在導出數據庫時,先加鎖,避免在導出時修改了數據庫導致數據不一致。
FLUSH TABLES WITH READ LOCK;
4. 記錄當前的同步位置
MariaDB [(none)]> show master status;
+--------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+------------------+
| master1-bin.000001 | 330 | | |
+--------------------+----------+--------------+------------------+
1 row in set (0.000 sec)
當前master 生成的日志文件: master1-bin.000001,
日志位置: 330。
5. 導出數據庫
mysqldump -uroot -proot --databases cqrs --master-data2 >cqrs_db.sql
這里只導出了需要同步的數據庫 cqrs, 當然你也可以導數所有數據庫。 cqrs_db.sql 的默認保存位置在 mariadb 程序所在目錄的bin 目錄下。
6. 解鎖數據庫
數據庫已導出,同步的位置也記錄了,現在解鎖數據庫,接下來對 master 數據庫的修改,都會記錄到 binlog。
UNLOCK TABLES ;
二、從(slave)數據庫配置
1. 配置 server_id
slave 的 server_id, 必須是唯一的,上面我們配置了 master 的 server_id=1, 這里的slave 設成2
[mysqld]
datadir=D:/projects/db/mariadb-10.5.8-winx64/data
port=3307
character-set-server=utf8
server_id=2
重啟 slave 數據庫
2. 導入數據庫
把從 master 導出的 cqrs_db.sql 復制到 slave 程序所在的bin目錄下, 然后登錄客戶端,執行
MariaDB [(none)]> source cqrs_db.sql;
查看數據庫
MariaDB [cqrs]> show databases;
+--------------------+
| Database |
+--------------------+
| cqrs |
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
可看到 cqrs 已經創建了。
3. 配置同步
CHANGE MASTER TO
MASTER_HOST='127.0.0.1',
MASTER_USER='replication_user',
MASTER_PASSWORD='bigs3cret',
MASTER_PORT=3306,
MASTER_LOG_FILE='master1-bin.000001',
MASTER_LOG_POS=330,
MASTER_CONNECT_RETRY=10;
MASTER_LOG_FILE,MASTER_LOG_POS 是在 Master 第4步記錄的日志文件名和開始同步的位置信息
4. 啟動同步
start slave;
5. 查看狀態
MariaDB [(none)]> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 127.0.0.1
Master_User: replication_user
Master_Port: 3306
Connect_Retry: 10
Master_Log_File: master1-bin.000001
Read_Master_Log_Pos: 1055
Relay_Log_File: 18Q7GVR3CS15BS9-relay-bin.000003
Relay_Log_Pos: 1282
Relay_Master_Log_File: master1-bin.000001
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: 1055
Relay_Log_Space: 1601
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: 5
Slave_Non_Transactional_Groups: 0
Slave_Transactional_Groups: 0
1 row in set (0.000 sec)
需要關注的屬性,Slave_IO_Running, Slave_SQL_Running,Slave_SQL_Running_State,和上面一致,說明已經成功了,接下來所有的master數據庫的修改都會同步到slave,依賴網絡和硬件性能,幾乎是毫秒級別的延遲,能滿足絕大部分的查詢業務。