Foreword
MySQL
Replication is applied from the high-performance, highly available main base. For a read operation more database-intensive applications, database requests by load balancing allocated to differentMySQL
servers, the database can effectively reduce pressure. When faced withMySQL
a single point of failure, failover can be achieved in a short time. In this paper,MySQL
elaborate built-in replication capabilities.
version
- MySQl:
5.7.17
- CentOS :
7.4.1708
- Docker:
1.13.1
Outline
MySQL
Copy the data flow:
- Before committing the transaction in the main library data updates, asynchronous events recorded in the binlog binary log file storage engine submission after this transaction logging is completed
- Starting from a library I / O connection with the primary thread creating libraries binlog for requesting to update the master library. Then binlog dump thread to create the main library, which is a binary dump thread, if there is a new event updates, you notice I / O thread; thread dump when the binary log is complete, no new log when the thread enters sleep status.
- After receiving from a library of I / O threads to the new event log, save to their relay log (relay logs) in
- Read relay log of events from the SQL thread library and perform updates save.
Configure the master from the library
Main Library my.cnf
Configuration
In the main library my.cnf
open binary log, and set the service Id.
log-bin = mysql-bin
server-id = 1
Noteserver-id
must be a unique number, you must master and slave is inconsistent, and the main items from the library must be set.
From the Library my.cnf
Configuration
log-bin = mysql-bin
server-id = 2
log-slave-updates = 1
read-only = 1
Also open from the library log-bin
, log-slave-updates
set to replay relay log from the library, to record their own binary log, allowing the main library from the library as other servers, will be forwarded to other binary log from the library, doing a master multi-slave the kinds of programs can be considered when the program.
Dockerfile build MySQL mirror
Construction of the required documents
Here master
and slave
file their preservation is not shared, first create a folder /usr/local/mysql
and then create the directory master
and slave
two directories, and then create their own data
folders
- data directory used to store data files directory
- Dockerfile save Dockerfile content
- SQL init.sql initialize the database
- my.cnf database configuration files, configuration mentioned above
- start.sh Dockerfile build script when MySQL
Dockerfile content
# 利用 mysql 镜像创建新的镜像
FROM mysql:5.7.17
ENV MYSQL_ROOT_PASSWORD ytao
COPY start.sh /mysql/start.sh
COPY my.cnf /etc/mysql/my.cnf
COPY init.sql /mysql/init.sql
EXPOSE 3306
CMD ["sh", "/mysql/start.sh"]
Here master
and slave
are based on the same image building, and other storage engine uses the best components in the same, or an exception may occur during replication.
init.sql initialization data
-- 创建 data_copy 数据库
DROP DATABASE IF EXISTS `data_copy`;
CREATE DATABASE `data_copy` /*!40100 DEFAULT CHARACTER SET utf8mb4 collate utf8mb4_general_ci */;
-- 创建 person 表
USE `data_copy`;
DROP TABLE IF EXISTS `person`;
CREATE TABLE `person` (
`id` int(32) NOT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Creating data_copy
databases and person
tables.
start.sh script
#!/bin/sh
echo '启动mysql'
service mysql start
sleep 5
echo '初始化数据库'
mysql -uroot -pytao < /mysql/init.sql
echo '初始化完成!'
tail -f /dev/null
Construction master
and slave
image and running the container
Construction of master
Mirror
docker build -t master/mysql .
Construction of slave
Mirror
docker build -t slave/mysql .
Building a successful return Successfuly
, or by docker images
viewing the mirror command
Mirroring just built to run container
# master 容器
docker run --name master -p 3306:3306 -v /usr/local/mysql/master/data/:/var/lib/mysql -d master/mysql
# slave 容器
docker run --name slave -p 3307:3306 -v /usr/local/mysql/slave/data/:/var/lib/mysql -d slave/mysql
Designated master
port 3306
, slave
port 3307
, mount the data directory for the saved data.
After connecting to the database to verify the success of database initialization
Check log-bin
whether to open
Create a replication account
I mentioned earlier O threads to be built from the library I / connected with the main library, so you need to use to verify the account. Account not only have connection rights (REPLICATION CLIENT), but also have permission to copy (REPLICATION SLAVE).
GRANT REPLICATION CLIENT, REPLICATION SLAVE ON *.* TO muser@'%' IDENTIFIED BY 'ytao';
Access address set here is open, the actual use of the process must be safe access to the address specified.
Start copy from the library
Connected to the main library from the library, after obtaining binary log replay. Here you must first configure the account created above connect, use the command set accordingly.
CHANGE MASTER TO
MASTER_HOST = '47.107.xx.xxx',
MASTER_PORT = 3306,
MASTER_USER = 'muser',
MASTER_PASSWORD = 'ytao',
MASTER_LOG_FILE = 'mysql-bin.000006';
Here copy has not started, you need to start again from the library
START SLAVE;
Use SHOW SLAVE STATUS\G;
the situation after the start command to view
Mark above output information Slave_IO_Running: Yes
and Slave_SQL_Running: Yes
you can see I / O threads and SQL thread has been started running.
Test data synchronization
If you add in the main library, update, or delete data, it should also change the data corresponding to the main library from the library.
Adding data to a master database
INSERT INTO `data_copy`.`person` (`id`, `name`) VALUES ('1', 'ytao');
Query data from the database, the data is synchronized over.
to sum up
The above is the simplest most basic configuration, it is understood that the above configuration, you can customize different options according to their own circumstances, to achieve a master multi-slave, master-master replication (active - active or active - passive mode) and so on to meet their own needs .
MySQL
While copying easy to use, but it is also accompanied by a number of issues we need to resolve in use, such as: can not be restored from the server to stop, data synchronization delays, etc., but fortunately, most of the problems we encounter in the industry It has been associated solutions. And interested in this area can go to find solutions to these problems now middleware implementations.
Personal blog: https://ytao.top
my public number ytao