mongodb復(fù)制集+分片生產(chǎn)環(huán)境實(shí)踐-創(chuàng)新互聯(lián)

三臺機(jī)器操作系統(tǒng)環(huán)境如下:

創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比呼中網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式呼中網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋呼中地區(qū)。費(fèi)用合理售后完善,10年實(shí)體公司更值得信賴。[mongodb@node1 ~]$ cat /etc/issue Red Hat Enterprise Linux Server release 6.6 (Santiago) Kernel \r on an \m [mongodb@node1 ~]$ uname -r 2.6.32-504.el6.x86_64 [mongodb@node1 ~]$ uname -m x86_64

架構(gòu)如下圖:

mongodb復(fù)制集+分片生產(chǎn)環(huán)境實(shí)踐

mongodb復(fù)制集+分片生產(chǎn)環(huán)境實(shí)踐

 文字描述:                                    1

192.168.42.41、shard1:10001、shard2:10002、shard3:10003、configsvr:10004、mongos:10005 注:shard1主節(jié)點(diǎn),shard2仲裁,shard3副本 192.168.42.42、shard1:10001、shard2:10002、shard3:10003、configsvr:10004、mongos:10005 注:shard1副本,shard2主節(jié)點(diǎn),shard3仲裁 192.168.42.43、shard1:10001、shard2:10002、shard3:10003、configsvr:10004、mongos:10005 注:shard1仲裁,shard2副本,shard3主節(jié)點(diǎn) node1:192.168.42.41 node2:192.168.42.42 node3:192.168.42.43

創(chuàng)建mongodb用戶

[root@node1 ~]# groupadd  mongodb [root@node1 ~]# useradd  -g mongodb mongodb [root@node1 ~]# mkdir /data [root@node1 ~]# chown mongodb.mongodb /data -R [root@node1 ~]# su - mongodb

創(chuàng)建目錄和文件

[mongodb@node1 ~]$ mkdir /data/{config,shard1,shard2,shard3,mongos,logs,configsvr,keyfile} -pv [mongodb@node1 ~]$ touch /data/keyfile/zxl [mongodb@node1 ~]$ touch /data/logs/shard{1..3}.log [mongodb@node1 ~]$ touch /data/logs/{configsvr,mongos}.log [mongodb@node1 ~]$ touch /data/config/shard{1..3}.conf [mongodb@node1 ~]$ touch /data/config/{configsvr,mongos}.conf

下載mongodb

[mongodb@node1 ~]$ wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.2.3.tgz [mongodb@node3 ~]$ tar fxz mongodb-linux-x86_64-rhel62-3.2.3.tgz -C /data [mongodb@node3 ~]$ ln -s /data/mongodb-linux-x86_64-rhel62-3.2.3 /data/mongodb

配置mongodb環(huán)境變量

[mongodb@node1 ~]$ echo "export PATH=$PATH:/data/mongodb/bin" >> ~/.bash_profile [mongodb@node1 data]$ source ~/.bash_profile

shard1.conf配置文件內(nèi)容如下:

[mongodb@node1 ~]$ cat /data/config/shard1.conf

systemLog:   destination: file   path: /data/logs/shard1.log   logAppend: true processManagement:   fork: true   pidFilePath: "/data/shard1/shard1.pid" net:   port: 10001 storage:   dbPath: "/data/shard1"   engine: wiredTiger   journal:     enabled: true   directoryPerDB: true operationProfiling:   slowOpThresholdMs: 10   mode: "slowOp"#security: #  keyFile: "/data/keyfile/zxl" #  clusterAuthMode: "keyFile" replication:   oplogSizeMB: 50   replSetName: "shard1_zxl"   secondaryIndexPrefetch: "all"

shard2.conf配置文件內(nèi)容如下:

[mongodb@node1 ~]$ cat /data/config/shard2.conf

systemLog:   destination: file   path: /data/logs/shard2.log   logAppend: true processManagement:   fork: true   pidFilePath: "/data/shard2/shard2.pid" net:   port: 10002 storage:   dbPath: "/data/shard2"   engine: wiredTiger   journal:     enabled: true   directoryPerDB: true operationProfiling:   slowOpThresholdMs: 10   mode: "slowOp"#security: #  keyFile: "/data/keyfile/zxl" #  clusterAuthMode: "keyFile" replication:   oplogSizeMB: 50   replSetName: "shard2_zxl"   secondaryIndexPrefetch: "all"

shard3.conf配置文件內(nèi)容如下:

[mongodb@node1 ~]$ cat /data/config/shard3.conf

