創(chuàng)建mongodb分片集群 + keyFile認證一、環(huán)境準備 1. 服務器節(jié)點信息【三臺服務器】 二、 集群環(huán)境設計 1. 三個 分片 復制集合 三、mongodb 安裝、`tar -zxvf mongodb-linux-x86_64-rhel70-4.2.5.tgz` mv mongodb-linux-x86_64-rhel70-4.2.5 /opt/mongodb4.2
四、添加環(huán)境變量 1. vim /etc/profile 五、搭建mongodb分片復制集1. 分別在三臺服務器(x.x.x.159、x.x.x.160、 x.x.x.161)節(jié)點 執(zhí)行如下命令 2. mongodb分片集群 配置文件 目錄 mkdir -p /opt/mongodb4.2/conf 3. 分片日志目錄 mkdir -p /opt/mongodb4.2/logs 4. 創(chuàng)建集群安全認證機制KeyFile openssl rand -base64 736 > /opt/mongodb4.2/conf/mongodb.keyfile Notice: keyfile 必須滿足以下條件: 1) 至少6個字符,小于1024字節(jié)。
5. 創(chuàng)建分片 數(shù)據(jù)存儲目錄 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. 創(chuàng)建分片配置文件 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. 分別在三臺服務器(x.x.x.159、x.x.x.160、 x.x.x.161)節(jié)點 執(zhí)行如下命令 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. 在其中任意一臺節(jié)點服務器登錄 本人使用 x.x.x.159節(jié)點 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自己選舉出來的 主庫 輸入命令: use admin db.createUser({ user: "username", pwd : "password", roles:[ {role:"root", db:"admin"} ] }) db.auth("username", "password") 八、搭建config節(jié)點復制集1. 分別在三臺服務器(x.x.x.159、x.x.x.160、 x.x.x.161)節(jié)點 執(zhí)行如下命令 2. 創(chuàng)建 config 數(shù)據(jù)存儲 目錄 mkdir -p /opt/mongodb4.2/mongo-cfg/data 3. 創(chuàng)建 config 日志 目錄 mkdir -p /opt/mongodb4.2/mongo-cfg/logs 4. 創(chuàng)建 config復制集 配置文件 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節(jié)點復制集 mongod -f /opt/mongodb4.2/conf/mongodb-cfg.conf 6. 登錄config節(jié)點 mongo -host x.x.x.(159/160/161) -port 28018 7.初始化 config節(jié)點 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. 創(chuàng)建認證用戶 use admin db.createUser({ user: "username", pwd : "password", roles:[ {role:"root", db:"admin"} ] }) db.auth("username", "password")
9. 開啟config復制集 認證 security: authorization: enabled keyFile: /opt/mongodb4.2/conf/mongodb.keyfile clusterAuthMode: keyFile
啟動 config復制集 九、搭建mongos節(jié)點(x.x.18.159) 1. 在 x.x.x.159 服務器上搭建客戶端 mkdir -p /opt/mongodb4.2/mongos/logs 3. 創(chuàng)建mongos 配置文件 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 mongos -f /opt/mongodb4.2/conf/mongos.conf 5. 登錄并初始化 mongos 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. 測試是否搭建成功 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. 分片命令 mongod -f /opt/mongodb4.2/conf/mongodb1.conf 啟動 mongod -f /opt/mongodb4.2/conf/mongodb1.conf --shutdown 關(guān)閉 OR mongo -port 27017 use admin db.shutdownServers()
2.config 復制集命令 mongod -f /opt/mongodb4.2/conf/mongodb-cfg.conf 啟動 mongod -f /opt/mongodb4.2/conf/mongodb-cfg.conf --shutdown 關(guān)閉 3. mongos 命令 mongos -f /opt/mongodb4.2/conf/mongos.conf 啟動 mongo -port 27017 關(guān)閉 use admin db.shutdownServers()
參考資料 https://my.oschina.net/u/563789/blog/3050068
|
|