简体   繁体   中英

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. I am able to start a mongo database shell by running the mongo command. 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?
  • How do I connect to a certain MongoDB database from within Node.js once it is up and running?

I have the mongoose package installed, but I am still confused on how to connect to a particular database with it.

How do I set up MongoDB to run persistently, so that I can connect to it?

Depends on how you installed the 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.

Try a ps -ef | grep mongod ps -ef | grep mongod to look for the mongod process.

If you "installed" by downloading the tar file, then take a look at using upstart or similar process for making it run persistently. 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?

Based on the Mongoose page , here is the basic connection process:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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