MongoDB Community版安装参考官方教程
说明:通过配置yum源,自动下载.rpm安装,
适用于CentOS 6或7、Red Hat Enterpriser 的64位操作系统。
环境:CentOS 7
一、配置MongoDB的yum源
在 /etc/yum.repos.d/ 目录下,创建mongodb-org-3.6.repo文件,使用yum。
vim /etc/yum.repos.d/mongodb-org-3.6.repo
往文件里添加,yum源信息,MongoDB 3.6 的内容如下
[mongodb-org-3.6]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc
清除缓存,使新配置yum源生效。
yum clean all
yum makecache
二、安装MongoDB 程序
可通以下其中一种方式安装
1、安装3.6最新稳定版
包含mongodb-org-server、mongodb-org-shell、mongodb-org-mongos、mongodb-org-tools
yum install -y mongodb-org
2、安装各程序包指定版本
yum install -y mongodb-org-3.6.2 mongodb-org-server-3.6.2 mongodb-org-shell-3.6.2 mongodb-org-mongos-3.6.2 mongodb-org-tools-3.6.2
另外,为防止意外升级,排除MongoDB相关包,可调整yum配置,添加以下内容到 /etc/yum.conf文件中。
exclude = mongodb-org,mongodb-org-server,mongodb-org-shell,mongodb-org-mongos,mongodb-org-tools
查看 /etc/selinux/config
如果使用SELinux,
当 SELINUX=enforcing 时,则需要允许MongoDB访问相关端口,如:
semanage port -a -t mongod_port_t -p tcp 27017
或者直接调整SELinux模式调为disabled 禁用,再或者为permissive
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=permissive
我这里调整为permissive,reboot重启系统生效。
三、使用程序
1、启动服务
service mongod start
start成功后,可选择把服务设置为开机自启。
chkconfig mongod on
2、使用MongoDB
通过本地使用mongo命令,启动mongo shell。
mongo --host 127.0.0.1:27017
Control+C退出。
另外,可通过 /etc/mongod.conf 知晓数据和日志文件存放哪里,尝试做目录的调整。
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# Where and how to store data.
storage:
dbPath: /var/lib/mongo
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1 # Listen to local interface only, comment to listen on all interfaces.
四、卸载程序
1、先停止服务
service mongod stop
2、移除相关包
yum erase $(rpm -qa | grep mongodb-org)
3、清理数据目录(数据文件和日志文件)
rm -r /var/log/mongodb
rm -r /var/lib/mongo