部署安装
1.安装支持软件包
yum -y install openssl-devel libcurl
2.解压缩MongoDB软件包
tar -zxvf mongodb-linux-x86_64-4.0.8.tgz
3.重命名并移动目录至/usr/local/目录下
mv mongodb-linux-x86_64-4.0.8 /usr/local/mongodb
4.创建MongoDB的数据存储目录、日志存储目录及日志文件
mkdir -p /data/mongodb/db
mkdir -p /data/mongodb/conf/
mkdir -p /data/mongodb/logs/
touch /data/mongodb/logs/mongodb.log
chmod -R 777 /data/mongodb/logs/mongodb.log
5.创建配置文件(启动参数)
vim /data/mongodb/conf/mongodb.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: /data/mongodb/logs/mongodb.log ##mongodb的日志文件路径
#Where and how to store data.
storage:
dbPath: /data/mongodb/db ##mongodb的数据文件路径
journal:
enabled: true
#engine:
#mmapv1:
#wiredTiger:
#how the process runs
processManagement:
fork: true #fork and run in background以守护进程的方式运行,创建服务器进程后台运行
pidFilePath: /data/mongodb/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo #Default sets the time zone database path to /usr/share/zoneinfo by default.
#network interfaces
net:
port: 27017 ##the MongoDB instance listens Default: 27017
bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces.
#security:
#operationProfiling:
#replication: ##删除“#”,开启复制集功能
## replSetName: test-rc ##名称为test-rc
#sharding:
##Enterprise-Only Options
#auditLog:
#snmp:
6.MongoDB 的可执行文件位于 bin 目录下,所以可以将其添加到 PATH 路径中:
vi /etc/profile
在文件末尾追加以下语句
#mongodb环境变量
export PATH=$PATH:/usr/local/mongodb/bin
7.重新加载环境变量
source /etc/profile
8.设置MongoDB开机自启动
vim /etc/rc.local
# 启动mongodb实例
rm -rf /data/mongodb/db/mongod.lock
/usr/local/mongodb/bin/mongod -f /data/mongodb/conf/mongodb.conf
9.启动MongoDB数据库
mongod -f /data/mongodb/conf/mongodb.conf
10.检查MongoDB进程
netstat -tunlp|grep mongod
或(如果没有命令执行ps或者
yum install net-tools
)
ps -aux|grep mongod
11.停止MongoDB数据库
mongod -f /data/mongodb/conf/mongodb.conf --shutdown
12.本地客户端连接并访问MongoDB数据库(默认端口为:27017)
mongo