MySQL(四) 主从复制(多源复制)

该文详细介绍了如何初始化并启动3个MySQL实例,使用mysqld_multi进行多实例管理。然后通过脚本创建复制用户并设置主从复制,使得3313实例成为3311和3312的从库。最后验证了多源复制的正确性,通过在主库上创建数据库,观察从库是否同步。文章深入浅出地展示了MySQL多源复制的实现过程。
摘要由CSDN通过智能技术生成

MySQL 主从复制(多源复制)

  • 初始化3个MySQL实例
  • **注意:**脚本有可能创建复制用户是失败的,需要检查下是否成功创建repl用户
  • 使用mysqld_multi管理启动MySQL多实例
[root@db03 ~]# cat multisource_env.sh
#!/bin/bash

mysql_cmd="/usr/local/mysql/bin/mysqld"

# 创建multi管理配置文件
mkdir -p /labs
cat > /labs/multisource.cnf <<EOF
[mysqld1]
server-id=3311
user=mysql
socket=/replication/3311/mysql.sock
port=3311
datadir=/replication/3311/data
log-error=/replication/3311/error.log
log-bin=/replication/3311/binlog/mysql-bin
relay-log=/replication/3311/relaylog/relay-bin
log-slave-updates
report-port=3311
report-host=localhost
relay-log-recovery=1
master-info-repository=TABLE
relay-log-info-repository=TABLE
gtid-mode=on
enforce-gtid-consistency

[mysqld2]
server-id=3312
user=mysql
socket=/replication/3312/mysql.sock
port=3312
datadir=/replication/3312/data
log-error=/replication/3312/error.log
log-bin=/replication/3312/binlog/mysql-bin
relay-log=/replication/3312/relaylog/relay-bin
log-slave-updates
report-port=3312
report-host=localhost
relay-log-recovery=1
master-info-repository=TABLE
relay-log-info-repository=TABLE
gtid-mode=on
enforce-gtid-consistency

[mysqld3]
server-id=3313
user=mysql
socket=/replication/3313/mysql.sock
port=3313
datadir=/replication/3313/data
log-error=/replication/3313/error.log
log-bin=/replication/3313/binlog/mysql-bin
relay-log=/replication/3313/relaylog/relay-bin
log-slave-updates
report-port=3313
report-host=localhost
relay-log-recovery=1
master-info-repository=TABLE
relay-log-info-repository=TABLE
gtid-mode=on
enforce-gtid-consistency
EOF

# 初始化实例
echo -e "----- init mysql -----"
mkdir -p /replication/33{11,12,13}/{binlog,relaylog}
chown -R mysql.mysql /replication/33{11,12,13}
${mysql_cmd} --no-defaults --initialize-insecure --user=mysql --datadir=/replication/3311/data
${mysql_cmd} --no-defaults --initialize-insecure --user=mysql --datadir=/replication/3312/data
${mysql_cmd} --no-defaults --initialize-insecure --user=mysql --datadir=/replication/3313/data

# 启动所有实例
echo -e "----- start mysql -----"
${mysql_cmd}_multi --defaults-file=/labs/multisource.cnf start 1-3

echo -e "----- sleep 10s -----"
sleep 10

# 创建复制用户 只对3311和3312创建
echo -e "----- create replication slave user -----"
${mysql_cmd} -P 3311 -e "grant replication slave on *.* to repl@'127.0.0.1' identified by 'oracle'; reset master;"
${mysql_cmd} -P 3312 -e "grant replication slave on *.* to repl@'127.0.0.1' identified by 'oracle'; reset master;"

