部署单机Linux版本
下载地址:
社区版:https://www.mongodb.com/try/download/community
企业版:https://www.mongodb.com/try/download/enterprise
下载企业版tgz免安装包
# 创建文件夹
mkdir /mongodb
#解压文件
tar -zxvf mongodb-linux-x86_64-enterprise-rhel70-4.4.5.tgz
- 创建存放数据、日志、程序标识符文件夹
mkdir -p /var/log/mongdb/
touch /var/log/mongdb/mongod.log
mkdir -p /var/lib/mongo
# 内存文件夹,重开机后必须重建此文件夹才能成功启动mongodb
mkdir -p /var/run/mongodb/
- 修改启动 文件
vim /etc/mongod.conf
文件内容
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
storage:
dbPath: /var/lib/mongo
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /var/run/mongodb/mongod.pid
timeZoneInfo: /usr/share/zoneinfo
net:
port: 27017
bindIp: 0.0.0.0
启动、终止服务
免安装版,进入bin目录
启动MongoDB服务
# 用配置文件启动
./mongod -f /etc/mongod.conf
# 用命令启动
./mongod --logpath /mongodb/data/shard/log/mongod.log --logappend --dbpath /mongodb/data/shard/data --journal --pidfilepath /var/run/mongodb/mongod.pid --port 27018 --bind_ip 0.0.0.0 --fork
确认MongoDB服务正常运行
# 登陆MongoDB
./mongo
# 指定端口
./mongo --port 27017
终止MongoDB服务
- 方法一:登录后在MongoDB shell下执行终止指令
# 切换至admin数据库
use admin
# 关闭服务
db.shutdownServer()
- 方法二:在操作系统下终止服务
ps awx|grep mongod
kill pid
配置单机权限–Auth属性值
vim /etc/mongod.conf
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
storage:
dbPath: /var/lib/mongo
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /var/run/mongodb/mongod.pid
timeZoneInfo: /usr/share/zoneinfo
net:
port: 27017
bindIp: 0.0.0.0
security:
authorization: enabled
重启服务
./mongod -f /etc/mongod.conf
登录mongodb
./mongo --port 27017
./mongo --username admin --password admin
加入具有管理集群权限的账号
use admin
# 用root权限可以管理整个集群
db.createUser({
"user":"<User_name>",pwd:"<User_pwd>",roles:["root"]})
db.createUser({
"user":"admin",pwd:"admin",roles:["root"]})
配置自启动服务
关闭SELinux
# 检查SELinux是否开启
/usr/sbin/sestatus -v
vim /etc/selinux/config
SELINUX=disabled
重启机器
reboot
设置mongod.service权限
chmod 754 /usr/lib/systemd/system/mongod.service
编辑自启动服务文件
编辑MongoDB服务文件(mongod.service)
vim /usr/lib/systemd/system/mongod.service
[Unit]
Description=mongodb
After=network.target
[Service]
ExecStart=/mongodb/mongodb-linux-x86_64-enterprise-rhel70-4.4.5/bin/mongod --config /etc/mongod.conf
ExecStop=/mongodb/mongodb-linux-x86_64-enterprise-rhel70-4.4.5/bin/mongod --shutdown --config /etc/mongod.conf
PIDFile=/var/run/mongodb/mongod.pid
ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb
ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb
ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb
PermissionsStartOnly=true
PIDFile=/var/run/mongodb/mongod.pid
Type=forking
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false
[Install]
WantedBy=multi-user.target
[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
RuntimeDirectory=mongodb
RuntimeDirectoryMode=0751
PIDFile=/var/run/mongodb/mongod.pid
ExecStart=/mongodb/mongodb-linux-x86_64-enterprise-rhel70-4.4.5/bin/mongod --config /etc/mongod.conf
ExecStop=/mongodb/mongodb-linux-x86_64-enterprise-rhel70-4.4.5/bin/mongod --shutdown --config /etc/mongod.conf
PrivateTmp=false
[Install]
WantedBy=multi-user.target
启动自启动服务
systemctl enable mongod.service
开启服务
systemctl start mongod.service
关闭自启动服务
systemctl disable mongod.service
查询服务状态
systemctl status mongod.service
停止服务
systemctl stop mongod.service