In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article shows you how to build a shard_replica cluster in mongodb. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Refer to the official documentation for general steps
Step 1: create a Config Server instance and configure the Config Server instance to become Replica Set
Step 2: create a Shard Server instance and configure the Shard Server instance to become Replica Set
Step 3: create a Route Server instance and configure the Config Server instance and Shard Server instance
Step 4: create the database and execute the db.printShardingStatus () command to verify
Several parameters including replSet, shardsvr and configsvr are involved.
ReplSet: all hosts in the configuration replica set,replica set must have the same replica set name.
Shardsvr: configure this mongod instance as a shard in a sharding cluster. The default port is 27018.
Configsvr: declare this mongod instance as the configuration server for the sharding cluster. The default port is 27019, the default dbpath directory is / data/configdb, and the configuration server stores metadata and configuration settings for the cluster. Starting with MongoDB 3.4, the configuration server must be deployed as a set of replicate set replicas to prevent the configuration service from being a single point service. If your cluster has only one configuration server, if the configuration server fails, the cluster will not be available
Several instances of Config Server, Shard Server and Route Server are involved.
1. Config Server: mongod instance, which stores the entire Cluster Metadata, including Chunk information.
2. Shard Server: mongod instance, which is used to store actual data blocks. In the actual production environment, a Shard Server role can be assumed by several machines grouped into a Replica Set to prevent a single point of failure of the host.
3. Route Server: mongos instance, front-end routing, through which the client is connected, and makes the whole cluster look like a single database, and the front-end applications can be used transparently.
Deployment planning
2 physical device servers with ip 172.22.138.157and 172.22.138.158, respectively
Config Server instances with port 29001 are created on the two servers, and the Config Server instances on the two servers form the Replica Set schema
Shard server instances with ports 28001, 28002 and 28003 are created on the two servers respectively, and shard server instances with the same port on the two servers form the Replica Set mode
Create a Route Server instance with port 27001 on both servers
Deploy Architectur
External programs-- > Route Server instance-- > Config Server instance-- > shard server instance
Route Server instance is not a single point. It is available on both servers. The configuration is the same, and both point to the same Config Server instance.
Config Server instance is not a single point, it is Replica Set mode
Shard server instance is not a single point, it is Replica Set mode
Step one:
Create a Config Server instance and configure the Config Server instance to become Replica Set
Edit the following files (two key parameters configsvr and replSet) on the two servers
Cat / mongodb/configsvr29001.conf
Port=29001
Dbpath=/mongodb/mongoconfig29001
Logpath=/mongodb/log/mongoconfig29001.log
Configsvr=true
ReplSet=config29001
Fork=true
Logappend=true
Directoryperdb=true
Bind_ip=0.0.0.0
Pidfilepath=/mongodb/mongoconfig29001.pid
1.2.Starting Config Server instances on both servers
Mongod-f / mongodb/configsvr29001.conf
1.3.Configuring Config Server instances of two servers into Replica Set
Log in to any server and do the following
Mongo-- host 172.22.138.157-- port 29001
Rs.initiate (
{
_ id: "config29001"
Configsvr: true
Members: [
{_ id: 0, host: "172.22.138.157purl 29001"}
{_ id: 1, host: "172.22.138.158purl 29001"}
]
}
)
Step two
Create a Shard Server instance and configure the Shard Server instance to become Replica Set
2.1. edit the following three files (two key parameters shardsvr and replSet) on the two servers
Cat / mongodb/shardsvr28001.conf
Port=28001
Dbpath=/mongodb/mongoshard28001
Logpath=/mongodb/log/mongoshard28001.log
Shardsvr=true
ReplSet=shard28001
Fork=true
Logappend=true
Directoryperdb=true
Verbose=true
Bind_ip=0.0.0.0
Pidfilepath=/mongodb/mongoshard28001.pid
Cat / mongodb/shardsvr28002.conf
Port=28002
Dbpath=/mongodb/mongoshard28002
Logpath=/mongodb/log/mongoshard28002.log
Shardsvr=true
ReplSet=shard28002
Fork=true
Logappend=true
Directoryperdb=true
Verbose=true
Bind_ip=0.0.0.0
Pidfilepath=/mongodb/mongoshard28002.pid
Cat / mongodb/shardsvr28003.conf
Port=28003
Dbpath=/mongodb/mongoshard28003
Logpath=/mongodb/log/mongoshard28003.log
Shardsvr=true
ReplSet=shard28003
Fork=true
Logappend=true
Directoryperdb=true
Verbose=true
Bind_ip=0.0.0.0
Pidfilepath=/mongodb/mongoshard28003.pid
2.2.The Shard Server instance is started on both servers
Mongod-f / mongodb/shardsvr28001.conf
Mongod-f / mongodb/shardsvr28002.conf
Mongod-f / mongodb/shardsvr28003.conf
2.3. configure three Shard Server instances of two servers into Replica Set
Log in to any server and do the following
Mongo-- host 172.22.138.157-- port 28001
Rs.initiate (
{
_ id: "shard28001"
Members: [
{_ id: 0, host: "172.22.138.157purl 28001"}
{_ id: 1, host: "172.22.138.158purl 28001"}
]
}
)
Mongo-- host 172.22.138.157-- port 28002
Rs.initiate (
{
_ id: "shard28002"
Members: [
{_ id: 0, host: "172.22.138.157purl 28002"}
{_ id: 1, host: "172.22.138.158purl 28002"}
]
}
)
Mongo-- host 172.22.138.157-- port 28003
Rs.initiate (
{
_ id: "shard28003"
Members: [
{_ id: 0, host: "172.22.138.157purl 28003"}
{_ id: 1, host: "172.22.138.158purl 28003"}
]
}
)
Step three
Create a Route Server instance
Edit the following files on the two servers (key parameter configdb, configure the Config Server instance in the first step above)
Cat / mongodb/Routesvr27001.conf
Port=27001
Logpath=/mongodb/log/mongoRoute27001.log
Configdb=config29001/172.22.138.157:29001172.22.138.158:29001
Fork=true
Bind_ip=0.0.0.0
Pidfilepath=/mongodb/mongoRoute27001.pid
3.2.Starting Config Server instances on both servers
Mongos-f / mongodb/Routesvr27001.conf
Add shard sharding to the cluster and configure the Shard Server instance in step 2 above
Log in to any server and do the following
Mongo-- host 172.22.138.157-- port 27001
Sh.addShard ("shard28001/172.22.138.157:28001172.22.138.158:28001")
Sh.addShard ("shard28002/172.22.138.157:28002172.22.138.158:28002")
Sh.addShard ("shard28003/172.22.138.157:28003172.22.138.158:28003")
The fourth step
Verification
Log in to any mongos instance and create four databases
Mongo-- host 172.22.138.157-- port 27001
Mongos > use TDB1
Mongos > db.createCollection ("test01")
Mongos > db.test01.insert ({hid:1,hname: "w001"})
Mongos > use TDB2
Mongos > db.createCollection ("test02")
Mongos > db.test02.insert ({hid:11,hname: "x001"})
Mongos > use TDB3
Mongos > db.createCollection ("test03")
Mongos > db.test03.insert ({hid:111,hname: "Y001"})
Mongos > use TDB4
Mongos > db.createCollection ("test04")
Mongos > db.test04.insert ({hid:1111,hname: "Z001"})
When you connect to any mongos instance, you can see the four databases. Execute db.printShardingStatus () to see that the four databases are assigned to different shard, TDB1 to shard28001, TDB2\ TDB3 to shard28002, TDB4 to shard28003, login to mongod instance shard cluster sharding port 28001 to see only TDB1 database, login to mongod instance cluster sharding port 28002 to see only TDB2 and TDB3 databases Log in to the mongod instance cluster sharding port 28003 to see only the TDB4 database. Through mongos, users can see all the databases. The shard server below is unnecessary for users to know.
Mongo-- host 172.22.138.158-- port 27001
Mongos > show dbs
TDB1 0.000GB
TDB2 0.000GB
TDB3 0.000GB
TDB4 0.000GB
Admin 0.000GB
Config 0.001GB
Mongos > db.printShardingStatus ()
-Sharding Status
Sharding version: {
"_ id": 1
"minCompatibleVersion": 5
"currentVersion": 6
"clusterId": ObjectId ("5d09cd1c38fe676e9fb94a92")
}
Shards:
{"_ id": "shard28001", "host": "shard28001/172.22.138.157:28001172.22.138.158:28001", "state": 1}
{"_ id": "shard28002", "host": "shard28002/172.22.138.157:28002172.22.138.158:28002", "state": 1}
{"_ id": "shard28003", "host": "shard28003/172.22.138.157:28003172.22.138.158:28003", "state": 1}
Active mongoses:
"3.6.12": 2
Autosplit:
Currently enabled: yes
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:
{"_ id": "TDB1", "primary": "shard28001", "partitioned": false}
{"_ id": "TDB2", "primary": "shard28002", "partitioned": false}
{"_ id": "TDB3", "primary": "shard28002", "partitioned": false}
{"_ id": "TDB4", "primary": "shard28003", "partitioned": false}
{"_ id": "config", "primary": "config", "partitioned": true}
Config.system.sessions
Shard key: {"_ id": 1}
Unique: false
Balancing: true
Chunks:
Shard28001 1
{"_ id": {"$minKey": 1}}-- > > {"_ id": {"$maxKey": 1}} on: shard28001 Timestamp (1,0)
Mongo-- host 172.22.138.157-- port 28001
Shard28001:PRIMARY > show dbs
TDB1 0.000GB
Admin 0.000GB
Config 0.000GB
Local 0.000GB
Mongo-- host 172.22.138.157-- port 28002
Shard28002:PRIMARY > show dbs
TDB2 0.000GB
TDB3 0.000GB
Admin 0.000GB
Config 0.000GB
Local 0.000GB
Mongo-- host 172.22.138.157-- port 28003
Shard28003:PRIMARY > show dbs
TDB4 0.000GB
Admin 0.000GB
Config 0.000GB
Local 0.000GB
Step five
If you want to fragment the table in a database, that is, if you want to put part of the table An into shared1 and part into shard2, you need to use sh.enableSharding ("db_name") to enable sharding for the database. After the database is enabled for sharding, the fourth step above verifies the result "partitioned": false becomes "partitioned": true. If the table already contains data, you must use the db.collection.createIndex () method to create an index on the sharding key before using shardCollection () to fragment the table. If the table is empty, MongoDB creates an index as part of shardCollection ().
Mongo-- host 172.22.138.158-- port 27001
Mongos > use testdb
Mongos > db.createCollection ("table1")
Mongos > db.printShardingStatus ()-- the database "partitioned" corresponding to testdb at this time: false
Mongos > sh.enableSharding ("testdb")
Mongos > db.printShardingStatus ()-- the database "partitioned" corresponding to testdb at this time: true
The table testdb.table1 is sliced according to the age field range (code is 1), and the number of blocks to be created initially is 5 when the hash fragmentation key is used to slice the empty collection.
Mongos > sh.shardCollection ("testdb.table1", {age:1}, {numInitialChunks:5})
Mongos > for (var iTuno witch I db.createCollection ("table2")
Mongos > sh.shardCollection ("testdb.table2", {age:1}, {numInitialChunks:1})
Mongos > for (var iTuno witch I db.createCollection ("table3")
Mongos > sh.shardCollection ("testdb.table2", {age: "hashed"}, {numInitialChunks:1})
Mongos > for (var itemositani
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.