MongoDB安装、配置

安装部署

  • 系统准备
(1)redhat或cnetos6.2以上系统
(2)系统开发包完整
(3)ip地址和hosts文件解析正常
(4)iptables防火墙&SElinux关闭
(5)关闭大页内存机制
  • 关闭大页内存机制
#永久关闭:root用户下/etc/rc.local最后添加如下代码
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
  echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
   echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi

#临时关闭:	
]# echo never > /sys/kernel/mm/transparent_hugepage/enabled		
]# echo never > /sys/kernel/mm/transparent_hugepage/defrag	

#其他系统关闭参照官方文档:https://docs.mongodb.com/manual/tutorial/transparent-huge-pages/
  • 关闭打开文件限制数
#修改/etc/security/limits.conf,最后加入
*  soft nofile 65535
*  hard nofile 65535
  • 创建mongod用户和组
]# useradd mongod
  • 创建mongo所需工作目录
]# mkdir -pv /mongodb/{conf,log,data}
mkdir: 已创建目录 "/mongodb"
mkdir: 已创建目录 "/mongodb/conf"
mkdir: 已创建目录 "/mongodb/log"
mkdir: 已创建目录 "/mongodb/data"
  • 下载软件包,解压后把bin目录下的内容复制到刚刚创建的目录
]# tar xf mongodb-linux-x86_64-rhel70-3.4.24.tgz 
]# mv mongodb-linux-x86_64-rhel70-3.4.24/bin/ /mongodb/
]# ls /mongodb/
bin  conf  data  log
  • 设置工作目录权限
]# chown -R mongod:mongod /mongodb/
  • 设置环境变量
]# vim /etc/profile.d/mongod.sh
export MONGO_PATH=/mongodb
export PATH=$PATH:$MONGO_PATH/bin
]# source  /etc/profile.d/mongod.sh                                      
  • 创建配置文件,注意文件权限
]# vim /mongodb/conf/mongodb.conf
logpath=/mongodb/log/mongodb.log
dbpath=/mongodb/data
port=27017
logappend=true
fork=true
  • 以mongod用户启动程序
]# su -s '/bin/bash' -c 'mongod -f /mongodb/conf/mongodb.conf' mongod
about to fork child process, waiting until server is ready for connections.
forked process: 81298
child process started successfully, parent exiting
  • 关闭程序
~]#  su -s '/bin/bash' -c 'mongod -f /mongodb/conf/mongodb.conf --shutdown' mongod
killing process with pid: 81392 

 配置文件

新版本的mongo官方建议使用yaml格式配置文件

#系统日志有关  
systemLog:
   destination: file        
   path: "/mongodb/log/mongodb.log"    #日志位置
   logAppend: true			          #日志以追加模式记录
#数据存储有关   
storage:
   journal:
      enabled: true
   dbPath: "/mongodb/data"            #数据路径的位置
#进程控制  
processManagement:
   fork: true                                  #后台守护进程
   pidFilePath: <string>	          #pid文件的位置,一般不用配置,可以去掉这行,自动生成到data中    
#网络配置有关   
net:			
   bindIp: <ip>                         #监听地址,如果不配置这行是监听在0.0.0.0
   port: <port>			      #端口号,默认不配置端口号,默认27017   
#安全验证有关配置      
security:
  authorization: enabled              #是否打开用户名密码验证

replication:
oplogSizeMB: <NUM>
replSetName: "<REPSETNAME>"
secondaryIndexPrefetch: "all"

sharding:
clusterRole: <string>
archiveMovedChunks: <boolean>

---for mongos only
replication:
localPingThresholdMs: <int>

sharding:
configDB: <string>
---

systemd接管mongodb

[root@db01 ~]# cat > /etc/systemd/system/mongod.service <<EOF
[Unit]
Description=mongodb 
After=network.target remote-fs.target nss-lookup.target
[Service]
User=mongod
Type=forking
ExecStart=/mongodb/bin/mongod --config /mongodb/conf/mongo.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/mongodb/bin/mongod --config /mongodb/conf/mongo.conf --shutdown
PrivateTmp=true  
[Install]
WantedBy=multi-user.target
EOF

[root@db01 ~]# systemctl restart mongod
[root@db01 ~]# systemctl stop mongod
[root@db01 ~]# systemctl start mongod

  

初学linux,每学到一点东西就写一点,如有不对的地方,恳请包涵!
原文地址:https://www.cnblogs.com/forlive/p/12699347.html