How to Install MongoDB on CententOS7

Intalling MongoDB

Enabling MongoDB repository

/etc/yum.repos.d下创建一个新的YUM仓库配置文件mongodb-org.repo

cd /etc/yum.repos.d
vim mongodb-org.repo

In/etc/yum.repos.d/mongodb-org.repo

[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc

Installing MongoDB

yum install mongodb-org

将会安装:

  • mongodb-org-server - The mongod daemon, and corresponding init scripts and configurations.
  • mongodb-org-mongos - The mongos daemon.
  • mongodb-org-shell - The mongo shell, an interactive JavaScript interface to MongoDB, used to perform administrative tasks thought the command line.
  • mongodb-org-tools - Contains several MongoDB tools for to importing and exporting data, statistics, as well as other utilities.

Starting MongoDB

systemctl start mongod
systemctl enable mongod

Verifying MongoDB Installation

mongod

进入MongoDB命令行,然后查看版本

db.version()

将会输出MongoDB的版本

Configuring MongoDB

编辑/etc/mongod.conf

默认的配置已经在绝大多数情况下够用了。然而在生产环境中,推荐不要注释安全模块的配置项,并且开启授权:

security: 
    authorization: enabled

修改配置之后给,重启mongod服务

systemctl restart mongod

Creating Administrative MongoDB User

如果开启了MongoDB authentication,创建一个admin的MongoDB用户来访问和管理MongoDB实例

首先访问mongo命令行:

mongo

切换至admin数据库:

use admin

// output
switched to db admin

创建一个有userAdminAnyDatabase角色的新的用户mongoAdmin

退出mongo shell:

quit()

使用mongoAdmin进行登录:

mongo -u mongoAdmin -p --authenticationDatabase admin
use admin