博客园 首页 私信博主 显示目录 隐藏目录 管理 动画

docker 安装 mysql 集群

 原文:mysql集群__院长大人_的博客-CSDN博客_mysql集群

下载 mysql 镜像

docker pull mysql:5.7

3.3.2 主节点创建
3.3.2.1 创建 Master 实例并启动

docker run -p 3307: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/conf.d \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7

#Centos7安全Selinux禁止了一些安全权限,导致mysql和mariadb在进行挂载/var/lib/mysql的时候会提示- 简书 (jianshu.com)
--privileged=true

参数说明

-p 3307:3306: 将容器的 3306 端口映射到宿主机的 3307 端口
-v /mydata/mysql/master/conf:/etc/mysql: 将配置文件夹挂载到主机
-v /mydata/mysql/master/log:/var/log/mysql: 将日志文件夹挂载到主机
-v /mydata/mysql/master/data:/var/lib/mysql/: 将配置文件夹挂载到主机
-e MYSQL_ROOT_PASSWORD=root: 初始化 root 用户的密码

3.3.2.2 修改 master 基本配置

vim /mydata/mysql/master/conf/my.cnf

为 my.cnf 写入一下配置 设置字符集

[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

注意: skip-name-resolve 一定要加, 不然连接 mysql 会超级慢

3.3.2.3 添加 master 主从复制部分配置
为 my.cnf 写入一下配置

vim /mydata/mysql/master/conf/my.cnf

#指定集群id
server_id=1 
#打开二进制日志功能
log-bin=mysql-bin
#关闭只读,可读可写
read-only=0 
#指定mysql的binlog日志记录哪个db
binlog-do-db=gulimall_ums 
binlog-do-db=gulimall_pms

#用来设置不需要同步的库,此处指定的mysql系统库
replicate-ignore-db=mysql 
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema

3.3.2.4 重启 master

docker restart mysql-master

3.3.3 从节点创建
3.3.3.1 创建  mysql-slaver-01 实例并启动

docker run -p 3317: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/conf.d/ \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7 

#Centos7安全Selinux禁止了一些安全权限,导致mysql和mariadb在进行挂载/var/lib/mysql的时候会提示- 简书 (jianshu.com)
--privileged=true # 查看日志 docker logs mysql-master docker logs mysql-slaver-01 # 建立错误可以删除 docker rm container_ID

3.3.3.2 修改 slaver-01基本配置

vim /mydata/mysql/slaver/conf/my.cnf

为 my.cnf 写入一下配置 基本上就是设置字符集

[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

3.3.3.3 添加 master 主从复制部分配置

# 注意此处id不能与主节点相同
server_id=2 
log-bin=mysql-bin

read-only=1
binlog-do-db=gulimall_ums
binlog-do-db=gulimall_pms

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

3.3.3.4 重启 slaver-01

docker restart mysql-slaver-01

补:打开另一台宿主机 创建 mysql-slaver-02 实例 并启动

docker run -d \
--name mysql-slaver-02 \
--privileged=true \-p 3308:3306 \
-v /mydata/mysql/slaver2/log:/var/log/mysql \
-v /mydata/mysql/slaver2/data:/var/lib/mysql \
-v /mydata/mysql/slaver2/conf:/etc/mysql/conf.d/ \
-e MYSQL_ROOT_PASSWORD=root \
mysql:5.7; 

# 查看容器 STATUS : Up 
docker ps -a
# 如果需要删除容器
docker rm mysql-slaver-02
# 查看容器 IP
docker inspect --format='{{.NetworkSettings.IPAddress}}' mysql-slaver-02      
172.17.0.2

vim /mydata/mysql/slaver2/conf/my.cnf
[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
or
#进入容器
docker exec
-it mysql-slave-02 /bin/bash
uname -r
yum install
vim vim
/etc/my.cnf
[client] port
=3306 default-character-set=utf8mb4 [mysql] default-character-set=utf8mb4 [mysqld] port=3306 # 同一局域网内注意要唯一 server_id=72 character-set-server = utf8mb4 collation-server=utf8_unicode_ci default-time_zone = '+8:00' # 开启二进制日志功能,以备 Slave 作为其它 Slave 的 Master 时使用 log-bin=mysql-slave-bin # relay_log 配置中继日志 relay_log=mysql-relay-bin init_connect='SET collation_connection = utf8_unicode_ci' init_connect='SET NAMES utf8' read-only=1 skip-character-set-client-handshake skip-name-resolve binlog-do-db=gulimall_ums binlog-do-db=gulimall_pms replicate-ignore-db=mysql replicate-ignore-db=sys replicate-ignore-db=information_schema replicate-ignore-db=performance_schema :wq #登入 mysql mysql -p root # 授权远程访问 grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option; # 刷新权限 flush privileges; exit; --systemctl restart mysqld;

 

3.3.4 为 master 授权用户来他的同步数据
3.3.4.1 进入docker容器

docker exec -it mysql-master /bin/bash

3.3.4.2 登入mysql

mysql –uroot -p
Enter password: root

3.3.4.2.1 授权 root 可以远程访问 ( 主从无关, 为了方便我们远程连接 mysql)

#授权
grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option;
#刷新权限
flush privileges;

3.3.4.2.2 添加用来同步的用户

grant replication slave on *.* to 'backup'@'%' identified by '123456';
backup 为账号名,123456 为账号密码

3.3.4.3 查看 master 状态

show master status;
exit;
exit;

3.3.5 为 slaver-01授权用户来他的同步数据
3.3.5.1 进入docker容器

docker exec -it mysql-slaver-01 /bin/bash

3.3.5.2 登入mysql

mysql –uroot -p
Enter password: root

3.3.5.2.1 授权 root 可以远程访问 ( 主从无关, 为了方便我们远程连接 mysql)

#授权
grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option;
#刷新权限
flush privileges;
exit;
exit;

查询容器IP    (容器名字 或  容器ID)

docker inspect --format='{{.NetworkSettings.IPAddress}}' mysql-master
docker inspect --format='{{.NetworkSettings.IPAddress}}' mysql-slaver-01

3.3.5.2.2 设置主库连接

# 使用容器插座 IP:PORT
change master to master_host='172.17.0.2', master_user='backup', master_password='123456', master_log_file='mysql-bin.000004', master_log_pos=0, master_port=3306,
master_connect_retry=6;
# 使用映射时,用宿主机插座 IP:PORT
change master to master_host='192.168.1.46', master_user='backup', master_password='123456', master_log_file='mysql-bin.000004', master_log_pos=0, master_port=3307,
master_connect_retry=3;
master_host: 主节点虚拟机ip
master_user: 用于连接主机的集群账号
master_password: 用于连接主机的集群密码
master_log_file 必须与3.3.4.3的File字段一致,
Position 不用一致后面会自动同步

3.3.5.2.3 启用主从同步

start slave;
# 需要停止时
stop slave;

3.3.5.3 查看从库状态

show slave status
show slave status \G

原文链接:https://blog.csdn.net/qq_31745863/article/details/122819194

posted @ 2022-08-15 17:42  CHANG_09  阅读(449)  评论(0编辑  收藏  举报