022、Python+fastapi,第一个Python项目走向第22步:ubuntu 24.04 docker 安装mysql8集群、redis集群(三)

这次来安装mysql8了,以前安装不是docker安装,这个我也是第一次,人人都有第一次嚒


前言

前面的redis安装还是花了点时间的,主要是网上教程,各有各的好,大家千万别取其长处,个人觉得这个环境影响很大?反正我照着装很多也不能安装完成。

这次安装mysql希望快点,不过也没关系,陪娃做作业,我就安装这玩意

我们直接安装集群。看时间允许,我们也安装个单机版的


一、MGR简介

确保数据库稳定运行是DBA核心价值之一。通过搭建灾备库,利用复制技术同步主库的数据更新,在主库不可用时,启用备库可以快速恢复数据库服务,减少对业务的影响,同时,备库也可用于负载均衡、数据备份、统计报表等场景。带来诸多好处的同时,自然也有其弊端:增加了额外的硬件、维护成本,以及复制中主从数据延迟或不一致等多种问题。MySQL在3.23.15版本中新增了复制功能,接下来是一系列的增强和优化:row based,半同步复制,GTID,多线程,多源复制,组复制等,本文使用2016年底官方GA的Group Replicaion(下文简称MGR)

架构图:

  • MGR(MySQL Group Replication)是MySQL 5.7.17提出的集群方案,它既可以很好地保证数据一致性,又具备故障检测和转移功能,MGR还支持多节点写入,这是一项被普遍看好的技术。
  • MGR(MySQL Group Replication)是MySQL自带的一个插件,可以灵活部署。MySQL MGR集群是多个MySQL Server节点共同组成的分布式集群,每个Server都有完整的副本,它是基于行(ROW)格式的二进制日志文件。
  • MGR是一个多主结构,通过组复制(Group Replication)机制实现各节点数据的一致性,这与Replication的异步复制、PXC的同步复制都不相同。
  • 如图所示,DB1、DB2、DB3构成的MGR集群,集群中每个DB都有MGR层,MGR层功能也可简单理解为由Paxos模块和冲突检测Certify模块实现
  • Paxos模块基于Paxos算法,确保所有节点收到相同广播消息,Transactionmessage(交易信息)就是广播消息的内容结构;冲突检测Certify(保证)模块进行冲突检测确保数据的最终一致性。
  • 当DB1上有事务T1要执行时,T1对DB1来说是本地事务,对于DB2、DB3来说是远端事务;DB1在事务T1被执行后,会把执行事务T1的信息广播给集群中各个节点,包括DB1本身;通过Paxos模块广播给MGR集群中的各个节点,半数以上的节点同意并且达成共识,之后共识信息进入各个节点的冲突检测Certify模块,各个节点各自进行冲突检测验证,最终保证事务在集群中的最终一致性。
  • 在冲突检测通过之后,本地事务T1在DB1直接提交即可,否则直接回滚。远端事务T1在DB2和DB3分别先更新到relaylog,然后应用到binlog,完成数据的同步,否则直接放弃该事务。
  • 优点:避免脑裂:MGR中不会出现脑裂的现象;数据一致性保障:MGR的冗余能力很好,能够保证Binlog Event至少被复制到超过一半的成员上,只要同时宕机的成员不超过半数便不会导致数据丢失

二、集群安装

1、环境

victor_t400@victort400:/home$ uname -a

Linux victort400 6.8.0-31-generic #31-Ubuntu SMP PREEMPT_DYNAMIC Sat Apr 20 00:40:06 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

victor_t400@victort400:/home$ docker --version

Docker version 24.0.7, build 24.0.7-0ubuntu4

2、拉取镜像

victor_t400@victort400:/home$ docker pull mysql:8.0.20

8.0.20: Pulling from library/mysql

Digest: sha256:8b7b328a7ff6de46ef96bcf83af048cb00a1c86282bfca0cb119c84568b4caf6

Status: Downloaded newer image for mysql:8.0.20

docker.io/library/mysql:8.0.20

victor_t400@victort400:/home$ docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

mysql latest 8251f0669c6e 3 days ago 623MB

redis latest 9509c4dd19fb 4 weeks ago 116MB
mysql 8.0.20 be0dbf01a0f3 3 years ago 541MB

3、创建容器网络

victor_t400@victort400:/home$ docker network create --subnet=172.72.0.0/24 mysql-net

96ab815083147c7bdba564c92585ee6d0b64e923beea43844f1c8c9eb08a0b71

victor_t400@victort400:/home$docker network ls

NETWORK ID NAME DRIVER SCOPE

90725f91b3dc bridge bridge local

d009f7a9343b host host local
96ab81508314 mysql-net bridge local

f9c89145d9ab none null local

a09e80dd0716 redis-net bridge local

4、创建mysql的数据目录

bash 复制代码
sudo mkdir -p /home/mysql-mgr/mysql-mgr-master/conf.d
sudo mkdir -p /home/mysql-mgr/mysql-mgr-master/data
sudo mkdir -p /home/mysql-mgr/mysql-mgr-slave01/conf.d
sudo mkdir -p /home/mysql-mgr/mysql-mgr-slave01/data
sudo mkdir -p /home/mysql-mgr/mysql-mgr-slave02/conf.d
sudo mkdir -p /home/mysql-mgr/mysql-mgr-slave02/data

5、拉起容器

密码设置不要用123456,这个就可以 LzloveYou123!

bash 复制代码
docker run -d --name mysql-mgr-master -h mysql-mgr-master \
   -p 13066:3306 -p 33011:33011 \
   --net=mysql-net --ip 172.72.0.101 \
   -v /home/mysql-mgr/mysql-mgr-master/conf.d:/etc/mysql/conf.d \
   -v /home/mysql-mgr/mysql-mgr-master/data:/var/lib/mysql/ \
   -e MYSQL_ROOT_PASSWORD=123456 \
   -e TZ=Asia/Shanghai \
   --privileged=true mysql:8.0.20
bash 复制代码
docker run -d --name mysql-mgr-slave01 -h mysql-mgr-slave01 \
   -p 23066:3306 -p 33012:33012 \
   --net=mysql-net --ip 172.72.0.102 \
   -v /home/mysql-mgr/mysql-mgr-slave01/conf.d:/etc/mysql/conf.d \
   -v /home/mysql-mgr/mysql-mgr-slave01/data:/var/lib/mysql/ \
   -e MYSQL_ROOT_PASSWORD=123456 \
   -e TZ=Asia/Shanghai \
   --privileged=true mysql:8.0.20
bash 复制代码
docker run -d --name mysql-mgr-slave02 -h mysql-mgr-slave02 \
   -p 33066:3306 -p 33013:33013 \
   --net=mysql-net --ip 172.72.0.103 \
   -v /home/mysql-mgr/mysql-mgr-slave02/conf.d:/etc/mysql/conf.d \
   -v /home/mysql-mgr/mysql-mgr-slave02/data:/var/lib/mysql/ \
   -e MYSQL_ROOT_PASSWORD=123456 \
   -e TZ=Asia/Shanghai \
   --privileged=true mysql:8.0.20

victor_t400@victort400:/home/mysql-mgr$ docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

26b69bc2a374 mysql "docker-entrypoint.s..." 4 seconds ago Up 2 seconds 0.0.0.0:33011->33011/tcp, :::33011->33011/tcp, 33060/tcp, 0.0.0.0:13066->3306/tcp, :::13066->3306/tcp mysql-mgr-master

ee9ac39c7cf9 mysql "docker-entrypoint.s..." 2 minutes ago Up 2 minutes 0.0.0.0:33012->33012/tcp, :::33012->33012/tcp, 33060/tcp, 0.0.0.0:23066->3306/tcp, :::23066->3306/tcp mysql-mgr-slave01

20f7e28c414e mysql "docker-entrypoint.s..." 2 minutes ago Up 2 minutes 0.0.0.0:33013->33013/tcp, :::33013->33013/tcp, 33060/tcp, 0.0.0.0:33066->3306/tcp, :::33066->3306/tcp mysql-mgr-slave02

这样就建了3个docker容器节点,运行了起来

6、修改配置mysql参数配置文件

a、删除旧的配置文件

bash 复制代码
sudo rm -rf /home/mysql-mgr/mysql-mgr-master/conf.d/my.cnf
sudo rm -rf /home/mysql-mgr/mysql-mgr-slave01/conf.d/my.cnf
sudo rm -rf /home/mysql-mgr/mysql-mgr-slave02/conf.d/my.cnf

b、mysql-mgr集群master配置文件

