mycat docker 主从复制

准备工作

获取mysql镜像:

docker pull mysql:5.7

创建相应的master节点和slave节点:

docker run -p 3339:3306 --name mysql_master -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
docker run -p 3340:3306 --name mysql_slave -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7

进入容器之后,不能使用vim进行操作,需要安装vim:

apt-get update && apt-get install vim -y

修改配置文件

在master容器中的/etc/mysql/mysql.conf.d/mysqld.cnf中添加如下内容:

[mysqld] #在配置文件中存在
server-id=1 # 1到2的32次方,不能是0,0表示不接受任何slave的连接,全局唯一
log-bin=mysql-bin #开启log-bin
# binlog-ignore-db=mysql # 不对mysql进行主从复制
# binlog-do-db=test #只对数据库test进行主从复制
# binlog_format=STATEMENT
# 要主从哪个数据库,根据需要填写
# 在mysql 5.6之后,推荐使用binlog_format=row, 只输出需要更新的字段

在slave容器中的/etc/mysql/mysql.conf.d/mysqld.cnf中添加如下内容:

server-id=2 #全局唯一
log-bin=mysql-slave-bin #开启log-bin
relay_log=mysql-relay-bin
#replicate-do-db    设定需要复制的数据库
#replicate-ignore-db 设定需要忽略的复制数据库
#replicate-do-table  设定需要复制的表
#replicate-ignore-table 设定需要忽略的复制表
#replicate-wild-do-table 同replication-do-table功能一样,但是可以通配符
#replicate-wild-ignore-table 同replication-ignore-table功能一样,但是可以加通配符

然后将master和slave容器进行重启:

docker restart mysql_master
docker restart mysql_slave

在主库中创建用户,并进行赋权:

CREATE USER 'slave'@'%' IDENTIFIED BY '123456';
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';

设置主从

先获取到master节点的ip地址:

docker inspect --format='{{.NetworkSettings.IPAddress}}' mysql_master

将获取到的ip地址记录下来,以被后面用。
从mysql_master中获取相应的信息:

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

在mysql_slave中执行下面的语句:

mysql> change master to master_host='172.17.0.3', master_user='slave', master_password='123456',master_port=3306,master_log_file='mysql-bin.000002', master_log_pos=154, master_connect_retry=30;
# master_host : mysql_master的ip地址
# master_user: 刚才在mysql_master创建的用户
# master_password:刚才在mysql_master创建的用户 的密码
# master_port:mysql_master 端口
# master_log_file:mysql_master  的binlog文件名称
# master_log_pos : mysql_master 的binlog写到哪个位置,从哪个位置开始复制。

在mysql_slave中执行下面的语句:

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State:
                  Master_Host: 172.17.0.3
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 30
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: No
            Slave_SQL_Running: No
              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: 154
              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: NULL
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: 0
                  Master_UUID:
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State:
           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:
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.17.0.3
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 30
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-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: 154
              Relay_Log_Space: 527
              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_UUID: 0368b534-996e-11ec-b978-0242ac110003
             Master_Info_File: /var/lib/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:

在执行start slave;命令之后,从节点就开始从主节点开始获取binlog日志,Slave_IO_Running,Slave_SQL_Running两个字段的值从NO变成YES。
在mysql_master中创建一个数据库来验证:

mysql> create database test;
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+

在mysql_slave中执行命令,看这个test数据库是否也存在:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+

如果存在说明主从复制已经配置好,可以继续往下执行,如果没有,按照之前的步骤进行检查。

Mycat

下载地址:

docker pull longhronshens/mycat-docker

创建文件

server.xml




    
    0  
    0  
        2
       
         
    
    
        
        0
        
        
        
        
        
        
        
        
        0
        
        0
        
        1m
        
        1k
        0
        
        384m
        
        true
    

    
    
        123456
        TESTDB
    
    
        123456
        TESTDB
        true
    

rule.xml





    
        
            id
            mod-long
        
    

    
        2
    

schema.xml






    
    
    
    
    
        select user()
   
        
            
        
    

balance值介绍:

balance=0:不开启读写分离机制,所有读操作都发送到当前可用的writehost上;
balance=1:全部的readhost和stand by write host参与select语句的负载均衡,即当双主双从模式下,其他的节点都参与select语句的负载均衡
balance=2:所有的读操作都随机在writehost、readhost上分发
balance=3:所有的毒请求随机的分发到readhost上执行,writehost上不负担读压力

writeType值介绍:

writeType=0,所有的写操作都发送到配置的第一个writehost,第一个挂了,切换都还存活的第二个
writeType=1,所有的写操作随机发送到配置的writehost中
#writehost的切换规则,需要写到配置文件中

switchType值介绍:

switchType=1,默认值,自动切换;
switchType=2.基于mysql主从同步的状态决定是否切换
switchType=-1,表示不自动切换

创建mycat容器:

docker run -d -p 8066:8066 -v /Users/apple/mycat/schema.xml:/usr/local/mycat/conf/schema.xml -v /Users/apple/mycat/rule.xml:/usr/local/mycat/conf/rule.xml -v /Users/apple/mycat/server.xml:/usr/local/mycat/conf/server.xml --name mycat longhronshens/mycat-docker

获取mycat的ip地址,从master节点的登录到这个系统中:

docker exec -it mysql_master bash
mysql -uroot -p123456 -h 172.17.0.5 -P 8066
mysql> show databases;
+----------+
| DATABASE |
+----------+
| TESTDB   |
+----------+
mysql>  create table psn(id int primary key,name varchar(20));
Query OK, 0 rows affected (0.02 sec)

在mysql_master 节点上就可以看到psn这个表:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| msb                |
| mysql              |
| performance_schema |
| sys                |
| test_master        |
+--------------------+
mysql> use test_master;
Database changed
mysql> show tables;
+-----------------------+
| Tables_in_test_master |
+-----------------------+
| psn                   |
+-----------------------+

参考文献

基于docker mycat 配置mysql主从复制
E: Unable to locate package vim 的解决方案---docker 镜像下安装Vim
Mycat+docker+mysql实现主从复制(一主一从)

你可能感兴趣的:(mycat docker 主从复制)