mysql8.0.3集群搭建

news/2025/1/15 12:36:17/

下载mysql安装包:

https://dev.mysql.com/downloads/mysql/5.7.html#downloads

准备环境

1、准备三台服务器并设置hosts

192.168.236.143 mysql1
192.168.236.144 mysql2
192.168.236.145 mysql3

2、设置免密登陆

#生成秘钥
ssh-keygen -t rsa
#一直按Enter即可
#将秘钥分别拷贝到另外两个机器
ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.236.144
ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.236.145
#测试
ssh 192.168.236.143

3、关闭防火墙

#关闭防火墙
systemctl stop firewalld
#禁用防火墙
systemctl disable firewalld

4、关闭selinux

vi /etc/selinux/config
#设置
SELINUX=disabled

重启服务器

安装mysql

#解压
[root@mysql1 data]# tar -xvf mysql-cluster-community-8.0.34-1.el7.x86_64.rpm-bundle.tar 
mysql-cluster-community-client-8.0.34-1.el7.x86_64.rpm
mysql-cluster-community-client-plugins-8.0.34-1.el7.x86_64.rpm
mysql-cluster-community-common-8.0.34-1.el7.x86_64.rpm
mysql-cluster-community-data-node-8.0.34-1.el7.x86_64.rpm
mysql-cluster-community-debuginfo-8.0.34-1.el7.x86_64.rpm
mysql-cluster-community-devel-8.0.34-1.el7.x86_64.rpm
mysql-cluster-community-embedded-compat-8.0.34-1.el7.x86_64.rpm
mysql-cluster-community-icu-data-files-8.0.34-1.el7.x86_64.rpm
mysql-cluster-community-java-8.0.34-1.el7.x86_64.rpm
mysql-cluster-community-libs-8.0.34-1.el7.x86_64.rpm
mysql-cluster-community-libs-compat-8.0.34-1.el7.x86_64.rpm
mysql-cluster-community-management-server-8.0.34-1.el7.x86_64.rpm
mysql-cluster-community-ndbclient-8.0.34-1.el7.x86_64.rpm
mysql-cluster-community-ndbclient-devel-8.0.34-1.el7.x86_64.rpm
mysql-cluster-community-nodejs-8.0.34-1.el7.x86_64.rpm
mysql-cluster-community-server-8.0.34-1.el7.x86_64.rpm
mysql-cluster-community-server-debug-8.0.34-1.el7.x86_64.rpm
mysql-cluster-community-test-8.0.34-1.el7.x86_64.rpm

1、依次安装

 rpm -ivh mysql-cluster-community-common-8.0.34-1.el7.x86_64.rpm --nodeps --forcerpm -ivh mysql-cluster-community-libs-8.0.34-1.el7.x86_64.rpm --nodeps --forcerpm -ivh mysql-cluster-community-client-8.0.34-1.el7.x86_64.rpm --nodeps --forcerpm -ivh mysql-cluster-community-server-8.0.34-1.el7.x86_64.rpm --nodeps --force

2、配置mysql

#初始化mysql
mysqld --initialize
#给mysql分组中的mysql用户赋权限
chown mysql:mysql /var/lib/mysql -R
#启动mysql服务器
systemctl start mysqld.service
#设置开机启动mysql
systemctl enable mysqld

3、查看mysql初始化的密码

[root@mysql1 data]# cat /var/log/mysqld.log | grep password
2023-08-09T05:11:28.204159Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: )c=&)ea<<3,O

密码即是最后面的)c=&)ea<<3,O
4、修改数据库密码

#进入mysql
mysql -u root -p
#然后输入密码,即可进入mysql
#修改密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '1234';
#退出mysql
exit;
#创建账号名和密码
create user 'admin'@'%' identified with mysql_native_password by 'admin';
#授权远程链接
grant all privileges on *.* to 'admin'@'%' with grant option;
#刷新权限
flush privileges;

5、mysql服务命令

#查看mysql版本
mysql -V
#启动mysql服务
systemctl start mysqld.service
#停止mysql服务
systemctl stop mysqld.service
#重启mysql服务
systemctl restart mysqld.service
#查看mysql服务当前状态
systemctl status mysqld.service
#设置mysql服务开机自启动
systemctl enable mysqld.service
#停止mysql服务开机自启动
systemctl disable mysqld.service

安装mysql shell

下载:https://downloads.mysql.com/archives/shell/

#安装
rpm -ivh mysql-shell-8.0.33-1.el7.x86_64.rpm

集群配置

1、修改配置文件

