In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
I would like to share with you an example analysis of the process of building a mongodb cluster under linux. I believe most people don't know much about it, so share this article for your reference. I hope you will learn a lot after reading this article. Let's learn about it together.
The cluster structure of mongodb is shown above.
There is an example of a mongo3.0 cluster online:
Https://www.yisu.com/article/191388.htm
Router provides an entrance, and mongo clients connect to the cluster through router (in this example, only one route cluster is configured)
Config Servers Auxiliary recording data fragmentation (a cluster)
Shard is a data sharding cluster (in this example, two are configured to verify sharding)
In this case, three mongo instances for each cluster (shard config)
Config and shard are the same type of process mongod.
Route is the mongos process.
Download mongo binary package
Https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-5.0.6.tgz
Decompression can be seen.
Generate a key with openssl for data communication within the mongo cluster
Openssl rand-base64 123 > keyfile
Mongod is configured as a piece (config and shard are common)
Mongo_node.conf:
Storage: engine: wiredTiger directoryPerDB: true journal: enabled: true systemLog: destination: file logAppend: trueoperationProfiling: slowOpThresholdMs: 10000replication: oplogSizeMB: 10240processManagement: fork: truesecurity: authorization: "disabled"
Configuration file for mongos (that is, route in the figure)
Mongos.conf:
SystemLog: destination: file logAppend: true processManagement: fork: true
Start the config cluster (3 mongod processes)
WORK_DIR=/home/???/go/mongodb/mongo_test KEYFILE=$WORK_DIR/key/keyfilecat $KEYFILECONFFILE=$WORK_DIR/conf/mongo_node.confcat $CONFFILEMONGOD=mongodecho $MONGOD$MONGOD-port 26001-- bind_ip_all-- configsvr-- replSet configReplSet-- keyFile $KEYFILE-- dbpath $WORK_DIR/config_cluster/conf_n1/data-- pidfilepath $WORK_DIR/config_cluster/conf_n1/db.pid-- logpath $WORK_DIR/config_cluster/conf_n1/db.log-- config $CONFFILE $MONGOD-- port 26002 -- bind_ip_all-- configsvr-- replSet configReplSet-- keyFile $KEYFILE-- dbpath $WORK_DIR/config_cluster/conf_n2/data-- pidfilepath $WORK_DIR/config_cluster/conf_n2/db.pid-- logpath $WORK_DIR/config_cluster/conf_n2/db.log-- config $CONFFILE $MONGOD-- port 26003-- bind_ip_all-- configsvr-replSet configReplSet-- keyFile $KEYFILE-- dbpath $WORK_DIR/config_cluster/conf_n3/data-- pidfilepath $WORK_DIR/ Config_cluster/conf_n3/db.pid-logpath $WORK_DIR/config_cluster/conf_n3/db.log-config $CONFFILE
After starting successfully
Use the command mongo-- port 26001-- host 127.0.0.1
Enter the shell of mongo as shown below.
Enter the following js code in shell to set up the config cluster
Cfg= {_ id: "configReplSet", configsvr: true, members: [{_ id:0, host:'127.0.0.1:26001'}, {_ id:1, host:'127.0.0.1:26002'}, {_ id:2, host:'127.0.0.1:26003'}]}; rs.initiate (cfg)
Three config mongo processes will automatically select a primary. After a while, when you enter shell, you will find that the shell prompt becomes primary.
Add an admin user to config by the way. (as long as a cluster is added once in the primary process, it will be automatically synchronized to secondary)
Use admindb.createUser ({user:'admin',pwd:'123456', roles: [{role:'clusterAdmin',db:'admin'}, {role:'userAdminAnyDatabase',db:'admin'}, {role:'dbAdminAnyDatabase',db:'admin'}, {role:'readWriteAnyDatabase',db:'admin'}]})
Similarly, shard also does the same operation of adding users to facilitate subsequent observation of the data.
Start shard
WORK_DIR=/home/???/go/mongodb/mongo_test KEYFILE=$WORK_DIR/key/keyfilecat $KEYFILECONFFILE=$WORK_DIR/conf/mongo_node.confcat $CONFFILEMONGOD=mongodecho $MONGODecho "start shard1 replicaset" $MONGOD-- port 27001-- bind_ip_all-- shardsvr-- replSet shard1-- keyFile $KEYFILE-- dbpath $WORK_DIR/shard1/sh2_n1/data-- pidfilepath $WORK_DIR/shard1/sh2_n1/db.pid-- logpath $WORK_DIR/shard1/sh2_n1/db.log-- config $CONFFILE$MONGOD-- port 27002- -bind_ip_all-- shardsvr-- replSet shard1-- keyFile $KEYFILE-- dbpath $WORK_DIR/shard1/sh2_n2/data-- pidfilepath $WORK_DIR/shard1/sh2_n2/db.pid-- logpath $WORK_DIR/shard1/sh2_n2/db.log-- config $CONFFILE$MONGOD-- port 27003-- bind_ip_all-- shardsvr-- replSet shard1-keyFile $KEYFILE-- dbpath $WORK_DIR/shard1/sh2_n3/data-- pidfilepath $WORK_DIR/shard1/sh2_n3/db.pid- -logpath $WORK_DIR/shard1/sh2_n3/db.log-- config $CONFFILE
Enter mongo shell with mongo-- port 27001-- host 127.0.0.1
Cfg= {_ id: "shard1", members: [{_ id:0, host:'127.0.0.1:27001'}, {_ id:1, host:'127.0.0.1:27002'}, {_ id:2, host:'127.0.0.1:27003'}]}; rs.initiate (cfg)
Also use the js that added the user before
And use the same method to start the shard2 cluster for experimental data slicing
Change the corresponding directory and part name to shard2
Start route
WORK_DIR=/home/???/go/mongodb/mongo_test KEYFILE=$WORK_DIR/key/keyfilecat $KEYFILECONFFILE=$WORK_DIR/conf/mongos.confcat $CONFFILEMONGOS=mongosecho $MONGOSecho "start mongos route instances" $MONGOS-port=25001-- bind_ip_all-- configdb configReplSet/127.0.0.1:26001127.0.0.1:26002127.0.0.1:26003-keyFile $KEYFILE-- pidfilepath $WORK_DIR/route/r_n1/db.pid-- logpath $WORK_DIR/route/r_n1/db.log-- Config $CONFFILE$MONGOS-- port 25002-- bind_ip_all-- configdb configReplSet/127.0.0.1:26001127.0.0.1:26002127.0.0.1:26003-- keyFile $KEYFILE-- pidfilepath $WORK_DIR/route/r_n2/db.pid-- logpath $WORK_DIR/route/r_n2/db.log-- config $CONFFILE$MONGOS-- port 25003-bind_ip_all-- configdb configReplSet/127.0.0.1:26001127.0.0.1:26002127.0.0 .1 keyFile 26003-- keyFile $KEYFILE-- pidfilepath $WORK_DIR/route/r_n3/db.pid-- logpath $WORK_DIR/route/r_n3/db.log-- config $CONFFILE
Route add shard
Enter shell with mongo-- port 25001-- host 127.0.0.1-u admin-p 123456
Or this can also be mongo mongodb://admin:123456@127.0.0.1:25001
Execute the following two lines of js in mongo shell
Sh.addShard ("shard1/127.0.0.1:27001") sh.addShard ("shard2/127.0.0.1:27011")
Create a mongo database and collection
And set slicing
Use test sh.enableSharding ("test") db.createCollection ("test_shard") sh.shardCollection ("test.test_shard", {_ id: "hashed"}, false, {numInitialChunks: 4})
Add data in mongo shell with the following js, you can modify the number of cycles to avoid long testing time
Var cnt = 0 for (var item0; I
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.