1、创建数据文件夹
[root@Mongo-server-A /]# mkdir data
[root@Mongo-server-A /]# cd /data
[root@Mongo-server-A data]# mkdir config
[root@Mongo-server-A data]# mkdir shard1
[root@Mongo-server-A data]# mkdir shard2
[root@Mongo-server-A data]# mkdir log
[root@Mongo-server-A data]# ls
2、进入mongodb目录
3、创建启动配置文件
[root@Mongo-server-A bin]# vi shard1.conf
dbpath = /data/shard1
shardsvr = true
replSet = shard1
bind_ip = 192.168.2.88
#auth = true
port = 10001
oplogSize = 100
logpath = /data/log/shard1.log
logappend = true
profile = 1
slowms = 5
rest = true
fork = true
shardsvr = true
replSet = shard1
bind_ip = 192.168.2.88
#auth = true
port = 10001
oplogSize = 100
logpath = /data/log/shard1.log
logappend = true
profile = 1
slowms = 5
rest = true
fork = true
[root@Mongo-server-A bin]# vi shard2.conf
dbpath = /data/shard2
shardsvr = true
replSet = shard2
bind_ip = 192.168.2.88
#auth = true
port = 10002
oplogSize = 100
logpath = /data/log/shard2.log
logappend = true
profile = 1
slowms = 5
rest = true
fork = true
shardsvr = true
replSet = shard2
bind_ip = 192.168.2.88
#auth = true
port = 10002
oplogSize = 100
logpath = /data/log/shard2.log
logappend = true
profile = 1
slowms = 5
rest = true
fork = true
[root@Mongo-server-A bin]# vi shard3.conf
dbpath = /data/shard3
shardsvr = true
replSet = shard3
bind_ip = 192.168.2.88
#auth = true
port = 10002
oplogSize = 100
logpath = /data/log/shard3.log
logappend = true
profile = 1
slowms = 5
rest = true
fork = true
shardsvr = true
replSet = shard3
bind_ip = 192.168.2.88
#auth = true
port = 10002
oplogSize = 100
logpath = /data/log/shard3.log
logappend = true
profile = 1
slowms = 5
rest = true
fork = true
4、启动
[root@Mongo-server-A bin]# ./mongod -f shard1.conf
[root@Mongo-server-A bin]# ./mongod -f shard2.conf
[root@Mongo-server-A bin]# ./mongod -f shard3.conf
5、对分片(shard1\shard2)配置进行初使化
config = {_id: "shard1", members: [
{_id: 0, host: "192.168.2.88:10001"},
{_id: 1, host: "192.168.2.89:10001"},
{_id: 2, host: "192.168.2.90:10001"}]
}
config = {_id: "shard2", members: [
{_id: 0, host: "192.168.2.88:10002"},
{_id: 1, host: "192.168.2.89:10002"},
{_id: 2, host: "192.168.2.90:10002"}]
config = {_id: "shard3", members: [
{_id: 0, host: "192.168.2.88:10003"},
{_id: 1, host: "192.168.2.89:10003"},
{_id: 2, host: "192.168.2.90:10003"}]
6、创建配置服务器启动文件
configsvr.conf
dbpath = /data/config
configsvr = true
port = 20000
logpath = /data/log/config.log
logappend = true
fork = true
7、启动配置服务
[root@Mongo-server-A bin]# ./mongod -f configsvr.conf
8、创建配置路由配置文件
mongos.conf
configdb = 192.168.2.88:20000
port = 30000
chunkSize = 5
logpath = /data/log/mongos.log
logappend = true
fork = true
9、启动路由并登录
[root@Mongo-server-A bin]# ./mongos -f mongos.conf
[root@Mongo-server-A bin]# ./mongo 192.168.2.88:30000
10、加入分片
mongos> use admin
switched to db admin
mongos> db.runCommand( {addshard:"shard1/192.168.2.88:10001,192.168.2.89:10001,192.168.2.90:10001", name:"shard1", maxsize:20480} )
switched to db admin
mongos> db.runCommand( {addshard:"shard1/192.168.2.88:10001,192.168.2.89:10001,192.168.2.90:10001", name:"shard1", maxsize:20480} )
{ "shardAdded" : "shard1", "ok" : 1 }
mongos> db.runCommand( {addshard:"shard2/192.168.2.88:10002,192.168.2.89:10002,192.168.2.90:10002", name:"shard2", maxsize:20480} )
mongos> db.runCommand( {addshard:"shard2/192.168.2.88:10002,192.168.2.89:10002,192.168.2.90:10002", name:"shard2", maxsize:20480} )
{ "shardAdded" : "shard2", "ok" : 1 }
mongos> db.runCommand( {addshard:"shard3/192.168.2.88:10003,192.168.2.89:10003,192.168.2.90:10003", name:"shard3", maxsize:20480} )
{ "shardAdded" : "shard3", "ok" : 1 }
11、查看分片
mongos> db.runCommand( { listshards : 1 } )
{
"shards" : [
{
"_id" : "shard1",
"host" : "shard1/192.168.2.88:10001"
},
{
"_id" : "shard2",
"host" : "shard2/192.168.2.88:10002"
}
],
"ok" : 1
}
{
"shards" : [
{
"_id" : "shard1",
"host" : "shard1/192.168.2.88:10001"
},
{
"_id" : "shard2",
"host" : "shard2/192.168.2.88:10002"
}
],
"ok" : 1
}
12、测试
mongos> use test
switched to db test
mongos> show dbs
admin 0.078125GB
config 0.1875GB
mongos> db.userinfo.insert({userid:10234,name:'fengyi'})
mongos> db.userinfo.find()
{ "_id" : ObjectId("4f60ee47fd58cd2bece6291b"), "userid" : 10234, "name" : "fengyi" }
mongos>
switched to db test
mongos> show dbs
admin 0.078125GB
config 0.1875GB
mongos> db.userinfo.insert({userid:10234,name:'fengyi'})
mongos> db.userinfo.find()
{ "_id" : ObjectId("4f60ee47fd58cd2bece6291b"), "userid" : 10234, "name" : "fengyi" }
mongos>
netstat -lntp|grep mongod