MongoDB 安装
Linux 版本: CentOS release 6.9
MongoDB 版本: MongoDB-Linux-x86_64-3.6.1.tgz
示例是在一台主机上安装 MongoDB 集群
| 端口 | ||
路由进程 mongo | 20001 | 20002 | 20003 |
配置服务器 config | 20011 | 20012 | 20013 |
分片服务器 shard1 | 20031 | 20032 | 20033 |
分片服务器 shard2 | 20041 | 20042 | 20043 |
1, 解压文件
执行解压命令: tar -zxvf MongoDB-Linux-x86_64-3.6.1.tgz
2, 创建数据, 日志目录, 结构如下 在同台机器创建三主三从三配置三路由架构 cp001,cp002,cp003 目录结构均一样
3,
1, 配置文件
dbpath 数据存放位置
logpath 日志文件位置
pidfilepath 进程号存放位置
port 端口号
fork 是否后台运行
replSet 副本集名称
configdb 监听的配置服务器, 只能有 1 个或者 3 个 configs 为配置服务器的副本集名字
1)mongo 配置服务器的配置
2)mongo 路由服务器配置
3)shard1 配置
4)shard2 配置
4, 启动 MongoDB
1) 启动配置
- /jq/MongoDB-Linux-x86_64-3.6.1/bin/mongod -f /jq/cp001/conf/config.conf
- /jq/MongoDB-Linux-x86_64-3.6.1/bin/mongod -f /jq/cp002/conf/config.conf
- /jq/MongoDB-Linux-x86_64-3.6.1/bin/mongod -f /jq/cp003/conf/config.conf
2) 启动复制集
- /jq/MongoDB-Linux-x86_64-3.6.1/bin/mongod -f /jq/cp001/conf/shard1.conf
- /jq/MongoDB-Linux-x86_64-3.6.1/bin/mongod -f /jq/cp002/conf/shard1.conf
- /jq/MongoDB-Linux-x86_64-3.6.1/bin/mongod -f /jq/cp003/conf/shard1.conf
- /jq/MongoDB-Linux-x86_64-3.6.1/bin/mongod -f /jq/cp001/conf/shard2.conf
- /jq/MongoDB-Linux-x86_64-3.6.1/bin/mongod -f /jq/cp002/conf/shard2.conf
- /jq/MongoDB-Linux-x86_64-3.6.1/bin/mongod -f /jq/cp003/conf/shard2.conf
3) 启动路由
/jq/MongoDB-Linux-x86_64-3.6.1/bin/mongos -f /jq/cp001/conf/mongos.conf
* 由于是同一台主机, 只执行一个配置
5, 登陆任意一个 config server 节点, 配置副本集
/jq/MongoDB-Linux-x86_64-3.6.1/bin/mongo --port 20011
- # 设置变量 这里的 id 号, 要和配置文件中的副本集名称相同
- config = {
- _id : "configs",
- members : [
- {
- _id : 0, host : "127.0.0.1:20011"
- },
- {
- _id : 1, host : "127.0.0.1:20012"
- },
- {
- _id : 2, host : "127.0.0.1:20013"
- }
- ]
- }
- # 初始化副本集
- rs.initiate(config)
6, 配置 shard
1) 配置 shard1, 进入任意一个 shard1 节点
/jq/MongoDB-Linux-x86_64-3.6.1/bin/mongo --port 20031
- # 设置变量
- config = {
- _id : "shard1",
- members : [
- {
- _id : 0, host : "127.0.0.1:20031"
- },
- {
- _id : 1, host : "127.0.0.1:20032"
- },
- {
- _id : 2, host : "127.0.0.1:20033"
- }
- ]
- }
- # 设置 shard1 初始化
- rs.initiate(config)
2) 配置 shard2, 进入任意一个 shard2 节点
/jq/MongoDB-Linux-x86_64-3.6.1/bin/mongo --port 20041
- # 设置变量
- config = {
- _id : "shard2",
- members : [
- {
- _id : 0, host : "127.0.0.1:20041"
- },
- {
- _id : 1, host : "127.0.0.1:20042"
- },
- {
- _id : 2, host : "127.0.0.1:20043"
- }
- ]
- }
- # 设置 shard2 初始化
- rs.initiate(config)
7, 启用分片
登陆任意一个 mongos
/jq/MongoDB-Linux-x86_64-3.6.1/bin/mongo --port 20001
切换到 admin 数据库
use admin
串联路由服务器与分配副本集
- sh.addShard("shard1/127.0.0.1:20031,127.0.0.1:20032,127.0.0.1:20033")
- sh.addShard("shard2/127.0.0.1:20041,127.0.0.1:20042,127.0.0.1:20043")
查看状态
sh.status()
8, 测试分片
* 不启用分片
- use admin
- # 指定 testdb 分片生效
- db.runCommand( {
- enablesharding :"testdb"
- });
- # 指定数据库里需要分片的集合和片键
- db.runCommand( {
- shardcollection : "testdb.table1",key : {
- id: 1
- }
- } )
- for (var i = 1;i<=10000;i++) db.table1.save({
- id:i,"test1":"item1"
- }); WriteResult({
- "nInserted" : 1
- });
- # 查看状态
- db.table1.stats();
* 启用分片
- use admin
- # 指定 testdb 分片生效
- db.runCommand( {
- enablesharding :"testdb2"
- });
- # 指定数据库里需要分片的集合和片键
- db.runCommand( {
- shardcollection : "testdb2.table",key : {
- _id: "hashed"
- }
- })
- for (var i = 1;i<=10000;i++) db.table.save({
- id:i,"test1":"item1"
- }); WriteResult({
- "nInserted" : 1
- });
- # 查看状态
- db.table1.stats();
停止 MongoDB
- /jq/MongoDB-Linux-x86_64-3.6.1/bin/mongod -f /jq/cp001/conf/config.conf --shutdown
- /jq/MongoDB-Linux-x86_64-3.6.1/bin/mongod -f /jq/cp002/conf/config.conf --shutdown
- /jq/MongoDB-Linux-x86_64-3.6.1/bin/mongod -f /jq/cp003/conf/config.conf --shutdown
- /jq/MongoDB-Linux-x86_64-3.6.1/bin/mongod -f /jq/cp001/conf/shard1.conf --shutdown
- /jq/MongoDB-Linux-x86_64-3.6.1/bin/mongod -f /jq/cp002/conf/shard1.conf --shutdown
- /jq/MongoDB-Linux-x86_64-3.6.1/bin/mongod -f /jq/cp003/conf/shard1.conf --shutdown
- /jq/MongoDB-Linux-x86_64-3.6.1/bin/mongod -f /jq/cp001/conf/shard2.conf --shutdown
- /jq/MongoDB-Linux-x86_64-3.6.1/bin/mongod -f /jq/cp002/conf/shard2.conf --shutdown
- /jq/MongoDB-Linux-x86_64-3.6.1/bin/mongod -f /jq/cp003/conf/shard2.conf --shutdown
- /jq/MongoDB-Linux-x86_64-3.6.1/bin/mongos -f /jq/cp001/conf/mongos.conf --shutdown
来源: http://www.bubuko.com/infodetail-3269488.html