mongodb分片集群 搭建 + keyFile认证
创建mongodb分片集群 + keyFile认证
一、环境准备
1. 服务器节点信息【三台服务器】
x.x.x.159、 x.x.x..160、 x.x.x..161
2. 服务器系统
linux x86_64 GNU/Linux 或者 centos7 或者 redhat
3. mongodb版本
mongodb v4.2.5
下载地址:https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.5.tgz
二、 集群环境设计
1. 三个 分片 复制集合
shard1(x.x.x.159:27017、 x.x.x.160:27017、 x.x.x.161:27017)
shard2(x.x.x.159:27018、 x.x.x.160:27018、 x.x.x.161:27018)
shard3(x.x.x.159:27019、 x.x.x.160:27019、 x.x.x.161:27019)
2. 一个 config 复制集合
(x.x.x.159:28018、 x.x.x.160:28018、 x.x.x.161:28018)
3. 一个 mongos 节点
(x.x.x.159:27000)
三、mongodb 安装、
1 2 | `tar -zxvf mongodb-linux-x86_64-rhel70-4.2.5.tgz` mv mongodb-linux-x86_64-rhel70-4.2.5 /opt/mongodb4.2 |
四、添加环境变量
1. vim /etc/profile
2. 文件末尾追加: export PATH=$PATH:/opt/mongodb/bin
3. 加载文件立刻生效: source /etc/profile
五、搭建mongodb分片复制集
1. 分别在三台服务器(x.x.x.159、x.x.x.160、 x.x.x.161)节点 执行如下命令
2. mongodb分片集群 配置文件 目录
1 | mkdir -p /opt/mongodb4.2/conf |
3. 分片日志目录
1 | mkdir -p /opt/mongodb4.2/logs |
4. 创建集群安全认证机制KeyFile
1 | openssl rand -base64 736 > /opt/mongodb4.2/conf/mongodb.keyfile |
Notice: keyfile 必须满足以下条件:
1) 至少6个字符,小于1024字节。
2) 认证的时候不考虑文件中的空白字符。
3) 连接副本集成员的KeyFile和启动mongos进程的 KeyFile文件内容必须一致。
4) 必须是base64编码,但不能有等号。
5) 文件权限必须是 600(chmod 600),不能分配任何权限给group成员和其他成员。
5. 创建分片 数据存储目录
1 2 3 | mkdir -p /opt/mongodb4.2/data/db1 【分片1】 mkdir -p /opt/mongodb4.2/data/db2 【分片2】 mkdir -p /opt/mongodb4.2/data/db3 【分片3】 |
6. 创建分片配置文件
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | vim /opt/mongodb4.2/conf/mongodb1.conf 【分片1 配置文件】 storage: journal: enabled: true wiredTiger: engineConfig: cacaeSizeGB: 2 directoryForIndexes: true systemLog: destination: file logAppend: true path: /opt/mongodb4.2/logs/mongodb1.log net: port: 27017 bindIp: 0.0.0.0 processManagement: fork: true replication: oplogSizeMB: 4000 replSetName: jxk1 sharding: clusterRole: shardsvr security: authorization: enabled keyFile: /opt/mongodb4.2/conf/mongodb.keyfile clusterAuthMode: keyFile vim /opt/mongodb4.2/conf/mongodb2.conf 【分片2 配置文件】 storage: journal: enabled: true wiredTiger: engineConfig: cacaeSizeGB: 2 directoryForIndexes: true systemLog: destination: file logAppend: true path: /opt/mongodb4.2/logs/mongodb2.log net: port: 27018 bindIp: 0.0.0.0 processManagement: fork: true replication: oplogSizeMB: 4000 replSetName: jxk2 sharding: clusterRole: shardsvr security: authorization: enabled keyFile: /opt/mongodb4.2/conf/mongodb.keyfile clusterAuthMode: keyFile vim /opt/mongodb4.2/conf/mongodb3.conf 【分片3 配置文件】 storage: journal: enabled: true wiredTiger: engineConfig: cacaeSizeGB: 2 directoryForIndexes: true systemLog: destination: file logAppend: true path: /opt/mongodb4.2/logs/mongodb3.log net: port: 27018 bindIp: 0.0.0.0 processManagement: fork: true replication: oplogSizeMB: 4000 replSetName: jxk3 sharding: clusterRole: shardsvr security: authorization: enabled keyFile: /opt/mongodb4.2/conf/mongodb.keyfile clusterAuthMode: keyFile 注意: 配置文件 yuml语法格式 直接拷贝下来 语法格式估计不能用 |
六、启动 分片复制集
1 2 3 4 | 1. 分别在三台服务器(x.x.x.159、x.x.x.160、 x.x.x.161)节点 执行如下命令 2. mongod -f /opt/mongodb4.2/conf/mongodb1.conf 3. mongod -f /opt/mongodb4.2/conf/mongodb2.conf 4. mongod -f /opt/mongodb4.2/conf/mongodb3.conf |
七、 初始化分片复制集 并 设置 分片复制集账号
1. 在其中任意一台节点服务器登录 本人使用 x.x.x.159节点
2. mongo -port 27017 【登录】
3. 进入mongodb命令输入终端 输入命令如下
1 2 3 4 5 6 7 8 9 10 | var conf = { _id: 'jxk' , members:[ {_id:1, host: 'x.x.x.159:27017' }, {_id:2, host: 'x.x.x.160:27017' }, {_id:3, host: 'x.x.x.161:27017' }, ], } rs.initiate(conf) rs.status() |
4. 找到mongodb自己选举出来的 主库 输入命令:
1 2 3 4 5 6 7 8 9 | use admin db.createUser({ user: "username" , pwd : "password" , roles:[ {role: "root" , db: "admin" } ] }) db.auth( "username" , "password" ) |
八、搭建config节点复制集
1. 分别在三台服务器(x.x.x.159、x.x.x.160、 x.x.x.161)节点 执行如下命令
2. 创建 config 数据存储 目录
1 | mkdir -p /opt/mongodb4.2/mongo-cfg/data |
3. 创建 config 日志 目录
1 | mkdir -p /opt/mongodb4.2/mongo-cfg/logs |
4. 创建 config复制集 配置文件
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 | vim /opt/mongodb4.2/conf/mongodb-cfg.conf systemLog: destination: file logAppend: true path: /opt/mongodb4.2/logs/mongodb3.log storage: journal: enabled: true dbPath: /opt/mongodb4.2/mongo-cfg/data directoryPerDB: true wiredTiger: engineConfig: cacaeSizeGB: 2 directoryForIndexes: true collectionConfig: blockCompressor: zstd indexConfig: prefixCompression: true net: port: 28018 bindIp: x.x.x.159 replication: oplogSizeMB: 4000 replSetName: configReplSet sharding: clusterRole: configsvr processManagement: fork: true |
5. 启动config节点复制集
1 | mongod -f /opt/mongodb4.2/conf/mongodb-cfg.conf |
6. 登录config节点
1 | mongo -host x.x.x.(159/160/161) -port 28018 |
7.初始化 config节点
1 2 3 4 5 6 7 8 9 10 | var conf = { _id: 'configReplSet' , members:[ {_id:0, host: "x.x.x.159:28018" }, {_id:1, host: "x.x.x.160:28018" }, {_id:2, host: "x.x.x.161:28018" }, ] } rs.initiate(conf) rs.status() |
8. 创建认证用户
1 2 3 4 5 6 7 8 9 | use admin db.createUser({ user: "username" , pwd : "password" , roles:[ {role: "root" , db: "admin" } ] }) db.auth( "username" , "password" ) |
9. 开启config复制集 认证
关闭 config复制集
在配置文件追加
vim /opt/mongodb4.2/conf/mongodb-cfg.conf
1 2 3 4 | security: authorization: enabled keyFile: /opt/mongodb4.2/conf/mongodb.keyfile clusterAuthMode: keyFile |
启动 config复制集
九、搭建mongos节点(x.x.18.159)
1. 在 x.x.x.159 服务器上搭建客户端
2. 创建mongos 日志目录
1 | mkdir -p /opt/mongodb4.2/mongos/logs |
3. 创建mongos 配置文件
vim /opt/mongodb4.2/conf/mongos.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | systemLog: destination: file logAppend: true path: /opt/mongodb4.2/mongos/logs/mongodb.log net: port: 27000 bindIp: 0.0.0.0 sharding: configDB: configReplSet/x.x.x.159:28018,x.x.x.160:28018,x.x.x.160:28018, processManagement: fork: true security: keyFile: /opt/mongodb4.2/conf/mongodb.keyfile clusterAuthMode: keyFile |
4. 启动mongos
1 | mongos -f /opt/mongodb4.2/conf/mongos.conf |
5. 登录并初始化 mongos
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | mongo -port 27000 use admin db.auth( "username" , "password" ) #这里使用的账号是 config复制集的账号 db.runCommand({ addshard: "jxk1/x.x.x.159:27017,x.x.x.160:27017,x.x.x.161:27017," , name: "shard1" }); db.runCommand({ addshard: "jxk2/x.x.x.159:27018,x.x.x.160:27018,x.x.x.161:27018," , name: "shard1" }); db.runCommand({ addshard: "jxk3/x.x.x.159:27019,x.x.x.160:27019,x.x.x.161:27019," , name: "shard1" }) |
6. 测试是否搭建成功
1 2 3 4 5 6 7 8 9 | db.runCommand( {enablesharding: "dbtest" }) db.runCommand( {shardcollection: "dbtest.one" , key:{id:1} }) use dbtest var tmp = []; for ( var i =0; i<10000000; i++){ tmp.push({ 'id' :i, "name" : "lunck dog " + i}); } db.one.insertMany(tmp); sh.status() |
十、常用命令汇总
1. 分片命令
1 2 3 4 5 6 | mongod -f /opt/mongodb4.2/conf/mongodb1.conf 启动 mongod -f /opt/mongodb4.2/conf/mongodb1.conf --shutdown 关闭 OR mongo -port 27017 use admin db.shutdownServers() |
2.config 复制集命令
1 2 | mongod -f /opt/mongodb4.2/conf/mongodb-cfg.conf 启动 mongod -f /opt/mongodb4.2/conf/mongodb-cfg.conf --shutdown 关闭 |
3. mongos 命令
1 2 3 4 | mongos -f /opt/mongodb4.2/conf/mongos.conf 启动 mongo -port 27017 关闭 use admin db.shutdownServers() |
参考资料
https://my.oschina.net/u/563789/blog/3050068
https://blog.csdn.net/qq_24434491/article/details/102907486
https://blog.csdn.net/lezeqe/article/details/90518179
https://blog.csdn.net/tototuzuoquan/article/details/78295040
https://www.freesion.com/article/6886370039/#8mongo_247
https://www.cnblogs.com/shaosks/p/5775757.html
https://www.cnblogs.com/wxmdevelop/p/7341292.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
2015-04-29 tp U函数 logs