vi /etc/my.cnf
#mysql1配置文件增加
server-id=1
gtid_mode=on
enforce_gtid_consistency=on
binlog_checksum=none
transaction_write_set_extraction = XXHASH64
loose-group_replication_recovery_use_ssl= ON
loose-group_replication_group_name = '5dbabbe6-8050-49a0-9131-1de449167446'
loose-group_replication_start_on_boot = OFFloose-group_replication_local_address = 'mysql1:24901'
loose-group_replication_group_seeds = 'mysql1:24901,mysql2:24902,mysql3:24903'
loose-group_replication_bootstrap_group = OFF#mysql2配置文件增加
server-id=2
gtid_mode=on
enforce_gtid_consistency=on
binlog_checksum=none
transaction_write_set_extraction = XXHASH64
loose-group_replication_recovery_use_ssl= ON
loose-group_replication_group_name = '5dbabbe6-8050-49a0-9131-1de449167446'
loose-group_replication_start_on_boot = OFFloose-group_replication_local_address = 'mysql2:24901'
loose-group_replication_group_seeds = 'mysql1:24901,mysql2:24902,mysql3:24903'
loose-group_replication_bootstrap_group = OFF#mysql3配置文件增加
server-id=3
gtid_mode=on
enforce_gtid_consistency=on
binlog_checksum=none
transaction_write_set_extraction = XXHASH64
loose-group_replication_recovery_use_ssl= ON
loose-group_replication_group_name = '5dbabbe6-8050-49a0-9131-1de449167446'
loose-group_replication_start_on_boot = OFFloose-group_replication_local_address = 'mysql3:24901'
loose-group_replication_group_seeds = 'mysql1:24901,mysql2:24902,mysql3:24903'
loose-group_replication_bootstrap_group = OFF

2、启动mysql并使用shell连接

#启动mysql
systemctl start mysqld.service
#在每个服务器上都是用shell连接mysql
mysqlsh admin@localhost:3306
#每个服务器都执行
dba.configureInstance();
dba.checkInstanceConfiguration("admin@localhost:3306");

3、进入mysql安装组复制插件

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

4、进入mysql shell创建集群(注意只在其中一台服务器上执行即可)

 MySQL  localhost:3306 ssl  JS > var cluster = dba.createCluster('myCluster');

5、添加节点

 MySQL  localhost:3306 ssl  JS > cluster.addInstance('admin@mysql1:3306');#创建节点即添加,可以不执行MySQL  localhost:3306 ssl  JS > cluster.addInstance('admin@mysql2:3306');MySQL  localhost:3306 ssl  JS > cluster.addInstance('admin@mysql3:3306');

6、查看当前节点状态

 MySQL  localhost:3306 ssl  JS > cluster.status()
{"clusterName": "myCluster", "defaultReplicaSet": {"name": "default", "primary": "mysql1:3306", "ssl": "REQUIRED", "status": "OK", "statusText": "Cluster is ONLINE and can tolerate up to ONE failure.", "topology": {"mysql1:3306": {"address": "mysql1:3306", "memberRole": "PRIMARY", "mode": "R/W", "readReplicas": {}, "replicationLag": "applier_queue_applied", "role": "HA", "status": "ONLINE", "version": "8.0.34"}, "mysql2:3306": {"address": "mysql2:3306", "memberRole": "SECONDARY", "mode": "R/O", "readReplicas": {}, "replicationLag": "applier_queue_applied", "role": "HA", "status": "ONLINE", "version": "8.0.34"}, "mysql3:3306": {"address": "mysql3:3306", "memberRole": "SECONDARY", "mode": "R/O", "readReplicas": {}, "replicationLag": "applier_queue_applied", "role": "HA", "status": "ONLINE", "version": "8.0.34"}}, "topologyMode": "Single-Primary"}, "groupInformationSourceMember": "mysql1:3306"
}

7、查看节点信息,任意节点即可

mysql> select * from performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION | MEMBER_COMMUNICATION_STACK |
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
| group_replication_applier | 2ef5a678-3673-11ee-b7fb-000c29d4f7b1 | mysql2      |        3306 | ONLINE       | SECONDARY   | 8.0.34         | MySQL                      |
| group_replication_applier | 2efc06f0-3673-11ee-8284-000c29b47d6f | mysql1      |        3306 | ONLINE       | PRIMARY     | 8.0.34         | MySQL                      |
| group_replication_applier | 2f144a98-3673-11ee-b663-000c2973412e | mysql3      |        3306 | ONLINE       | SECONDARY   | 8.0.34         | MySQL                      |
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
3 rows in set (0.00 sec)

安装mysql router

下载地址:https://downloads.mysql.com/archives/router/

# 安装
rpm -ivh mysql-router-community-8.0.33-1.el7.x86_64.rpm 
#查看mysqlrouter版本
mysqlrouter --version

配置router

vim /etc/mysqlrouter/mysqlrouter.conf
#增加如下配置
[routing:secondary]
bind_address = 0.0.0.0
bind_port = 7001
destinations = mysql1:3306,mysql2:3306,mysql3:3306
routing_strategy = round-robin[routing:primary]
bind_address = 0.0.0.0
bind_port = 7002
destinations = mysql1:3306,mysql2:3306,mysql3:3306
routing_strategy = first-available

启动mysql router

mysqlrouter -c /etc/mysqlrouter/mysqlrouter.conf &