bash 复制代码
sudo touch /home/mysql-mgr/mysql-mgr-master/conf.d/my.cnf
sudo chmod 777 /home/mysql-mgr/mysql-mgr-master/conf.d/my.cnf
sudo cat > /home/mysql-mgr/mysql-mgr-master/conf.d/my.cnf <<"EOF"
[mysqld]
user=mysql
port=3306
character_set_server=utf8mb4
secure_file_priv=''
server-id = 2024050401
default-time-zone = '+8:00'
 
log_timestamps = SYSTEM
log-bin = 
binlog_format=row
binlog_checksum=NONE
log-slave-updates=1
skip-name-resolve
auto-increment-increment=2
auto-increment-offset=1
gtid-mode=ON
enforce-gtid-consistency=on
default_authentication_plugin=mysql_native_password
max_allowed_packet = 500M
 
master_info_repository=TABLE
relay_log_info_repository=TABLE
relay_log=mysql-mgr-master-relay-bin-ip101
 
 
transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
loose-group_replication_start_on_boot=OFF
loose-group_replication_local_address= "172.72.0.101:33011"
loose-group_replication_group_seeds= "172.72.0.101:33011,172.72.0.102:33012,172.72.0.103:33013"
loose-group_replication_bootstrap_group=OFF
loose-group_replication_ip_whitelist="172.72.0.101,172.72.0.102,172.72.0.103"
 
report_host=172.72.0.101
report_port=3306
 
EOF

c、 mysql-mgr集群slave01配置文件

bash 复制代码
sudo touch /home/mysql-mgr/mysql-mgr-slave01/conf.d/my.cnf
sudo chmod 777 /home/mysql-mgr/mysql-mgr-slave01/conf.d/my.cnf
sudo cat >  /home/mysql-mgr/mysql-mgr-slave01/conf.d/my.cnf <<"EOF"
[mysqld]
user=mysql
port=3306
character_set_server=utf8mb4
secure_file_priv=''
server-id = 2024050402
default-time-zone = '+8:00'
log_timestamps = SYSTEM
log-bin = 
binlog_format=row
binlog_checksum=NONE
log-slave-updates=1
gtid-mode=ON
enforce-gtid-consistency=ON
skip_name_resolve
default_authentication_plugin=mysql_native_password
max_allowed_packet = 500M
 
master_info_repository=TABLE
relay_log_info_repository=TABLE
relay_log=mysql-mgr-slave-relay-bin-ip102
 
 
transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
loose-group_replication_start_on_boot=OFF
 
loose-group_replication_local_address= "172.72.0.102:33012"
loose-group_replication_group_seeds= "172.72.0.101:33011,172.72.0.102:33012,172.72.0.103:33013"
 
loose-group_replication_bootstrap_group=OFF
loose-group_replication_ip_whitelist="172.72.0.101,172.72.0.102,172.72.0.103"
 
report_host=172.72.0.102
report_port=3306
 
EOF
 

d、 mysql-mgr集群slave02配置文件

bash 复制代码
sudo touch /home/mysql-mgr/mysql-mgr-slave02/conf.d/my.cnf
sudo chmod 777 /home/mysql-mgr/mysql-mgr-slave02/conf.d/my.cnf
sudo cat > /home/mysql-mgr/mysql-mgr-slave02/conf.d/my.cnf <<"EOF"
[mysqld]
user=mysql
port=3306
character_set_server=utf8mb4
secure_file_priv=''
server-id = 2024050403
default-time-zone = '+8:00'
log_timestamps = SYSTEM
log-bin = 
binlog_format=row
binlog_checksum=NONE
log-slave-updates=1
gtid-mode=ON
enforce-gtid-consistency=ON
skip_name_resolve
default_authentication_plugin=mysql_native_password
max_allowed_packet = 500M
 
 
master_info_repository=TABLE
relay_log_info_repository=TABLE
relay_log=mysql-mgr-slave-relay-bin-ip103
 
 
transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
loose-group_replication_start_on_boot=OFF
loose-group_replication_local_address= "172.72.0.103:33013"
loose-group_replication_group_seeds= "172.72.0.101:33011,172.72.0.102:33012,172.72.0.103:33013"
loose-group_replication_bootstrap_group=OFF
loose-group_replication_ip_whitelist="172.72.0.101,172.72.0.102,172.72.0.103"
 
report_host=172.72.0.103
report_port=3306
 
