基于Docker配置mysql主从集群

Linux安装Docker和mysql,树莓派也安装Docker和mysql,配置mysql主从集群

1. Docker核心概念

Host主机:安装了Docker程序的机器

Client客户端:连接Docker主机进行操作

Registries仓库:保存各种打包好的软件镜像

images镜像:软件打包好的镜像,放在仓库中

container容器:镜像启动后的实例称为一个容器,容器独立运行一个或一组应用

2. linux安装Docker

要求CentOS内核版本高于3.10 – CentOS 6.5以上版本

uname -r 查看内核版本

1
2
[root@localhost local]# uname -r
3.10.0-1062.1.2.el7.x86_64

yum update 升级安装包

yum install docker 安装

systemctl start docker 启动docker

docker -v 查看docker版本

systemctl enable docker 开机启动docker

sytemctl stop docker 停止docker

1
2
3
4
5
6
7
8
9
[root@192 yum.repos.d]# docker -v
Docker version 1.13.1, build cccb291/1.13.1
[root@192 yum.repos.d]#
[root@192 yum.repos.d]#
[root@192 yum.repos.d]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@192 yum.repos.d]#
[root@192 yum.repos.d]# systemctl start docker
[root@192 yum.repos.d]#

https://docs.docker.com/install/linux/docker-ce/centos/

官网docker 安装版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 卸载原有docker
[root@192 /]# sudo yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine

# Install required packages 安装依赖包
[root@192 /]# sudo yum install -y yum-utils device-mapper-persistent-data lvm2

# 添加docker源
[root@192 /]# sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

#安装
[root@192 /]# sudo yum install docker-ce docker-ce-cli containerd.io

[root@192 /]# docker -v
Docker version 19.03.8, build afacb8b

2.1. 镜像加速

这里使用的是 阿里云提供的镜像加速 ,登录并且设置密码之后在左侧的 镜像加速器 可以找到专属加速器地址,复制下来。

2.2. 以普通用户身份运行docker

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

systemctl daemon-reload

systemctl restart docker

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.40/containers/json: dial unix /var/run/docker.sock: connect: permission denied

groupadd docker 添加docker用户组

gpasswd -a $USER docker 将登陆用户加入到docker用户组中

newgrp docker 更新用户组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
[linux@localhost ~]$ docker ps
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
[linux@localhost ~]$
[linux@localhost ~]$ systemctl daemon-reload
==== AUTHENTICATING FOR org.freedesktop.systemd1.reload-daemon ===
Authentication is required to reload the systemd state.
Authenticating as: root
Password:
==== AUTHENTICATION COMPLETE ===
[linux@localhost ~]$
[linux@localhost ~]$ systemctl restart docker
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to manage system services or units.
Authenticating as: root
Password:
==== AUTHENTICATION COMPLETE ===
[linux@localhost ~]$
[linux@localhost ~]$
[linux@localhost ~]$ docker ps
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.40/containers/json: dial unix /var/run/docker.sock: connect: permission denied
[linux@localhost ~]$ su
[root@localhost linux]# groupadd docker
groupadd:“docker”组已存在
[root@localhost linux]# gpasswd -a $USER docker
正在将用户“linux”加入到“docker”组中
[root@localhost linux]# newgrp docker
[root@localhost linux]# su linux
[linux@localhost ~]$
[linux@localhost ~]$
[linux@localhost ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
19d9ac40ba76 elasticsearch:5.6.11 "/docker-entrypoint.…" 15 hours ago Up 3 minutes 0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp elasticsearch
7122d84209e4 kibana:5.6.11 "/docker-entrypoint.…" 17 hours ago Up 3 minutes 0.0.0.0:5601->5601/tcp kibana
[linux@localhost ~]$

2.3. Docker常用操作

操作 命令 说明
检索 docker search 关键字 docker hub上检索镜像信息
拉取 docker pull 镜像名:tag :tag可选,默认为latest
列表 docker images 查看所有本地镜像
删除 docker rmi image-id 删除指定的本地镜像

2.3.1. 容器操作

软件镜像 – 运行镜像 – 产生容器

QQ.exe– 镜像 – 运行的QQ

命令 说明
docker images 查看所有镜像
docker run –name 自定义容器名 -d(后台运行) 镜像名 -p(暴露端口) 3306:3306 启动某个镜像
docker ps 查看正在运行的容器
docker stop 容器名/容器id 停止容器
docker ps -a 查看所有存在的容器
docker rm 容器id 删除容器
docker start 容器id 启动容器
docker run 镜像名:tag -p 主机端口:容器内部端口 -p 指定端口映射
service firewall status/stop 防火墙状态/关闭
docker logs 容器id 查看日志

2.3.2. 所用操作

1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost linux]# firewall-cmd --list-port

