Modify configuration mode in container
1. Assume that the reader docker has been installed on CentOS 6.5 (or other operating systems)
Detected version:
docker -v
Docker version 1.7.1, build 786b29d/1.7.1
2. Pull out the mirror
docker pull hub.c.163.com/library/mysql:5.7 # Or elsewhere
docker images #See if there is a list of mirrors
3. Start a mysql container
Create a new directory under the host path to share data with the docker container:
mkdir –p /home/mysql/master-data
Start the container:
docker run \
--name=mysql-master \
-v /home/mysql/master-data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD='123456' \
-d hub.c.163.com/library/mysql:5.7
- name = specify container name
- v Host Path: In-container Path
- e environment variable
- d detach mode
Note: Host path must exist in advance
4. Configure master
How do I get into the container that just started?
docker exec -it mysql-master bash
So you go into a container filled with mysql and you can execute the command to try:
mysql –u root –p Input password show databases; use mysql; show tables; exit;
Now let's modify the configuration of mysql as master
cd /etc/mysql/conf.d
Modify or add master. CNF to this directory (using vi command)
Since the official image of mysql is not installed with vi, we need to install it first:
apt-get update && apt-get install vim
After installation, execute: vi master.cnf
The input is as follows:
[mysqldump]
user=root
password='123456'
[mysqld]
max_allowed_packet=8M
lower_case_table_names=1
character_set_server=utf8
max_connections=900
max_connect_errors=600
server-id=1
log-bin=mysql-bin
slow_query_log=1
long_query_time=1
log_error
5. Restart master and authorize slave
Go back to the host and execute:
docker restart mysql-master
Enter the container again and login mysql.
(
mysql –u root –p Input password
)
Execute the following statement:
Authorize slave to replicate data: GRANT REPLICATION SLAVE ON *.* TO 'rep1'@'%' IDENTIFIED BY '123456';
Grant copying privileges on any object to a rep1 account from any ip using a password of 123456
View and record the main mysql information:
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
Note the values of file and position.
6. Go back to the host and start another container
In step 3:
Create a new directory under the host path to share data with the docker container:
mkdir –p /home/mysql/slave1-data
Start the container:
docker run \
--name=mysql-slave1\
-v /home/mysql/ slave1-data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD='123456' \
-d hub.c.163.com/library/mysql:5.7
Note: -- link mysql-master:master links to main mysql. The container address of the main MySQL can be represented by the master.
7. Configure slave, as step 4, into mysql-slave 1
cd /etc/mysql/conf.d
vi slave1.cnf enters the following:
[mysqldump]
user=root
password='123456'
[mysqld]
max_allowed_packet=8M
lower_case_table_names=1
character_set_server=utf8
max_connections=900
max_connect_errors=600
server-id=2
log-bin=mysql-bin
slow_query_log=1
long_query_time=1
log_error
Then back to the host and restart:
docker restart mysql-slave1
At this point, MySQL's master and slave servers are running, requiring the reader to ensure that the data on both sides is the same. Only when both servers have the same data can a master-slave replication connection be established.
8. Configure the slave library to connect the master library, enter the slave container again, log in mysql, and execute on the slave library.
mysql> change master to
master_host='master',
master_port=3306,
master_user='rep1',
master_password='123456',
master_log_file='mysql-bin.000003', master_log_pos=437;
Last two items
MASTER_LOG_FILE and MASTER_LOG_POS are the values we just recorded on master(show master status).
Then start the slave thread synchronization from the library
mysql>START SLAVE;
Viewing Synchronization Status from Library
mysql>show slave status;
If you see that the Slave_Io_State field has:
Waiting for master to send event ...
That's a success!!!
9, validation
As long as the data is inserted or modified in the primary server database, it can be automatically copied to the slave server.
Use of data volumes
In steps 3 and 6, we all go to the container to edit the configuration file. In fact, we can also write the configuration file on the host computer and share it with the container, so that we don't need to write the configuration file in the container or cut it as follows:
Host:
mkdir –p /root/mysql-master/cnfdir
vi /root/mysql-master/cnfdir/master.cnf
Type the following:
[mysqldump]
user=root
password='123456'
[mysqld]
max_allowed_packet=8M
lower_case_table_names=1
character_set_server=utf8
max_connections=900
max_connect_errors=600
server-id=1
log-bin=mysql-bin
slow_query_log=1
long_query_time=1
log_error
The configuration file is then passed in as a shared data volume:
Start the MySQL container and mount the shared data volume of the host to the container
docker run \
--name mysql-master2 \
-e MYSQL_ROOT_PASSWORD='123456' \
-v /root/mysql-master/datadir/:/var/lib/mysql/ \
-v /root/mysql-master/cnfdir/:/etc/mysql/conf.d/ \
-d 573ca163b053
Note - v/root/mysql-master/cnfdir/: /etc/mysql/conf.d/
Slave is similar:
mkdir –p /root/mysql-slave1/cnfdir
vi /root/mysql-slave1/cnfdir/slave1.cnf
Type the following:
[mysqldump]
user=root
password='123456'
[mysqld]
max_allowed_packet=8M
lower_case_table_names=1
character_set_server=utf8
max_connections=900
max_connect_errors=600
server-id=2
log-bin=mysql-bin
slow_query_log=1
long_query_time=1
log_error
Then start slave1:
docker run \
--name mysql-slave1 \
-e MYSQL_ROOT_PASSWORD='123456' \
-v /root/mysql-slave1/datadir/:/var/lib/mysql/ \
-v /root/mysql-slave1/cnfdir/:/etc/mysql/conf.d/ \
—link mysql-master2:master \
-d 573ca163b053
Both libraries are started, enter the main library to authorize, view the log file name and location; enter the slave library to connect to the main library and open synchronization.