使用 mkcert 本地部署启动了 TLS/SSL 加密通讯的 MongoDB 副本集和分片集群

news2025/2/14 1:28:59

在这里插入图片描述

MongoDB 是支持客户端与 MongoDB 服务器之间启用 TLS/SSL 进行加密通讯的, 对于 MongoDB 副本集和分片集群内部的通讯, 也可以开启 TLS/SSL 认证. 本文会使用 mkcert 创建 TLS/SSL 证书, 基于创建的证书, 介绍 MongoDB 副本集、分片集群中启动 TLS/SSL 通讯的方法.

我们将会在本地部署启用了 SSL/TLS 通讯的副本集、分片集群.

安装 mkcert 和 MongoDB

在介绍 MongoDB 副本集和 MongoDB 分片集群中启用 SSL/TLS 通讯前, 我们先在本地安装好 MongoDB 和 mkcert.

mkcert 是一个 Go 实现的命令行工具, 方便我们使用一行命令就创建好 TLS/SSL 证书. 这里我们以 Ubuntu Linux 为例子:

# 需要安装有 Go
go install filippo.io/mkcert@latest

你也可以参考 mkcert 文章中描述的安装方法进行安装: mkcert installation.

接下来我们安装 MongoDB Server 和 MongoDB Shell 命令行工具. 你可以在 https://www.mongodb.com/try/download/community 下载到对应的二进制 (mongod、mongos) 文件压缩包. 后续我们将会以 MongoDB@2.0.26 版本为例:

❯ mongod --version
db version v5.0.26
Build Info: {
    "version": "5.0.26",
    "gitVersion": "0b4f1ea980b5380a66425a90b414106a191365f4",
    "openSSLVersion": "OpenSSL 1.1.1f  31 Mar 2020",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "ubuntu2004",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

注意, 如果你使用了高本版的 MongoDB, 需要单独下载 MongoDB Shell 命令行客户端工具. 可以在这里下载 https://www.mongodb.com/try/download/shell.

接下来让我们看看如何在 MongoDB 中启用 TLS/SSL 通讯.

MongoDB 副本集中启用 TLS/SSL

让我们先看看怎么在副本集中启用 SSL/TLS.

  1. 第一步, 我们先使用 mkcert 生成待会 MongoDB 服务器 mongod 使用的证书
# 将 CA 证书存放在 mkcert 目录下
export CAROOT=$(pwd)/mkcert
# 安装 CA
mkcert -install
# 将证书和密钥合并, 后续 mongod 会使用到, 一般用来校验客户端使用的证书
cat mkcert/rootCA.pem mkcert/rootCA-key.pem > mkcert/CA.pem

# 生成 mongod 使用的服务器证书, 这个证书在通信的时候会传递给客户端校验合法性
mkcert -cert-file mongo-tls.crt -key-file mongo-tls.key localhost 127.0.0.1 ::1
# 同样, 合并证书和密钥
cat mongo-tls.crt mongo-tls.key > mongo-tls.pem

# 生成 mongo 客户端使用的证书, 这个证书后续不只用于客户端于服务器的通讯, 也用于副本集成员内部认证时使用
mkcert -client -cert-file mongo-tls-client.crt -key-file mongo-tls-client.key localhost 127.0.0.1 ::1
cat mongo-tls-client.crt mongo-tls-client.key > mongo-tls-client.pem
  1. 第二步, 我们使用上述生成的证书 pem 文件来启动副本集, 副本集各成员使用的配置文件如下:
❯ cat etc/primary.conf.yaml
replication:
  replSetName: mongo_replica_set

storage:
  dbPath: build/mongo_replica_set/mongodata_primary

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: logs/mongo_replica_set_mongod_primary.log
  verbosity: 0

# network interfaces
net:
  tls:
    mode: requireTLS
    CAFile: mkcert/CA.pem
    certificateKeyFile: mongo-tls.pem
    clusterFile: mongo-tls-client.pem # https://www.mongodb.com/docs/manual/tutorial/configure-ssl/#member-certificate-requirements
    allowConnectionsWithoutCertificates: true
  port: 47017
  bindIp: 127.0.0.1,localhost
  compression:
    compressors: zlib

# how the process runs
processManagement:
  fork: true
  timeZoneInfo: /usr/share/zoneinfo

# Member x.509 Certificate
# https://www.mongodb.com/docs/manual/tutorial/configure-x509-member-authentication/
security:
  clusterAuthMode: x509
❯ cat etc/secondary_a.conf.yaml 
replication:
  replSetName: mongo_replica_set

storage:
  dbPath: build/mongo_replica_set/mongodata_secondary_a

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: logs/mongo_replica_set_mongod_secondary_a.log
  verbosity: 0

# network interfaces
net:
  tls:
    mode: requireTLS
    CAFile: mkcert/CA.pem
    certificateKeyFile: mongo-tls.pem
    clusterFile: mongo-tls-client.pem # https://www.mongodb.com/docs/manual/tutorial/configure-ssl/#member-certificate-requirements
    allowConnectionsWithoutCertificates: true
  port: 47018
  bindIp: 127.0.0.1,localhost
  compression:
    compressors: zlib

# how the process runs
processManagement:
  fork: true
  timeZoneInfo: /usr/share/zoneinfo

# Member x.509 Certificate
# https://www.mongodb.com/docs/manual/tutorial/configure-x509-member-authentication/
security:
  clusterAuthMode: x509
❯ cat etc/secondary_b.conf.yaml
replication:
  replSetName: mongo_replica_set

storage:
  dbPath: build/mongo_replica_set/mongodata_secondary_b

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: logs/mongo_replica_set_mongod_secondary_b.log
  verbosity: 0

# network interfaces
net:
  tls:
    mode: requireTLS
    CAFile: mkcert/CA.pem
    certificateKeyFile: mongo-tls.pem
    clusterFile: mongo-tls-client.pem # https://www.mongodb.com/docs/manual/tutorial/configure-ssl/#member-certificate-requirements
    allowConnectionsWithoutCertificates: true
  port: 47019
  bindIp: 127.0.0.1,localhost
  compression:
    compressors: zlib

# how the process runs
processManagement:
  fork: true
  timeZoneInfo: /usr/share/zoneinfo

# Member x.509 Certificate
# https://www.mongodb.com/docs/manual/tutorial/configure-x509-member-authentication/
security:
  clusterAuthMode: x509

其中主节点(primary)监听的地址为 127.0.0.1:47017, 从节点监听的地址为 127.0.0.1:47018127.0.0.1:47019. 这是典型的 PSS 架构部署的 MongoDB 副本集, 网络拓扑如下:

在这里插入图片描述

我们使用 mongod 启用上述配置文件, 注意配置文件中 certificate 相关字段引用到的 mkcert 生成的配置文件, mongod 启用命令如下:

mkdir logs
mkdir build

mongod --config "etc/primary.conf.yaml"
mongod --config "etc/secondary_a.conf.yaml"
mongod --config "etc/secondary_b.conf.yaml"

# 初始化副本集
mongo --port 47017 --tls <<EOF
db.adminCommand({replSetInitiate: { 
  _id: "mongo_replica_set", 
  members: [
    { _id: 0, host: "127.0.0.1:47017", priority: 2}, 
    { _id: 1, host: "127.0.0.1:47018", priority: 1}, 
    { _id: 2, host: "127.0.0.1:47019", priority: 1} ],
  settings: {
    electionTimeoutMillis: 3000
  }
}})
EOF

启动完成后, 我们使用 MongoDB Shell 命令客户端尝试连接主 (primary) 节点 127.0.0.1:47017, 命令如下:

❯ mongo --port 47017
MongoDB shell version v5.0.26
connecting to: mongodb://127.0.0.1:47017/?compressors=disabled&gssapiServiceName=mongodb
Error: network error while attempting to run command 'isMaster' on host '127.0.0.1:47017'  :
connect@src/mongo/shell/mongo.js:372:17
@(connect):2:6
exception: connect failed
exiting with code 1

会看到连接会失败, 这是因为 MongoDB 服务器强制开启了 TLS/SSL 通讯, 配置文件中相关字段如下:

net:
  tls:
    mode: requireTLS

这时候 mongo 客户端连接的使用需要走 TLS/SSL, 命令如下:

❯ mongo --port 47017 --tls
MongoDB shell version v5.0.26
connecting to: mongodb://127.0.0.1:47017/?compressors=disabled&gssapiServiceName=mongodb
{"t":{"$date":"2025-02-06T14:22:24.093Z"},"s":"I",  "c":"NETWORK",  "id":5490002, "ctx":"thread4","msg":"Started a new thread for the timer service"}
Implicit session: session { "id" : UUID("0a5698d1-81b5-4aee-800b-809da69baf58") }
MongoDB server version: 5.0.26
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
mongo_replica_set:PRIMARY>

可以看到我们能正常连接到副本集. 通过 tcpdump 能网络抓包工具, 我们可以看到通信流量是被加密过的. 接下来我们看看如何在 MongoDB 分片集群 (Sharding Cluster) 中启用 TLS/SSL.

MongoDB 分片集群中启用 TLS/SSL

接下来我们将本地部署的 MongoDB 分片集群拓扑大致如下, 其中两个 mongos、一个 config shard、一个数据分片 mongo shard a:

在这里插入图片描述

  1. 同样, 我们也需要生成 mongod、mongos、mongo 客户端使用的证书:
# 将 CA 证书存放在 mkcert 目录下
export CAROOT=$(pwd)/mkcert
# 安装 CA
mkcert -install
# 将证书和密钥合并, 后续 mongod 会使用到, 一般用来校验客户端使用的证书
cat mkcert/rootCA.pem mkcert/rootCA-key.pem > mkcert/CA.pem

# 生成 mongod 使用的服务器证书, 这个证书在通信的时候会传递给客户端校验合法性
mkcert -cert-file mongo-tls.crt -key-file mongo-tls.key localhost 127.0.0.1 ::1
# 同样, 合并证书和密钥
cat mongo-tls.crt mongo-tls.key > mongo-tls.pem

# 生成 mongo 客户端使用的证书, 这个证书后续不只用于客户端于服务器的通讯, 也用于副本集成员内部认证时使用
mkcert -client -cert-file mongo-tls-client.crt -key-file mongo-tls-client.key localhost 127.0.0.1 ::1
cat mongo-tls-client.crt mongo-tls-client.key > mongo-tls-client.pem
  1. 我们先启用 mongo config shard 集群配置分片, 一般用于存储集群的路由信息等数据, 主节点启动配置如下, clusterFile 字段指定了集群成员间内部认证使用的证书:
> cat etc/mongo_config_shard/mongo_cfg_primary.yaml
sharding:
  clusterRole: configsvr

replication:
  replSetName: config_shard_repl

storage:
  dbPath: build/config_shard_repl/mongodata_primary

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: logs/config_shard_repl_mongod_primary.log
  verbosity: 0

# network interfaces
net:
  tls:
    mode: requireTLS
    CAFile: mkcert/CA.pem
    certificateKeyFile: mongo-tls.pem
    clusterFile: mongo-tls-client.pem # https://www.mongodb.com/docs/manual/tutorial/configure-ssl/#member-certificate-requirements
    allowConnectionsWithoutCertificates: true
  port: 27017
  bindIp: localhost,127.0.0.1
  compression:
    compressors: zlib

# how the process runs
processManagement:
  fork: true
  timeZoneInfo: /usr/share/zoneinfo

# https://www.mongodb.com/docs/manual/tutorial/configure-x509-member-authentication/
security:
  clusterAuthMode: x509

从节点使用的配置可以在这里看到: ShardingCluster/etc/mongo_config_shard, 启动命令如下:

mongod --config "etc/mongo_config_shard/mongo_cfg_primary.yaml"
mongod --config "etc/mongo_config_shard/mongo_cfg_secondary_a.yaml"
mongod --config "etc/mongo_config_shard/mongo_cfg_secondary_b.yaml"

# 初始化副本集
mongo --port 27017 --tls <<EOF
db.adminCommand({replSetInitiate: { 
  _id: "config_shard_repl", 
  members: [
    { _id: 0, host: "127.0.0.1:27017", priority: 2}, 
    { _id: 1, host: "127.0.0.1:27018", priority: 1}, 
    { _id: 2, host: "127.0.0.1:27019", priority: 1} ],
  settings: {
    electionTimeoutMillis: 3000
  }
}})
EOF
  1. 启动数据分片 (mongo shard a), 这个分片一般用于存储业务数据, 实际的生产使用会有多个, 主从节点配置文件可以在 ShardingCluster/etc/mongo_shard_a 中找到, 与配置分片的各节点配置除访问地址外大致相同, 各节点启用命令如下:
mongod --config "etc/mongo_shard_a/mongo_cfg_primary.yaml"
mongod --config "etc/mongo_shard_a/mongo_cfg_secondary_a.yaml"
mongod --config "etc/mongo_shard_a/mongo_cfg_secondary_b.yaml"

# 初始化副本集
mongo --port 37017 --tls <<EOF
db.adminCommand({replSetInitiate: { 
  _id: "shard_a_repl", 
  members: [
    { _id: 0, host: "127.0.0.1:37017", priority: 2}, 
    { _id: 1, host: "127.0.0.1:37018", priority: 1}, 
    { _id: 2, host: "127.0.0.1:37019", priority: 1} ],
  settings: {
    electionTimeoutMillis: 3000
  }
}})
EOF
  1. 接下来我们通过如下配置启动 mongos 路由器, mongo 客户端一般通过 mongos 访问业务数据, mongos 的启用配置如下:
❯ cat etc/mongos/mongos_a_cfg.yaml 
# network interfaces
net:
  tls:
    mode: requireTLS
    CAFile: mkcert/CA.pem
    certificateKeyFile: mongo-tls.pem
    clusterFile: mongo-tls-client.pem # https://www.mongodb.com/docs/manual/tutorial/configure-ssl/#member-certificate-requirements
    allowConnectionsWithoutCertificates: true
  port: 27011
  bindIp: localhost,127.0.0.1
sharding:
  configDB: config_shard_repl/127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019
systemLog:
  destination: file
  logAppend: true
  path: logs/mongos_a.log
  verbosity: 0

# https://www.mongodb.com/docs/manual/tutorial/configure-x509-member-authentication/
security:
  clusterAuthMode: x509
❯ cat etc/mongos/mongos_b_cfg.yaml
# network interfaces
net:
  tls:
    mode: requireTLS
    CAFile: mkcert/CA.pem
    certificateKeyFile: mongo-tls.pem
    clusterFile: mongo-tls-client.pem # https://www.mongodb.com/docs/manual/tutorial/configure-ssl/#member-certificate-requirements
    allowConnectionsWithoutCertificates: true
  port: 27012
  bindIp: localhost,127.0.0.1
sharding:
  configDB: config_shard_repl/127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019
systemLog:
  destination: file
  logAppend: true
  path: logs/mongos_b.log
  verbosity: 0

# https://www.mongodb.com/docs/manual/tutorial/configure-x509-member-authentication/
security:
  clusterAuthMode: x509

mongos 启动命令如下:

mongos --config "etc/mongos/mongos_a_cfg.yaml"
mongos --config "etc/mongos/mongos_b_cfg.yaml"

# Cluster Member enable X503 authenticate, need auth access for db
mongo --port 27011 --tls <<EOF
use admin
db.createUser(
  {
    user: "mongo_super_user",
    pwd: "mongo_super_user_pwd",
    roles: [
      { role: "userAdminAnyDatabase", db: "admin" },
      { role: "readWriteAnyDatabase", db: "admin" },
      { role: "clusterAdmin", "db" : "admin" }
    ]
  }
)
EOF

# mongos 添加分片
mongo --port 27011 --tls --username mongo_super_user --password mongo_super_user_pwd <<EOF
sh.addShard( "shard_a_repl/127.0.0.1:37017,127.0.0.1:37018,127.0.0.1:37019")
EOF
mongo --port 27012 --tls --username mongo_super_user --password mongo_super_user_pwd <<EOF
sh.addShard( "shard_a_repl/127.0.0.1:37017,127.0.0.1:37018,127.0.0.1:37019")
EOF
  1. 待分片集群初始化完成后, 我们即可通过如下命令走 TLS/SSL 加密通讯访问分片集群数据:
mongo --port 27011 --tls --username mongo_super_user --password mongo_super_user_pwd <<EOF
show dbs;

quit();
EOF

Good~

结语

好了, 相信你跟着本篇文章成功在本地环境部署了开启 TLS/SSL 加密通讯的副本集或者 MongoDB 分片集群, 我已经将相关配置文件整理到了 GitHub 仓库中方便你后续快速参考使用, 访问地址为: https://github.com/yeshan333/mongo-deployment-with-tls.

git clone git@github.com:yeshan333/mongo-deployment-with-tls.git
cd /mongo-deployment-with-tls

bash run.sh ReplicaSet

参考

  • MongoDB configure-ssl

本文由博客一文多发平台 OpenWrite 发布!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2297643.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

P3372 【模板】线段树 1【题解2】

本题题解分两篇 此篇为第贰篇&#xff0c;用树状数组做 第壹篇&#xff1a;P3372 【模板】线段树 1【题解1】 本文讲解树状数组解决区间修改区间查询 其它树状数组相关文章&#xff1a; 树状数组讲解单点修改/查询树状数组解决区间修改单点查询 P3372 【模板】线段树 1 题…

使用 EDOT 监测由 OpenAI 提供支持的 Python、Node.js 和 Java 应用程序

作者&#xff1a;来自 Elastic Adrian Cole Elastic 很自豪地在我们的 Python、Node.js 和 Java EDOT SDK 中引入了 OpenAI 支持。它们为使用 OpenAI 兼容服务的应用程序添加日志、指标和跟踪&#xff0c;而无需任何代码更改。 介绍 去年&#xff0c;我们宣布了 OpenTelemetry…

CNN-BiGRU卷积神经网络双向门控循环单元多变量多步预测,光伏功率预测

CNN-BiGRU卷积神经网络双向门控循环单元多变量多步预测&#xff0c;光伏功率预测 代码下载&#xff1a;CNN-BiGRU卷积神经网络双向门控循环单元多变量多步预测&#xff0c;光伏功率预测 一、引言 1.1、研究背景及意义 随着全球能源危机和环境问题的日益严重&#xff0c;可再…

mysql8.0使用MGR实现高可用与利用MySQL Router构建读写分离MGR集群

MGR是MySQL Group Replication的缩写&#xff0c;即MySQL组复制。 在以往&#xff0c;我们一般是利用MySQL的主从复制或半同步复制来提供高可用解决方案&#xff0c;但这存在以下几个比较严重的问题&#xff1a; 主从复制间容易发生复制延迟&#xff0c;尤其是在5.6以前的版本…

保研考研机试攻略:python笔记(4)

🐨🐨🐨15各类查找 🐼🐼二分法 在我们写程序之前,我们要定义好边界,主要是考虑区间边界的闭开问题。 🐶1、左闭右闭 # 左闭右闭 def search(li, target): h = len(li) - 1l = 0#因为都是闭区间,h和l都可以取到并且相等while h >= l:mid = l + (h - l) // 2…

关于conda换镜像源,pip换源

目录 1. 查看当前下载源2. 添加镜像源2.1清华大学开源软件镜像站2.2上海交通大学开源镜像站2.3中国科学技术大学 3.删除镜像源4.删除所有镜像源&#xff0c;恢复默认5.什么是conda-forge6.pip换源 1. 查看当前下载源 conda config --show channels 如果发现多个 可以只保留1个…

分布式服务框架 如何设计一个更合理的协议

1、概述 前面我们聊了如何设计一款分布式服务框架的问题&#xff0c;并且编码实现了一个简单的分布式服务框架 cheese, 目前 cheese 基本具备分布式服务框架的基本功能。后面我们又引入了缓存机制&#xff0c;以及使用Socket替代了最开始的 RestTemplate。并且还学习了网络相关…

前端快速生成接口方法

大家好&#xff0c;我是苏麟&#xff0c;今天聊一下OpenApi。 官网 &#xff1a; umijs/openapi - npm 安装命令 npm i --save-dev umijs/openapi 在根目录&#xff08;项目目录下&#xff09;创建文件 openapi.config.js import { generateService } from umijs/openapi// 自…

mysql 学习12 存储引擎,mysql体系结构

mysql 体系结构 存储引擎简介 存储引擎 就是 存储数据&#xff0c;建立索引&#xff0c;更新/查询 数据等技术的实现方式。 存储引擎 是基于表的&#xff0c;而不是基于库的&#xff0c;所以存储引擎也可以称为 表类型 mysql默认的使用InnoDB 做为存储引擎 查看一下我们之前…

稀土抑烟剂——为汽车火灾安全增添防线

一、稀土抑烟剂的基本概念 稀土抑烟剂是一类基于稀土元素&#xff08;如稀土氧化物和稀土金属化合物&#xff09;开发的高效阻燃材料。它可以显著提高汽车内饰材料的阻燃性能&#xff0c;减少火灾发生时有毒气体和烟雾的产生。稀土抑烟剂不仅能提升火灾时的安全性&#xff0c;…

Unity进阶教程AOI算法原理详解

最新课程《全栈双客户端(Unity/Cocos) TurnKey方案》更新了AOI专题&#xff0c;今天分享一下AOI算法的实现原理。 AOI的功能和作用 在MMORPG网路游戏当中&#xff0c;单服同时在线一般都会有几千人。当有个玩家执行一个操作&#xff0c;理想情况下要把玩家的操作广播同步给单…

【C】链表算法题7 -- 环形链表||

leetcode链接https://leetcode.cn/problems/linked-list-cycle-ii/description/ 问题描述 给定一个链表的头节点 head &#xff0c;返回链表开始入环的第一个节点。 如果链表无环&#xff0c;则返回 null。如果链表中有某个节点&#xff0c;可以通过连续跟踪 next 指针再次到…

STM32系统架构介绍

STM32系统架构 1. CM3/4系统架构2. CM3/4系统架构-----存储器组织结构2.1 寄存器地址映射&#xff08;特殊的存储器&#xff09;2.2 寄存器地址计算2.3 寄存器的封装 3. CM3/4系统架构-----时钟系统 STM32 和 ARM 以及 ARM7是什么关系? ARM 是一个做芯片标准的公司&#xff0c…

window patch按块分割矩阵

文章目录 1. excel 示意2. pytorch代码3. window mhsa 1. excel 示意 将一个三维矩阵按照window的大小进行拆分成多块2x2窗口矩阵&#xff0c;具体如下图所示 2. pytorch代码 pytorch源码 import torch import torch.nn as nn import torch.nn.functional as Ftorch.set_p…

机器学习(李宏毅)——BERT

一、前言 本文章作为学习2023年《李宏毅机器学习课程》的笔记&#xff0c;感谢台湾大学李宏毅教授的课程&#xff0c;respect&#xff01;&#xff01;&#xff01; 读这篇文章必须先了解self-attention、Transformer&#xff0c;可参阅我其他文章。 二、大纲 BERT简介self-…

深度学习-111-大语言模型LLM之基于langchain的结构化输出功能实现文本分类

文章目录 1 langchain的结构化输出1.1 推荐的使用流程1.2 模式定义1.3 返回结构化输出1.3.1 工具调用(方式一)1.3.2 JSON模式(方式二)1.3.3 结构化输出法(方式三)2 文本分类2.1 定义分类模式2.2 配置分类提示模板2.3 初始化分类模型2.4 分类示例3 参考附录1 langchain的结构化输…

常见的排序算法:插入排序、选择排序、冒泡排序、快速排序

1、插入排序 步骤&#xff1a; 1.从第一个元素开始&#xff0c;该元素可以认为已经被排序 2.取下一个元素tem&#xff0c;从已排序的元素序列从后往前扫描 3.如果该元素大于tem&#xff0c;则将该元素移到下一位 4.重复步骤3&#xff0c;直到找到已排序元素中小于等于tem的元素…

C++17 中的 std::gcd:探索最大公约数的现代 C++ 实现

文章目录 一、std::gcd 的基本用法&#xff08;一&#xff09;包含头文件&#xff08;二&#xff09;函数签名&#xff08;三&#xff09;使用示例 二、std::gcd 的实现原理三、std::gcd 的优势&#xff08;一&#xff09;简洁易用&#xff08;二&#xff09;类型安全&#xff…

OpenWRT中常说的LuCI是什么——LuCI介绍(一)

我相信每个玩openwrt的小伙伴都或多或少看到过luci这个东西&#xff0c;但luci到底是什么东西&#xff0c;可能还不够清楚&#xff0c;今天就趁机来介绍下&#xff0c;openwrt中的luci&#xff0c;到底是个什么东西。 什么是LuCI&#xff1f; 首先&#xff0c;LuCI是OpenWRT中…

机器学习核心算法解析

机器学习核心算法解析 机器学习是人工智能的核心技术之一&#xff0c;它通过从数据中学习模式并做出预测或决策。本文将深入解析机器学习的核心算法&#xff0c;包括监督学习、无监督学习和强化学习&#xff0c;并通过具体案例和代码示例帮助读者理解这些算法的实际应用。 1. …