[root@localhost linux]# firewall-cmd --zone=public --add-port=8080/tcp --permanent
success
[root@localhost linux]# firewall-cmd --query-port=8080/tcp
no
[root@localhost linux]# firewall-cmd --reload
success
[root@localhost linux]# firewall-cmd --query-port=8080/tcp
yes
[root@localhost local]# firewall-cmd --list-port
8080/tcp

2.4. 查看IP

不同于CentOS 6 ifconfig

1
[root@localhost local]# ip addr

2.5. 安装tomcat

1
2
3
4
5
6
7
8
9
[root@localhost linux]# docker pull tomcat
[root@localhost linux]# docker run --name tomcat -p 8080:8080 -d tomcat
[root@localhost linux]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
84facfefca1b tomcat "catalina.sh run" About a minute ago Up About a minute 0.0.0.0:8080->8080/tcp tomcat
[root@localhost linux]#
[root@localhost linux]# docker stop 84facfefca1b
84facfefca1b
[root@localhost linux]# docker start 84facfefca1b

2.5.1. 进入docker tomcat终端

1
2
3
4
5
6
7
8
[root@localhost linux]# docker exec it tomcat /bin/bash
root@84facfefca1b:/usr/local/tomcat#
root@84facfefca1b:/usr/local/tomcat# cd conf
root@84facfefca1b:/usr/local/tomcat/conf# ls
Catalina catalina.properties jaspic-providers.xml logging.properties tomcat-users.xml web.xml
catalina.policy context.xml jaspic-providers.xsd server.xml tomcat-users.xsd
root@84facfefca1b:/usr/local/tomcat/conf#
root@84facfefca1b:/usr/local/tomcat/conf# exit;

http://192.168.0.105:8080

3. 树莓派安装docker

apt-get install curl

curl -sSL https://get.docker.com | sh

docker -v

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
root@pi:/home/pi# apt-get install curl
root@pi:/home/pi# curl -sSL https://get.docker.com | sh
# Executing docker install script, commit: f45d7c11389849ff46a6b4d94e0dd1ffebca32c1
+ sh -c apt-get update -qq >/dev/null
+ sh -c DEBIAN_FRONTEND=noninteractive apt-get install -y -qq apt-transport-https ca-certificates curl >/dev/null
+ sh -c curl -fsSL "https://download.docker.com/linux/raspbian/gpg" | apt-key add -qq - >/dev/null
Warning: apt-key output should not be parsed (stdout is not a terminal)
+ sh -c echo "deb [arch=armhf] https://download.docker.com/linux/raspbian buster stable" > /etc/apt/sources.list.d/docker.list
+ sh -c apt-get update -qq >/dev/null
+ [ -n ]
+ sh -c apt-get install -y -qq --no-install-recommends docker-ce >/dev/null
+ sh -c docker version
Client: Docker Engine - Community
Version: 19.03.4
API version: 1.40
Go version: go1.12.10
Git commit: 9013bf5
Built: Fri Oct 18 16:03:00 2019
OS/Arch: linux/arm
Experimental: false

Server: Docker Engine - Community
Engine:
Version: 19.03.4
API version: 1.40 (minimum version 1.12)
Go version: go1.12.10
Git commit: 9013bf5
Built: Fri Oct 18 15:56:55 2019
OS/Arch: linux/arm
Experimental: false
containerd:
Version: 1.2.10
GitCommit: b34a5c8af56e510852c35414db4c1f4fa6172339
runc:
Version: 1.0.0-rc8+dev
GitCommit: 3e425f80a8c931f88e6d94a8c831b9d5aa481657
docker-init:
Version: 0.18.0
GitCommit: fec3683
If you would like to use Docker as a non-root user, you should now consider
adding your user to the "docker" group with something like:

sudo usermod -aG docker your-user

Remember that you will have to log out and back in for this to take effect!

WARNING: Adding a user to the "docker" group will grant the ability to run
containers which can be used to obtain root privileges on the
docker host.
Refer to https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface
for more information.
root@pi:/home/pi# docker -v
Docker version 19.03.4, build 9013bf5

4. portainer远程管理docker

更新Centos docker 镜像加速地址

curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://ef017c13.m.daocloud.io

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[root@localhost linux]# curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://ef017c13.m.daocloud.io
docker version >= 1.12
{"registry-mirrors": ["http://ef017c13.m.daocloud.io"],}
Success.
You need to restart docker to take effect: sudo systemctl restart docker
[root@localhost linux]# systemctl restart docker
Job for docker.service failed because the control process exited with error code. See "systemctl status docker.service" and "journalctl -xe" for details.
[root@localhost linux]#
[root@localhost linux]# systemctl status docker.service
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since 二 2019-10-22 04:08:08 CST; 23s ago
Docs: http://docs.docker.com
Process: 4485 ExecStart=/usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt native.cgroupdriver=systemd --userland-proxy-path=/usr/libexec/docker/docker-proxy-current --init-path=/usr/libexec/docker/docker-init-current --seccomp-profile=/etc/docker/seccomp.json $OPTIONS $DOCKER_STORAGE_OPTIONS $DOCKER_NETWORK_OPTIONS $ADD_REGISTRY $BLOCK_REGISTRY $INSECURE_REGISTRY $REGISTRIES (code=exited, status=1/FAILURE)
Main PID: 4485 (code=exited, status=1/FAILURE)

10月 22 04:08:08 localhost.localdomain systemd[1]: Starting Docker Application Container Engine...
10月 22 04:08:08 localhost.localdomain dockerd-current[4485]: unable to configure the Docker daemon with file /etc/docke...ring
10月 22 04:08:08 localhost.localdomain systemd[1]: docker.service: main process exited, code=exited, status=1/FAILURE
10月 22 04:08:08 localhost.localdomain systemd[1]: Failed to start Docker Application Container Engine.
10月 22 04:08:08 localhost.localdomain systemd[1]: Unit docker.service entered failed state.
10月 22 04:08:08 localhost.localdomain systemd[1]: docker.service failed.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost linux]# vi /etc/docker/daemon.json

##去掉,
{"registry-mirrors": ["http://ef017c13.m.daocloud.io",]}

[root@localhost linux]# sudo systemctl restart docker
[root@localhost linux]#

portainer是一个docker管理工具,提供一个web管理界面方便大家管理自己的容器

  1. 下载 Docker 图形化界面 portainer

    1
    [root@localhost linux]# docker pull portainer/portainer
  2. 创建 portainer 容器

    1
    [root@localhost linux]# docker volume create portainer_data
  3. 运行 portainer

    1
    [root@localhost linux]# sudo docker run -d -p 9000:9000 --name portainer --restart always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer

运行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@localhost linux]# docker pull portainer/portainer
Using default tag: latest
Trying to pull repository docker.io/portainer/portainer ...
latest: Pulling from docker.io/portainer/portainer
d1e017099d17: Pull complete
7585cb582294: Pull complete
Digest: sha256:497e7cb961cec56a39134f0b106d44bdf0500bd58914cd89582e3c93e5b75d67
Status: Downloaded newer image for docker.io/portainer/portainer:latest
[root@localhost linux]#
[root@localhost linux]#
[root@localhost linux]# docker volume create portainer_data
portainer_data
[root@localhost linux]#
[root@localhost linux]#
[root@localhost linux]# sudo docker run -d -p 9000:9000 --name portainer --restart always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer
939ddacac964916e11ab214b75f112094261e12b48cf666a2c27e6cea9c59156
[root@localhost linux]#

