docket的mysql读写分离_docker配置mysql主从实现读写分离

1.获取mysql镜像

docker pull mysql:5.7

2.创建工作目录

cd ~

mkdir docker-mysql-config

3.创建主从mysql配置文件

创建主mysql配置文件

vim ~/docker-mysql-config/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/

创建从mysql配置文件

vim ~/docker-mysql-config/slave01.cnf

复制主mysql配置文件,注意:

将server-id = 1 修改为server-id = 2

4.运行主myqsl容器

docker run -d -e MYSQL_ROOT_PASSWORD=123 --name mysql-master -v ~/docker-mysql-config/master.cnf:/etc/mysql/my.cnf -p 3306:3306 mysql:5.7

5.运行从mysql容器

docker run -d -e MYSQL_ROOT_PASSWORD=123 --name mysql-slave01 -v ~/docker-mysql-config/slave01.cnf:/etc/mysql/my.cnf -p 3307:3306 mysql:5.7

6.主从服务配置

终端1:主mysql容器

docker exec -it mysql-master bash

mysql -uroot -p123

mysql>

终端2:从mysql容器

docker exec -it mysql-slave01 bash

mysql -uroot -p123

mysql>

主mysql容器配置

mysql> grant replication slave on *.* to 'slave01'@'%' identified by 'slave01';

mysql> flush privileges;

mysql> show master status;

+------------------+----------+--------------+------------------+-------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------+----------+--------------+------------------+-------------------+

| mysql-bin.000004 | 1160 | | | |

+------------------+----------+--------------+------------------+-------------------+

1 row in set (0.00 sec)

从mysql容器配置

mysql> change master to master_host='192.168.99.100',

master_user='slave01',

master_password='slave01',

master_log_file='mysql-bin.000004',

master_log_pos=0, ## 从master什么记录位置开始读取,从0开始读取可以复制master整个库表

master_port=3306;

mysql> start slave;

mysql> show slave status \G

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 172.17.0.2

Master_User: slave

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000004

Read_Master_Log_Pos: 1160

Relay_Log_File: 3dc37943fb7a-relay-bin.000002

Relay_Log_Pos: 320

Relay_Master_Log_File: mysql-bin.000004

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Replicate_Do_DB:

Replicate_Ignore_DB:

Replicate_Do_Table:

Replicate_Ignore_Table:

Replicate_Wild_Do_Table:

Replicate_Wild_Ignore_Table:

Last_Errno: 0

Last_Error:

Skip_Counter: 0

Exec_Master_Log_Pos: 1160

Relay_Log_Space: 534

Until_Condition: None

Until_Log_File:

Until_Log_Pos: 0

Master_SSL_Allowed: No

Master_SSL_CA_File:

Master_SSL_CA_Path:

Master_SSL_Cert:

Master_SSL_Cipher:

Master_SSL_Key:

Seconds_Behind_Master: 0

Master_SSL_Verify_Server_Cert: No

Last_IO_Errno: 0

Last_IO_Error:

Last_SQL_Errno: 0

Last_SQL_Error:

Replicate_Ignore_Server_Ids:

Master_Server_Id: 1

Master_UUID: 39f6cd9a-f870-11e7-b14e-0242ac110002

Master_Info_File: /var/lib/mysql/master.info

SQL_Delay: 0

SQL_Remaining_Delay: NULL

Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates

Master_Retry_Count: 86400

Master_Bind:

Last_IO_Error_Timestamp:

Last_SQL_Error_Timestamp:

Master_SSL_Crl:

Master_SSL_Crlpath:

Retrieved_Gtid_Set:

Executed_Gtid_Set:

Auto_Position: 0

Replicate_Rewrite_DB:

Channel_Name:

Master_TLS_Version:

1 row in set (0.00 sec)

只要一下两个为Yes,代表配置完成

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

若Slave_SQL_Running为No,表示执行SQL出错,因为slave数据库没有mater数据库的库和表结构,需要把master和slave同步一下后,执行一下命令

mysql> stop slave ;

mysql> set GLOBAL SQL_SLAVE_SKIP_COUNTER=1; #客户端运行,用来跳过几个事件,只有当同步进程出现错误而停止的时候才可以执行。

mysql> start slave ;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值