【docker】docker安装mysql并配置主从复制

目录

工作环境

一、安装mysql

二、主服务器配置

1、查看mysql的日志文件,配置文件,二进制文件位置

2、重新主库

3、配置文件

4、创建数据同步账号

三、从服务器配置

1、创建从库

二、配置文件

三、配置主从

1、进入主数据库,查看主从同步状态

2、进入从数据库,配置主从

四、验证

工作环境

cetenos7,Docker版本为20.10.19

虚拟机IP地址为:192.168.10.150

一、安装mysql

注意:两台主机都需要安装docker和拉取mysql镜像

        docker的安装请访问 【kubernetes】k8s集群搭建(完整详解)_维运的博客-CSDN博客_如何搭建k8s集群

[root@hadoop01 ~]# systemctl enable docker --now
[root@hadoop01 ~]# docker pull mysql
[root@hadoop01 ~]# docker images        #查看下载的mysql镜像
REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
mysql        latest    40b83de8fb1a   2 days ago   535MB

二、主服务器配置

1、查看mysql的日志文件,配置文件,二进制文件位置

[root@hadoop01 ~]# docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql
[root@hadoop01 ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS        PORTS                                                  NAMES
f9f12cd5c68d   mysql     "docker-entrypoint.s…"   2 seconds ago   Up 1 second   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   happy_rhodes

[root@hadoop01 ~]# docker exec -it f9f12cd5c68d /bin/bash
#查看mysql日志位置
bash-4.4# cd /var/log/           
bash-4.4# ls
mysqld.log
bash-4.4# ls -l
total 0
-rw-r-----. 1 mysql mysql 0 Oct 12 21:38 mysqld.log
#查看mysql二进制位置
bash-4.4# cd /var/lib/mysql
mysql/         mysql-files/   mysql-keyring/ 
#查看mysql配置文件位置
bash-4.4# cd /etc/mysql/
bash-4.4# ls
conf.d
#下载日志文件和配置文件
[root@hadoop01 ~]# docker cp f9f12cd5c68d:/var/log/mysqld.log /mydata/mysql-master/log/
[root@hadoop01 ~]# docker cp a4e3bed3be0c:/etc/mysql/conf.d /mydata/mysql-master/conf
[root@hadoop01 ~]# docker rm -f happy_rhodes     #删掉刚刚的创建的容器

2、重新主库

[root@hadoop01 ~]# docker run -d -p 3307:3306 --name mysql-master \
-v /mydata/mysql-master/log:/var/log/ \
-v /mydata/mysql-master/data:/var/lib/mysql \
-v /mydata/mysql-master/conf/:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=root \
mysql 
[root@hadoop01 ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS        PORTS                                                  NAMES
23cb81ae44fe   mysql     "docker-entrypoint.s…"   2 seconds ago   Up 1 second   33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp   mysql-master

3、配置文件

[root@hadoop01 ~]# cat /mydata/mysql-master/conf/my.cnf 
[mysqld]
server_id=101                            #server_id唯一
binlog-ignore-db=mysql                   #指定不需要同步的数据库名称
log-bin=mall-mysql-bin                   #开启二进制日志功能
binlog_cache_size=1M                     #设置二进制日志使用内存大小(事务)
binlog_format=mixed                      #设置使用的二进制日志的格式(mixed,statement,row)
expire_logs_days=7                       #二进制日志过期清理时间,默认值为0,表示不自动清理
slave_skip_errors=1062     #跳过主从复制中遇到的所有的错误或指定类型的错误,避免slave端复制中断
                           #1062错误是指一些组件重复,1032错误是主从数据库数据不一致 
[root@hadoop01 ~]# docker restart mysql-master                               

4、创建数据同步账号

[root@hadoop01 ~]# docker exec -it mysql-master bash
bash-4.4# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.31 MySQL Community Server - GPL

Copyright (c) 2000, 2022, 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.

mysql> create user 'slave'@'%' identified by '123456';
Query OK, 0 rows affected (0.19 sec)

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

三、从服务器配置

1、创建从库

#先创建一个容器,下载文件
[root@hadoop01 ~]# docker run -itd  -p 3308:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql
[root@hadoop01 ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                                                  NAMES
bec706ad4ff7   mysql     "docker-entrypoint.s…"   5 seconds ago   Up 4 seconds   33060/tcp, 0.0.0.0:3308->3306/tcp, :::3308->3306/tcp   infallible_lehmann
23cb81ae44fe   mysql     "docker-entrypoint.s…"   2 days ago      Up 3 hours     33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp   mysql-master
[root@hadoop01 ~]# mkdir /mydata/mysql-slave/log -p
[root@hadoop01 ~]# mkdir /mydata/mysql-slave/conf 
[root@hadoop01 ~]# docker cp infallible_lehmann:/var/log/mysqld.log /mydata/mysql-slave/log/
[root@hadoop01 ~]# docker cp infallible_lehmann:/etc/mysql/conf.d /mydata/mysql-slave/conf
[root@hadoop01 ~]# docker rm -f infallible_lehmann 
#创建新的容器
[root@hadoop01 ~]# docker run -d -p 3308:3306 --name mysql-slave \
-v /mydata/mysql-slave/log:/var/log/log \
-v /mydata/mysql-slave/data:/var/lib/mysql \
-v /mydata/mysql-master/conf/:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=root \
mysql
[root@hadoop01 ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                                                  NAMES
735b22b8b8fc   mysql     "docker-entrypoint.s…"   3 minutes ago   Up 3 minutes   33060/tcp, 0.0.0.0:3308->3306/tcp, :::3308->3306/tcp   mysql-slave
23cb81ae44fe   mysql     "docker-entrypoint.s…"   2 days ago      Up 3 hours     33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp   mysql-master

二、配置文件

[root@hadoop01 ~]# cat /mydata/mysql-slave/conf/my.cnf 
[mysqld]
server_id=102
binlog-ignore-db=mysql
log-bin=mall-mysql-bin
binlog_cache_size=1M
binlog_format=mixed
expire_logs_days=7
slave_skip_errors=1062
replay_log=mall-mysql-replay-bin    #配置中级日志
log_slave_updates=1                 #1代表slave写进自己的二进制
read_only=1                         #设置只读,super权限除外
[root@hadoop01 ~]# docker restart mysql-slave 

三、配置主从

1、进入主数据库,查看主从同步状态

[root@hadoop01 ~]# docker exec -it mysql-master bash
bash-4.4# mysql -uroot -p
mysql> show master status;
+-----------------------+----------+--------------+------------------+-------------------+
| File                  | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------------+----------+--------------+------------------+-------------------+
| mall-mysql-bin.000003 |      157 |              | mysql            |                   |
+-----------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

注意:后面配置主从需要这个

2、进入从数据库,配置主从

[root@hadoop01 ~]# docker exec -it mysql-slave bash
bash-4.4# mysql -uroot -proot
mysql> change master to \
     master_host='192.168.10.150', \
     master_user='slave', \
     master_password='123456', \
     master_port=3307, \
     master_log_file='mall-mysql-bin.000003',
     master_log_pos=157, \
     master_connect_retry=30;

代码解释

查看主从状态

mysql> start slave;
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Connecting to source
                  Master_Host: 192.168.10.150
                  Master_User: slave
                  Master_Port: 3307
                Connect_Retry: 30
              Master_Log_File: mall-mysql-bin.000004
          Read_Master_Log_Pos: 157
               Relay_Log_File: 735b22b8b8fc-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mall-mysql-bin.000004
             Slave_IO_Running: Connecting
            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: 157
              Relay_Log_Space: 337
              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: 2061
                Last_IO_Error: error connecting to master 'slave@192.168.10.150:3307' - retry-time: 30 retries: 1 message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 0
                  Master_UUID: 
             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: 221018 08:43:26
     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)

 出现了错误

Last_IO_Errno: 2061,Last_IO_Error: error connecting to master 'slave@192.168.10.150:3307' - retry-time: 30 retries: 1 message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.

解决如下:

mysql> alter user 'slave'@'%' identified with mysql_native_password by'123456';
mysql> flush privileges;

这时又会抛出一个问题

Last_IO_Errno: 13117
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the --replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it).

解决方法如下 

#进入从数据库容器
mysql> show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 101   |
+---------------+-------+
1 row in set (0.04 sec)

mysql> set global server_id=102;
mysql> stop slave;
mysql> start slave;

检查一下

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 192.168.10.150
                  Master_User: slave
                  Master_Port: 3307
                Connect_Retry: 30
              Master_Log_File: mall-mysql-bin.000005
          Read_Master_Log_Pos: 157
               Relay_Log_File: 735b22b8b8fc-relay-bin.000003
                Relay_Log_Pos: 331
        Relay_Master_Log_File: mall-mysql-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: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 157
              Relay_Log_Space: 548
              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: 101
                  Master_UUID: 020c90b6-4c75-11ed-ade7-0242ac110002
             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)

此处正常显示为Slave_IO_Running: Yes,Slave_SQL_Running: Yes

四、验证

#在主库上创建一个数据库
mysql> create database ooo;
Query OK, 1 row affected (0.02 sec)
#在从库上查看
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| ooo                |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

完成。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

维运

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

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

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

打赏作者

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

抵扣说明:

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

余额充值