EOF
bash 复制代码
sudo chmod 644 /home/mysql-mgr/mysql-mgr-master/conf.d/my.cnf
sudo chmod 644 /home/mysql-mgr/mysql-mgr-slave01/conf.d/my.cnf
sudo chmod 644 /home/mysql-mgr/mysql-mgr-slave02/conf.d/my.cnf

7、重启容器

bash 复制代码
docker restart `docker ps -a |grep mysql-mgr |awk -F " " '{print $1}'`

结果:

victor_t400@victort400:/home$ docker restart `docker ps -a |grep mysql-mgr |awk -F " " '{print $1}'`

3bba9175355e

329c7a28c17c

68df336dad9b

8、进入容器,配置集群

a、进入主节点

红色部分代表要操作的

进入主节点容器

victor_t400@victort400:/home$ docker exec -it mysql-mgr-master /bin/bash

登录mysql

root@mysql-mgr-master:/# mysql -uroot -p

输入密码
Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 8

Server version: 8.0.20 MySQL Community Server - GPL

Copyright (c) 2000, 2020, 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.

查看hostname,server_id和server_uuid

mysql> select @@hostname,@@server_id,@@server_uuid;

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

| @@hostname | @@server_id | @@server_uuid |

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

| mysql-mgr-master | 2024050401 | 54c9ae3b-09f8-11ef-9b8d-0242ac480065 |

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

1 row in set (0.01 sec)

主节点安装MGR插件

mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so';

Query OK, 0 rows affected (0.06 sec)

显示安装的插件

mysql> show plugins;

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

| Name | Status | Type | Library | License |

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

太多了,删除一部分

| mysqlx_cache_cleaner | ACTIVE | AUDIT | NULL | GPL |

| mysqlx | ACTIVE | DAEMON | NULL | GPL |

| group_replication | ACTIVE | GROUP REPLICATION | group_replication.so | GPL |

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

45 rows in set (0.01 sec)

这里开始设置复制账号(所有节点执行)

mysql> SET SQL_LOG_BIN=0;

Query OK, 0 rows affected (0.00 sec)

mysql> CREATE USER repl@'%' IDENTIFIED BY '123456';

