最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to connect to MongoDB in local network with express and mongoose - Stack Overflow

programmeradmin3浏览0评论

I'm setting up a server with express and mongoose and I'd like it to be usable in other machines present in my local network. The bind_ip variable has already been set to 0.0.0.0 in the Mongodb configuration file.

const connection = mongoose
    .connect(
        "mongodb://192.168.254.104/db",
        {
            useNewUrlParser: true
        }
    )
    .then(() => console.log("Connected to MongoDB"))
    .catch(error => console.log(error));

I've tried connecting on my mobile phone but the server response says it didn't find the database.

I'm setting up a server with express and mongoose and I'd like it to be usable in other machines present in my local network. The bind_ip variable has already been set to 0.0.0.0 in the Mongodb configuration file.

const connection = mongoose
    .connect(
        "mongodb://192.168.254.104/db",
        {
            useNewUrlParser: true
        }
    )
    .then(() => console.log("Connected to MongoDB"))
    .catch(error => console.log(error));

I've tried connecting on my mobile phone but the server response says it didn't find the database.

Share Improve this question asked Feb 7, 2019 at 12:38 gian29gian29 7342 gold badges7 silver badges9 bronze badges 5
  • 1 You're missing the port number and better to use localhost in most situations I think – nanobar Commented Feb 7, 2019 at 12:43
  • Do you want to expose the mongodb server to your LAN? Because the code you're showing is server side code. Does that mean you want to run multiple express servers? – user5734311 Commented Feb 7, 2019 at 12:45
  • I only want them to use the same express server and database. – gian29 Commented Feb 7, 2019 at 12:47
  • Right, in that case there's no need to expose the mongodb server to your LAN. Your express server is already reachable from all machines in your LAN, and only the express server needs to access the mongodb server. Since the two are on the same machine, there's no need for any special configuration stuff at all. – user5734311 Commented Feb 7, 2019 at 12:49
  • Hello, If your problem resolved, you should accept the answer and help the community. – Prathamesh More Commented Aug 20, 2020 at 14:02
Add a comment  | 

3 Answers 3

Reset to default 9

First, you should run a MongoDB server on Local.

Default running port is: 27017

mongoose.connect('mongodb://localhost:27017/', {
    dbName: 'event_db',
    useNewUrlParser: true,
    useUnifiedTopology: true 
}, err => err ? console.log(err) : console.log('Connected to database'));

Or you can do

mongoose.connect('mongodb://localhost:27017/event_db');

This is the format of mongodb connection String:

mongodb://[username:password@]host1[:port1][,...hostN[:portN]]][/[database][?options]]

If you want to connect to a localhost database then the string would be like this:

"mongodb://localhost:27017/mydb"

Where "mydb" is the DB name on your local machine.

try this way :

mongoose.Promise = Promise;
mongoose.set('useCreateIndex', true);
var mongooseOptions = {  useNewUrlParser: true }

mongoose.connect('mongodb://localhost:27017/MyDatabase', mongooseOptions, function(err) {
    if (err) {
        console.error('System could not connect to mongo server.')
        console.log(err)     
    } else {
        console.log('System connected to mongo server.')
    }
});
发布评论

评论列表(0)

  1. 暂无评论