IT干货

使用docker安装mysql主从库

2017-03-03  本文已影响155人  3517902f1986

本文将介绍如何使用docker在centos7中安装mysql的主从库。

准备配置文件

主库配置(/opt/mysql/master/master.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

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/

只是在默认配置文件中加上

log-bin = mysql-bin 
server-id = 1 
Slave配置文件(/opt/mysql/slave/slave.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

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/

只是在默认配置文件中加上

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

启动数据库

Master启动
#sudo docker run -d -e MYSQL_ROOT_PASSWORD=root --name mysql-master -v /opt/mysql/master/master.cnf:/etc/mysql/my.cnf -p 3307:3306 mysql:5.7

启动验证

#sudo docker exec -it mysql-master bash
#mysql -uroot -proot
>show databases;
Slave启动
#sudo docker run -d -e MYSQL_ROOT_PASSWORD=root --name mysql-slave -v /opt/mysql/slave/slave.cnf:/etc/mysql/my.cnf -p 3317:3306 mysql:5.7

启动验证

#sudo docker exec -it mysql-slave bash
#mysql -uroot -proot
>show databases;

配置主从

Master配置

连接进master的docker:

#sudo docker exec -it mysql-master bash
#mysql -uroot -proot
>SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
>GRANT REPLICATION SLAVE ON *.* to 'backup'@'%' identified by '123456';
>start master;

配置成功后查看:

Slave配置

连接进slave的docker:

#sudo docker exec -it mysql-slave bash
#mysql -uroot -proot
>SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
>change master to master_host='192.168.249.128',master_user='backup',master_password='123456',master_log_file='mysql-bin.000004',master_log_pos=861,master_port=3307;
>start slave;

#master_host为docker的地址不能写127.0.0.1
#master_user是在主库创建的用户
#master_log_pos是主库show master status;查询出的Position

查看show slave status。如下为成功状态

如果发现配置不成功。show slave status显示

可以尝试在宿主机开启防火墙:

#sudo iptables -I INPUT -s 0/0 -p tcp --dport 3307 -j ACCEPT
//这条规则的意思是,想要在输入数据INPUT中,protocol为tcp/IP的方式,访问端口3306,都会被允许的

添加一个从库

启动一个mysql从库的docker实例

增加一个配置文件

#mkdir /opt/mysql/slave2
#cp /opt/mysql/slave/slave.cnf /opt/mysql/slave2/slave.cnf
#vim /opt/mysql/slave2/slave.cnf
修改如下配置
log-bin = mysql-bin 
server-id = 3

启动docker

#sudo docker run -d -e MYSQL_ROOT_PASSWORD=root --name mysql-slave2 -v /opt/mysql/slave2/slave.cnf:/etc/mysql/my.cnf -p 3327:3306 mysql:5.7
修改slave2配置

连接进slave的docker:

#sudo docker exec -it mysql-slave2 bash
#mysql -uroot -proot
>SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
>change master to master_host='192.168.249.128',master_user='backup',master_password='123456',master_log_file='mysql-bin.000004',master_log_pos=861,master_port=3307;
>start slave;

#master_host为docker的地址不能写127.0.0.1
#master_user是在主库创建的用户
#master_log_pos是主库show master status;查询出的Position

这样就为主从配置增加了一个slave。
super easy!!!!

上一篇 下一篇

猜你喜欢

热点阅读