Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5253132
  • 博文数量: 1004
  • 博客积分: 8199
  • 博客等级: 中将
  • 技术积分: 13042
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-25 20:19
个人简介

脚踏实地、勇往直前!

文章分类

全部博文(1004)

文章存档

2020年(1)

2019年(93)

2018年(208)

2017年(81)

2016年(49)

2015年(50)

2014年(170)

2013年(52)

2012年(177)

2011年(93)

2010年(30)

分类: NOSQL

2017-07-20 09:04:00

环境
配置服务器:
127.0.1.1:27017
127.0.1.1:27018
127.0.1.1:27019
路由服务器
127.0.0.1:27020
分片服务器
127.0.0.1:27021
127.0.0.1:27022

db:3.4.4

------------------------配置服务器---------------------------
1.配置服务器
程序路径
mkdir -P /opt/mongodb/config_server/c1/mongodb344
mkidr -P /opt/mongodb/config_server/c2/mongodb344
mkdir -p /opt/mongodb/config_server/c3/mongodb344

数据文件路径
mkdir -P /opt/mongodb/config_server/c1/mongodb344/data
mkidr -P /opt/mongodb/config_server/c2/mongodb344/data
mkdir -p /opt/mongodb/config_server/c3/mongodb344/data

配置文件路径
mkdir -P /opt/mongodb/config_server/c1/mongodb344/conf
mkidr -P /opt/mongodb/config_server/c2/mongodb344/conf
mkdir -p /opt/mongodb/config_server/c3/mongodb344/conf

日志文件路径
mkdir -P /opt/mongodb/config_server/c1/mongodb344/log
mkdir -P /opt/mongodb/config_server/c2/mongodb344/log
mkdir -P /opt/mongodb/config_server/c3/mongodb344/log

2.配置文件
[root@izwz9gxsbwybwg9n5xdi47z conf]# more config_server.cnf
port = 27017
fork = true
dbpath = /opt/mongodb/config_server/c1/mongodb344/data
logpath = /opt/mongodb/config_server/c1/mongodb344/log/logs
logappend = true
configsvr = true
directoryperdb = true
replSet=configdb

[root@izwz9gxsbwybwg9n5xdi47z conf]# more config_server.cnf
port = 27018
fork = true
dbpath = /opt/mongodb/config_server/c2/mongodb344/data
logpath = /opt/mongodb/config_server/c2/mongodb344/log/logs
logappend = true
configsvr = true
directoryperdb = true
replSet=configdb

[root@izwz9gxsbwybwg9n5xdi47z conf]# more config_server.cnf
port = 27019
fork = true
dbpath = /opt/mongodb/config_server/c3/mongodb344/data
logpath = /opt/mongodb/config_server/c3/mongodb344/log/logs
logappend = true
configsvr = true
directoryperdb = true
replSet=configdb

3.启动
./mongod -f /opt/mongodb/config_server/c1/mongodb344/conf/config_server.cnf
./mongod -f /opt/mongodb/config_server/c2/mongodb344/conf/config_server.cnf
./mongod -f /opt/mongodb/config_server/c3/mongodb344/conf/config_server.cnf

4.创建副本集
登陆其中一个节点执行:
./mongo 127.0.0.1:27017

configdb1={_id:'configdb',members:[
{_id:0,host:'127.0.0.1:27017',priority:3},
{_id:1,host:'127.0.0.1:27018',priority:1},
{_id:2,host:'127.0.0.1:27019',priority:2}]
}

rs.initiate(configdb1)
这里的configdb需要跟配置文件里replSet定义的一致.


-------------路由服务器---------------------------------
1.程序路径
mkdir -P /opt/mongodb/route_server/mongodb344
mkdir -P /opt/mongodb/route_server/mongodb344/conf
mkdir -P /opt/mongodb/route_server/mongodb344/log

2.配置文件
[root@izwz9gxsbwybwg9n5xdi47z conf]# more route_server.cnf 
port=27020
configdb=configdb/127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019
logpath=/opt/mongodb/route_server/mongodb344/logs
fork=true

3.启动
./mongos -f /opt/mongodb/route_server/mongodb344/conf/route_server.cnf


----------------------分片服务器----------------------------
1.程序路径
mkdir -P /opt/mongodb/shard1/mongodb344
mkdir -P /opt/mongodb/shard2/mongodb344

数据文件路径
mkdir -P /opt/mongodb/shard1/mongodb344/data
mkdir -P /opt/mongodb/shard2/mongodb344/data

配置文件路径
mkdir -P /opt/mongodb/shard1/mongodb344/conf
mkdir -P /opt/mongodb/shard2/mongodb344/conf

日志文件路径
/opt/mongodb/shard1/mongodb344/log
/opt/mongodb/shard2/mongodb344/log

2.配置文件
[root@izwz9gxsbwybwg9n5xdi47z conf]# more shard1.cnf
port = 27021
fork = true
dbpath = /opt/mongodb/shard1/mongodb344/data
logpath = /opt/mongodb/shard1/mongodb344/log/logs
logappend = true
shardsvr = true

[root@izwz9gxsbwybwg9n5xdi47z conf]# more shard2.cnf
port = 27022
fork = true
dbpath = /opt/mongodb/shard2/mongodb344/data
logpath = /opt/mongodb/shard2/mongodb344/log/logs
logappend = true
shardsvr = true

3.启动
./mongod -f /opt/mongodb/shard1/mongodb344/conf/shard1.cnf
./mongod -f /opt/mongodb/shard2/mongodb344/conf/shard2.cnf


--------------分片部署--------------------------------
1.在路由服务器上执行,添加分片服务器
./mongo 127.0.0.1:27020
mongos>use admin
mongos>db.runCommand({"addshard":"127.0.0.1:27021",allowLocal:true})
mongos>db.runCommand({"addshard":"127.0.0.1:27022",allowLocal:true})

2.查看分片情况
mongos> use adminuse admin
switched to db admin
mongos> db.runCommand({listshards:1});db.runCommand({listshards:1});
{
        "shards" : [
                {
                        "_id" : "shard0000",
                        "host" : "127.0.0.1:27021",
                        "state" : 1
                },
                {
                        "_id" : "shard0001",
                        "host" : "127.0.0.1:27022",
                        "state" : 1
                }
        ],
        "ok" : 1
}
mongos> 

3.启用分片功能
在路由服务器上执行
是数据库hxl启用分片功能
mongos>db.runCommand({"enablesharding":"hxl"});

为集合person设置片键,这里使用id做hash分片
mongos>use admin
mongos>db.runCommand({"shardcollection":"hxl.person","key":{"_id":"hashed"}})
{ "collectionsharded" : "hxl.person", "ok" : 1 }

写入数据
mongos>use hxl
mongos>for (var i=0;i<100000;i++){db.person.insert({"name":"hxl"+i,"age":i})}
mongos> db.person.count();
100000


查看分片结果
mongos> db.printShardingStatus();

查看集合的分片记录情况
mongos> use hxl;
mongos> db.person.stats()


-- The End --

阅读(2041) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~