博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在Ubuntu 18.04上安装MongoDB
阅读量:2531 次
发布时间:2019-05-11

本文共 4793 字,大约阅读时间需要 15 分钟。

MongoDB is a free and open-source NoSQL Database used commonly in today’s web applications.

MongoDB是一个免费的开源NoSQL数据库,在当今的Web应用程序中普遍使用。

In this tutorial, we will show you how to install and setup MongoDB on your Ubuntu 18.04 machine in a few simple steps!

在本教程中,我们将通过几个简单的步骤向您展示如何在Ubuntu 18.04计算机上安装和设置MongoDB!



步骤1:使用Apt在Ubuntu上安装MongoDB (Step 1: Install MongoDB on Ubuntu Using Apt)

Since the MongoDB package is available in Ubuntu’s official repositories, we can simply use apt to install MongoDB on our system.

由于MongoDB软件包在Ubuntu的官方存储库中可用,因此我们可以简单地使用apt在我们的系统上安装MongoDB。

But before that, update all system critical packages first.

但是在此之前,请先更新所有系统关键软件包。

sudo apt update

Now, install the MongoDB package using:

现在,使用以下命令安装MongoDB软件包:

sudo apt install mongodb
install mongodb on Ubuntu
Install MongoDB on Ubuntu
在Ubuntu上安装MongoDB

This will install the latest version of MongoDB on our system.

这将在我们的系统上安装最新版本的MongoDB。

As for any Database System, there is a server and a service associated with the task of Database Management. The MongoDB server is automatically started after the installation.

对于任何数据库系统,都有与数据库管理任务关联的服务器服务 。 安装后,MongoDB服务器将自动启动。

To ensure that our installation went smoothly, we need to ensure that the MongoDB service and the server are working correctly.

为确保安装顺利进行,我们需要确保MongoDB服务和服务器正常运行。

步骤2:检查MongoDB服务 (Step 2: Check the MongoDB Service)

To check the status of the MongoDB service, use systemctl:

要检查MongoDB服务的状态,请使用systemctl

sudo systemctl status mongodb

You’ll get an output similar to the below screenshot.

您将获得类似于以下屏幕截图的输出。

Mongodb Ubuntu Check Service
Mongodb Ubuntu Check Service
Mongodb Ubuntu检查服务

So now that we know that our service is running, let’s verify if our MongoDB Server is responding to our requests or not.

因此,既然我们知道我们的服务正在运行,就让我们验证一下MongoDB服务器是否正在响应我们的请求。

步骤3:测试MongoDB服务器 (Step 3: Test the MongoDB Server)

By default, the MongoDB Server is located at the localhost and uses port 27017. So the address of the server is:

默认情况下,MongoDB服务器位于本地主机,并使用端口27017 。 因此服务器的地址为:

127.0.0.1:27017

Let’s test our server by running this command to find the Connection Status using mongo command:

让我们通过运行以下命令来测试服务器,以使用mongo命令查找连接状态:

mongo --eval 'db.runCommand({ connectionStatus: 1 })'

You should get a response like the below screenshot, having the “ok” field as 1, ensuring that our Connection is valid.

您应该收到类似以下屏幕截图的响应,将“ ok ”字段设置为1,确保我们的连接有效。

 Check Connection Status
Mongodb Ubuntu Check Connection Status
Mongodb Ubuntu检查连接状态


步骤4:允许远程访问数据库 (Step 4: Allow remote access to the Database)

By default, the MongoDB server is only allowed by the ufw firewall to operate locally.

默认情况下,仅ufw防火墙允许MongoDB服务器在本地运行。

To allow access to MongoDB on it’s port 27017 from anywhere, we need to allow incoming connections on this port.

要允许从任何地方访问其端口27017上的MongoDB,我们需要允许该端口上的传入连接。

But this is very insecure, since any person can access the Database. To prevent this, only allow IP Addresses which correspond to trusted machines/servers.

但这是非常不安全的,因为任何人都可以访问数据库。 为避免这种情况,仅允许使用与受信任机器/服务器相对应的IP地址。

sudo ufw allow from TRUSTED_IP/32 to any port 27017

Replace TRUSTED_IP with any trusted IP address in the above command.

用以上命令中的任何受信任IP地址替换TRUSTED_IP

This allows connections from your trusted server to reach the MongoDB Database, since ufw now allows these connections.

由于ufw现在允许这些连接,因此这允许来自受信任服务器的连接到达MongoDB数据库。

But we still need to ensure that the MongoDB server does not only listen locally. Therefore, we need to add our trusted machine’s IP to the mongodb.conf configuration file.

但是我们仍然需要确保MongoDB服务器不仅在本地监听。 因此,我们需要将受信任机器的IP添加到mongodb.conf配置文件中。

Use your favorite text editor and edit /etc/mongodb.conf.

使用您喜欢的文本编辑器并编辑/etc/mongodb.conf

sudo vi /etc/mongodb.conf

Go to the bind_ip value.

转到bind_ip值。

Mongodb Config File
Mongodb Ubuntu Config File
Mongodb Ubuntu配置文件

Add your trusted machine’s public IP Address here, by appending to the field using a comma (,).

通过使用逗号 (,)附加到该字段,在此处添加受信任计算机的公共IP地址。

The modified file will look something like this:

修改后的文件将如下所示:

Add Server Address
Mongodb Ubuntu Add Server Address
Mongodb Ubuntu添加服务器地址

Exit the editor, after saving changes

保存更改后退出编辑器



步骤5:重新启动MongoDB服务 (Step 5: Restart the MongoDB Service)

Now, we are almost done! We simply need to restart the mongodb service to apply our changes.

现在,我们快完成了! 我们只需要重新启动mongodb服务即可应用更改。

sudo systemctl restart mongodb

We have now successfully configured our MongoDB Server to accept remote connections!

现在,我们已成功配置MongoDB服务器以接受远程连接!



结论 (Conclusion)

In this tutorial. we showed you how you can install and configure the latest version of MongoDB on your Ubuntu 18.04 system. We also made sure that we can accept specific remote connections to our database from another trusted server.

在本教程中。 我们向您展示了如何在Ubuntu 18.04系统上安装和配置最新版本的MongoDB。 我们还确保可以接受来自其他受信任服务器到数据库的特定远程连接。

I hope this served you well and cleared any of your remaining doubts regarding MongoDB installation!

希望这对您有帮助,并消除了您对MongoDB安装的任何疑问!



翻译自:

转载地址:http://uulzd.baihongyu.com/

你可能感兴趣的文章
字符串处理sdut 2411
查看>>
javascript之进阶
查看>>
多个窗体间控件的调用
查看>>
[总结]编程中遇到的vc提示的一些警告
查看>>
Python学习笔记-EXCEL操作
查看>>
9.for循环
查看>>
百度PaddlePaddle再获新技能 智能推荐、对话系统、控制领域都能搞定!
查看>>
SELINUX setsebool
查看>>
C#的Socket程序(TCP/IP)总结
查看>>
java源码生成可运行jar
查看>>
用一个常见的生活小例子来解释和演示面向接口编程
查看>>
找出数组中两个只出现一次的数字
查看>>
【TYVJ水题三连发】1502 兴建高铁 1568 Rabbit Number 1673 位图
查看>>
centos 允许远程连接mysql
查看>>
C#之用XmlWriter保存XML数据
查看>>
光和颜色
查看>>
Metro UI风格配色方案
查看>>
WGAN (原理解析)
查看>>
PHP json_encode中文乱码解决方法
查看>>
解决ie6、ie7下float为right换行的情况
查看>>