MySQL之间数据复制的基础是二进制日志文件(binary log file)。一台MySQL数据库一旦启用二进制日志后,其作为master,它的数据库中所有操作都会以“事件”的方式记录在二进制日志中,其他数据库作为slave通过一个I/O线程与主服务器保持通信,并监控master的二进制日志文件的变化,如果发现master二进制日志文件发生变化,则会把变化复制到自己的中继日志中,然后slave的一个SQL线程会把相关的“事件”执行到自己的数据库中,以此实现从数据库和主数据库的一致性,也就实现了主从复制。

在这里插入图片描述
实现MySQL主从复制需要进行的配置:

  • 主服务器:
    • 开启二进制日志
    • 配置唯一的server-id
    • 获得master二进制日志文件名及位置
    • 创建一个用于slave和master通信的用户账号
  • 从服务器:
    • 配置唯一的server-id
    • 使用master分配的用户账号读取master二进制日志
    • 启用slave服务

一、封装Docker镜像

1、创建MySQL Dockerfile

操作系统镜像采用 ubuntu:trusty
MySQL采用最新的8.0版本, https://dev.mysql.com/doc/refman/8.0/en/ ,端口采用3306

FROM ubuntu:trusty

# Add MySQL configuration
COPY my.cnf /etc/mysql/conf.d/my.cnf
COPY mysqld_charset.cnf /etc/mysql/conf.d/mysqld_charset.cnf
COPY mysql-apt-config_0.8.12-1_all.deb /mysql-apt-config_0.8.12-1_all.deb

