docker Mysql configuration based master copy from the structures Docker

Mysql based Docker's main building from copy

 

Why build based Docker?

  • Limited resources
  • Virtual machine set up on the machine configuration is required, and a complicated step to install mysql
  • A plurality of containers can be run on a single machine Docker
  • Docker containers are independent, separate IP, do not conflict
  • Docker use simple steps to start the second level in the container

Structures from the master server using Docker

First pull docker mirror, we here use the 5.7 version of mysql:

docker pull mysql:5.7

This image is then used to start the container, where the need to start from two separate containers master

Master (Main):

docker run -p 3339:3306 --name mymysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7

Slave (from):

docker run -p 3340:3306 --name mymysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7

Foreign Port Master map is 3339, the external port Slave mapping is 3340. Because docker containers are independent of each other, each container has its own independent IP, so a different container using the same port and will not conflict. Here we should try to use mysql default port 3306, or may not appear in the issue by ip connection docker container mysql.

Use docker psthe command to view the containers are running:

mark

At this time, other tools can be used to test the connection mysql Navicat

mark

Configuration Master (Main)

By docker exec -it 627a2368c865 /bin/bashcommand Master into the interior of the container, you can also docker exec -it mysql-master /bin/bashcommand to enter. 627a2368c865 is the id of the container, and mysql-master is the name of the vessel.

cd /etc/mysqlSwitch to the under / etc / mysql directory, and then vi my.cnfto edit my.cnf. At this point will be reported out bash: vi: command not found, we need to install vim themselves inside the docker container. Use apt-get install vimcommand to install vim

It will appear the following questions:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package vim

Execution apt-get update, and then executed again apt-get install vimto successfully install vim. Then we can use vim to edit my.cnf, add the following configuration in the my.cnf:

[mysqld]
## 同一局域网内注意要唯一
server-id=100  
## 开启二进制日志功能,可以随便取(关键) log-bin=mysql-bin

After configuration is complete, you need to restart the mysql service configuration to take effect. Use service mysql restartcomplete restart. Will make the docker container to stop the restart mysql service, we also need to docker start mysql-masterstart the container.

The next step is created in the Master database data synchronization user, the user grants permission slave REPLICATION SLAVE and REPLICATION CLIENT privilege for the primary synchronization between the data from the database.

CREATE USER 'slave'@'%' IDENTIFIED BY '123456';

GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';

mark

Configuring the Slave (from)

And configuring Master (primary) as added to the configuration file follows the my.cnf Slave:

[mysqld]
## 设置server_id,注意要唯一
server-id=101  
## 开启二进制日志功能,以备Slave作为其它Slave的Master时使用 log-bin=mysql-slave-bin ## relay_log配置中继日志 relay_log=edu-mysql-relay-bin 

After the configuration and the need to restart the mysql service operation and configuration of the container docker Master (primary) consistent.

Link Master (primary) and Slave (from)

In the Master into the mysql, executeshow master status;

mark

Behind the value of the File and Position fields will be used, before the latter operation is completed, the need to ensure that Master library can not do anything, otherwise it will cause a state change, changes in the value of File and Position fields.

In Slave enter mysql, executechange master to master_host='172.17.0.2', master_user='slave', master_password='123456', master_port=3306, master_log_file='mysql-bin.000001', master_log_pos= 2830, master_connect_retry=30;

Command Description:

MASTER_HOST  : Master address, referring to the independent ip container, by docker inspect --format='{{.NetworkSettings.IPAddress}}' 容器名称|容器idip query container

mark

MASTER_PORT : Master port number, port number refers to a container

MASTER_USER : user data for synchronization

master_password : used to synchronize the user's password

MASTER_LOG_FILE : Specifies Slave start copying data from which log file, i.e., File field value in the above-mentioned

master_log_pos value from which to start reading Position, Position i.e. the above-mentioned fields:

master_connect_retry:如果连接失败,重试的时间间隔,单位是秒,默认是60秒

在Slave 中的mysql终端执行show slave status \G;用于查看主从同步状态。

mark

正常情况下,SlaveIORunning 和 SlaveSQLRunning 都是No,因为我们还没有开启主从复制过程。使用start slave开启主从复制过程,然后再次查询主从同步状态show slave status \G;

mark

SlaveIORunning 和 SlaveSQLRunning 都是Yes,说明主从复制已经开启。此时可以测试数据同步是否成功。

主从复制排错:

mark

使用start slave开启主从复制过程后,如果SlaveIORunning一直是Connecting,则说明主从复制一直处于连接状态,这种情况一般是下面几种原因造成的,我们可以根据 Last_IO_Error提示予以排除。

  1. 网络不通

    检查ip,端口

  2. 密码不对

    检查是否创建用于同步的用户和用户密码是否正确

  3. pos不对

    检查Master的 Position

测试主从复制

测试主从复制方式就十分多了,最简单的是在Master创建一个数据库,然后检查Slave是否存在此数据库。

Master:

mark

Slave:

