搭建 mariadb 數據庫主從同步

1、主(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 過時天數。mysql

而後重啓數據庫即生效。sql

2. 建立執行同步的數據庫用戶

CREATE USER 'replication_user'@'%' IDENTIFIED BY 'bigs3cret';
GRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'%';

用戶名: replication_user, 密碼: bigs3cret,能夠修改爲你想要的。數據庫

3. 鎖住數據庫

在導出數據庫時,先加鎖,避免在導出時修改了數據庫致使數據不一致。bash

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 ;

2、從(slave)數據庫配置

1. 配置 server_id

slave 的 server_id, 必須是惟一的,上面咱們配置了 master 的 server_id=1, 這裏的slave 設成2code

[mysqld]
datadir=D:/projects/db/mariadb-10.5.8-winx64/data
port=3307
character-set-server=utf8
server_id=2

重啓 slave 數據庫orm

2. 導入數據庫

把從 master 導出的 cqrs_db.sql 複製到 slave 程序所在的bin目錄下, 而後登陸客戶端,執行server

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,依賴網絡和硬件性能,幾乎是毫秒級別的延遲,能知足絕大部分的查詢業務。

相關文章
相關標籤/搜索