MongoDB 单机

--mongod --shardsvr --replSet rs_a --dbpath /data/db --fork --logpath /data/db/log/rs_a.log --logappend run


--=====================================================================================================


mkdir /data/db /data/configdb /data/config /data/log /data/mongodbexe


---------------------------------------------------------
-- 启动shards
---------------------------------------------------------
 -- Note:
 -- shards 是存储实际数据块的地方
 --  --shardsvr will default the port to 27018
 -- 为了高可用性和性能,可以做成一级 Replica sets
 -- In the mongo shell,create the replica set (可以在数据库的命令行中生成集合)
 -- 例子:
-- > cfg = {
-- _id : "rs_a",
-- members : [
-- {_id : 0, host : "e1.acme.com:27018", priority : 1},
-- {_id : 1, host : "e2.acme.com:27018", priority : 1},
-- {_id : 2, host : "w1.acme.com:27018", priority : 0}
-- ]
-- }


-- > rs.initiate(cfg)
---------------------------------------------------------


mongod --shardsvr --dbpath /data/db/a --port 10000   run > /data/log2/sharda.log &
cat /data/log2/sharda.log
mongod --shardsvr --dbpath /data/db/b --port 10001   run > /data/log2/shardb.log &
cat /data/log2/shardb.log


---------------------------------------------------------
-- 启动Config Servers
---------------------------------------------------------
 -- 用于存储集群的元数据,如分片和块数据信息
 -- --configsvr will default the port to 27019
--------------------------------------------------------- 
mongod --configsvr --dbpath /data/db/config --port 20000 > /data/log2/configdb.log &
cat /data/log2/configdb.log


--mongos --configdb localhost:20000 > /data/log2/mongos.log &
--mongos --configdb localhost:30000 run > /data/log2/mongos.log &


mongos --port 30000 --configdb 127.0.0.1:20000  > /data/log2/mongos2.log &
cat /data/log2/mongos2.log
---------------------------------------------------------
-- 启动Routers 路由
---------------------------------------------------------
-- 用户连接 --> 路由 ---> 依Config Servers的信息  --> 连接实际的服务器(shards server) --> 返回数据到客户端
--configdb 用于指定配置服务器地址,可以多个,用逗号隔开
--  mongos 本身也有端口号, 有时如果不指定port,会启不来
    -- --configdb 后面是一串configsvr的ip及端口,用逗号隔开
-- 例子:
-- mongos --configdb c1.acme.com:27019,c2.acme.com:27019,c3.acme.com:27019
---------------------------------------------------------
mongos --port 30000 --configdb 127.0.0.1:20000     //--dbpath /data/configdb


---------------------------------------------------------
--配置
---------------------------------------------------------
--addshard:添加Shard Server, 其它相关命令 listshards 和 removeshard
--enablesharding: 用于设置可以被分布存储的数据库
--shardcollection: 用于设置具体被切块的集合名称,且必须指定Shard Key,系统会自动创建索引
--注: Sharded Collection 只能有一个unique index,且必须是shard key 
--例子: 
-- mongo
-- use admin   --(<serverhostname>[:<port>])
-- db.runCommand({addshard:"127.0.0.1:10000",name:"shard1",maxSize:40000})
-- db.runCommand({addshard:"127.0.0.1:10001",name:"shard2"})
--  
--   也可以如下:   --( replicaSetName/<serverhostname>[:port])
--   db.adminCommand( { addShard : "rs_a/e1.acme.com:27018,e2.acme.com:27018,w1.acme.com:27018" } )
-- db.adminCommand( { addShard : "rs_b/e3.acme.com:27018,e4.acme.com:27018,w2.acme.com:27018" } )
-- db.adminCommand( { addShard : "rs_c/e5.acme.com:27018,e6.acme.com:27018,w3.acme.com:27018" } )
---------------------------------------------------------  
--mongo
--use admin
--db.runCommand({addshard:"127.0.0.1:10000",name:"shard1",maxSize:40000})
--db.runCommand({addshard:"127.0.0.1:10001",name:"shard2"})

mongos> use admin
switched to db admin
mongos> db.runCommand({addshard:"127.0.0.1:10000",name:"shard1",maxSize:40000})
{ "shardAdded" : "shard1", "ok" : 1 }
mongos> db.runCommand({addshard:"127.0.0.1:10001",name:"shard2"})
{ "shardAdded" : "shard2", "ok" : 1 }
mongos> db.runCommand( { listshards : 1 } );
{
"shards" : [
{
"_id" : "shard1",
"host" : "127.0.0.1:10000",
"maxSize" : NumberLong(40000)
},
{
"_id" : "shard2",
"host" : "127.0.0.1:10001"
}
],
"ok" : 1
}
mongos>


---------------------------------------------------------
--enable sharding for any database 
mongos> use admin
switched to db admin
--db.runCommand( { enablesharding : "<dbname>" } );
mongos> db.runCommand( { enablesharding : "test" } )

{ "ok" : 1 }

--enable sharding for any collection 
--> db.runCommand( { shardcollection : "<namespace>",
    --               key : <shardkeypatternobject> });
  
mongos> db.runCommand( { shardcollection : "test.people", key : {name : 1} } )

{ "collectionsharded" : "test.people", "ok" : 1 }
mongos>

/*
--http://www.mongodb.org/display/DOCS/Configuring+Sharding
You can use the {unique: true} option to ensure that the underlying index enforces uniqueness so long as the unique index is a prefix of the shard key. (note: prior to version 2.0 this worked only if the collection is empty).


db.runCommand( { shardcollection : "test.users" , key : { email : 1 } , unique : true } );
If the "unique: true" option is not used, the shard key does not have to be unique.


db.runCommand( { shardcollection : "test.products" , key : { category : 1, _id : 1 } } );
You can shard on multiple fields if you are using a compound index.


In the end, picking the right shard key for your needs is extremely important for successful sharding. Choosing a Shard Key.


--http://www.mongodb.org/display/DOCS/Choosing+a+Shard+Key
> db.fs.chunks.ensureIndex({files_id: 1});
> db.runCommand({ shardcollection : "test.fs.chunks", key : { files_id : 1 }})
{ "collectionsharded" : "test.fs.chunks", "ok" : 1 }
*/
--=====================================================================================================
 --其它可选参数(Optional Parameters)
--maxSize
-- The addshard command accepts an optional maxSize parameter.  This parameter lets you 
-- tell the system a maximum amount of disk space in megabytes to use on the specified shard.  
-- If unspecified, the system will use the entire disk.  maxSize is useful when you have machines 
-- with different disk capacities or when you want to prevent storage of too much data on a particular shard.
--例子: 指定最大可用磁盘空间
--> db.runCommand( { addshard : "sf103", maxSize:100000/*MB*/ } );
   
 -- Listing shards
 -- To see current set of configured shards, run the listshards command:
 --  例子: 
 -- db.runCommand( { listshards : 1 } );
 
--Removing a shard
-- Remove Shards from an Existing Shard Cluster
--  http://docs.mongodb.org/manual/tutorial/remove-shards-from-cluster/
-- Remove Chunks from the Shard
-- 例子:
-- db.runCommand( { removeshard: "mongodb0" } )





--=====================================================================================================


[mongodb@localhost data]$ mongo
MongoDB shell version: 2.0.7
connecting to: test
mongos> db.runCommand( { enablesharding : "test" } )
{ "ok" : 0, "errmsg" : "access denied - use admin db" }



MAIL:[email protected]
我的CSDN BLOG: http://blog.csdn.net/xcl168

你可能感兴趣的:(mongodb)