RUN apt-get update && apt-get install -y wget && apt-get install -y vim
RUN dpkg -i /mysql-apt-config_0.8.12-1_all.deb && \
    apt-get -yq install mysql-server pwgen && \
    rm -rf /var/lib/apt/lists/* && \
    rm /etc/mysql/conf.d/mysqld_safe_syslog.cnf && \
    if [ ! -f /usr/share/mysql/my-default.cnf ] ; then cp /etc/mysql/my.cnf /usr/share/mysql/my-default.cnf; fi && \
    mysql_install_db > /dev/null 2>&1 && \
    touch /var/lib/mysql/.EMPTY_DB

# Add MySQL scripts
COPY import_sql.sh /import_sql.sh
COPY run.sh /run.sh

ENV MYSQL_USER=admin \
    MYSQL_PASS=**Random** \
    ON_CREATE_DB=**False** \
    REPLICATION_MASTER=**False** \
    REPLICATION_SLAVE=**False** \
    REPLICATION_USER=replica \
    REPLICATION_PASS=replica

# Add VOLUMEs to allow backup of config and databases
VOLUME  ["/etc/mysql", "/var/lib/mysql"]

EXPOSE 3306
CMD ["/run.sh"]

2、创建MySQL配置文件

my.cnf

[mysqld]
bind-address=0.0.0.0
# http://www.percona.com/blog/2008/05/31/dns-achilles-heel-mysql-installation/
skip_name_resolve
#server-id
#log-bin

mysqld_charset.cnf

[mysqld]
character_set_server=utf8
character_set_filesystem=utf8
collation-server=utf8_general_ci
init-connect='SET NAMES utf8'
init_connect='SET collation_connection = utf8_general_ci'
skip-character-set-client-handshake

3、创建MySQL脚本文件

import_sql.sh

#!/bin/bash

if [[ $# -ne 3 ]]; then
    echo "Usage: $0 <username> <password> </path/to/sql_file.sql>"
    exit 1
fi

echo "=> Starting MySQL Server"
/usr/bin/mysqld_safe > /dev/null 2>&1 &
PID=$!

RET=1
while [[ $RET -ne 0 ]]; do
    echo "=> Waiting for confirmation of MySQL service startup"
    sleep 5
    mysql -u"$1" -p"$2" -e "status" > /dev/null 2>&1
    RET=$?
done

echo "   Started with PID ${PID}"

echo "=> Importing SQL file"
mysql -u"$1" -p"$2" < "$3"

echo "=> Stopping MySQL Server"
mysqladmin -u"$1" -p"$2" shutdown

echo "=> Done!"

run.sh

#!/bin/bash

set -m
set -e

VOLUME_HOME="/var/lib/mysql"
CONF_FILE="/etc/mysql/conf.d/my.cnf"
LOG="/var/log/mysql/error.log"

# Set permission of config file
chmod 644 ${CONF_FILE}
chmod 644 /etc/mysql/conf.d/mysqld_charset.cnf

StartMySQL ()
{
    /usr/bin/mysqld_safe ${EXTRA_OPTS} > /dev/null 2>&1 &
    # Time out in 1 minute
    LOOP_LIMIT=60
    for (( i=0 ; ; i++ )); do
        if [ ${i} -eq ${LOOP_LIMIT} ]; then
            echo "Time out. Error log is shown as below:"
            tail -n 100 ${LOG}
            exit 1
        fi
        echo "=> Waiting for confirmation of MySQL service startup, trying ${i}/${LOOP_LIMIT} ..."
        sleep 1
        mysql -uroot -e "status" > /dev/null 2>&1 && break
    done
}

CreateMySQLUser()
{
    if [ "$MYSQL_PASS" = "**Random**" ]; then
        unset MYSQL_PASS
    fi

    PASS=${MYSQL_PASS:-$(pwgen -s 12 1)}
    _word=$( [ ${MYSQL_PASS} ] && echo "preset" || echo "random" )
    echo "=> Creating MySQL user ${MYSQL_USER} with ${_word} password"

    mysql -uroot -e "CREATE USER '${MYSQL_USER}'@'%' IDENTIFIED BY '$PASS'"
    mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO '${MYSQL_USER}'@'%' WITH GRANT OPTION"
    echo "=> Done!"
    echo "========================================================================"
    echo "You can now connect to this MySQL Server using:"
    echo ""
    echo "    mysql -u$MYSQL_USER -p$PASS -h<host> -P<port>"
    echo ""
    echo "Please remember to change the above password as soon as possible!"
    echo "MySQL user 'root' has no password but only allows local connections"
    echo "========================================================================"
}

OnCreateDB()
{
    if [ "$ON_CREATE_DB" = "**False**" ]; then
        unset ON_CREATE_DB
    else
        echo "Creating MySQL database ${ON_CREATE_DB}"
        mysql -uroot -e "CREATE DATABASE IF NOT EXISTS ${ON_CREATE_DB};"
        echo "Database created!"
    fi
}

ImportSql()
{
    for FILE in ${STARTUP_SQL}; do
        echo "=> Importing SQL file ${FILE}"
        if [ "$ON_CREATE_DB" ]; then
            mysql -uroot "$ON_CREATE_DB" < "${FILE}"
        else
            mysql -uroot < "${FILE}"
        fi
    done
}

# Main
if [ ${REPLICATION_MASTER} == "**False**" ]; then
    unset REPLICATION_MASTER
fi

if [ ${REPLICATION_SLAVE} == "**False**" ]; then
    unset REPLICATION_SLAVE
fi

# Initialize empty data volume and create MySQL user
if [[ ! -d $VOLUME_HOME/mysql ]]; then
    echo "=> An empty or uninitialized MySQL volume is detected in $VOLUME_HOME"
    echo "=> Installing MySQL ..."
    if [ ! -f /usr/share/mysql/my-default.cnf ] ; then
        cp /etc/mysql/my.cnf /usr/share/mysql/my-default.cnf
    fi
    mysql_install_db || exit 1
    touch /var/lib/mysql/.EMPTY_DB
    echo "=> Done!"
else
    echo "=> Using an existing volume of MySQL"
fi

# Set MySQL REPLICATION - MASTER
if [ -n "${REPLICATION_MASTER}" ]; then
    echo "=> Configuring MySQL replication as master (1/2) ..."
    if [ ! -f /replication_set.1 ]; then
        RAND="$(date +%s | rev | cut -c 1-2)$(echo ${RANDOM})"
        echo "=> Writting configuration file '${CONF_FILE}' with server-id=${RAND}"
        sed -i "s/^#server-id.*/server-id = ${RAND}/" ${CONF_FILE}
        sed -i "s/^#log-bin.*/log-bin = mysql-bin/" ${CONF_FILE}
        touch /replication_set.1
    else
        echo "=> MySQL replication master already configured, skip"
    fi
fi

# Set MySQL REPLICATION - SLAVE
if [ -n "${REPLICATION_SLAVE}" ]; then
    echo "=> Configuring MySQL replication as slave (1/2) ..."
    if [ -n "${MYSQL_PORT_3306_TCP_ADDR}" ] && [ -n "${MYSQL_PORT_3306_TCP_PORT}" ]; then
        if [ ! -f /replication_set.1 ]; then
            RAND="$(date +%s | rev | cut -c 1-2)$(echo ${RANDOM})"
            echo "=> Writting configuration file '${CONF_FILE}' with server-id=${RAND}"
            sed -i "s/^#server-id.*/server-id = ${RAND}/" ${CONF_FILE}
            sed -i "s/^#log-bin.*/log-bin = mysql-bin/" ${CONF_FILE}
            touch /replication_set.1
        else
            echo "=> MySQL replication slave already configured, skip"
        fi
    else
        echo "=> Cannot configure slave, please link it to another MySQL container with alias as 'mysql'"
        exit 1
    fi
