docker+MySQL+讀寫分離

1、拉取mysql鏡像文件
docker pull mysql
2、查看鏡像
docker images
3、建立配置文件目錄
mkdir /data/docker/mysql/{master,slave} -pvhtml

4、主從配置文件
主:/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_passwordmysql

5、啓動主從數據庫的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';sql


建立從庫數據文件目錄
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';docker

6、配置主從複製
查看兩個容器的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",bash

一、登陸主庫添加複製帳號
MySQL [(none)]> CREATE USER 'slave'@'%' IDENTIFIED BY 'slave';
Query OK, 0 rows affected (0.09 sec)spa

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

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

二、查看主庫狀態
MySQL [(none)]> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000004 | 1363     |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+中間件

三、配置從庫複製信息
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

相關文章
相關標籤/搜索