echo -e "----- Done -----"
运行脚本后可以看到3311/3312/3313实例全部启动
[root@db03 ~]# sh multisource_env.sh
----- init mysql -----
2020-04-23T09:31:44.655575Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-04-23T09:31:44.835648Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-04-23T09:31:44.871478Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-04-23T09:31:44.927662Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 3f4a0c4e-8545-11ea-b265-02000aba3d09.
2020-04-23T09:31:44.928659Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-04-23T09:31:44.929158Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
2020-04-23T09:31:46.839819Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-04-23T09:31:47.014100Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-04-23T09:31:47.047423Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-04-23T09:31:47.103188Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 40960162-8545-11ea-b2ea-02000aba3d09.
2020-04-23T09:31:47.104036Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-04-23T09:31:47.104698Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
2020-04-23T09:31:48.987927Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-04-23T09:31:49.138326Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-04-23T09:31:49.168599Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-04-23T09:31:49.225036Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 41d9c677-8545-11ea-b44d-02000aba3d09.
2020-04-23T09:31:49.225900Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-04-23T09:31:49.226466Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
----- start mysql -----
----- sleep 10s -----
----- create replication slave user -----
----- Done -----

[root@db03 ~]# netstat -tunpl |grep mysqld
tcp        0      0 0.0.0.0:3311            0.0.0.0:*               LISTEN      2842/mysqld        
tcp        0      0 0.0.0.0:3312            0.0.0.0:*               LISTEN      2845/mysqld        
tcp        0      0 0.0.0.0:3313            0.0.0.0:*               LISTEN      2848/mysqld
在3313实例对3311/3312创建主从连接信息
  • 也就是说3313实例同时做3311和3312两个实例的从库。(注意:两个主库不要搞冲突的数据)
[root@db03 ~]# mysql -P 3313
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

root@localhost[(none)]> prompt 3313 >;
PROMPT set to '3313 >'
3313 >

3313 >CHANGE MASTER TO
    ->    MASTER_HOST='127.0.0.1',
    ->    MASTER_USER='repl',
    ->    MASTER_PASSWORD='oracle',
    ->    MASTER_PORT=3311,
    ->    MASTER_CONNECT_RETRY=10,
    ->    MASTER_AUTO_POSITION=1
    ->    FOR CHANNEL 'master1';
Query OK, 0 rows affected, 2 warnings (0.01 sec)

3313 >CHANGE MASTER TO
    ->    MASTER_HOST='127.0.0.1',
    ->    MASTER_USER='repl',
    ->    MASTER_PASSWORD='oracle',
    ->    MASTER_PORT=3312,
    ->    MASTER_CONNECT_RETRY=10,
    ->    MASTER_AUTO_POSITION=1
    ->    FOR CHANNEL 'master2';
Query OK, 0 rows affected, 2 warnings (0.00 sec)

3313 >start slave for channel 'master1';
Query OK, 0 rows affected (0.01 sec)

3313 >start slave for channel 'master2';
Query OK, 0 rows affected (0.01 sec)
查看多源复制从节点状态信息
3313 >show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 127.0.0.1
                  Master_User: repl
                  Master_Port: 3311
                Connect_Retry: 10
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: relay-bin-master1.000003
                Relay_Log_Pos: 367
        Relay_Master_Log_File: mysql-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: 154
              Relay_Log_Space: 789
              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: 3311
                  Master_UUID: 3f4a0c4e-8545-11ea-b265-02000aba3d09
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 1
         Replicate_Rewrite_DB:
                 Channel_Name: master1
           Master_TLS_Version:
*************************** 2. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 127.0.0.1
                  Master_User: repl
                  Master_Port: 3312
                Connect_Retry: 10
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: relay-bin-master2.000003
                Relay_Log_Pos: 367
        Relay_Master_Log_File: mysql-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: 154
              Relay_Log_Space: 789
              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: 3312
                  Master_UUID: 40960162-8545-11ea-b2ea-02000aba3d09
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 1
         Replicate_Rewrite_DB:
                 Channel_Name: master2
           Master_TLS_Version:
2 rows in set (0.00 sec)
创建数据库检查3313实例是否可以同步创建
[root@db03 ~]# mysql -P 3311 -e "create database test1;"
[root@db03 ~]# mysql -P 3312 -e "create database test2;
[root@db03 ~]# mysql -P 3313 -e "show databases;"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test1              |
| test2              |
+--------------------+
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

正在输入中…………

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值