fi


echo "=> Starting MySQL ..."
StartMySQL
tail -F $LOG &

# Create admin user and pre create database
if [ -f /var/lib/mysql/.EMPTY_DB ]; then
    echo "=> Creating admin user ..."
    CreateMySQLUser
    OnCreateDB
    rm /var/lib/mysql/.EMPTY_DB
fi


# Import Startup SQL
if [ -n "${STARTUP_SQL}" ]; then
    if [ ! -f /sql_imported ]; then
        echo "=> Initializing DB with ${STARTUP_SQL}"
        ImportSql
        touch /sql_imported
    fi
fi

# Set MySQL REPLICATION - MASTER
if [ -n "${REPLICATION_MASTER}" ]; then
    echo "=> Configuring MySQL replication as master (2/2) ..."
    if [ ! -f /replication_set.2 ]; then
        echo "=> Creating a log user ${REPLICATION_USER}:${REPLICATION_PASS}"
        mysql -uroot -e "CREATE USER '${REPLICATION_USER}'@'%' IDENTIFIED BY '${REPLICATION_PASS}'"
        mysql -uroot -e "GRANT REPLICATION SLAVE ON *.* TO '${REPLICATION_USER}'@'%'"
        mysql -uroot -e "reset master"
        echo "=> Done!"
        touch /replication_set.2
    else
        echo "=> MySQL replication master already configured, skip"
    fi
fi

# Set MySQL REPLICATION - SLAVE
if [ -n "${REPLICATION_SLAVE}" ]; then
    echo "=> Configuring MySQL replication as slave (2/2) ..."
    if [ -n "${MYSQL_PORT_3306_TCP_ADDR}" ] && [ -n "${MYSQL_PORT_3306_TCP_PORT}" ]; then
        if [ ! -f /replication_set.2 ]; then
            echo "=> Setting master connection info on slave"
            mysql -uroot -e "CHANGE MASTER TO MASTER_HOST='${MYSQL_PORT_3306_TCP_ADDR}',MASTER_USER='${MYSQL_ENV_REPLICATION_USER}',MASTER_PASSWORD='${MYSQL_ENV_REPLICATION_PASS}',MASTER_PORT=${MYSQL_PORT_3306_TCP_PORT}, MASTER_CONNECT_RETRY=30"
            mysql -uroot -e "start slave"
            echo "=> Done!"
            touch /replication_set.2
        else
            echo "=> MySQL replication slave already configured, skip"
        fi
    else
        echo "=> Cannot configure slave, please link it to another MySQL container with alias as 'mysql'"
        exit 1
    fi
fi

fg

4、创建MySQL镜像

按照自定义的tag创建MySQL镜像

docker build -t wxsc/mysql:stable .

查看镜像信息
在这里插入图片描述

二、启动Master容器

docker run -d --privileged=true -e REPLICATION_MASTER=true -e REPLICATION_PASS=admin -e MYSQL_PASS=vdi_system -e MYSQL_USER=vdi_system -p 3306:3306 -v /home/workspace/mysql/sock/master:/var/run/mysqld -v /home/workspace/mysql/data/master:/var/lib/mysql --name mysql wxsc/mysql:stable
  • -p,表示暴露的端口,即本机端口3306与容器端口3306映射
  • -v /home/workspace/mysql/sock/master:/var/run/mysqld挂载sock文件(前面为本地文件夹,后面为容器中的文件夹)
  • -v /home/workspace/mysql/data/master:/var/lib/mysql挂载数据
  • -e,表示传入的环境变量,这里修改了mysql的用户名和密码,即MYSQL_USER = vdi_system,MYSQL_PASS = vdi_system

Permission denied错误

错误1: changing ownership of ‘/var/lib/mysql’: Permission denied
chown: changing ownership of '/var/lib/mysql': Permission denied
Cannot change ownership of the database directories to the 'mysql'
user.  Check that you have the necessary permissions and try again.
错误2: Can’t read dir of ‘/etc/mysql/conf.d/’ (Errcode: 13 - Permission denied)
mysqld: Can't read dir of '/etc/mysql/conf.d/' (Errcode: 13 - Permission denied) mysqld: [ERROR] Fatal error in defaults handling. Program aborted!

