docker+MySQL+读写分离

Posted fansik

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker+MySQL+读写分离相关的知识,希望对你有一定的参考价值。

一、拉取mysql镜像文件
docker pull mysql
二、查看镜像
docker images
三、创建配置文件目录
mkdir /data/docker/mysql/{master,slave} -pv

四、主从配置文件
主:/data/docker/mysql/master/my.cnf
[mysqld]
server-id=1
log-bin=master-bin
log-bin-index=master-bin.index
default-authentication-plugin=mysql_native_password
从:/data/docker/mysql/slave/my.cnf
[mysqld]
server-id=2
skip-slave-start=true
read_only=ON
relay-log=relay-bin
relay-log-index=relay-bin.index
default-authentication-plugin=mysql_native_password

五、启动主从数据库的docker实例
创建主库数据文件目录
mkdir /data/docker/mysql/master/data
启动主库实例
docker run -d -e MYSQL_ROOT_PASSWORD=123456 --name mysql-master -v /data/docker/mysql/master/data:/var/lib/mysql -v /data/docker/mysql/master/my.cnf:/etc/my.cnf -p 3306:3306 mysql
创建以后要到容器里面去修改密码:否则会报错,据说是MySQL的新特性:
报错如下:
ERROR 2059 (HY000): Authentication plugin \'caching_sha2_password\' cannot be loaded: /usr/lib64/mysql/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory
进入容器
docker exec -it mysql-master /bin/bash
登录MySQL
mysql -uroot -p123456 -h127.0.0.1
修改密码
ALTER USER \'root\'@\'%\' IDENTIFIED WITH mysql_native_password BY \'fansik\';


创建从库数据文件目录
mkdir /data/docker/mysql/slave/data
启动从库实例
docker run -d -e MYSQL_ROOT_PASSWORD=123456 --name mysql-slave -v /data/docker/mysql/slave/data:/var/lib/mysql -v /data/docker/mysql/slave/my.cnf:/etc/my.cnf -p 3307:3306 mysql
创建以后要到容器里面去修改密码:否则会报错,据说是MySQL的新特性:
报错如下:
ERROR 2059 (HY000): Authentication plugin \'caching_sha2_password\' cannot be loaded: /usr/lib64/mysql/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory
进入容器
docker exec -it mysql-slave /bin/bash
登录从MySQL
mysql -uroot -p123456 -h127.0.0.1 -P3307
修改密码
ALTER USER \'root\'@\'%\' IDENTIFIED WITH mysql_native_password BY \'fansik\';

六、配置主从复制
查看两个容器的ip地址
主库:
[root@10-17-16-106 data]# docker inspect 07 |grep \'IPAddress\'
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.2",
"IPAddress": "172.17.0.2",

从库:
[root@10-17-16-106 data]# docker inspect 72 |grep \'IPAddress\'
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.3",
"IPAddress": "172.17.0.3",

1、登录主库添加复制账号
MySQL [(none)]> CREATE USER \'slave\'@\'%\' IDENTIFIED BY \'slave\';
Query OK, 0 rows affected (0.09 sec)

MySQL [(none)]> GRANT REPLICATION SLAVE ON *.* TO \'slave\'@\'%\';
Query OK, 0 rows affected (0.01 sec)

MySQL [(none)]> flush privileges;
Query OK, 0 rows affected (0.01 sec)

2、查看主库状态
MySQL [(none)]> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000004 | 1363     |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+

3、配置从库复制信息
change master to master_host=\'172.17.0.2\',master_user=\'slave\',master_password=\'slave\',master_log_file=\'master-bin.000004\',master_log_pos=1363,master_port=3306;

读写分离配置,使用中间件为amoeba
参考文档:http://www.cnblogs.com/fansik/p/7168501.html

以上是关于docker+MySQL+读写分离的主要内容,如果未能解决你的问题,请参考以下文章

docker+MySQL+读写分离

docker搭建mysql读写分离

docker安装mycat并实现mysql读写分离和分库分表

docker+Atlas+mysql实现主从同步,读写分离

docker部署django项目mysql主从搭建django实现读写分离

Docker Mysql5.7 + Mycat1.6.7.1主从复制读写分离+水平分库分表配置