Docker-based MySQL master-slave replication

1. Environment

Build on linux environment

2. Version

docker

mysql:

wget https://dev.mysql.com/get/downloads/mysql-5.7.22-1.el7.i686.rpm-bundle.tar

3, install mysql

cd /usr/local installation path

wget https://dev.mysql.com/get/downloads/mysql-5.7.22-1.el7.i686.rpm-bundle.tar

tar -zxvf  mysql-5.7.22-1.el7.i686.rpm-bundle.tar

4. Install docker

The installation of docker uses:

1. Install docker

   # yum install -y docker

2. Start docker

   # systemctl start docker

3. Install the docker image

   # docker pull [docker image address]

   Use Alibaba Cloud Mirror

   Modify /etc/docker/daemon.json

   {

    "registry-mirrors": ["https://uulfe4bd.mirror.aliyuncs.com"]

}

 4. Restart docker after modification

   https://dev.aliyun.com/search.html to query image files.

   # systemctl restart docker

   5. Get the docker image

MySQL installation needs to be built on the basis of centos, and docker needs to be installed in docker

   # docker pull https://registry.cn-hangzhou.aliyuncs.com/moensun/centos7

View the created centos7 image docker images record ID 3db94df3e006

   6. Create a docker container

   # docker run -tid 3db94df3e006 (you can see it when looking at the centos7 image) /usr/sbin/init

Rename container name

docker rename priceless_poitras mysql-master1

Re-view docker ps -a The container name modification is complete 

Since it is master-slave replication, two containers mysql-master1 and mysql-slave2 should be created. Here, the mysql-slave2 container is created

(Because mysql-slave1 was created before, this time mysql-slave2 was created, and the name can be named by yourself. This time I just want to write a blog, which is convenient for future learning and for students who want to learn, one master mysql-master1, one From mysql-slave2, if you build it later, you can build the master master according to your own needs, with multiple masters and multiple slaves)

7, MySQL file copy

Docker file copy (container <=> docker)

# docker cp original path docker container path (system = "docker)

# docker cp MySQL-server-5.6.38-1.el7.x86_64.rpm mysql_master2:/root/

# docker cp docker container path original path (docker=>system)

将之前mysql解压后的文件分别拷贝到mysql-master1和mysql-slave2
 docker cp mysql-community-common-5.7.22-1.el7.x86_64.rpm mysql-master1:/root/
docker cp mysql-community-client-5.7.22-1.el7.x86_64.rpm mysql-master1:/root/
docker cp mysql-community-libs-5.7.22-1.el7.x86_64.rpm mysql-master1:/root/
docker cp mysql-community-server-5.7.22-1.el7.x86_64.rpm mysql-master1:/root/
docker cp mysql-community-server-5.7.22-1.el7.x86_64.rpm mysql-slave2:/root/
docker cp mysql-community-client-5.7.22-1.el7.x86_64.rpm mysql-slave2:/root/
docker cp mysql-community-libs-5.7.22-1.el7.x86_64.rpm mysql-slave2:/root/
docker cp mysql-community-common-5.7.22-1.el7.x86_64.rpm mysql-slave2:/root/

8. Enter the container

# docker exec -it container name/bin/bash

# docker exec -it mysql_master1 /bin/bash

Open two remote login windows, enter the main container and the slave container respectively

主,docker exec -it mysql_master1 /bin/bash

从,docker exec -it mysql_slave2/bin/bash

9, MySQL master-slave replication

cd to the root directory

ll Check the rpm of the copied mysql

Install mysql, install mysql, you need to install mysql dependencies

(1) Install the centos system to install mysql dependencies:

1.     perl perl-Module-Build

2.     autoconf

3.     libaio

4.     namuctl-libs

5.     net-tools

 yum install -y perl perl-Module-Build net-tools autoconf libaio numactl-libs

install mysql

(2) in the root directory

rpm -ivh mysql-community-*.rpm

start mysql

systemctl  start mysqld

I usually report an error to see the log is that the disk does not have the allocated size

Need to add innodb_buffer_pool_size = 8M to vi /etc/my.cnf

