数据库
首页 > 数据库> > docker 搭建mysql集群

docker 搭建mysql集群

作者:互联网

docker 版本:Docker version 20.10.14, build a224086

使用win10系统

查看版本命令(docker -v

实现一主二从

准备

1、拉取镜像mysql-8.0

c:\>docker pull mysql:8.0

查看镜像

c:\>docker images

2、准备配置信息

D:/mysql8/master/cnf
D:/mysql8/master/data

D:/mysql8/slave1/cnf
D:/mysql8/slave1/data

D:/mysql8/slave2/cnf
D:/mysql8/slave2/data

配置

[mysqld]
# 设置server_id,注意要唯一
server-id=1
# 开启binlog
log-bin=mysql-bin
# binlog缓存
binlog_cache_size=1M
# binlog格式(mixed、statement、row,默认格式是statement)
binlog_format=mixed
# 设置字符编码为utf8mb4
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4

放到 D:/mysql8/master/cnf里。

[mysqld]
# 设置server_id,注意要唯一
server-id=2
# 开启binlog
log-bin=mysql-slave-bin
# relay_log配置中继日志
relay_log=edu-mysql-relay-bin
# 如果需要同步函数或者存储过程
log_bin_trust_function_creators=false
# binlog缓存
binlog_cache_size=1M
# binlog格式(mixed、statement、row,默认格式是statement)
binlog_format=mixed
# 设置字符编码为utf8mb4
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
slave_skip_errors=1062
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4

分别放到D:/mysql8/slave1/cnfD:/mysql8/slave2/cnf里。

创建主从

C:\>docker run -it -d -p 3307:3306 --name master -v d:/mysql8/master/cnf:/etc/mysql/conf.d -v d:/mysql8/master/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:8

C:\>docker run -it -d -p 3308:3306 --name slave1 -v d:/mysql8/slave1/cnf:/etc/mysql/conf.d -v d:/mysql8/slave1/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:8

C:\>docker run -it -d -p 3309:3306 --name slave2 -v d:/mysql8/slave2/cnf:/etc/mysql/conf.d -v d:/mysql8/slave2/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:8

-p 3307:3306 把物理机的3307端口与docker中的端口3306进行映射

-v 给容器挂载存储卷,挂载到容器的某个目录

-e MYSQL_ROOT_PASSWORD=123456 设置MySQL的root账号密码为123456

docker ps

c:\> exec -it mysql /bin/bash
bash-4.4# mysql -uroot -p
mysql> show variables like 'mysql_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 2     |
+---------------+-------+

# 创建用户名:reader  密码:reader
mysql> CREATE USER reader IDENTIFIED BY 'reader';
Query OK, 0 rows affected (0.03 sec)
# 给reader同步权限
mysql> GRANT REPLICATION SLAVE ON *.* to 'reader'@'%';
Query OK, 0 rows affected (0.00 sec)
# 立即生效
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      157 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

# 再打开个控制台,查看master在docker中的地址
C:\>docker inspect --format='{{.NetworkSettings.IPAddress}}' master
'172.17.0.2'

如果出现:[Warning] World-writable config file '/etc/mysql/conf.d/mysql.cnf' is ignored.

登录docker容器后修改/etc/mysql/conf.d/mysql.cnf的权限chmod 664 mysql.conf

# 配置连接主服务器的参数
mysql> change master to master_host='172.17.0.2',master_user='reader',master_password='reader',master_log_file='mysql-bin.000001',master_log_pos=157;
# 启动同步
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.03 sec)
# 查看状态
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 172.17.0.2
                  Master_User: reader
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 157
               Relay_Log_File: edu-mysql-relay-bin.000002
                Relay_Log_Pos: 326
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes # 这个
            Slave_SQL_Running: Yes # 和这个都是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: 157
              Relay_Log_Space: 540
              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: 11a8e0c8-030b-11ed-bbc9-0242ac110003
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Replica 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, 1 warning (0.00 sec)

没有成功的话,检查账号、密码、地址、pos等。

停止连接:mysql> stop slave;

在主服务器上查看结果

mysql> show slave hosts;
+-----------+------+------+-----------+--------------------------------------+
| Server_id | Host | Port | Master_id | Slave_UUID                           |
+-----------+------+------+-----------+--------------------------------------+
|         2 |      | 3306 |         1 | 56031934-030b-11ed-bc3c-0242ac110004 |
|         3 |      | 3306 |         1 | 9218281c-030b-11ed-bb85-0242ac110005 |
+-----------+------+------+-----------+--------------------------------------+

可能存在的问题:

mysql> start slave;
ERROR 1872 (HY000): Slave failed to initialize relay log info structure from the repository

解决办法:先执行 reset slave; 再执行start slave;

标签:mysql8,mysql,cnf,Master,master,集群,docker
来源: https://www.cnblogs.com/mantishell/p/16482540.html