# CentOS7配置MongoDB

# 介绍

[TOC]

环境:CentOS7

# [官网 (opens new window)]下载mongodb-linux-x86_64-rhel62-3.4.3.tgz

# 通过FTP(WinSCP,FileZilla等)工具上传到“/apps”目录

# 使用解压命令解压到当前目录

tar -zvxf mongodb-linux-x86_64-rhel62-3.4.3.tgz
1

# 开启27017端口

firewall-cmd --zone=public --add-port=27017/tcp --permanent    (--permanent永久生效,没有此参数重启后失效)
#重新载入
firewall-cmd --reload
1
2
3

# 创建data文件夹用于存放数据,创建logs文件用于存放日志

cd mongodb-linux-x86_64-rhel62-3.4.3
mkdir data
touch logs
1
2
3

# 启动MongoDB

/apps/mongodb-linux-x86_64-rhel62-3.4.3/bin/mongod -dbpath=/apps/mongodb-linux-x86_64-rhel62-3.4.3/data -logpath=/apps/mongodb-linux-x86_64-rhel62-3.4.3/logs  --fork --auth
1

# 创建超级用户

cd /apps/mongodb-linux-x86_64-rhel62-3.4.3/bin
./mongo
use admin
db.createUser(
...   {
...     user: "admin",
...     pwd: "admin",
...     roles: [ { role: "root", db: "admin" } ]
...   }
... )
1
2
3
4
5
6
7
8
9
10

# 登陆admin用户

db.auth('admin','admin');
1

# 创建登陆用户

use test
db.createUser(
...   {
...     user: "test",
...     pwd: "test",
...     roles: [ { role: "readWrite", db: "test" } ]
...   }
... )
1
2
3
4
5
6
7
8

# 设置开机启动

#设置开机启动
#修改/etc/下的rc.local添加如下命令
/apps/mongodb-linux-x86_64-rhel62-3.4.3/bin/mongod -dbpath=/apps/mongodb-linux-x86_64-rhel62-3.4.3/data -logpath=/apps/mongodb-linux-x86_64-rhel62-3.4.3/logs  --fork --auth
1
2
3