Best if there is no error

 

After the first startup, there will be an initialization process, which will generate a random password for the root account.

cat /var/log/mysqld.log  | grep password

connect to mysql 

mysql -uroot -pkzl \ (kVo-w0lg

where (needs escaping

set reset password

Note: If you only want to set a simple password, you need to modify two global parameters:

mysql> set global validate_password_length=1;set global validate_password_policy=0;

set password = password('your password')

set password = password(‘123456’)

Remote login authorization

mysql> grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;

mysql> flush privileges;

 

master-slave replication process

(operating on the host):

vi /etc/my.cnf

Configuration file my.cnf

[client]

port                        = 3306

socket                      = /var/lib/mysql/mysql.sock

[mysqld]

# Whether the host or the slave server-id cannot be the same

server-id                    = 2

port                        = 3306

##If there is one, it can be removed

datadir = / var / lib / mysql

socket                       = /var/lib/mysql/mysql.sock

default-storage-engine          = InnoDB

log-bin                      = mysql-bin

log-bin-index                 = mysql-bin.index

relay-log                    = mysql-relay

relay-log-index               = mysql-relay.index

expire-logs-days              = 10

max-binlog-size              = 100M

max_binlog_cache_size        = 8M

log-slave-updates             = 1

binlog_cache_size            = 4M

# use MIXED binlog

binlog_format               = MIXED

#binlog_format              = ROW

#replicate-do-db             = db%.%

#replicate-ignore-db          = mysql.%

# ignore tables

replicate-wild-ignore-table     = mysql.%

sync_binlog                 = 1

relay_log_recovery           = 1

log_slave_updates           = 1

skip-name-resolve

sql_mode=STRICT_TRANS_TABLES

[mysqldump]

quick

max_allowed_packet = 32M

1. Create a synchronous replication user

    mysql> create user 'repl'@'172.17.0.%' identified by '123456';

2. Empower synchronous replication users

    mysql> grant replication slave on *.* to 'repl'@'172.17.0.%' identified by '123456';

mysql> flush privileges;

3. Enable binlog

    Note a few pits when configuring:

    The pit of Replication-do-db, if there are multiple libraries, use multi-line Replication-do-db for configuration

The pit of Replication-ignore-db, if multiple libraries are ignored, use multi-line Replication-ignore-db for configuration

4. restart mysql

#systemctl restart mysqld

5. connect to mysql

show master status \G;

 

Slave operation:

vi /etc/my.cnf

Configuration file my.cnf

[client]

port                        = 3306

socket                      = /var/lib/mysql/mysql.sock

[mysqld]

# Whether the host or the slave server-id cannot be the same

server-id                    = 2

port                        = 3306

##If there is one, it can be removed

datadir = / var / lib / mysql

socket                       = /var/lib/mysql/mysql.sock

default-storage-engine          = InnoDB

log-bin                      = mysql-bin

log-bin-index                 = mysql-bin.index

relay-log                    = mysql-relay

relay-log-index               = mysql-relay.index

expire-logs-days              = 10

max-binlog-size              = 100M

max_binlog_cache_size        = 8M

log-slave-updates             = 1

binlog_cache_size            = 4M

# use MIXED binlog

binlog_format               = MIXED

#binlog_format              = ROW

#replicate-do-db             = db%.%

#replicate-ignore-db          = mysql.%

# ignore tables

replicate-wild-ignore-table     = mysql.%

sync_binlog                 = 1

relay_log_recovery           = 1

log_slave_updates           = 1

skip-name-resolve

sql_mode=STRICT_TRANS_TABLES

[mysqldump]

quick

max_allowed_packet = 32M

Connect mysql operation:

1. Stop slave;

The most critical statement of master-slave replication:

2. Change master to

         Master_host=’172.17.0.2’,

         Master_user=’repl’,

         Master_password=’123456’,

         Master_log_file=’mysql-bin.000001’,

         Master_log_pos=120;

3. Start slave;

4.show slave status \G;

test

 主mysql:create database db1;

 

从 mysql> show databases;

created db1 from mysql

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324934078&siteId=291194637