##集群环境:三台服务器
server1:10.62.0.128主;server2:10.62.0.135从;server3:10.62.0.159从
每一台分为6个片:shard1~shard6端口为47017,47018,47019,47020,47021,47022;每一片建三个副本集;配置服务器端口号:37017;路由服务器端口号:30000;
(1).解压安装文件
[gsunis@gsunis Desktop]$ tar zxvf mongodb-linux-x86_64-3.0.6.tgz
(2).将mongoDB文件移动到/home/gsunis/mongodb目录下
[root@gsunis Desktop]# mv mongodb-linux-x86_64-3.0.6 /home/gsunis/mongodb
(3).数据存储目录
[root@gsunis bin]# mkdir -p /home/gsunis/data/db
(4).启动mongod服务
[root@gsunis Desktop]# cd /home/gsunis/mongodb/bin
[root@gsunis bin]# ./mongod --port 27017 --dbpath=/home/gsunis/data/db
(5).启动mongo客户端
打开新的Terminal
[gsunis@gsunis Desktop]$ su
查看mongod服务启动情况
[root@gsunis Desktop]# netstat -ntpl | grep 27017
[root@gsunis Desktop]# cd /home/gsunis/mongodb/bin/
[root@gsunis bin]# ./mongo
###正常关闭mongoDB服务
./mongo
use admin
db.shutdownServer() 或者ps -ef|grep mongodb kill -2 pid
##以上是简单的安装mongodb,下面是开始建立集群,分片和建立副本集
###创建目录
mkdir -p /data/conf
mkdir -p /data/data1
mkdir -p /data/data2
mkdir -p /data/data3
mkdir -p /data/data4
mkdir -p /data/data5
mkdir -p /data/data6
(6).编辑mongo配置文件
sudo vi conf.cnf
//conf.cnf
port=37017
dbpath=/home/gsunis/data/conf
logappend = true
fork=true
logpath=/home/gsunis/data/conf/mongodb.log
nojournal=false
configsvr=true
profile=1
sudo vi db1.cnf
//db1.cnf
replSet=shard1
port=47017
dbpath=/home/gsunis/data/data1
logappend = true
fork=true
logpath=/home/gsunis/data/data1/mongodb.log
nojournal=false
shardsvr=true
profile=1
sudo vi db2.cnf
//db2.cnf
replSet=shard2
port=47018
dbpath=/home/gsunis/data/data2
logappend = true
fork=true
logpath=/home/gsunis/data/data2/mongodb.log
nojournal=false
shardsvr=true
profile=1
sudo vi db3.cnf
//db3.cnf
replSet=shard3
port=47019
dbpath=/home/gsunis/data/data3
logappend = true
fork=true
logpath=/home/gsunis/data/data3/mongodb.log
nojournal=false
shardsvr=true
profile=1
sudo vi db4.cnf
//db4.cnf
replSet=shard4
port=47020
dbpath=/home/gsunis/data/data4
logappend = true
fork=true
logpath=/home/gsunis/data/data4/mongodb.log
nojournal=false
shardsvr=true
profile=1
sudo vi db5.cnf
//db5.cnf
replSet=shard5
port=47021
dbpath=/home/gsunis/data/data5
logappend = true
fork=true
logpath=/home/gsunis/data/data5/mongodb.log
nojournal=false
shardsvr=true
profile=1
sudo vi db6.cnf
//db6.cnf
replSet=shard6
port=47022
dbpath=/home/gsunis/data/data6
logappend = true
fork=true
logpath=/home/gsunis/data/data6/mongodb.log
nojournal=false
shardsvr=true
profile=1
(7).编写启动mongo服务的脚本
sudo vi mg.sh
#!/bin/bash
instance=$1
action=$2
case "$action" in
'start')
/home/gsunis/mongodb/bin/mongod -f /home/gsunis/data/"$instance".cnf ;;
'stop')
/home/gsunis/mongodb/bin/mongod -f /home/gsunis/data/"$instance".cnf --shutdown;;
'restart')
/home/gsunis/mongodb/bin/mongod -f /home/gsunis/data/"$instance".cnf --shutdown
/home/gsunis/mongodb/bin/mongod -f /home/gsunis/data/"$instance".cnf;;
esac
(8).启动mongo服务脚本
sudo vi start.sh
#!/bin/bash
rm /data/conf/*.lock
rm /data/data1/*.lock
rm /data/data2/*.lock
rm /data/data3/*.lock
rm /data/data4/*.lock
rm /data/data5/*.lock
rm /data/data6/*.lock
/home/gsunis/data/mg.sh db1 start
/home/gsunis/data/mg.sh db2 start
/home/gsunis/data/mg.sh db3 start
/home/gsunis/data/mg.sh db4 start
/home/gsunis/data/mg.sh db5 start
/home/gsunis/data/mg.sh db6 start
sudo vi stop.sh
#!/bin/bash
/home/gsunis/data/mg.sh conf stop
/home/gsunis/data/mg.sh db1 stop
/home/gsunis/data/mg.sh db2 stop
/home/gsunis/data/mg.sh db3 stop
/home/gsunis/data/mg.sh db4 stop
/home/gsunis/data/mg.sh db5 stop
/home/gsunis/data/mg.sh db6 stop
###准备工作做好,先启动配置服务器(先启动主,依次启动两从机)
/home/gsunis/data/mg.sh conf start
###再启动路由服务器(先启动主,依次启动两从机)
/home/gsunis/mongodb/bin/mongos --configdb 10.62.0.128:37017,10.62.0.135:37017,10.62.0.159:37017 --port 30000 --chunkSize 1 --logpath /home/gsunis/data/mongos1.log --logappend --fork
###最后启动mongod(先启动主,依次启动两从机)
/home/gsunis/mongodb/data/start.sh
(9).shard服务器副本集的建立(只需要在主机上做即可,两从机会自动复制)
./mongo --port 47017
config = {_id: 'shard1', members: [
{_id: 0, host: '10.62.0.128:47017'},
{_id: 1, host: '10.62.0.135:47017'},
{_id: 2, host: '10.62.0.159:47017'}]
}
rs.initiate(config)
Ctrl + C 退出
- rs.add({"_id":2,“host”:“10.62.0.135:47017”}) #在线增加复制集
./mongo --port 47018
config = {_id: 'shard2', members: [
{_id: 0, host: '10.62.0.128:47018'},
{_id: 1, host: '10.62.0.135:47018'},
{_id: 2, host: '10.62.0.159:47018'}]
}
rs.initiate(config)
./mongo --port 47019
config = {_id: 'shard3', members: [
{_id: 0, host: '10.62.0.128:47019'},
{_id: 1, host: '10.62.0.135:47019'},
{_id: 2, host: '10.62.0.159:47019'}]
}
rs.initiate(config)
./mongo --port 47020
config = {_id: 'shard4', members: [
{_id: 0, host: '10.62.0.128:47020'},
{_id: 1, host: '10.62.0.135:47020'},
{_id: 2, host: '10.62.0.159:47020'}]
}
rs.initiate(config)
./mongo --port 47021
config = {_id: 'shard5', members: [
{_id: 0, host: '10.62.0.128:47021'},
{_id: 1, host: '10.62.0.135:47021'},
{_id: 2, host: '10.62.0.159:47021'}]
}
rs.initiate(config)
./mongo --port 47022
config = {_id: 'shard6', members: [
{_id: 0, host: '10.62.0.128:47022'},
{_id: 1, host: '10.62.0.135:47022'},
{_id: 2, host: '10.62.0.159:47022'}]
}
rs.initiate(config)
配置sharding:登录到mongos,添加shard节点
./mongo --port 30000
use admin
db.runCommand({addshard:"shard1/10.62.0.128:47017,10.62.0.159:47017,10.62.0.135:47017"});
db.runCommand({addshard:"shard2/10.62.0.128:47018,10.62.0.159:47018,10.62.0.135:47018"});
db.runCommand({addshard:"shard3/10.62.0.128:47019,10.62.0.159:47019,10.62.0.135:47019"});
db.runCommand({addshard:"shard4/10.62.0.128:47020,10.62.0.159:47020,10.62.0.135:47020"});
db.runCommand({addshard:"shard5/10.62.0.128:47021,10.62.0.159:47021,10.62.0.135:47021"});
db.runCommand({addshard:"shard6/10.62.0.128:47022,10.62.0.159:47022,10.62.0.135:47022"});
use config
db.shards.find()
启动路由服务出错
错误:
解决方法:
- (1).关闭所有mongo相关服务
- (2).先启配置服务 Config Server
- (3).启动路由服务 mongos
- (4).最后启动所有mongod服务 mongod
###远程copy
scp /home/gsunis/Tools/mongodb-linux-x86_64-3.0.6.tgz gsunis@10.62.0.135:/home/gsunis/