Nodejs mongodb bind_ip绑外网ip,出错了,求解答

Nodejs mongodb bind_ip绑外网ip,出错了,求解答

2015-01-02T18:00:49.935+0800 [initandlisten] allocator: system 2015-01-02T18:00:49.935+0800 [initandlisten] options: { config: “/etc/mongodb.conf”, net: { bindIp: “113.46.173.141”, port: 8598 }, processManagement: { fork: true }, security: { authorization: “enabled” }, storage: { dbPath: “/usr/local/mongodb/data”, journal: { enabled: true } }, systemLog: { destination: “file”, logAppend: true, path: “/usr/local/mongodb/logs” } } 2015-01-02T18:00:49.970+0800 [initandlisten] journal dir=/usr/local/mongodb/data/journal 2015-01-02T18:00:49.970+0800 [initandlisten] recover : no journal files present, no recovery needed 2015-01-02T18:00:50.121+0800 [initandlisten] ERROR: listen(): bind() failed errno:99 Cannot assign requested address for socket: 113.46.173.141:8598


2 回复

根据你提供的日志信息,MongoDB 在尝试绑定到指定的外网 IP 地址(113.46.173.141)时遇到了错误。错误信息 Cannot assign requested address for socket 表明 MongoDB 无法将服务绑定到该 IP 地址。这可能是由于以下原因导致的:

  1. IP 地址未正确配置:确保你的服务器确实拥有这个 IP 地址。
  2. 防火墙或安全组设置:可能需要检查服务器上的防火墙规则或云服务提供商的安全组设置,确保允许从外部访问该端口。
  3. 网络配置问题:某些服务器配置可能不允许绑定到特定的 IP 地址。

解决步骤

1. 确认 IP 地址

首先,确认服务器是否拥有这个 IP 地址。你可以通过运行以下命令来查看服务器的 IP 地址:

ifconfig

或者在 Linux 系统中使用:

ip addr show

确保 113.46.173.141 出现在输出结果中。

2. 检查防火墙设置

确保防火墙允许外部访问。你可以使用以下命令来开放端口:

sudo ufw allow 8598

如果你使用的是其他防火墙工具,如 iptablesfirewalld,请相应地配置规则。

3. 修改 MongoDB 配置文件

修改 /etc/mongodb.conf 文件,确保 bindIp 设置为正确的 IP 地址。例如:

net:
  bindIp: 113.46.173.141
  port: 8598

保存并重启 MongoDB 服务:

sudo systemctl restart mongod

示例代码

假设你已经解决了上述问题,你可以使用 Node.js 和 mongodb 包连接到 MongoDB 数据库。以下是一个简单的示例:

const MongoClient = require('mongodb').MongoClient;

const url = 'mongodb://113.46.173.141:8598';
const dbName = 'testdb';

MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true }, (err, client) => {
    if (err) throw err;
    console.log("Connected successfully to server");

    const db = client.db(dbName);
    // Perform actions on the collection object
    db.collection('users').find({}).toArray((err, result) => {
        if (err) throw err;
        console.log(result);
        client.close();
    });
});

总结

确保 MongoDB 的配置文件中的 bindIp 设置正确,并且防火墙和网络配置允许外部访问。如果仍然遇到问题,请检查系统日志以获取更多信息。


根据你提供的错误日志,问题在于 MongoDB 试图绑定到一个无法访问的外部 IP 地址。这通常是由于服务器没有为该 IP 地址配置正确的网络设置导致的。

解决方案

  1. 确保服务器已配置外部 IP 地址: 确认你的服务器已经正确配置了 113.46.173.141 这个外部 IP 地址。你可以通过以下命令检查:

    ip addr show
    

    确保输出中包含 113.46.173.141 这个 IP 地址,并且其状态是 UP

  2. 防火墙配置: 确认防火墙允许 MongoDB 使用的端口(在这个例子中是 8598)。你可以使用以下命令添加规则:

    sudo ufw allow 8598/tcp
    
  3. 修改 MongoDB 配置文件: 修改 /etc/mongodb.conf 文件中的 bindIp 参数,使其绑定到所有可用的 IP 地址或仅限于可访问的 IP 地址。例如:

    net:
      bindIp: 113.46.173.141,127.0.0.1
      port: 8598
    

示例代码

如果你希望使用 Node.js 连接到 MongoDB,可以参考以下代码:

const { MongoClient } = require('mongodb');

const uri = "mongodb://113.46.173.141:8598"; // 替换为你的 MongoDB 地址和端口
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });

async function run() {
    try {
        await client.connect();
        console.log("Connected successfully to server");
        const database = client.db('yourDatabaseName');
        const collection = database.collection('yourCollectionName');

        // 操作数据库
        // ...

    } finally {
        await client.close();
    }
}

run().catch(console.dir);

请确保在运行此代码之前,MongoDB 已经成功启动并且可以响应来自 113.46.173.141 的请求。

回到顶部