Fork me on GitHub

MongoDB安装

(1).解压安装文件

[gsunis@gsunis Desktop]$ tar zxvf mongodb-linux-x86_64-3.0.6.tgz

(2).将mongoDB文件移动到/usr/local/mongodb目录下

[root@gsunis Desktop]# mv mongodb-linux-x86_64-3.0.6 /usr/local/mongodb
export PATH=/usr/local/mongodb/bin:$PATH

(3).数据存储目录

[root@gsunis bin]# mkdir -p /home/gsunis/data/db
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 

(4).启动mongod服务

[root@gsunis Desktop]# cd /usr/local/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 /usr/local/mongodb/bin/
[root@gsunis bin]# ./mongo

(6).编辑mongo配置文件

sudo vi conf.cnf

//conf.cnf  
rest=true  
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  
rest=true  
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  
rest=true  
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  
rest=true  
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  
rest=true  
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  
rest=true  
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  
rest=true  
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')  
/usr/local/mongodb/bin/mongod -f /home/gsunis/data/"$instance".cnf ;;  
   'stop')  
/usr/local/mongodb/bin/mongod -f /home/gsunis/data/"$instance".cnf --shutdown;;  
   'restart')  
/usr/local/mongodb/bin/mongod -f /home/gsunis/data/"$instance".cnf --shutdown  
/usr/local/mongodb/bin/mongod -f /home/gsunis/data/"$instance".cnf;;  
esac  

(8).启动docker实例的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 conf start 
/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 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 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  

正常关闭mongoDB服务

./mongo
use admin
db.shutdownServer()

(9).shard服务器副本集的建立

./mongo --port 47017

config = {_id: 'shard1', members: [
 {_id: 0, host: '10.62.0.128:47017'},
 {_id: 1, host: '10.62.0.159:47017'}]
 }
rs.initiate(config)

Ctrl + C 退出

./mongo --port 47018

config = {_id: 'shard2', members: [
 {_id: 0, host: '10.62.0.128:47018'},
 {_id: 1, 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.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.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.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.159:47022'}]
 }
rs.initiate(config)

本文标题:MongoDB安装

文章作者:行锋

发布时间:2019年02月14日 - 16:02

最后更新:2019年02月14日 - 16:02

原始链接:https://chetaofeng.github.io/2019/02/14/数据库-2018-12-10-MongoDB搭建/

许可协议: 署名-非商业性使用 转载请保留原文链接及作者

-------------本文结束感谢您的阅读-------------
坚持原创技术分享,您的支持将鼓励我继续创作!