1.基本信息
版本 | 3.0.7 |
---|---|
安装机器 | 三台机器 |
账号 | hadoop |
源路径 | /opt/software/mongodb-linux-x86_64-3.0.7.tar.gz |
目标路径 | /opt/mongodb/ -> opt/mongodb-linux-x86_64-3.0.7 |
依赖关系 | 无 |
2.安装过程
1).使用hadoop账号解压到/opt/hadoop/目录下:
[hadoop @bgs -5p173-wangwenting opt]$ su hadoop [hadoop @bgs -5p173-wangwenting opt]$ cd /opt/software [hadoop @bgs -5p173-wangwenting software]$ tar -zxvf mongodb-${version}.tar.gz -C /opt |
2).设置软链接:
[hadoop @bgs -5p173-wangwenting software]$ cd /opt [hadoop @bgs -5p173-wangwenting opt]$ ln -s /opt/mongodb-${version} /opt/mongodb |
3).配置环境变量,并使其生效:
[hadoop @bgs -5p173-wangwenting opt]$ $ vim /etc/profile 添加以下内容: export MONGODB_HOME=/opt/mongodb export PATH=${MONGODB_HOME}/bin:$PATH 使环境变量生效: [hadoop @bgs -5p173-wangwenting opt]$ source /etc/profile |
4).在/opt/hadoop/mongodb/目录下建立data相关目录(须保证目录为空):
[hadoop @bgs -5p173-wangwenting opt]$ mkdir -p /opt/mongodb/data/configsvr [hadoop @bgs -5p173-wangwenting opt]$ mkdir -p /opt/mongodb/data/rs0- 0 [hadoop @bgs -5p173-wangwenting opt]$ mkdir -p /opt/mongodb/data/rs1- 0 |
5).在conf目录下修改configsvr.conf文件:
[hadoop @bgs -5p173-wangwenting opt]$ vim /opt/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文件:
[hadoop @bgs -5p173-wangwenting opt]$ vim /opt/mongodb/conf/mongos.conf 修改以下内容 path: "/opt/mongodb/logs/mongos.log" pidFilePath: "/opt/mongodb/mongos.pid" |
7).继续修改rs0.conf文件:
[hadoop @bgs -5p173-wangwenting opt]$ vim /opt/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文件:
[hadoop @bgs -5p173-wangwenting opt]$ vim /opt/mongodb/conf/rs1.conf 修改以下内容 path: "/opt/mongodb/logs/rs1-0.log" pidFilePath: "/opt/mongodb/rs1-0.pid" dbPath: "/opt/mongodb/data/rs1-0" |
9).在bgs-5p173-wangwenting上复制mongodb到bgs-5p174-wangwenting机器并按照步骤3修改环境变量:
[hadoop @bgs -5p173-wangwenting opt]$ scp -r /opt/mongodb-${version} hadoop @bgs -5p174-wangwenting:/opt [hadoop @bgs -5p174-wangwenting opt]$ ln -s /opt/mongodb-${version} /opt/mongodb [hadoop @bgs -5p174-wangwenting opt]$ vim /etc/profile 添加以下内容: export MONGODB_HOME=/opt/mongodb export PATH=${MONGODB_HOME}/bin:$PATH 使环境变量生效: [hadoop @bgs -5p173-wangwenting hadoop]$ source /etc/profile |
10).修改data目录下的文件夹名:
[hadoop @bgs -5p174-wangwenting opt]$ mv /opt/mongodb/data/rs0- 0 /opt/mongodb/data/rs0- 1 [hadoop @bgs -5p174-wangwenting opt]$ mv /opt/mongodb/data/rs1- 0 /opt/mongodb/data/rs1- 1 |
11).在bgs-5p174-wangwenting上修改rs0.conf文件:
[hadoop @bgs -5p174-wangwenting opt]$ vim /opt/mongodb/conf/rs0.conf 修改以下内容 path: "/opt/mongodb/logs/rs0-1.log" pidFilePath: "/opt/mongodb/rs0-1.pid" dbPath: "/opt/mongodb/data/rs0-1" |
12).在bgs-5p174-wangwenting修改rs1.conf文件:
[hadoop @bgs -5p174-wangwenting opt]$ vim /opt/mongodb/conf/rs1.conf 修改以下内容 path: "/optp/mongodb/logs/rs1-1.log" pidFilePath: "/opt/mongodb/rs1-1.pid" dbPath: "/opt/mongodb/data/rs1-1" |
13).在bgs-5p173-wangwenting上复制mongodb到bgs-5p175-wangwenting机器:
[hadoop @bgs -5p173-wangwenting opt]$ scp -r /opt/mongodb-${version} hadoop @bgs -5p175-wangwenting:/opt [hadoop @bgs -5p175-wangwenting opt]$ ln -s /opt/mongodb-${version} /opt/mongodb |
14).在bgs-5p175-wangwenting修改data目录下的文件夹名:
[hadoop @bgs -5p175-wangwenting opt]$ mv /opt/mongodb/data/rs0- 0 /opt/mongodb/data/rs0- 2 [hadoop @bgs -5p175-wangwenting opt]$ mv /opt/mongodb/data/rs1- 0 /opt/mongodb/data/rs1- 2 |
15).在bgs-5p175-wangwenting上修改rs0.conf文件:
[hadoop @bgs -5p175-wangwenting hadoop]$ vim /opt/mongodb/conf/rs0.conf 修改以下内容 path: "/opt/mongodb/logs/rs0-2.log" pidFilePath: "/opt/mongodb/rs0-2.pid" dbPath: "/opt/mongodb/data/rs0-2" |
16).在bgs-5p175-wangwenting上修改rs1.conf文件:
[hadoop @bgs -5p175-wangwenting hadoop]$ vim /opt/mongodb/conf/rs1.conf 修改以下内容 path: "/opt/mongodb/logs/rs1-2.log" pidFilePath: "/opt/mongodb/rs1-2.pid" dbPath: "/opt/mongodb/data/rs1-2" |
17).rs0配置:三台机器顺序启动mongod的服务:
[hadoop @bgs -5p173-wangwenting opt]$ mongod --config /opt/mongodb/conf/rs0.conf [hadoop @bgs -5p174-wangwenting opt]$ mongod --config /opt/mongodb/conf/rs0.conf [hadoop @bgs -5p175-wangwenting opt]$ mongod --config /opt/mongodb/conf/rs0.conf |
18).在baseline01上执行连接到mongod实例:
[hadoop @bgs -5p173-wangwenting opt]$ mongo 172.24 . 5.173 : 27017 连接成功后执行 >rs0conf = { _id: "rs0" , members: [ { _id: 0 , host: "172.24.5.173:27017" } ] } >rs.initiate( rs0conf ) >rs.add( "172.24.5.174:27017" ) >rs.add( "172.24.5.175:27017" ) >rs.status() //半分钟后执行该命令,若出现一主两从则说明rs0配置成功 |
rs1配置:
三台机器顺序启动mongod的服务:
[hadoop @bgs -5p173-wangwenting opt]$ mongod --config /opt/mongodb/conf/rs1.conf [hadoop @bgs -5p174-wangwenting opt]$ mongod --config /opt/mongodb/conf/rs1.conf [hadoop @bgs -5p175-wangwenting opt]$ mongod --config /opt/mongodb/conf/rs1.conf |
在bgs-5p173-wangwenting上执行连接到mongod实例:
[hadoop @bgs -5p173-wangwenting opt]$ mongo 172.24 . 5.173 : 27018 >rs1conf = { _id: "rs1" , members: [ { _id: 0 , host: "172.24.5.173:27018" } ] } >rs.initiate( rs1conf ) >rs.add( "172.24.5.174:27018" ) >rs.add( "172.24.5.175:27018" ) >rs.status() //半分钟后执行该命令,若出现一主两从则说明rs1配置成功 |
19).三台机器分别启动配置服务器:
[hadoop @bgs -5p173-wangwenting opt]$ mongod --config /opt/mongodb/conf/configsvr.conf [hadoop @bgs -5p174-wangwenting opt]$ mongod --config /opt/mongodb/conf/configsvr.conf [hadoop @bgs -5p175-wangwenting opt]$ mongod --config /opt/mongodb/conf/configsvr.conf |
20).三台机器分别启动mongos路由:
[hadoop @bgs -5p173-wangwenting opt]$ mongos --config /opt/mongodb/conf/mongos.conf [hadoop @bgs -5p174-wangwenting opt]$ mongos --config /opt/mongodb/conf/mongos.conf [hadoop @bgs -5p175-wangwenting opt]$ mongos --config /opt/mongodb/conf/mongos.conf |
21).在172.24.5.173上连接到mongos:
[hadoop @bgs -5p175-wangwenting opt]$ mongo 172.24 . 5.173 : 27000 |
22).切换到admin数据库,添加分片:
mongos> use admin mongos> db.runCommand({addshard: "rs0/172.24.5.173:27017,172.24.5.174:27017,172.24.5.175:27017" }) mongos> db.runCommand({addshard: "rs1/172.24.5.173:27018,172.24.5.174:27018,172.24.5.175: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).测试插入数据:
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上分别有数据,说明部署成功 |