首页 > 代码库 > linux上安装mongodb
linux上安装mongodb
安装版本:3.0.7
安装机器:所有机器
安装过程:
1.使用hadoop账号解压到/opt/hadoop/目录下:
$ tar -zxvf mongodb-${version}.tar.gz -C /opt/hadoop/
2.设置软链接:
$ ln -s /opt/hadoop/mongodb-${version} /opt/hadoop/mongodb
3.配置环境变量,并使其生效:
# vim /etc/profile
export MONGODB_HOME=/opt/hadoop/mongodb
export PATH=${MONGODB_HOME}/bin:$PATH
# source /etc/profile
4.在/opt/hadoop/mongodb/目录下建立data相关目录(须保证目录为空):
$ mkdir -p /opt/hadoop/mongodb/data/configsvr
$ mkdir -p /opt/hadoop/mongodb/data/rs0-0
$ mkdir -p /opt/hadoop/mongodb/data/rs1-0
5.在conf目录下修改configsvr.conf文件:
$ vim /opt/hadoop/mongodb/conf/configsvr.conf
修改以下内容
path: "/opt/hadoop/mongodb/logs/configsvr.log"
pidFilePath: "/opt/hadoop/mongodb/configsvr.pid"
dbPath: "/opt/hadoop/mongodb/data/configsvr"
6.继续修改mongos.conf文件:
$ vim /opt/hadoop/mongodb/conf/mongos.conf
修改以下内容
path: "/opt/hadoop/mongodb/logs/mongos.log"
pidFilePath: "/opt/hadoop/mongodb/mongos.pid"
7.继续修改rs0.conf文件:
$ vim /opt/hadoop/mongodb/conf/rs0.conf
修改以下内容
path: "/opt/hadoop/mongodb/logs/rs0-0.log"
pidFilePath: "/opt/hadoop/mongodb/rs0-0.pid"
dbPath: "/opt/hadoop/mongodb/data/rs0-0"
8.继续修改rs1.conf文件:
$ vim /opt/hadoop/mongodb/conf/rs1.conf
修改以下内容
path: "/opt/hadoop/mongodb/logs/rs1-0.log"
pidFilePath: "/opt/hadoop/mongodb/rs1-0.pid"
dbPath: "/opt/hadoop/mongodb/data/rs1-0"
9.在baseline01上复制mongodb到baseline02机器并按照步骤3修改环境变量:
$ scp -r /opt/hadoop/mongodb-${version} hadoop@baseline02:/opt/hadoop
$ ln -s /opt/hadoop/mongodb-${version} /opt/hadoop/mongodb
10.修改data目录下的文件夹名:
$ mv /opt/hadoop/mongodb/data/rs0-0 /opt/hadoop/mongodb/data/rs0-1
$ mv /opt/hadoop/mongodb/data/rs1-0 /opt/hadoop/mongodb/data/rs1-1
11.在baseline02上修改rs0.conf文件:
$ vim /opt/hadoop/mongodb/conf/rs0.conf
修改以下内容
path: "/opt/hadoop/mongodb/logs/rs0-1.log"
pidFilePath: "/opt/hadoop/mongodb/rs0-1.pid"
dbPath: "/opt/hadoop/mongodb/data/rs0-1"
12.在baseline02上修改rs1.conf文件:
$ vim /opt/hadoop/mongodb/conf/rs1.conf
修改以下内容
path: "/opt/hadoop/mongodb/logs/rs1-1.log"
pidFilePath: "/opt/hadoop/mongodb/rs1-1.pid"
dbPath: "/opt/hadoop/mongodb/data/rs1-1"
13.在baseline01上复制mongodb到baseline03机器:
$ scp -r /opt/hadoop/mongodb-${version} hadoop@baseline03:/opt/hadoop
$ ln -s /opt/hadoop/mongodb-${version} /opt/hadoop/mongodb
14.修改data目录下的文件夹名:
$ mv /opt/hadoop/mongodb/data/rs0-0 /opt/hadoop/mongodb/data/rs0-2
$ mv /opt/hadoop/mongodb/data/rs1-0 /opt/hadoop/mongodb/data/rs1-2
15.在baseline03上修改rs0.conf文件:
$ vim /opt/hadoop/mongodb/conf/rs0.conf
修改以下内容
path: "/opt/hadoop/mongodb/logs/rs0-2.log"
pidFilePath: "/opt/hadoop/mongodb/rs0-2.pid"
dbPath: "/opt/hadoop/mongodb/data/rs0-2"
16.在baseline03上修改rs1.conf文件:
$ vim /opt/hadoop/mongodb/conf/rs1.conf
修改以下内容
path: "/opt/hadoop/mongodb/logs/rs1-2.log"
pidFilePath: "/opt/hadoop/mongodb/rs1-2.pid"
dbPath: "/opt/hadoop/mongodb/data/rs1-2"
17.rs0配置:
三台机器顺序启动mongod的服务:
$ mongod --config /opt/hadoop/mongodb/conf/rs0.conf
18.在baseline01上执行连接到mongod实例:
$ mongo baseline01:27017
连接成功后执行:
>rs0conf = {
_id: "rs0",
members: [
{
_id: 0,
host: "baseline01:27017"
}
]
}
>rs.initiate( rs0conf )
>rs.add("baseline02:27017")
>rs.add("baseline03:27017")
>rs.status() //半分钟后执行该命令,若出现一主两从则说明rs0配置成功
rs1配置:
三台机器顺序启动mongod的服务:
$ mongod --config /opt/hadoop/mongodb/conf/rs1.conf
在baseline01上执行连接到mongod实例:
$ mongo baseline01:27018
>rs1conf = {
_id: "rs1",
members: [
{
_id: 0,
host: "baseline01:27018"
}
]
}
>rs.initiate( rs1conf )
>rs.add("baseline02:27018")
>rs.add("baseline03:27018")
>rs.status() //半分钟后执行该命令,若出现一主两从则说明rs1配置成功
19.三台机器分别启动配置服务器:
$ mongod --config /opt/hadoop/mongodb/conf/configsvr.conf
20.三台机器分别启动mongos路由:
$ mongos --config /opt/hadoop/mongodb/conf/mongos.conf
21.在baseline01上连接到mongos:
$ mongo baseline01:27000
22.切换到admin数据库,添加分片:
mongos> use admin
mongos>db.runCommand({addshard:"rs0/baseline01:27017,baseline02:27017,baseline03:27017"})
mongos>db.runCommand({addshard:"rs1/baseline01:27018,baseline02:27018,baseline03:27018"})
23.查看分片状态:
mongos>db.runCommand({listshards:1})
24.为testdb数据库启动分片:
mongos>db.runCommand({enablesharding:"testdb"})
mongos>db.runCommand({shardcollection:"testdb.test_table",key:{id:1}})
25.在baseline01上连接到mongos:
$ mongo baseline01:27000
26测试插入数据:
mongos>use testdb
mongos>for(var i = 1; i <= 50000; i++) db.test_table.insert( { "id":i, "name":"str"+i, "date":new Date() } )
WriteResult({ "nInserted" : 1 })
mongos>db.test_table.stats()
查看分片状态,若rs0和rs1上分别有数据,说明部署成功
linux上安装mongodb