添加docker权限

1
2
3
4
5
6
7
8
9
[root@localhost linux]# sudo groupadd docker 
[root@localhost linux]#
[root@localhost linux]# sudo gpasswd -a $USER docker
正在将用户“linux”加入到“docker”组中
[root@localhost linux]#
[root@localhost linux]# newgrp docker
[root@localhost linux]# vi /etc/group

docker:x:1001:linux

开启9000端口

1
2
3
4
5
[root@localhost yum.repos.d]# firewall-cmd --zone=public --add-port=9000/tcp --permanent
success
[root@localhost yum.repos.d]#
[root@localhost yum.repos.d]# firewall-cmd --reload
success

docker开启2375端口

1
2
3
4
5
6
7
8
[root@localhost linux]# vi /lib/systemd/system/docker.service

ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock \

[root@localhost linux]# systemctl daemon-reload
[root@localhost linux]#
[root@localhost linux]# sudo systemctl restart docker
[root@localhost linux]#

开启2375端口

1
2
3
4
5
6
[root@localhost yum.repos.d]# firewall-cmd --zone=public --add-port=2375/tcp --permanent
success
[root@localhost yum.repos.d]#
[root@localhost yum.repos.d]# firewall-cmd --reload
success
[root@localhost yum.repos.d]#

远程访问192.168.0.105:9000

远程连接

注册在docker中的用户名以开放的2375端口

成功

5. docker安装mysql – Linux

5.1. 安装mysql

docker pull mysql

5.2. 运行mysql

–name 容器名

-p 端口映射

-e 配置mysql root密码

-d 部署镜像名

docker run –name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql

5.3. 开启3306端口

firewall-cmd –zone=public –add-port=3306/tcp –permanent

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@localhost linux]# docker pull mysql
Using default tag: latest
Trying to pull repository docker.io/library/mysql ...
latest: Pulling from docker.io/library/mysql
Status: Downloaded newer image for docker.io/mysql:latest
[root@localhost linux]#
[root@localhost linux]# docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql
f2048782743b9b0d2f0e5e830998c43c3e9f7b0352b6ff3a721a890f3e559530
[root@localhost linux]#
[root@localhost linux]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f2048782743b mysql "docker-entrypoint..." 5 seconds ago Up 4 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp mysql
939ddacac964 portainer/portainer "/portainer" 21 hours ago Up 14 minutes 0.0.0.0:9000->9000/tcp portainer
[root@localhost linux]#
[root@localhost linux]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success
[root@localhost linux]#
[root@localhost linux]#
[root@localhost linux]# firewall-cmd --reload
success
[root@localhost linux]#

远程连接mysql

RROR 2059 (HY000): Authentication plugin ‘caching_sha2_password’ cannot be loaded

5.4. 配置权限

进入mysql容器

docker exec -it mysql /bin/bash

mysql -uroot -p

访问mysql

use mysql;

设置密码

ALTER USER ‘root‘@’%’ IDENTIFIED WITH mysql_native_password BY ‘123456’;

host 是 % 代表可以任意ip访问 plugin 一定是 mysql_native_password 不然客户端连接不了

查看所有用户

select user,host,plugin from mysql.user;

exit退出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
[root@localhost linux]# docker exec -it mysql /bin/bash
root@36fb6e3122de:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.18 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
mysql> show databases
-> ;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.04 sec)

mysql>
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.02 sec)

mysql> select user,host,plugin from mysql.user;
+------------------+-----------+-----------------------+
| user | host | plugin |
+------------------+-----------+-----------------------+
| root | % | mysql_native_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session | localhost | caching_sha2_password |
| mysql.sys | localhost | caching_sha2_password |
| root | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+
5 rows in set (0.00 sec)

mysql> exit;

退出后docker重启mysql

1
2
3
4
5
6
[root@localhost linux]# docker stop mysql
mysql
[root@localhost linux]#
[root@localhost linux]# docker start mysql
mysql
[root@localhost linux]#

5.5. 测试链接

5.6. 测试语句

1
create database springboot;

linux终端

