如何在CentOS 7上安装MongoDB

In this article, we dive in and see how you can install MongoDB on CentOS 7. MongoDB is a free, flexible and opensource NoSQL database, different from the regular SQL databases like PostgreSQL and MySQL. In MongoDB, data is stored in JSON format and does not require a schema.

在本文中,我们将深入探讨如何在CentOS 7上安装MongoDB。MongoDB是一个免费,灵活且开源的NoSQL数据库,与诸如PostgreSQL和MySQL的常规SQL数据库不同。 在MongoDB中,数据以JSON格式存储,不需要架构。

安装MongoDB (Installing MongoDB)

At the time of penning down this guide, the latest version of MongoDB was version 4.0. Before proceeding with the installation process, confirm the latest release here.

在撰写本指南时,MongoDB的最新版本为4.0版。 在继续安装过程之前,请在此处确认最新版本。

步骤1:启用MongoDB存储库 (Step 1: Enabling MongoDB repository)

Using your favorite text editor, create a new YUM repository called mongodb-org.repo

使用您喜欢的文本编辑器,创建一个名为mongodb-org.repo的新YUM存储库

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

Next, add the following content.

接下来,添加以下内容。

[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

步骤2:安装MongoDB (Step 2: Installing MongoDB)

With the MongoDB repository enabled, install MongoDB using the command below.

启用MongoDB存储库后,使用以下命令安装MongoDB。

$ sudo yum install mongodb-org

Sample Output

样本输出

When prompted to import the MongoDB GPG key, type y and press ENTER.

当提示您导入MongoDB GPG密钥时,键入y并按ENTER

Sample Output

样本输出

As part of the Mongo-org package, the following packages will also be installed.

作为Mongo-org软件包的一部分,还将安装以下软件包。

  1. mongodb-org-server – This is the mongod daemon, plus corresponding init scripts and configurations.

    mongodb-org-server –这是mongod守护程序,以及相应的初始化脚本和配置。
  2. mongodb-org-mongos – This is the mongos daemon.

    mongodb-org-mongos –这是mongos守护程序。
  3. mongodb-org-shell – This is the mongo shell, which is an interactive JavaScript interface to MongoDB, used to perform administrative tasks through the command line.

    mongodb-org-shell –这是mongo shell,它是MongoDB的交互式JavaScript接口,用于通过命令行执行管理任务。
  4. mongodb-org-tools – This package contains several MongoDB tools used for importing and exporting data, statistics, as well as other utilities.

    mongodb-org-tools –该软件包包含几个用于导入和导出数据,统计信息以及其他实用程序的MongoDB工具。

步骤3:启动MongoDB (Step 3: Starting MongoDB)

With MongoDB successfully installed, start the MongoDB daemon using the command as shown.

成功安装MongoDB后,使用所示命令启动MongoDB守护程序。

$ sudo systemctl start mongod

You can also enable it to start on boot by running the following command.

您还可以通过运行以下命令使它在启动时启动。

$ sudo systemctl enable mongod

To confirm that MongoDB daemon is running, execute:

要确认MongoDB守护程序正在运行,请执行:

$ sudo systemctl status mongod

Sample Output

样本输出

Awesome, we have successfully installed MongoDB on the CentOS 7 server. Let’s now see how we can configure the database.

太棒了,我们已经在CentOS 7服务器上成功安装了MongoDB。 现在让我们看看如何配置数据库。

步骤4:配置MongoDB (Step 4: Configuring MongoDB)

For best practices, it’s recommended to have authentication to access the database server, because as it stands, any user can have access to it.

为了获得最佳实践,建议进行身份验证以访问数据库服务器,因为就目前而言,任何用户都可以访问它。

To achieve this, open the /etc/mongod.conf file and uncomment the following lines.

为此,请打开/etc/mongod.conf文件,然后取消注释以下几行。

security:
  authorization: enabled

To connect to the MongoDB database run the mongo command below.

要连接到MongoDB数据库,请运行以下mongo命令。

$ mongo

To connect to the admin database, run:

要连接到管理数据库,请运行:

use admin

Sample Output

样本输出

To create a new user called mongoAdmin with the userAdminAnyDatabase role run:

要使用userAdminAnyDatabase角色创建一个名为mongoAdmin的新用户, userAdminAnyDatabase运行:

db.createUser(
  {
    user: "mongoAdmin", 
    pwd: "changeMe", 
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

Sample Output

样本输出

Successfully added user: {
	"user" : "mongoAdmin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	]
}

To exit from the Mongo shell run:

要退出Mongo Shell,请运行:

quit()

To login using the admin user account we just created, run:

要使用我们刚刚创建的管理员用户帐户登录,请运行:

$ mongo -u mongoAdmin -p --authenticationDatabase admin

Provide the password and later, you will drop to the Mongo shell.

提供密码,稍后,您将进入Mongo Shell。

To display users created in the system, run the command below to switch to the admin user.

要显示在系统中创建的用户,请运行以下命令切换到admin用户。

use admin

Then run the following command.

然后运行以下命令。

show users

Sample Output

样本输出

Wonderful! We have successfully installed MongoDB on CentOS and created an Admin user for authentication.

精彩! 我们已经在CentOS上成功安装了MongoDB,并创建了一个Admin用户进行身份验证。

翻译自: https://www.journaldev.com/27330/install-mongodb-centos

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值