简体   繁体   English

在Ubuntu上设置MongoDB和Node.js.

[英]Setting up MongoDB and Node.js on Ubuntu

I am try to set up an environment using Node.js and MongoDB on an installation of ubuntu. 我尝试在安装ubuntu时使用Node.js和MongoDB建立一个环境。 I am able to start a mongo database shell by running the mongo command. 我可以通过运行mongo命令启动mongo数据库shell。 But I am unsure of how to accomplish a couple things. 但我不确定如何完成一些事情。

  • How do I set up MongoDB to run persistently, so that I can connect to it? 如何设置MongoDB持久运行,以便我可以连接到它?
  • How do I connect to a certain MongoDB database from within Node.js once it is up and running? 一旦启动并运行,我如何从Node.js中连接到某个MongoDB数据库?

I have the mongoose package installed, but I am still confused on how to connect to a particular database with it. 我安装了mongoose软件包,但我仍然对如何使用它连接到特定数据库感到困惑。

How do I set up MongoDB to run persistently, so that I can connect to it? 如何设置MongoDB持久运行,以便我可以连接到它?

Depends on how you installed the MongoDB. 取决于你如何安装MongoDB。

If you used the whole apt-get installation process, it generally installs an init.d script and the whole thing just runs with the default settings. 如果您使用整个apt-get安装过程,它通常会安装一个init.d脚本,整个过程只使用默认设置运行。

Try a ps -ef | grep mongod 试试ps -ef | grep mongod ps -ef | grep mongod to look for the mongod process. ps -ef | grep mongod寻找mongod进程。

If you "installed" by downloading the tar file, then take a look at using upstart or similar process for making it run persistently. 如果您通过下载tar文件“安装”,那么请查看使用upstart或类似过程以使其持续运行。 You'll want to use config files. 你会想要使用配置文件。

How do I connect to a certain MongoDB database from within Node.js once it is up and running? 一旦启动并运行,我如何从Node.js中连接到某个MongoDB数据库?

Based on the Mongoose page , here is the basic connection process: 基于Mongoose页面 ,这是基本的连接过程:

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/my_database');

The standard connection string format is detailed here . 标准连接字符串格式在此处详述。 Note that in my experience, not all drivers support all of the connection string features equally well, so do test your configuration. 请注意,根据我的经验,并非所有驱动程序都支持所有连接字符串功能,因此请测试您的配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM