centos 7 部署docker mysql 5.6 容器主从服务

centos 7  部署docker mysql 5.6 容器主从服务

 

 

yum install -y wget docker

yum upgrade

 

给当前目录授权以便挂载到容器

mkdir -p /data/mysql

chcon -Rt svirt_sandbox_file_t /data/mysql

 

在宿主机上安装mysql client 

下载 mysql client 

wget http://cdn.mysql.com//Downloads/MySQL-5.6/MySQL-client-5.6.33-1.rhel5.x86_64.rpm

 

需要安装 perl 

yum install perl

 

rpm -ivh MySQL-client-5.6.33-1.rhel5.x86_64.rpm

 

必要时清理当前实验机器上的所有docker容器

docker stop `docker ps -a -q`

docker rm `docker ps -a -q`

 

拉取mysql5.6镜像

docker pull mysql:5.6

 

编写mysql主从服务的配置文件

/conf/my-m.cnf

# Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

#
# The MySQL Community Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

[client]
port        = 3306
socket      = /var/run/mysqld/mysqld.sock

[mysqld_safe]
pid-file    = /var/run/mysqld/mysqld.pid
socket      = /var/run/mysqld/mysqld.sock
nice        = 0

[mysqld]
user        = mysql
pid-file    = /var/run/mysqld/mysqld.pid
socket      = /var/run/mysqld/mysqld.sock
port        = 3306
basedir     = /usr
datadir     = /var/lib/mysql
tmpdir      = /tmp
lc-messages-dir = /usr/share/mysql
explicit_defaults_for_timestamp

innodb_buffer_pool_size=8M

log-bin = mysql-bin 
server-id = 1 

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address   = 127.0.0.1

#log-error  = /var/log/mysql/error.log

# Recommended in standard MySQL setup
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#
!includedir /etc/mysql/conf.d/

 

innodb_buffer_pool_size=8M  

log-bin = mysql-bin   

server-id = 1 

/conf/my-s.cnf

# Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

#
# The MySQL Community Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

[client]
port        = 3306
socket      = /var/run/mysqld/mysqld.sock

[mysqld_safe]
pid-file    = /var/run/mysqld/mysqld.pid
socket      = /var/run/mysqld/mysqld.sock
nice        = 0

[mysqld]
user        = mysql
pid-file    = /var/run/mysqld/mysqld.pid
socket      = /var/run/mysqld/mysqld.sock
port        = 3306
basedir     = /usr
datadir     = /var/lib/mysql
tmpdir      = /tmp
lc-messages-dir = /usr/share/mysql
explicit_defaults_for_timestamp

innodb_buffer_pool_size=8M

log-bin = mysql-bin 
server-id = 2

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address   = 127.0.0.1

#log-error  = /var/log/mysql/error.log

# Recommended in standard MySQL setup
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#
!includedir /etc/mysql/conf.d/

 

innodb_buffer_pool_size=8M  

log-bin = mysql-bin   

server-id = 2  

 

启动容器

master:

docker run -p 3307:3306 --name mysql-master -v $PWD/conf/my-m.cnf:/etc/mysql/my.cnf -v $PWD/logs-master:/logs -v $PWD/data-master:/mysql_data -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6

 

 

slave:

docker run -p 3308:3306 --name mysql-slave -v $PWD/conf/my-s.cnf:/etc/mysql/my.cnf -v $PWD/logs-slave:/logs -v $PWD/data-slave:/mysql_data -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6

 

 

命令说明:

  ● -p 3306:3306:将容器的3306端口映射到主机的3306端口

  ● -v $PWD/conf/my.cnf:/etc/mysql/my.cnf:将主机当前目录下的conf/my.cnf挂载到容器的/etc/mysql/my.cnf

  ● -v $PWD/logs:/logs:将主机当前目录下的logs目录挂载到容器的/logs

  ● -v $PWD/data:/mysql_data:将主机当前目录下的data目录挂载到容器的/mysql_data

  ● -e MYSQL_ROOT_PASSWORD=123456:初始化root用户的密码

 

查看容器的IP地址

master:

docker inspect  c290eb923d6a

slave:

docker inspect  7ffeb7add7ba

 

从宿主机登录master

 

mysql -uroot -p123456 -h172.17.0.2 -P 3306

登录后执行

GRANT REPLICATION SLAVE ON *.* to 'backup'@'%' identified by '123456';

show master status;

得到结果

mysql-bin.000004 | 312 

 

 

从宿主机登录slave:

mysql -uroot -p123456 -h172.17.0.3 -P 3306

change master to master_host='172.17.0.2',master_user='backup',master_password='123456',

master_log_file='mysql-bin.000004',master_log_pos=312,master_port=3306;

 

 

start slave;

show slave status;

 

如果看到Waiting for master send event.. 什么的就成功了,你现在在主库上的修改,都会同步到从库上

 

 

需要的时候stop slave;

 

参考:

http://blog.csdn.net/qq362228416/article/details/48569293

猜你喜欢

转载自crabdave.iteye.com/blog/2329333