使用 C# 驱动程序在 MongoDB 中进行分片
我尝试尝试分片并制作一个示例配置:两个分片的最简单配置。这是bat文件中的代码:
cd c:\mongodb-win32-x86_64-1.8.3-rc1\bin
call mongod --shardsvr --dbpath /data/db/Shard--port 10000
cd c:\mongodb-win32-x86_64-1.8.3-rc1\bin
call mongod --shardsvr --dbpath /data/db/Shard2 --port 10001
cd c:\mongodb-win32-x86_64-1.8.3-rc1\bin
mongod --configsvr --dbpath /data/db/config --port 20000
cd c:\mongodb-win32-x86_64-1.8.3-rc1\bin
mongos --configdb 192.168.0.23:20000
cd c:\mongodb-win32-x86_64-1.8.3-rc1\bin
mongo
use admin
db.runCommand( { addshard : "192.168.0.23:10000" } )
db.runCommand( { addshard : "192.168.0.23:10001" } )
db.runCommand( { enablesharding : "Shard" } )
db.runCommand( { shardcollection : "Shard.Customers", key :
{LocalIdentifier : 1} } )
当我尝试执行一个简单的插入代码到该数据库时,它会执行 但两个碎片都是空的。这是插入代码:
public void SaveBatch(IEnumerable<object> entities)
{
MongoCollection.InsertBatch(typeof(object), entities, SafeMode.True);
}
这也是在连接上执行的代码:
string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
mongoServer = mongoServer.Create(connectionString);
mongoDatabase = mongoServer.GetDatabase("Shard");
mongoCollection = mongoDatabase.GetCollection<Customer>("Customers");
mongoCollection.EnsureIndex(
IndexKeys.Ascending(new[] {"CampaignId", "LocalIdentifier", "ProjectIdentifier", "FirstName", "LastName"}));
所以我没有设法使分片工作。你能告诉我我是什么吗 错误之处:配置、连接或插入?什么是正确的 怎么办?
I try to experiment with sharding and make a sample configuration: the simplest one for two shards. Here is the code from bat files:
cd c:\mongodb-win32-x86_64-1.8.3-rc1\bin
call mongod --shardsvr --dbpath /data/db/Shard--port 10000
cd c:\mongodb-win32-x86_64-1.8.3-rc1\bin
call mongod --shardsvr --dbpath /data/db/Shard2 --port 10001
cd c:\mongodb-win32-x86_64-1.8.3-rc1\bin
mongod --configsvr --dbpath /data/db/config --port 20000
cd c:\mongodb-win32-x86_64-1.8.3-rc1\bin
mongos --configdb 192.168.0.23:20000
cd c:\mongodb-win32-x86_64-1.8.3-rc1\bin
mongo
use admin
db.runCommand( { addshard : "192.168.0.23:10000" } )
db.runCommand( { addshard : "192.168.0.23:10001" } )
db.runCommand( { enablesharding : "Shard" } )
db.runCommand( { shardcollection : "Shard.Customers", key :
{LocalIdentifier : 1} } )
When I try executing a simple inserting code to this DB, it executes
but both shards are empty. Here is the inserting code:
public void SaveBatch(IEnumerable<object> entities)
{
MongoCollection.InsertBatch(typeof(object), entities, SafeMode.True);
}
Also this is the code executing on connection:
string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
mongoServer = mongoServer.Create(connectionString);
mongoDatabase = mongoServer.GetDatabase("Shard");
mongoCollection = mongoDatabase.GetCollection<Customer>("Customers");
mongoCollection.EnsureIndex(
IndexKeys.Ascending(new[] {"CampaignId", "LocalIdentifier", "ProjectIdentifier", "FirstName", "LastName"}));
So I did not manage to make the shards work. Can you tell me what I am
wrong at: configuring, connecting or inserting? And what is the right
way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在我看来,您正在对 Shards.Customers 进行分片,但插入到 Riverdale.Customers 中。我错过了什么吗?
Seems to me you're sharding Shards.Customers, but inserting into Riverdale.Customers. Am I missing something?
问题中发布的解决方案效果很好
The solution posted in the question works fine