1
2
3
4
5
6
7
8
9
10
11
12
13
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| springboot |
| sys |
+--------------------+
5 rows in set (0.01 sec)

mysql> exit;

6. docker安装mysql – 树莓派

在树莓派上官方mysql镜像无法使用,因为树莓派的架构为arm

使用映像 [https://hub.docker.com/r/hypriot/rpi-mysql/]

下载镜像

docker pull hypriot/rpi-mysql

运行镜像

docker run –name mysql -e MYSQL_ROOT_PASSWORD=123456 -d -p

加入权限

docker exec -it mysql /bin/bash

mysql -uroot -p

配置密码

use mysql;

grant all privileges on . to ‘root‘@’%’ identified by ‘123456’ with grant option;

flush privileges;

放行端口 -- root下

iptables -I INPUT -i eth0 -p tcp –dport 3307 -j ACCEPT

iptables -I OUTPUT -o eth0 -p tcp –sport 3307 -j ACCEPT

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
pi@pi:~ $ docker pull hypriot/rpi-mysql
Using default tag: latest
latest: Pulling from hypriot/rpi-mysql
docker.io/hypriot/rpi-mysql:latest
pi@pi:~ $
pi@pi:~ $
pi@pi:~ $ docker run --name mysql -e MYSQL_ROOT_PASSWORD=123456 -d -p 3307:3306 hypriot/rpi-mysql
c3a05071cbb680b969fa48f49b806ee30c5a40c7776796e8e75d03bee419108d
pi@pi:~ $
pi@pi:~ $ docker exec -it mysql /bin/bash
root@c3a05071cbb6:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.60-0+deb7u1 (Debian)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
mysql>
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql>
mysql>
mysql> select user,host,plugin from mysql.user;
+------+------+--------+
| user | host | plugin |
+------+------+--------+
| root | % | |
+------+------+--------+
1 row in set (0.01 sec)

mysql>
mysql> grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
Query OK, 0 rows affected (0.00 sec)

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

mysql>
mysql> exit
Bye
root@c3a05071cbb6:/# exit
exit
pi@pi:~ $ su
密码:
root@pi:/home/pi#
root@pi:/home/pi# iptables --version
iptables v1.8.2 (nf_tables)
root@pi:/home/pi#
root@pi:/home/pi# iptables -I INPUT -i eth0 -p tcp --dport 3307 -j ACCEPT
root@pi:/home/pi#
root@pi:/home/pi# iptables -I OUTPUT -o eth0 -p tcp --sport 3307 -j ACCEPT
root@pi:/home/pi#
root@pi:/home/pi# exit
exit
pi@pi:~ $

测试

树莓派docker中mysql

1
2
3
4
5
6
7
8
9
10
11
12
13
mysql> 
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| springboot |
+--------------------+
4 rows in set (0.00 sec)

mysql> exit;

6.1. 建立主从集群

###主实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
-p 3308:3306 #将容器3306端口映射到主机3308端口

--name mysql-master #容器名mysql-master

-v /mydata/mysql/master/log:/var/log/mysql #日志文件挂载到主机/mydata/mysql/master/log

-v /mydata/mysql/master/data:/var/lib/mysql #数据挂载到/mydata/mysql/master/data

-v /mydata/mysql/master/conf:/etc/mysql # 配置文件挂载到/mydata/mysql/master/conf

-e MYSQL_ROOT_PASSWORD=123456 #初始化root用户密码

--user=mysql #默认拒绝用root账号启动mysql服务

-d hypriot/rpi-mysql #运行镜像指定

命令

1
docker run -p 3308:3306 --name mysql-master -v /mydata/mysql/master/log:/var/log/mysql -v /mydata/mysql/master/data:/var/lib/mysql -v /mydata/mysql/master/conf:/etc/mysql -e MYSQL_ROOT_PASSWORD=123456 --user=mysql -d hypriot/rpi-mysql

6.1.0.1. 配置远程调用权限

将mysql加入用户组,%表示所有IP都有连接权限

grant all privileges on *.* to 'mysql'@'%' identified by '123456' with grant option;

使配置生成

flush privileges;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
pi@pi:~ $ docker exec -it mysql-master /bin/bash
mysql@7ce91168422b:/$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.5.60-0+deb7u1 (Debian)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> grant all privileges on *.* to 'mysql'@'%' identified by '123456' with grant option;
Query OK, 0 rows affected (0.01 sec)

mysql> select user,host,plugin from mysql.user;
+-------+--------------+--------+
| user | host | plugin |
+-------+--------------+--------+
| root | localhost | |
| root | b32ea1f1ae91 | |
| root | 127.0.0.1 | |
| root | ::1 | |
| root | % | |
| mysql | % | |
+-------+--------------+--------+
6 rows in set (0.00 sec)

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

mysql>

6.1.0.2. 配置主文件 - 读写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
user=mysql

server_id=1
log-bin=mysql-bin
read-only=0
binlog-do-db=gmall_ums
binlog-do-db=gmall_pms
binlog-do-db=gmall_oms
binlog-do-db=gmall_sms
binlog-do-db=gmall_cms


replicate-ignore-db=mysql
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema

6.1.1. 从实例

1
2
3
4
root@pi:/mydata/mysql/slaver/conf# docker run -p 3309:3306 --name mysql-slaver-01 -v /mydata/mysql/slaver/log:/var/log/mysql -v /mydata/mysql/slaver/data:/var/lib/mysql -v /mydata/mysql/slaver/conf:/etc/mysql -e MYSQL_ROOT_PASSWORD=123456 --privileged=true  -d hypriot/rpi-mysql
root@pi:/mydata/mysql/slaver/conf# iptables -I INPUT -i eth0 -p tcp --dport 3309 -j ACCEPT
root@pi:/mydata/mysql/slaver/conf# iptables -I OUTPUT -o eth0 -p tcp --sport 3309 -j ACCEPT
root@pi:/mydata/mysql/slaver/conf# nano my.cnf

6.1.1.1. 从文件配置 - 读

1
2
3
4
5
6
7
8
9
10
11
12
13
server_id=2
log-bin=mysql-bin
read-only=1
binlog-do-db=gmall_ums
binlog-do-db=gmall_pms
binlog-do-db=gmall_oms
binlog-do-db=gmall_sms
binlog-do-db=gmall_cms


replicate-ignore-db=mysql
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema

6.1.2. 开启同步

6.1.2.1. 主

添加用来同步的用户 GRANT REPLICATION SLAVE ON . to ‘backup‘@’%’ identified by ‘123456’;

查看master状态 show master status\G;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
root@pi:/mydata/mysql# docker exec -it mysql-master /bin/bash
mysql@098fea9cade2:/$
mysql@098fea9cade2:/$ mysql -uroot -p
# 添加用来同步的用户
mysql> GRANT REPLICATION SLAVE ON *.* to 'backup'@'%' identified by '123456';
# 查看master状态
mysql> show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000003
Position: 107
Binlog_Do_DB: gmall_ums,gmall_pms,gmall_oms,gmall_sms,gmall_cms
Binlog_Ignore_DB:
1 row in set (0.00 sec)

ERROR:
No query specified

mysql> show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 1 |
+---------------+-------+
1 row in set (0.00 sec)

6.1.2.2. 从

主库连接

change master to master_host=’192.168.0.112’,master_user=’backup’,master_password=’123456’,master_log_file=’mysql-bin.000002’,master_log_pos=0,master_port=3308;

启动从库同步

start slave;

查看状态

show slave status\G;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
mysql> pi@pi:~ $ docker exec -it mysql-slaver-01 /bin/bash
root@9887598fafc8:/# mysql -uroot -p

mysql> change master to master_host='192.168.0.112',master_user='backup',master_password='123456',master_log_file='mysql-bin.000002',master_log_pos=0,master_port=3308;
Query OK, 0 rows affected (0.03 sec)

mysql>
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.112
Master_User: backup
Master_Port: 3308
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 107
Relay_Log_File: 9887598fafc8-relay-bin.000003
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB: mysql,information_schema,performance_schema
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: 107
Relay_Log_Space: 874
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
1 row in set (0.00 sec)

ERROR:
No query specified

mysql>
本文结束  感谢您的阅读