当前位置 博文首页 > Grissom007:搭建 mariadb 数据库主从同步

    Grissom007:搭建 mariadb 数据库主从同步

    作者:Grissom007 时间:2021-02-03 20:28

    一、主(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,依赖网络和硬件性能,几乎是毫秒级别的延迟,能满足绝大部分的查询业务。

    bk
    下一篇:没有了