Docker is used to build primary/secondary replication of MySQL.
First, we need to install Docker in the VIRTUAL machine
I am using Centos 7 to operate, please check whether it has been installed before installation. Docker (docker, docker, docker) yum list installed | grep docker
Now we are ready to install Docker, run the command
sudo yum install docker
Start the Docker container
sudo service docker start
Copy the code
MySQL > install sudo docker search MySQL > install MySQL5.7
Docker pull mysql: 5.7
docker run --name mysql3307 -p 3307:3306 --privileged=true -e MYSQL_ROOT_PASSWORD=root -eMYSQL_DATABASE=ybx -e MYSQL_USER=user -eMYSQL_PASSWORD=pass -v /home/mysql/docker-data/3307/conf:/etc/mysql/conf.d -v /home/mysql/docker-data/3307/data/:/var/lib/mysql -v /home/mysql/docker-data/3307/logs/:/var/log/mysql -dMysql: 5.7Copy the code
-v represents the location where the docker MySQL file is mounted to the host. , the folder is automatically created if the host does not have it.
docker run --name mysql3308 -p 3308:3306 --privileged=true -e MYSQL_ROOT_PASSWORD=root -eMYSQL_DATABASE=ybx -e MYSQL_USER=user -eMYSQL_PASSWORD=pass -v /home/mysql/docker-data/3308/conf:/etc/mysql/conf.d -v /home/mysql/docker-data/3308/data/:/var/lib/mysql -v /home/mysql/docker-data/3308/logs/:/var/log/mysql -dMysql: 5.7Copy the code
Start two MySQL so there are two lines of code. Pass after the run is complete
docker ps
Copy the code
Check that they have been started successfully
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
#datadir=/home/mysql/docker-data/3307/data
#socket=/home/mysql/docker-data/3307/mysql.sock
character_set_server=utf8
init_connect='SET NAMES utf8'
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
#log-error=/home/mysql/docker-data/3307/logs/mysqld.log
#pid-file=/home/mysql/docker-data/3307/mysqld.pid
The id of the slave machine needs to be changed
server-id=1283307
# Enable replication
log-bin=mysql-bin
If there are two hosts in the system, the id of the host is 2. If there are three hosts, the id of the host is 3
auto_increment_increment=2
The machine can be changed from 1 to 2
auto_increment_offset=1
# ignore case
lower_case_table_names=1
# MSTest database to synchronize, multiple databases to synchronize now only one YBX
binlog-do-db=ybx
# Database to ignore
binlog-ignore-db=mysql
#rpl_semi_sync_master_enabled=1
#rpl_semi_sync_master_timeout=10000
Copy the code
After the configuration is complete, I need to restart my mysql3307 and mysQL3308 containers and create a user on the master host to add slave and replication permissions
# connect to 3307mysql configuration permission, my host is 3307
Mysql > select * from 'mysql'; mysql > select * from 'mysql'; mysql > select * from 'mysql'
docker exec -it mysql3307 bash
# connect MySQL
mysql -uroot -proot
# set permissions
GRANT REPLICATION SLAVE,FILE,REPLICATION CLIENT ON *.* TO 'repluser'@The '%' IDENTIFIED BY '123456';
# refresh permission
FLUSH PRIVILEGES;
Copy the code
SHOW MASTER STATUS
Then set salav to your IP address, port number, account number, password, and offset. Offset is the Postition value of the master host.
change master to master_host='192.168.1.128',master_port=3307,master_user='repluser',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=629;
Start slave, start SQL and IO threads
start slave;
Check the status of the slave
show slave status\G
Copy the code
After starting the SQL and IO threads of slave, the figure shows yes
SHOW PROCESSLIST;
The host runs SHOW PROCESSLIST; In the following figure, you can see that the host has sent binlog logs to the slave machine to wait for more update operations
Now that the master/slave database is set up, we can test the effect. Now my host and slave yBX database have no table information, we will create a user table
CREATE TABLE `user` (
`id` int NOT NULL,
`name` varchar(255) NULL,
PRIMARY KEY (`id`)
);
Copy the code