Docker-based replication from MySQL to achieve the main

MySQL

Foreword

MySQLReplication 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 different MySQLservers, the database can effectively reduce pressure. When faced with MySQLa single point of failure, failover can be achieved in a short time. In this paper, MySQLelaborate built-in replication capabilities.

version

  • MySQl: 5.7.17
  • CentOS :7.4.1708
  • Docker: 1.13.1

Outline

MySQLCopy the data flow:

  1. 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
  2. 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.
  3. After receiving from a library of I / O threads to the new event log, save to their relay log (relay logs) in
  4. Read relay log of events from the SQL thread library and perform updates save.
    Master-slave replication processes

Configure the master from the library

Main Library my.cnfConfiguration

In the main library my.cnfopen 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.cnfConfiguration

log-bin = mysql-bin
server-id = 2
log-slave-updates = 1
read-only = 1

Also open from the library log-bin, log-slave-updatesset 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 masterand slavefile their preservation is not shared, first create a folder /usr/local/mysqland then create the directory masterand slavetwo directories, and then create their own datafolders
File Directory

  • 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 masterand slaveare 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_copydatabases and persontables.

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 masterand slaveimage and running the container

Construction of masterMirror

docker build -t master/mysql .

Construction of slaveMirror

docker build -t slave/mysql .

Building a successful return Successfuly, or by docker imagesviewing the mirror command
Dockerfile successfully constructed

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 masterport 3306, slaveport 3307, mount the data directory for the saved data.

After connecting to the database to verify the success of database initialization
Database initialization tables

Check log-binwhether to open
log-bin is turned on

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
Copy the startup information

Mark above output information Slave_IO_Running: Yesand Slave_SQL_Running: Yesyou 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.
Query data from library

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 .
MySQLWhile 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
My public number

Guess you like

Origin www.cnblogs.com/ytao-blog/p/11780468.html