Query OK, 0 rows affected (0.04 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO repl@'%';

Query OK, 0 rows affected (0.04 sec)

mysql> FLUSH PRIVILEGES;

Query OK, 0 rows affected (0.01 sec)

mysql> SET SQL_LOG_BIN=1;

Query OK, 0 rows affected (0.00 sec)

mysql> CHANGE MASTER TO MASTER_USER='repl', MASTER_PASSWORD='123456' FOR CHANNEL 'group_replication_recovery';

Query OK, 0 rows affected, 2 warnings (0.74 sec)

启动MGR主模式,启动MGR,在主库上执行

mysql> SET GLOBAL group_replication_bootstrap_group=ON;

Query OK, 0 rows affected (0.00 sec)

mysql> START GROUP_REPLICATION;

Query OK, 0 rows affected (4.43 sec)

mysql> SET GLOBAL group_replication_bootstrap_group=OFF;

Query OK, 0 rows affected (0.00 sec)

查看MGR组信息

mysql> SELECT * FROM performance_schema.replication_group_members;

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

| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION |

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

| group_replication_applier | 54c9ae3b-09f8-11ef-9b8d-0242ac480065 | 172.72.0.101 | 3306 | ONLINE | PRIMARY | 8.0.20 |

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

1 row in set (0.00 sec)

mysql>

重启了一下,就全关了,为什么啊为什么啊,前面拉起时,没有--restart always 设置,所以不自启动,后面我们再来设置,现在用下面的来

victor_t400@victort400:~$ docker start 26b69bc2a374

26b69bc2a374

victor_t400@victort400:~$ docker start ee9ac39c7cf9

ee9ac39c7cf9

victor_t400@victort400:~$ docker start 20f7e28c414e

20f7e28c414e

docker update --restart=always 容器名称

b、进入slave01节点

进入从节点01容器

victor_t400@victort400:/home$ docker exec -it mysql-mgr-slave01 /bin/bash

进入数据库

root@mysql-mgr-slave01:/# mysql -uroot -p

Enter password:在这里搞半天,输入密码不行,结果是不需要密码,不是明明我前面设置密码的,而且和主节点一模一样,主节点需要密码,他不要,你说见鬼了,

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 17

Server version: 8.0.20 MySQL Community Server - GPL

Copyright (c) 2000, 2020, 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.

slave01安装MGR插件

mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so';

Query OK, 0 rows affected (0.07 sec)

显示插件

mysql> show plugins;

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

| Name | Status | Type | Library | License |

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

| binlog | ACTIVE | STORAGE ENGINE | NULL | GPL |

。。。。。。。

| mysqlx_cache_cleaner | ACTIVE | AUDIT | NULL | GPL |

| mysqlx | ACTIVE | DAEMON | NULL | GPL |
| group_replication | ACTIVE | GROUP REPLICATION | group_replication.so | GPL |

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

45 rows in set (0.00 sec)

设置复制账号

mysql> SET SQL_LOG_BIN=0;

Query OK, 0 rows affected (0.00 sec)

mysql> CREATE USER repl@'%' IDENTIFIED BY '123456';

Query OK, 0 rows affected (0.09 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO repl@'%';

Query OK, 0 rows affected (0.06 sec)

mysql> FLUSH PRIVILEGES;

Query OK, 0 rows affected (0.00 sec)

mysql> SET SQL_LOG_BIN=1;

Query OK, 0 rows affected (0.00 sec)

mysql> CHANGE MASTER TO MASTER_USER='repl', MASTER_PASSWORD='123456' FOR CHANNEL 'group_replication_recovery';

Query OK, 0 rows affected, 2 warnings (0.64 sec)

加入MGR从节点01:

mysql> START GROUP_REPLICATION;

Query OK, 0 rows affected (5.02 sec)

查看MGR组信息,有两个了

mysql> SELECT * FROM performance_schema.replication_group_members;

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

| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION |

+---------------------------+--------------------------------------+--------------+-------------+--------------+-------------+----------------+
| group_replication_applier | 54c9ae3b-09f8-11ef-9b8d-0242ac480065 | 172.72.0.101 | 3306 | ONLINE | PRIMARY | 8.0.20 |
| group_replication_applier | 6607103a-09f8-11ef-bb93-0242ac480066 | 172.72.0.102 | 3306 | RECOVERING | SECONDARY | 8.0.20 |

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

2 rows in set (0.00 sec)

mysql>

c、进入进入slave02节点

和节点slave01一样,就不说明了,一步一步来好了,有些可以一起执行,但是一步步好像也不费什么事情

bash 复制代码
docker exec -it mysql-mgr-slave02 bash
--------------------------- 
mysql -uroot -p
-------------------------------
-----后面都是在mysql执行,可以一把梭
INSTALL PLUGIN group_replication SONAME 'group_replication.so';
show plugins;
SET SQL_LOG_BIN=0;
CREATE USER repl@'%' IDENTIFIED BY '123456';
GRANT REPLICATION SLAVE ON *.* TO repl@'%';
FLUSH PRIVILEGES;
SET SQL_LOG_BIN=1;
CHANGE MASTER TO MASTER_USER='repl', MASTER_PASSWORD='123456' FOR CHANNEL 'group_replication_recovery';
START GROUP_REPLICATION;
SELECT * FROM performance_schema.replication_group_members;

mysql> SELECT * FROM performance_schema.replication_group_members;

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

| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION |

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

| group_replication_applier | 54c9ae3b-09f8-11ef-9b8d-0242ac480065 | 172.72.0.101 | 3306 | ONLINE | PRIMARY | 8.0.20 |

| group_replication_applier | 6607103a-09f8-11ef-bb93-0242ac480066 | 172.72.0.102 | 3306 | ONLINE | SECONDARY | 8.0.20 |

| group_replication_applier | 6dedeb9f-09f8-11ef-8447-0242ac480067 | 172.72.0.103 | 3306 | ONLINE | SECONDARY | 8.0.20 |

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

3 rows in set (0.00 sec)

这里可以看到3个都online 下面就测试了

只有主节点可以写入,其他节点只读,MGR单主模式搭建成功。

9、测试一下

内网测试(docker内网,我们进入到容器里面测试的)

目前我们还在slave02节点,下面创建一个数据库,结果--super-read-only应该是只读的意思,不让写

mysql> CREATE DATABASE test_mgr CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

ERROR 1290 (HY000): The MySQL server is running with the --super-read-only option so it cannot execute this statement

那我们退出 slave02节点,进入slave01节点

victor_t400@victort400:/home$ docker exec -it mysql-mgr-slave01 /bin/bash

root@mysql-mgr-slave01:/# mysql -uroot -p

Enter password:我是直接回车

mysql> CREATE DATABASE test_mgr CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

ERROR 1290 (HY000): The MySQL server is running with the --super-read-only option so it cannot execute this statement

mysql>

那我们重新打开一个ssh终端 ,进入主节点,可以看到,同样代码,主节点是可以创建数据库的

victor_t400@victort400:~$ docker exec -it mysql-mgr-master /bin/bash

root@mysql-mgr-master:/# mysql -uroot -p

Enter password:这里要输密码

mysql> CREATE DATABASE test_mgr CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

Query OK, 1 row affected (0.34 sec)

mysql> SHOW DATABASES;

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

| Database |

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

| information_schema |

| mysql |

| performance_schema |

| sys |

| test_mgr |

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

5 rows in set (0.02 sec)

创建一张表

mysql> use test_mgr;

Database changed

mysql> create table bookuser(

-> id INT PRIMARY KEY AUTO_INCREMENT, -- id

-> username varchar(32), --

-> userphone char(11), --

-> userpassword varchar(45) --

-> );

Query OK, 0 rows affected (1.60 sec)

mysql> show tables;

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

| Tables_in_test_mgr |

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

| bookuser |

| employees |

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

2 rows in set (0.00 sec)

插入数据

bash 复制代码
insert into test_mgr.bookuser(id,username,userphone,userpassword) values (1,'Jack1','12345678911','good#111');
insert into test_mgr.bookuser(id,username,userphone,userpassword) values (2,'Jack2','12345678912','good#113');
insert into test_mgr.bookuser(id,username,userphone,userpassword) values (3,'Jack3','12345678913','good#112');
insert into test_mgr.bookuser(id,username,userphone,userpassword) values (4,'Jack4','12345678914','good#113');
insert into test_mgr.bookuser(id,username,userphone,userpassword) values (5,'Jack5','12345678915','good#144');
insert into test_mgr.bookuser(id,username,userphone,userpassword) values (6,'Jack6','12345678916','good#133');
insert into test_mgr.bookuser(id,username,userphone,userpassword) values (7,'Jack7','12345678917','good#144');
insert into test_mgr.bookuser(id,username,userphone,userpassword) values (8,'Jack8','12345678918','good#145');
insert into test_mgr.bookuser(id,username,userphone,userpassword) values (9,'Jack9','12345678919','good#112');
insert into test_mgr.bookuser(id,username,userphone,userpassword) values (10,'Jack10','12345678921','good#1178');

查询数据

mysql> select *from bookuser;

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

| id | username | userphone | userpassword |

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

| 2 | Jack2 | 12345678912 | good#113 |

| 3 | Jack3 | 12345678913 | good#112 |

| 4 | Jack4 | 12345678914 | good#113 |

| 5 | Jack5 | 12345678915 | good#144 |

| 6 | Jack6 | 12345678916 | good#133 |

| 7 | Jack7 | 12345678917 | good#144 |

| 8 | Jack8 | 12345678918 | good#145 |

| 9 | Jack9 | 12345678919 | good#112 |

| 10 | Jack10 | 12345678921 | good#1178 |

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

9 rows in set (0.00 sec)

我们刚才那个终端还没有关掉,过去测试一下;

在 mysql-mgr-slave01数据库中

显示数据库,可以看到有喔,这可是在主节点那边建的,说明同步过来了

mysql> show databases;

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

| Database |

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

| information_schema |

| mysql |

| performance_schema |

| sys |

| test_mgr |

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

5 rows in set (0.01 sec)

显示tables 有啊有

mysql> use test_mgr;

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> show tables;

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

| Tables_in_test_mgr |

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

| bookuser |

| employees |

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

2 rows in set (0.00 sec)

查询数据

mysql> select *from bookuser;

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

| id | username | userphone | userpassword |

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

| 2 | Jack2 | 12345678912 | good#113 |

| 3 | Jack3 | 12345678913 | good#112 |

| 4 | Jack4 | 12345678914 | good#113 |

| 5 | Jack5 | 12345678915 | good#144 |

| 6 | Jack6 | 12345678916 | good#133 |

| 7 | Jack7 | 12345678917 | good#144 |

| 8 | Jack8 | 12345678918 | good#145 |

| 9 | Jack9 | 12345678919 | good#112 |

| 10 | Jack10 | 12345678921 | good#1178 |

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

9 rows in set (0.00 sec)

进入slave02中,也是可以的啊

mysql> select *from test_mgr.bookuser;

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

| id | username | userphone | userpassword |

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

| 2 | Jack2 | 12345678912 | good#113 |

| 3 | Jack3 | 12345678913 | good#112 |

| 4 | Jack4 | 12345678914 | good#113 |

| 5 | Jack5 | 12345678915 | good#144 |

| 6 | Jack6 | 12345678916 | good#133 |

| 7 | Jack7 | 12345678917 | good#144 |

| 8 | Jack8 | 12345678918 | good#145 |

| 9 | Jack9 | 12345678919 | good#112 |

| 10 | Jack10 | 12345678921 | good#1178 |

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

9 rows in set (0.00 sec)

那我们集群安装就结束了,祝大家明天五一最后一天开开心心玩。

忘了,外网还没测试呢?用nvicat测试

先开放端口13066 23066 33066

sudo ufw allow 13066
sudo ufw allow 23066
sudo ufw allow 33066
sudo ufw reload

看看我们插入的数据还在。

10、还有个问题没解决

服务器重启后,mysql没启动,得设置下自启动

victor_t400@victort400:~$ docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

3bba9175355e mysql:8.0.20 "docker-entrypoint.s..." 2 hours ago Exited (0) About a minute ago mysql-mgr-slave02

329c7a28c17c mysql:8.0.20 "docker-entrypoint.s..." 2 hours ago Exited (0) About a minute ago mysql-mgr-slave01

68df336dad9b mysql:8.0.20 "docker-entrypoint.s..." 2 hours ago Exited (0) About a minute ago mysql-mgr-master

bash 复制代码
#注意容器id用自己的
docker start 3bba9175355e
docker update --restart=always 3bba9175355e
docker start 329c7a28c17c
docker update --restart=always 329c7a28c17c
docker start 68df336dad9b
docker update --restart=always 68df336dad9b

重启后,好了,完工

victor_t400@victort400:~$ docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

3bba9175355e mysql:8.0.20 "docker-entrypoint.s..." 2 hours ago Up 5 minutes 0.0.0.0:33013->33013/tcp, :::33013->33013/tcp, 33060/tcp, 0.0.0.0:33066->3306/tcp, :::33066->3306/tcp mysql-mgr-slave02

329c7a28c17c mysql:8.0.20 "docker-entrypoint.s..." 2 hours ago Up 5 minutes 0.0.0.0:33012->33012/tcp, :::33012->33012/tcp, 33060/tcp, 0.0.0.0:23066->3306/tcp, :::23066->3306/tcp mysql-mgr-slave01

68df336dad9b mysql:8.0.20 "docker-entrypoint.s..." 2 hours ago Up 5 minutes 0.0.0.0:33011->33011/tcp, :::33011->33011/tcp, 33060/tcp, 0.0.0.0:13066->3306/tcp, :::13066->3306/tcp mysql-mgr-master

总结

redis和mysql安装,以前都是现成的,或者别人安装好的,自己装有时候用宝塔,还有些像php的环境,很少自己装,集群更加没装过,这样感谢csdn的朋友们提供的教程,不过也花了我很多时间,版本问题才是最大的问题,很多不一样

相关推荐
Kai HVZ13 分钟前
python爬虫----爬取视频实战
爬虫·python·音视频
古希腊掌管学习的神15 分钟前
[LeetCode-Python版]相向双指针——611. 有效三角形的个数
开发语言·python·leetcode
赵钰老师16 分钟前
【R语言遥感技术】“R+遥感”的水环境综合评价方法
开发语言·数据分析·r语言
m0_7482448318 分钟前
StarRocks 排查单副本表
大数据·数据库·python
就爱学编程24 分钟前
重生之我在异世界学编程之C语言小项目:通讯录
c语言·开发语言·数据结构·算法
B站计算机毕业设计超人25 分钟前
计算机毕业设计PySpark+Hadoop中国城市交通分析与预测 Python交通预测 Python交通可视化 客流量预测 交通大数据 机器学习 深度学习
大数据·人工智能·爬虫·python·机器学习·课程设计·数据可视化
路人甲ing..28 分钟前
jupyter切换内核方法配置问题总结
chrome·python·jupyter
游客52039 分钟前
opencv中的常用的100个API
图像处理·人工智能·python·opencv·计算机视觉
Oneforlove_twoforjob1 小时前
【Java基础面试题025】什么是Java的Integer缓存池?
java·开发语言·缓存
emoji1111111 小时前
前端对页面数据进行缓存
开发语言·前端·javascript