测试连接情况

#连接mysql
mysql -h 192.168.236.145 -P 7001 -uadmin -padmin
mysql -h 192.168.236.145 -P 7002 -uadmin -padmin
#执行命令获取Mysql中配置的server_id,每次执行返回的server_id不一样,因为使用轮询策略
mysql -h192.168.236.145 -uadmin -padmin -e "select @@server_id" -P 7001
#执行命令获取Mysql中配置的server_id,每次执行返回的server_id都一样
mysql -h192.168.236.145 -uadmin -padmin -e "select @@server_id" -P 7002

查看监听状态

[root@mysql3 data]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name           
tcp6       0      0 ::1:7001                :::*                    LISTEN      13179/mysqlrouter   
tcp6       0      0 ::1:7002                :::*                    LISTEN      13179/mysqlrouter   

测试

使用工具连接测试,关掉其中一台数据库仍然可以连接
通过mysql命令查询,primary已经切换了

mysql> select * from performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION | MEMBER_COMMUNICATION_STACK |
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
| group_replication_applier | 2ef5a678-3673-11ee-b7fb-000c29d4f7b1 | mysql2      |        3306 | ONLINE       | PRIMARY     | 8.0.34         | MySQL                      |
| group_replication_applier | 2efc06f0-3673-11ee-8284-000c29b47d6f | mysql1      |        3306 | ONLINE       | SECONDARY   | 8.0.34         | MySQL                      |
| group_replication_applier | 2f144a98-3673-11ee-b663-000c2973412e | mysql3      |        3306 | ONLINE       | SECONDARY   | 8.0.34         | MySQL                      |
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
3 rows in set (0.05 sec)

http://www.ppmy.cn/news/1021828.html

相关文章

Kubernetes入门 三、命令行工具 kubectl

目录 语法操作示例资源操作Pod 与集群资源类型与别名格式化输出 kubectl 是 Kubernetes 集群的命令行工具&#xff0c;通过它能够对集群本身进行管理&#xff0c;并能够在集群上进行容器化应用的安装和部署。 语法 使用以下语法从终端窗口运行 kubectl 命令&#xff1a; kub…

第十五章、【Linux】例行性工作调度

15.1 什么是例行性工作调度 在不考虑硬件与服务器的链接状态下&#xff0c;Linux可以帮助提醒许多任务。Linux调度就是通过crontab与at这两个东西。 15.1.1 Linux工作调度的种类&#xff1a;at,cron 从上面的说明当中&#xff0c;我们可以很清楚的发现两种工作调度的方式&am…

将应用设置成系统App/获取Android设备SN号

1&#xff0c;和系统签名一致&#xff1b;&#xff08;签名设置签名文件&#xff09; 2&#xff0c;配置Manifest 至此你的App就是一个系统App了&#xff0c;可以执行一些系统App才能有的操作&#xff0c;如获取机器SN号&#xff1a; public String getSerialNumber() {Strin…

智慧工地源码,Spring Cloud+ Vue+UniApp开发,微服务架构

智慧工地源码&#xff0c;智慧工地云平台源码 智慧工地APP源码 智慧工地的核心是数字化&#xff0c;它通过传感器、监控设备、智能终端等技术手段&#xff0c;实现对工地各个环节的实时数据采集和传输&#xff0c;如环境温度、湿度、噪音等数据信息&#xff0c;将数据汇集到云…

前端高级面试题-JS

1. 原型 / 构造函数 / 实例 原型( prototype ): ⼀个简单的对象&#xff0c;⽤于实现对象的 属性继承。可以简单的理解成对象的爹。在 Firefox 和 Chrome 中&#xff0c;每个 JavaScript 对象中都包含⼀个__proto__ (⾮标准)的属性指向它爹(该对象的原型)&#xff0c;可 obj.p…

整理mongodb文档:改

个人博客 整理mongodb文档:改 求关注&#xff0c;求批评&#xff0c;求进步 文章概叙 本文主要讲的是mongodb的updateOne以及updateMany&#xff0c;主要还是在shell下进行操作&#xff0c;也讲解下主要的参数upsert以及更新的参数。 数据准备 本次需要准备的数据不是很多…

git checkout -b

创建分支&#xff1a; $ git branch mybranch 切换分支&#xff1a; $ git checkout mybranch 创建并切换分支&#xff1a; $ git checkout -b mybranch 更新master主线上的东西到该分支上&#xff1a;$git rebase master 切换到master分支&#xff1a;$git checkout master …

Tubi 前端测试:迁移 Enzyme 到 React Testing Library

前端技术发展迅速&#xff0c;即便不说是日新月异&#xff0c;每年也都推出新框架和新技术。Tubi 的产品前端代码仓库始建于 2015 年&#xff0c;至今 8 年有余。可喜的是&#xff0c;多年来紧随 React 社区的发展&#xff0c;Tubi 绝大多数的基础框架选型都遵循了社区流行的最…