systemLog:   destination: file   path: /data/logs/shard3.log   logAppend: true processManagement:   fork: true   pidFilePath: "/data/shard3/shard3.pid" net:   port: 10003 storage:   dbPath: "/data/shard3"   engine: wiredTiger   journal:     enabled: true   directoryPerDB: true operationProfiling:   slowOpThresholdMs: 10   mode: "slowOp" #security: #  keyFile: "/data/keyfile/zxl" #  clusterAuthMode: "keyFile" replication:   oplogSizeMB: 50   replSetName: "shard3_zxl"   secondaryIndexPrefetch: "all"

configsvr.conf配置文件內(nèi)容如下:

[mongodb@node1 ~]$ cat /data/config/configsvr.conf

systemLog:   destination: file   path: /data/logs/configsvr.log   logAppend: true processManagement:   fork: true   pidFilePath: "/data/configsvr/configsvr.pid" net:   port: 10004 storage:   dbPath: "/data/configsvr"   engine: wiredTiger   journal:     enabled: true #security: #  keyFile: "/data/keyfile/zxl" #  clusterAuthMode: "keyFile" sharding:   clusterRole: configsvr

mongos.conf配置文件內(nèi)容如下:

[mongodb@node3 ~]$ cat /data/config/mongos.conf

systemLog:   destination: file   path: /data/logs/mongos.log   logAppend: true processManagement:   fork: true   pidFilePath: /data/mongos/mongos.pid net:   port: 10005 sharding:   configDB: 192.168.42.41:10004,192.168.42.42:10004,192.168.42.43:10004 #security: #  keyFile: "/data/keyfile/zxl" #  clusterAuthMode: "keyFile"

注:以上操作只是在node1機(jī)器上操作,請把上面這些操作步驟在另外2臺機(jī)器操作一下,包括創(chuàng)建用戶創(chuàng)建目錄文件以及安裝mongodb等,以及文件拷貝到node2、node3對應(yīng)的目錄下,拷貝之后查看一下文件的屬主屬組是否為mongodb。

啟動各個(gè)機(jī)器節(jié)點(diǎn)的mongod,shard1、shard2、shard3

