CentOS 7.2 基于Docker实现MySQL主从架构

环境准备

硬件:IBM 3650 M4
系统:CentOS 7.2 64位
Docker版本:1.13
mysql版本:5.6

1.安装Docker(略)

2.加载mysql镜像

#官方mysql镜像
sudo docker pull mysql            ###官方mysql镜像实际上是8.0.11,略坑,不推荐

###直接使用mysql5.6
sudo docker pull mysql5.6

3.添加主从数据库的配置文件

##新建目录和文件
sudo mkdir -p /usr/local/mysqlData/master/cnf
sudo mkdir -p /usr/local/mysqlData/master/data
sudo mkdir -p /usr/local/mysqlData/slave/cnf
sudo mkdir -p /usr/local/mysqlData/slave/data
sudo touch /usr/local/mysqlData/master/cnf/mysql.cnf
sudo touch /usr/local/mysqlData/slave/cnf/mysql.cnf

编辑 /usr/local/mysqlData/master/cnf/mysql.cnf

[mysqld]
pid-file    = /var/run/mysqld/mysqld.pid
socket    = /var/run/mysqld/mysqld.sock
datadir    = /var/lib/mysql

symbolic-links=0

character-set-server = utf8
#skip-networking
innodb_print_all_deadlocks = 1
max_connections = 2000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 4M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M

read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 28M
key_buffer_size = 4M

thread_cache_size = 8

query_cache_type = 1
query_cache_size = 8M
query_cache_limit = 2M

ft_min_word_len = 4

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

performance_schema = 0
explicit_defaults_for_timestamp

#lower_case_table_names = 1

interactive_timeout = 28800
wait_timeout = 28800

# Recommended in standard MySQL setup

sql_mode=NO_ENGINE_SUBSTITUTION,NO_AUTO_CREATE_USER,STRICT_TRANS_TABLES

[mysqldump]
quick
max_allowed_packet = 16M

[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M

编辑/usr/local/mysqlData/slave/cnf/mysql.cnf

[mysqld]
pid-file    = /var/run/mysqld/mysqld.pid
socket    = /var/run/mysqld/mysqld.sock
datadir    = /var/lib/mysql

symbolic-links=0

character-set-server = utf8
#skip-networking
innodb_print_all_deadlocks = 1
max_connections = 2000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 4M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M

read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 28M
key_buffer_size = 4M

thread_cache_size = 8

query_cache_type = 1
query_cache_size = 8M
query_cache_limit = 2M

ft_min_word_len = 4

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

performance_schema = 0
explicit_defaults_for_timestamp

#lower_case_table_names = 1

interactive_timeout = 28800
wait_timeout = 28800

# Recommended in standard MySQL setup

sql_mode=NO_ENGINE_SUBSTITUTION,NO_AUTO_CREATE_USER,STRICT_TRANS_TABLES

[mysqldump]
quick
max_allowed_packet = 16M

[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M

4.启用master和slave容器

##master容器启用
sudo docker run -itd -p 3307:3306 --name master -v /usr/local/mysqlData/master/cnf:/etc/mysql/conf.d -v /usr/local/mysqlData/master/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:5.6
##进入master容器
sudo docker exec -it master /bin/bash
root@ebc7017a58ef:/#

##slave容器启用
sudo docker run -itd -p 3308:3306 --name slave -v /usr/local/mysqlData/slave/cnf:/etc/mysql/conf.d -v /usr/local/mysqlData/slave/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:5.6
root@ee3bb51b4d14:/#

5.数据库开启远程访问

##防火墙设置允许端口访问
sudo firewall-cmd --add-port=3307/tcp --permanent
sudo firewall-cdm --add-port=3308/tcp --permanent
sudo firewall-cmd --reload

##设置master数据库访问权限
root@ebc7017a58ef:/# mysql -uroot -p123456
mysql>grant all privileges on *.* to reader@'%' identified by 'readerpwd' with grant option;
Query OK, 0 rows affected (0.01 sec)

mysql>flush privileges;
Query OK, 0 rows affected (0.01 sec)

##设置slave数据库访问权限
root@ee3bb51b4d14:/# mysql -uroot -p123456
mysql>grant all privileges on *.* to slave@'%' identified by 'slavepwd' with grant option;
Query OK, 0 rows affected (0.01 sec)

mysql>flush privileges;
Query OK, 0 rows affected (0.01 sec)

6.设置主从同步

##登录master数据库查看master状态
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 |      204 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql>

其中mysql-bin.000002为日志文件,后面会用到

##配置slave
mysql> change master to master_host='172.17.0.6',master_user='reader',master_password='readerpwd',master_log_file='mysql-bin.000002',master_log_pos=501;

##参数说明
master_host,master数据库的ip地址
master_user,连接master数据库的用户名
master_password,master_user的密码
master_log_file,日志文件,上一步中,master status对应的文件

##启动slave,slave的操作有几个,start,stop和restart
mysql> start slave;

##查看slave状态
mysql> show slave status;

posted on 2018-06-11 17:58  alexzcl  阅读(158)  评论(0编辑  收藏  举报

导航