mysql8.0主从搭建

主节点

**

[xuhaixing@xhx151 ~]$ docker run --name mysql-master1 --privileged=true  -v /opt/module/docker/mysql/master1:/var/lib/mysql -p 3307:3306 -d -e MYSQL_ROOT_PASSWORD=xuhaixing mysql:8.0.19
[xuhaixing@xhx151 ~]$ docker exec -it mysql-master1  /bin/bash
root@dd6e3341b2fb:/# vi /etc/mysql/my.cnf

**

加入配置,主从server-id需要唯一

**

//docker容器内地mysql,
//使用docker exec -it 容器id /bin/bash进入
//方法一cd etc/mysql找到my.cnf,添加。
//方法二也可映射出mysql的my.cnf,方便修改
server-id=100
log-bin=mysql-bin

**

创建一个用户

**

mysql> create user 'xhxcopy'@'%' identified with mysql_native_password by 'xuhaixing';
Query OK, 0 rows affected (0.04 sec)

mysql> grant replication slave on *.* to 'xhxcopy'@'%';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

**

重启服务

记住file的值和Position的值

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 |      155 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

从节点

[xuhaixing@xhx151 ~]$ docker run --name mysql-slave1 --privileged=true  -v /opt/module/docker/mysql/salve1:/var/lib/mysql -p 3310:3306 -d -e MYSQL_ROOT_PASSWORD=xuhaixing mysql:8.0.19

[xuhaixing@xhx151 mysql]$ docker exec -it mysql-slave1 /bin/bash
root@7b63b2d8a900:/# vi /etc/mysql/my.cnf

加入

server-id=105
relay-log=relay-log

登录mysql

root@eb80f55cf178:/# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.23 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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.

运行这段话配置信息换成你自己的

change master to MASTER_HOST='59.110.41.100',
MASTER_PORT=8089,
MASTER_USER='zhou',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000003',
MASTER_LOG_POS=155 ;

这是运行这里运行start slave就好如果发生异常重来就需要先运行stop slave;

stop slave;//停止
start slave;//运行
//start slave;如果出现异常 场景一
ERROR 1872 (HY000): Slave failed to initialize relay log info structure from the repository
//执行
reset slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)
//之后在启动
start slave;
Query OK, 0 rows affected, 1 warning (0.02 sec)
//如果你之前启动了一个io但是并没有实现主从,然后你没有关闭运行 如果出现异常 场景二
change master to MASTER_HOST='59.110.41.100',
MASTER_PORT=8089,
MASTER_USER='zhou',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000003',
MASTER_LOG_POS=155 ;
//会报错
ERROR 3021 (HY000): This operation cannot be performed with a running slave io thread; run STOP SLAVE IO_THREAD FOR CHANNEL '' first.
//这个时候你需要执行关闭io然后在运行即可
STOP SLAVE IO_THREAD;

查询详情

show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.94.151
                  Master_User: xhxcopy
                  Master_Port: 3307
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 2707
               Relay_Log_File: 7b63b2d8a900-relay-bin.000002
                Relay_Log_Pos: 516
        Relay_Master_Log_File: mysql-bin.000003
             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: 2707
              Relay_Log_Space: 731
              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: 100
                  Master_UUID: 59e27f31-7051-11ea-b1a3-0242ac110002
             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: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
       Master_public_key_path: 
        Get_master_public_key: 0
            Network_Namespace: 
1 row in set (0.00 sec)

这里都为Yes才是成功

Slave_IO_Running: Yes
    Slave_SQL_Running: Yes
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值