[mongodb@node1 ~]$ mongod -f /data/config/shard1.conf mongod: /usr/lib64/libcrypto.so.10: no version information available (required by m mongod: /usr/lib64/libcrypto.so.10: no version information available (required by m mongod: /usr/lib64/libssl.so.10: no version information available (required by mong mongod: relocation error: mongod: symbol TLSv1_1_client_method, version libssl.so.1n file libssl.so.10 with link time reference

注:無法啟動,看到相應(yīng)的提示后

解決:安裝openssl即可,三臺機(jī)器均安裝openssl-devel,redhat6.6基本不存在這種情況,centos可能會遇到。

[mongodb@node1 ~]$ su - root Password:  [root@node1 ~]# yum install openssl-devel -y

再次切換mongodb用戶啟動三臺機(jī)器上的mongod,shard1、shard2、shard3

[mongodb@node1 ~]$ mongod -f /data/config/shard1.conf about to fork child process, waiting until server is ready for connections. forked process: 1737 child process started successfully, parent exiting [mongodb@node1 ~]$ mongod -f /data/config/shard2.conf about to fork child process, waiting until server is ready for connections. forked process: 1760 child process started successfully, parent exiting [mongodb@node1 ~]$ mongod -f /data/config/shard3.conf about to fork child process, waiting until server is ready for connections. forked process: 1783 child process started successfully, parent exiting

注意:如果這一步報(bào)錯(cuò),可以看他的提示信息,或者/data/logs/shard{1..3}.log的日志。

進(jìn)入node1機(jī)器上的mongod:10001登錄

[mongodb@node1 ~]$ mongo --port 10001 MongoDB shell version: 3.2.3 connecting to: 127.0.0.1:10001/test Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user Server has startup warnings:  2016-03-08T13:28:18.508+0800 I CONTROL  [initandlisten]  2016-03-08T13:28:18.508+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/epage/enabled is 'always'. 2016-03-08T13:28:18.508+0800 I CONTROL  [initandlisten] **        We suggest settin 2016-03-08T13:28:18.508+0800 I CONTROL  [initandlisten]  2016-03-08T13:28:18.508+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/epage/defrag is 'always'. 2016-03-08T13:28:18.508+0800 I CONTROL  [initandlisten] **        We suggest settin 2016-03-08T13:28:18.508+0800 I CONTROL  [initandlisten]

注:提示warning......

解決:在三臺機(jī)器上均操作一下內(nèi)容即可

[mongodb@node2 config]$ su - root Password [root@node1 ~]# echo never > /sys/kernel/mm/transparent_hugepage/enabled [root@node1 ~]# echo never > /sys/kernel/mm/transparent_hugepage/defrag

一定要關(guān)閉三臺機(jī)器上的mongod實(shí)例,然后再次啟動三臺機(jī)器上mongod實(shí)例,否則不生效。

[mongodb@node1 ~]$ netstat -ntpl|grep mongo|awk '{print $NF}'|awk -F'/' '{print $1}'|xargs kill  [mongodb@node1 ~]$ mongod -f /data/config/shard1.conf [mongodb@node1 ~]$ mongod -f /data/config/shard2.conf [mongodb@node1 ~]$ mongod -f /data/config/shard3.conf看端口是否存在: [mongodb@elk_node1 ~]$ ss -atunlp|grep mong tcp    LISTEN     0      128                    *:10001                 *:*      users:(("mongod",2630,6)) tcp    LISTEN     0      128                    *:10002                 *:*      users:(("mongod",2654,6)) tcp    LISTEN     0      128                    *:10003                 *:*      users:(("mongod",2678,6))

配置復(fù)制集

node1機(jī)器上操作配置復(fù)制集

[mongodb@node1 config]$ mongo --port 10001 MongoDB shell version: 3.2.3 connecting to: 127.0.0.1:10001/test > use admin switched to db admin> config = { _id:"shard1_zxl", members:[ ... ... {_id:0,host:"192.168.42.41:10001"}, ... ... {_id:1,host:"192.168.42.42:10001"}, ... ... {_id:2,host:"192.168.42.43:10001",arbiterOnly:true} ... ... ] ... ... }{ "_id" : "shard1_zxl", "members" : [ { "_id" : 0, "host" : "192.168.42.41:10001" }, { "_id" : 1, "host" : "192.168.42.42:10001" }, { "_id" : 2, "host" : "192.168.42.43:10001", "arbiterOnly" : true } ] }> rs.initiate(con config                 connect(               connectionURLTheSame(  constructor > rs.initiate(config) { "ok" : 1 }

node2機(jī)器上操作配置復(fù)制集

[mongodb@node2 config]$ mongo --port 10002 MongoDB shell version: 3.2.3 connecting to: 127.0.0.1:10002/test Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user > use admin switched to db admin> config = { _id:"shard2_zxl", members:[ ... ... {_id:0,host:"192.168.42.42:10002"}, ... ... {_id:1,host:"192.168.42.43:10002"}, ... ... {_id:2,host:"192.168.42.41:10002",arbiterOnly:true} ... ... ] ... ... } { "_id" : "shard2_zxl", "members" : [ { "_id" : 0, "host" : "192.168.42.42:10002" }, { "_id" : 1, "host" : "192.168.42.43:10002" }, { "_id" : 2, "host" : "192.168.42.41:10002", "arbiterOnly" : true } ] }> rs.initiate(config) { "ok" : 1 }

node3機(jī)器上操作配置復(fù)制集

[mongodb@node3 config]$ mongo --port 10003 MongoDB shell version: 3.2.3 connecting to: 127.0.0.1:10003/test Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user > use admin switched to db admin>  config = {_id:"shard3_zxl", members:[ ... ... {_id:0,host:"192.168.42.43:10003"}, ... ... {_id:1,host:"192.168.42.41:10003"}, ... ... {_id:2,host:"192.168.42.42:10003",arbiterOnly:true} ... ... ] ... ... } { "_id" : "shard3_zxl", "members" : [ { "_id" : 0, "host" : "192.168.42.43:10003" }, { "_id" : 1, "host" : "192.168.42.41:10003" }, { "_id" : 2, "host" : "192.168.42.42:10003", "arbiterOnly" : true } ] }> rs.initiate(config) { "ok" : 1 }

注:以上是配置rs復(fù)制集,相關(guān)命令如:rs.status(),查看各個(gè)復(fù)制集的狀況

啟動三臺機(jī)器上的configsvr和mongos節(jié)點(diǎn)

[mongodb@node1 logs]$ mongod -f /data/config/configsvr.conf about to fork child process, waiting until server is ready for connections. forked process: 6317 child process started successfully, parent exiting[mongodb@node1 logs]$ mongos -f /data/config/mongos.conf about to fork child process, waiting until server is ready for connections. forked process: 6345 child process started successfully, parent exiting

查看端口是否啟動:

[mongodb@elk_node1 ~]$ ss -atunlp|grep mong tcp    LISTEN     0      128                    *:10001                 *:*      users:(("mongod",2630,6)) tcp    LISTEN     0      128                    *:10002                 *:*      users:(("mongod",2654,6)) tcp    LISTEN     0      128                    *:10003                 *:*      users:(("mongod",2678,6)) tcp    LISTEN     0      128                    *:10004                 *:*      users:(("mongod",3053,6)) tcp    LISTEN     0      128                    *:10005                 *:*      users:(("mongos",3157,21))

配置shard分片

在node1機(jī)器上配置shard分片

[mongodb@node1 config]$ mongo --port 10005 MongoDB shell version: 3.2.3 connecting to: 127.0.0.1:10005/test mongos> use admin switched to db adminmongos> db.runCommand({addshard:"shard1_zxl/192.168.42.41:10001,192.168.42.42:10001,192.168.42.43:10001"}); { "shardAdded" : "shard1_zxl", "ok" : 1 }mongos> db.runCommand({addshard:"shard2_zxl/192.168.42.41:10002,192.168.42.42:10002,192.168.42.43:10002"}); { "shardAdded" : "shard2_zxl", "ok" : 1 }mongos> db.runCommand({addshard:"shard3_zxl/192.168.42.41:10003,192.168.42.42:10003,192.168.42.43:10003"}); { "shardAdded" : "shard3_zxl", "ok" : 1 }

查看shard信息

mongos> sh.status() --- Sharding Status ---    sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("56de6f4176b47beaa9c75e9d") }   shards: {  "_id" : "shard1_zxl",  "host" : "shard1_zxl/192.168.42.41:10001,192.168.42.42:10001" } {  "_id" : "shard2_zxl",  "host" : "shard2_zxl/192.168.42.42:10002,192.168.42.43:10002" } {  "_id" : "shard3_zxl",  "host" : "shard3_zxl/192.168.42.41:10003,192.168.42.43:10003" }   active mongoses: "3.2.3" : 3   balancer: Currently enabled:  yes Currently running:  no Failed balancer rounds in last 5 attempts:  0 Migration Results for the last 24 hours:  No recent migrations   databases:

查看分片狀態(tài)

mongos> db.runCommand( {listshards : 1 } ) { "shards" : [ { "_id" : "shard1_zxl", "host" : "shard1_zxl/192.168.42.41:10001,192.168.42.42:10001" }, { "_id" : "shard2_zxl", "host" : "shard2_zxl/192.168.42.42:10002,192.168.42.43:10002" }, { "_id" : "shard3_zxl", "host" : "shard3_zxl/192.168.42.41:10003,192.168.42.43:10003" } ], "ok" : 1 }

啟用shard分片的庫名字為'zxl',即為庫

mongos> sh.enableSharding("zxl") { "ok" : 1 }

設(shè)置集合的名字以及字段,默認(rèn)自動建立索引,zxl庫,haha集合

mongos> sh.shardCollection("zxl.haha",{age: 1, name: 1}) { "collectionsharded" : "zxl.haha", "ok" : 1 }

模擬在haha集合中插入10000數(shù)據(jù)

mongos> for (i=1;i<=10000;i++) db.haha.insert({name: "user"+i, age: (i%150)}) WriteResult({ "nInserted" : 1 })

可以使用上面mongos> sh.status()命令查看各個(gè)shard分片情況,以上就是復(fù)制集和shard分片搭建完成。

結(jié)果如下:

mongos> sh.status() --- Sharding Status ---    sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("56de6f4176b47beaa9c75e9d") }   shards: {  "_id" : "shard1_zxl",  "host" : "shard1_zxl/192.168.42.41:10001,192.168.42.42:10001" } {  "_id" : "shard2_zxl",  "host" : "shard2_zxl/192.168.42.42:10002,192.168.42.43:10002" } {  "_id" : "shard3_zxl",  "host" : "shard3_zxl/192.168.42.41:10003,192.168.42.43:10003" }   active mongoses: "3.2.3" : 3   balancer: Currently enabled:  yes Currently running:  no Failed balancer rounds in last 5 attempts:  0 Migration Results for the last 24 hours:  2 : Success   databases: {  "_id" : "zxl",  "primary" : "shard3_zxl",  "partitioned" : true } zxl.hahashard key: { "age" : 1, "name" : 1 } unique: false balancing: true chunks: shard1_zxl1 shard2_zxl1 shard3_zxl1 { "age" : { "$minKey" : 1 }, "name" : { "$minKey" : 1 } } -->> { "age" : 2, "name" : "user2" } on : shard1_zxl Timestamp(2, 0)  { "age" : 2, "name" : "user2" } -->> { "age" : 22, "name" : "user22" } on : shard2_zxl Timestamp(3, 0)  { "age" : 22, "name" : "user22" } -->> { "age" : { "$maxKey" : 1 }, "name" : { "$maxKey" : 1 } } on : shard3_zxl Timestamp(3, 1)

以上就是mongodb3.2復(fù)制集和shard分片搭建就此完成.有什么疑問可以給我留言,看到第一時(shí)間解決。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

本文題目:mongodb復(fù)制集+分片生產(chǎn)環(huán)境實(shí)踐-創(chuàng)新互聯(lián)
文章轉(zhuǎn)載:http://muchs.cn/article24/coecje.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航移動網(wǎng)站建設(shè)、品牌網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計(jì)、全網(wǎng)營銷推廣網(wǎng)站收錄

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作