Linux Mysql 主从复制和级联复制

# 三台主机:
# 主节点:10.0.0.21
# 从节点1:10.0.0.22,从节点2:10.0.0.23

# 主节点配置
[21:46:32 root@centos7 ~]#cat /etc/my.cnf
[mysqld]
server-id=21
log-bin
datadir=/data/mysql
skip_name_resolve=1
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid

[client]
socket=/data/mysql/mysql.sock
[mysql]
prompt="\r:\m:\s(\u@\h) [\d]>\_"

# 在mysql里面配置丛节点复制的账户
:31: (root@localhost) [(none)]> create user repluser@'10.0.0.%' identified by 'replpass';
Query OK, 0 rows affected (0.00 sec)

:33: (root@localhost) [(none)]> grant replication slave on *.* to repluser@'10.0.0.%';
Query OK, 0 rows affected (0.00 sec)

# 记录当前binlog的地址
:43: (root@localhost) [(none)]> show master logs;
+--------------------+-----------+
| Log_name           | File_size |
+--------------------+-----------+
| centos7-bin.000001 |       177 |
| centos7-bin.000002 |     20780 |
+--------------------+-----------+

# 从节点设置
# 首先配置my.cnf
[21:50:27 root@centos7 ~]#cat /etc/my.cnf
[mysqld]
server-id=22
datadir=/data/mysql
skip_name_resolve=1
log-bin
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
read-only=on
relay-log=relay-log
relay-log-index=relay-log.index


[client]
socket=/data/mysql/mysql.sock
[mysql]
prompt="\r:\m:\s(\u@\h) [\d]>\_"

# 在mysql里面配置主节点信息
:44: (root@localhost) [(none)]> CHANGE MASTER TO
    ->   MASTER_HOST='10.0.0.21',
    ->   MASTER_USER='repluser',
    ->   MASTER_PASSWORD='replpass',
    ->   MASTER_PORT=3306,
    ->   MASTER_LOG_FILE='centos7-bin.000002',
    ->   MASTER_LOG_POS= 20780;
Query OK, 0 rows affected, 2 warnings (0.00 sec)

:44: (root@localhost) [(none)]> start slave;

# 若配置错误,请stop slave, 然后再reset slave,最后再重新配置CHANGE MASTER TO

# 查看是否成功
:44: (root@localhost) [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.0.21
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: centos7-bin.000002
          Read_Master_Log_Pos: 20780
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 322
        Relay_Master_Log_File: centos7-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: 20780
              Relay_Log_Space: 523
              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: 21
                  Master_UUID: 779fe189-d421-11eb-83d6-000c299f2dc7
             Master_Info_File: /data/mysql/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: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)


# 级联复制

# 主节点导出数据
mysqldump -A -F --single-transaction --master-data=1 > /data/all.sql
# copy到从节点
scp all.sql 10.0.0.22:/data
scp all.sql 10.0.0.23:/data

# 中间节点设置

[23:21:33 root@centos7 ~]#cat /etc/my.cnf
[mysqld]
server-id=22
datadir=/data/mysql
skip_name_resolve=1
log-bin
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
read-only=on
log-slave-updates


[client]
socket=/data/mysql/mysql.sock
[mysql]
prompt="\r:\m:\s(\u@\h) [\d]>\_"

# 重启mysqld

[23:21:42 root@centos7 ~]#vim /data/all.sql

CHANGE MASTER TO
MASTER_HOST='10.0.0.21',
MASTER_USER='repluser',
MASTER_PASSWORD='replpass',
MASTER_PORT=3306,
MASTER_LOG_FILE='centos7-bin.000003',
MASTER_LOG_POS=154;

# 关闭二进制日志

# 在mysql里面导入all.log

# 记录当前二进制位置

# 开启二进制日志

# start slave;

# :25: (root@localhost) [(none)]> flush privileges;

:25: (root@localhost) [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.21
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: centos7-bin.000003
Read_Master_Log_Pos: 154
Relay_Log_File: centos7-relay-bin.000002
Relay_Log_Pos: 322
Relay_Master_Log_File: centos7-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:

# 在第三个节点设置第二个节点为master

[23:26:52 root@centos7 ~]#cat /etc/my.cnf
[mysqld]
server-id=23
log-bin
datadir=/data/mysql
skip_name_resolve=1
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
read-only=on


[client]
socket=/data/mysql/mysql.sock
[mysql]
prompt="\r:\m:\s(\u@\h) [\d]>\_"

# 重启服务

# 编辑all.log的change master to中间节点


CHANGE MASTER TO
MASTER_HOST='10.0.0.22',
MASTER_USER='repluser',
MASTER_PASSWORD='replpass',
MASTER_PORT=3306,
MASTER_LOG_FILE='centos7-bin.000005',
MASTER_LOG_POS= 154;

 # 导入all。log

#start slave

# show slave

:28: (root@localhost) [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.22
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: centos7-bin.000005
Read_Master_Log_Pos: 306
Relay_Log_File: centos7-relay-bin.000002
Relay_Log_Pos: 474
Relay_Master_Log_File: centos7-bin.000005
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:

 

posted @ 2021-06-23 21:52  每天都在学习的自己  阅读(117)  评论(0)    收藏  举报