mark

 

为什么基于Docker搭建?

  • 资源有限
  • 虚拟机搭建对机器配置有要求,并且安装mysql步骤繁琐
  • 一台机器上可以运行多个Docker容器
  • Docker容器之间相互独立,有独立ip,互不冲突
  • Docker使用步骤简便,启动容器在秒级别

利用Docker搭建主从服务器

首先拉取docker镜像,我们这里使用5.7版本的mysql:

docker pull mysql:5.7

然后使用此镜像启动容器,这里需要分别启动主从两个容器

Master(主):

docker run -p 3339:3306 --name mymysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7

Slave(从):

docker run -p 3340:3306 --name mymysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7

Master对外映射的端口是3339,Slave对外映射的端口是3340。因为docker容器是相互独立的,每个容器有其独立的ip,所以不同容器使用相同的端口并不会冲突。这里我们应该尽量使用mysql默认的3306端口,否则可能会出现无法通过ip连接docker容器内mysql的问题。

使用docker ps命令查看正在运行的容器:

mark

此时可以使用Navicat等工具测试连接mysql

mark

配置Master(主)

通过docker exec -it 627a2368c865 /bin/bash命令进入到Master容器内部,也可以通过docker exec -it mysql-master /bin/bash命令进入。627a2368c865是容器的id,而mysql-master是容器的名称。

cd /etc/mysql切换到/etc/mysql目录下,然后vi my.cnf对my.cnf进行编辑。此时会报出bash: vi: command not found,需要我们在docker容器内部自行安装vim。使用apt-get install vim命令安装vim

会出现如下问题:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package vim

执行apt-get update,然后再次执行apt-get install vim即可成功安装vim。然后我们就可以使用vim编辑my.cnf,在my.cnf中添加如下配置:

[mysqld]
## 同一局域网内注意要唯一
server-id=100  
## 开启二进制日志功能,可以随便取(关键) log-bin=mysql-bin

配置完成之后,需要重启mysql服务使配置生效。使用service mysql restart完成重启。重启mysql服务时会使得docker容器停止,我们还需要docker start mysql-master启动容器。

下一步在Master数据库创建数据同步用户,授予用户 slave REPLICATION SLAVE权限和REPLICATION CLIENT权限,用于在主从库之间同步数据。

CREATE USER 'slave'@'%' IDENTIFIED BY '123456';

GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';

mark

配置Slave(从)

和配置Master(主)一样,在Slave配置文件my.cnf中添加如下配置:

[mysqld]
## 设置server_id,注意要唯一
server-id=101  
## 开启二进制日志功能,以备Slave作为其它Slave的Master时使用 log-bin=mysql-slave-bin ## relay_log配置中继日志 relay_log=edu-mysql-relay-bin 

配置完成后也需要重启mysql服务和docker容器,操作和配置Master(主)一致。

链接Master(主)和Slave(从)

在Master进入mysql,执行show master status;

mark

File和Position字段的值后面将会用到,在后面的操作完成之前,需要保证Master库不能做任何操作,否则将会引起状态变化,File和Position字段的值变化。

在Slave 中进入 mysql,执行change master to master_host='172.17.0.2', master_user='slave', master_password='123456', master_port=3306, master_log_file='mysql-bin.000001', master_log_pos= 2830, master_connect_retry=30;

命令说明:

master_host :Master的地址,指的是容器的独立ip,可以通过docker inspect --format='{{.NetworkSettings.IPAddress}}' 容器名称|容器id查询容器的ip

mark

master_port:Master的端口号,指的是容器的端口号

master_user:用于数据同步的用户

master_password:用于同步的用户的密码

master_log_file:指定 Slave 从哪个日志文件开始复制数据,即上文中提到的 File 字段的值

master_log_pos:从哪个 Position 开始读,即上文中提到的 Position 字段的值

master_connect_retry:如果连接失败,重试的时间间隔,单位是秒,默认是60秒

在Slave 中的mysql终端执行show slave status \G;用于查看主从同步状态。

mark

正常情况下,SlaveIORunning 和 SlaveSQLRunning 都是No,因为我们还没有开启主从复制过程。使用start slave开启主从复制过程,然后再次查询主从同步状态show slave status \G;

mark

SlaveIORunning 和 SlaveSQLRunning 都是Yes,说明主从复制已经开启。此时可以测试数据同步是否成功。

主从复制排错:

mark

Use start slaveopen master-slave replication process, if SlaveIORunning been the Connecting, then the master-slave replication has been connected, this situation is generally caused by several reasons below, we can be excluded in accordance with Last_IO_Error tips.

  1. network issue

    Check ip, port

  2. Wrong password

    Check if created for synchronizing users and user password is correct

  3. not pos

    Check the Master of Position

Test master-slave replication

Test master-slave replication will be very much, the simplest is to create a database Master, Slave and then check whether this database.

Master:

mark

Slave:

mark

 

Guess you like

Origin www.cnblogs.com/liujunyue/p/11373587.html