系统环境
Centos7.6.181
软件:mongodb-linux-x86_64-3.6.12.tgz
[root@server10 ~]# tar zxf mongodb-linux-x86_64-3.6.12.tgz
[root@server10 ~]# mv mongodb-linux-x86_64-3.6.12 mongodb
[root@server10 ~]# mkdir /usr/local/mongodb
[root@server10 ~]# cd mongodb/
[root@server10 mongodb]# ls
bin LICENSE-Community.txt MPL-2 README THIRD-PARTY-NOTICES
[root@server10 mongodb]# cp -r /usr/local/mongodb/
[root@server10 mongodb]# cd /usr/local/mongodb/
[root@server10 mongodb]# mkdir -p /home/data/mongodb/mongodb_data
[root@server10 mongodb]# mkdir -p /home/data/mongodb/mongodb_log
#建立数据目录和日志目录
[root@server10 home]# /usr/local/mongodb/bin/mongod --port 27017 --fork --dbpath=/home/data/mongodb/mongodb_data/
--logpath=/home/data/mongodb/mongodb_log/mongodb.log --logappend
about to fork child process, waiting until server is ready for connections.
forked process: 3346
child process started successfully, parent exiting #起一个实例
[root@server10 ~]# netstat -antlp
PID/Program name
tcp 0 0 127.0.0.1:27017 0.0.0.0:
LISTEN 3346/mongod #看进程
[root@server10 ~]# cd /usr/local/mongodb/bin/
[root@server10 bin]# ls
bsondump mongod mongofiles mongoreplay mongostat
install_compass mongodump mongoimport mongorestore mongotop
mongo mongoexport mongoperf mongos
[root@server10 bin]# ./mongo #测试

2+3
5
2*6
12
use admin
switched to db admin
db.shutdownServer() #关闭数据库
exit
[root@server10bin]#./mongod -dbpath=/home/data/mongodb/mongodb_data/
[root@server10 bin]# cd /usr/local/mongodb/
[root@server10 mongodb]# vim mongodb.conf #自定义配置文件
port=27017
dbpath=/home/data/mongodb/mongodb_data/
logpath=/home/data/mongodb/mongodb_log/mongodb.log
logappend=true
[root@server10 mongodb]# ./bin/mongod -f mongodb.conf #加载配置文件,进程可令起终端查看
[root@server10 ~]# cd /etc/init.d/
[root@server10 init.d]# vim mongodb #写启动脚本
#!/bin/sh

BEGIN INIT INFO

Short-Description: mongodb

Description: mongo db server

END INIT INFO

PROGRAM=/usr/local/mongodb/bin/mongod
MONGOPID=ps -ef | grep 'mongod' | grep -v grep | awk '{print $2}'

test -x $PROGRAM || exit 0

case "$1" in
start)
ulimit -n 3000
echo "Starting MongoDB server"
$PROGRAM --fork --quiet -f /usr/local/mongodb/mongodb.conf
echo "Started MongoDB server"
;;
stop)
echo "Stopping MongoDB server"
if [ ! -z "$MONGOPID" ]; then
kill -15 $MONGOPID
fi
echo "Stopped MongoDB server"
;;
status)
;;
*)
echo "Usage: mongodb {start|stop|status}"
exit 1
esac

exit 0

[root@server10 init.d]# chmod +x mongodb
[root@server10 init.d]# /etc/init.d/mongodb start
Starting MongoDB server
about to fork child process, waiting until server is ready for connections.
forked process: 3735
child process started successfully, parent exiting
Started MongoDB server
[root@server10 mongodb]# netstat -antlp |grep :27017
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 3735/mongod
[root@server10 bin]# ./mongo

2+5
7
use xr
switched to db xr
db
xr
show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
db.xr.insert({"name":"zhang"}) #建库xr须插入数据才可show
WriteResult({ "nInserted" : 1 })
show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
xr 0.000GB
db.shutdownServer() #需要admin关闭数据库
shutdown command only works with the admin database; try 'use admin'
use admin
switched to db admin
db.shutdownServer()
复制集多节点:
[root@server10 ~]# vim ~/.bashrc #加环境变量
export PATH=/usr/local/mongodb/bin:$PATH
[root@server10 ~]# source ~/.bashrc
[root@server10 ~]# cd /home/data/
[root@server10 data]# mkdir {n1,n2,n3}
[root@server10 data]# mongod --replSet repSetTest --dbpath /home/data/n1 --logpath /home/data/n1/n1.log --port 27000 --smallfiles --oplogSize 128 --fork

[root@server10 data]# mongod --replSet repSetTest --dbpath /home/data/n2 --logpath /home/data/n2/n2.log --port 27001 --smallfiles --oplogSize 128 --fork

[root@server10 data]# mongod --replSet repSetTest --dbpath /home/data/n3 --logpath /home/data/n3/n3.log --port 27002 --smallfiles --oplogSize 128 --fork
依次起3个节点
[root@server10 ~]# netstat -antlp |grep mongod #查看进程
tcp 0 0 127.0.0.1:27000 0.0.0.0: LISTEN 4299/mongod
tcp 0 0 127.0.0.1:27001 0.0.0.0:
LISTEN 3866/mongod
tcp 0 0 127.0.0.1:27002 0.0.0.0:* LISTEN
[root@server10 data]# mongo localhost:27000

db.person.insert({name:'181',age:28})
WriteResult({ "writeError" : { "code" : 10107, "errmsg" : "not master" } }) #提示非主节点

cfg = { # 添加复制集的配置文件
... '_id':'repSetTest',
... 'members':[
... {'_id':0, 'host': 'localhost:27000'},
... {'_id':1, 'host': 'localhost:27001'},
... {'_id':2, 'host': 'localhost:27002'}
... ]
... }

rs.initiate(cfg) #初始化配置文件
repSetTest:SECONDARY> rs.status() #查看状态提示27000为主节点,其余2个端口为辅助节点

repSetTest:PRIMARY> db.isMaster() #查找master
repSetTest:PRIMARY> db.replTest.insert({_id:1,value:'123'})
WriteResult({ "nInserted" : 1 })
查看:
repSetTest:PRIMARY> db.replTest.findOne()
{ "_id" : 1, "value" : "123" }
[root@server10 data]# mongo localhost:27001 #开启slave查询
repSetTest:SECONDARY> rs.slaveOk(true)
repSetTest:SECONDARY> db.replTest.find()
{ "_id" : 1, "value" : "123" }
[root@server10 data]# kill -9 3895 #杀掉主节点进程
[root@server10 data]# mongo localhost:27000 #连接会报错
[root@server10 ~]# mongo localhost:27001 #//连接到27001端口,如下面的查询,27000连接失败,27002已经提升为PRIMARY

root@server10~]# mongod --replSet repSetTest --dbpath /home/data/n1 --logpath /home/data/n1/n1.log --port 27000 --smallfiles --oplogSize 128 --fork #重启27000此时27000为辅助副本