原因

因为Centos7安全Selinux禁止了一些安全权限,导致mysql和mariadb在进行挂载/var/lib/mysql的时候会提示如下信息

解决方法

在docker run中加入–privileged=true 给容器加上特定权限

错误3: Can’t start server : Bind on unix socket: Permission denied
chown: changing ownership of '/var/lib/mysql': Permission denied
Cannot change ownership of the database directories to the 'mysql'
user.  Check that you have the necessary permissions and try again.

解决方法

找到.sock文件设置的路径,给此文件所在文件夹更改为mysql的用户所有,并且增加所有用户的读写权限

chmod -R 777 /home/workspace/mysql
docker start CONTAINER ID 

三、启动Slave容器

docker run -d --privileged=true -e REPLICATION_SLAVE=true -e MYSQL_PASS=vdi_system -e MYSQL_USER=vdi_system -p 3307:3306 -v /home/workspace/mysql/sock/slave:/var/run/mysqld -v /home/workspace/mysql/data/slave:/var/lib/mysql --name mysql-slave --link mysql:mysql wxsc/mysql:stable
  • -p,表示暴露的端口,即本机端口3307与容器端口3306映射
  • –link,该参数的格式为–link :alias,其中name和id是源容器的name和id,alias是源容器在link下的别名。主从模式alias必须叫mysql,否则会提示 `Cannot configure slave, please link it to another MySQL container with alias as ‘mysql’!

查看容器信息
在这里插入图片描述

四、启动MySQL主从同步

1、登录数据库

登录主数据库

mysql -h 192.168.212.213 -uvdi_system -p

在这里插入图片描述
登录从数据库

mysql -h 192.168.212.213 -P3307 -uvdi_system -p

在这里插入图片描述

2、配置主数据库

查看binlog日志的状态

mysql > show variables like '%log_bin%';

在这里插入图片描述
更改配置文件/etc/mysql/my.cnf

server-id		= 1
log_bin			= /var/lib/mysql/mysql-bin

在这里插入图片描述重启镜像

主数据库加锁,阻止对数据库进行任何的写操作

mysql > flush tables with read lock;

获取二进制日志的信息

mysql > show master status;

在这里插入图片描述

  • File:当前使用的二进制日志的文件名
  • Position:日志里面的位置信息

导出主数据库数据

mysqldump -uvdi_system -p'vdi_system' -P 3306 -S /home/workspace/mysql/sock/master/mysqld.sock --all-databases > /home/workspace/mysql/backup/mysql_bak.$(date +%F).sql

在这里插入图片描述
数据量大可以采用压缩方式导出

mysqldump -uvdi_system -p'vdi_system' -P 3306 -S /home/workspace/mysql/sock/master/mysqld.sock --all-databases | gzip > /home/workspace/mysql/backup/mysql_bak.$(date +%F).sql.gz

在这里插入图片描述
主数据库解锁

mysql > unlock tables;

3、配置从数据库

更改配置文件/etc/mysql/my.cnf

server-id		= 2
log_bin			= /var/lib/mysql/mysql-bin

如果有多个从服务器,那么每个服务器上配置的server-id都必须不一致。从服务器上无需配置log-bin。如果需要在从服务器上进行数据备份和灾难恢复,或者让从服务器变成一个主服务器,也可以配置log-bin选项。

导入主数据库数据

mysql -uvdi_system -p'vdi_system' -P 3307 -S /home/workspace/mysql/sock/slave/mysqld.sock < /home/workspace/mysql/backup/mysql_bak.2019-04-03.sql

配置同步参数(先检查状态)

mysql> CHANGE MASTER TO
-> MASTER_HOST='master_host_name',
-> MASTER_PORT='master_port',
-> MASTER_USER='replication_user_name',
-> MASTER_PASSWORD='replication_password',
-> MASTER_LOG_FILE='recorded_log_file_name',
-> MASTER_LOG_POS='recorded_log_pos',
  • MASTER_HOST:主数据库服务器IP地址
  • MASTER_PORT: 主数据库服务器端口
  • MASTER_USER,MASTER_PASSWORD:主从同步的用户名密码
  • MASTER_LOG_FILE,MASTER_LOG_POS:主数据库服务器状态

启动主从同步进程

mysql > start slave;

检查状态

mysql > show slave status \G

在这里插入图片描述

  • Slave_IO_Running:负责从库去主库读取二进制日志,并写入到从库的中继日志
  • Slave_SQL_Running:负责将中继日志转换成SQL语句后执行
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