MongoDB(11)MongoDB的默认端口号是多少?

MongoDB的默认端口号是 27017。在默认配置下,如果没有在配置文件或启动命令中指定其他端口号,MongoDB将使用这个端口号。

示例说明

下面我们通过在不同操作系统下启动MongoDB实例,并展示如何连接到默认端口27017来进行验证。

在Ubuntu上验证默认端口
  1. 启动MongoDB服务

    安装完MongoDB后,可以使用以下命令启动MongoDB服务:

    bash 复制代码
    sudo systemctl start mongod
  2. 验证MongoDB服务状态

    确认MongoDB正在运行并监听默认端口27017:

    bash 复制代码
    sudo systemctl status mongod

    你可以使用netstatss命令来确认MongoDB正在监听默认端口:

    bash 复制代码
    sudo netstat -plnt | grep 27017

    或者:

    bash 复制代码
    sudo ss -plnt | grep 27017
  3. 连接到MongoDB

    启动MongoDB shell,连接到默认端口27017:

    bash 复制代码
    mongo --port 27017
在Windows上验证默认端口
  1. 启动MongoDB服务

    通过安装程序默认安装的情况下,MongoDB会作为Windows服务启动。可以通过以下命令检查服务状态:

    cmd 复制代码
    sc query MongoDB
  2. 验证MongoDB服务状态

    可以使用netstat命令确认MongoDB是否监听默认端口27017:

    cmd 复制代码
    netstat -ano | findstr :27017
  3. 连接到MongoDB

    打开命令提示符,启动MongoDB shell:

    cmd 复制代码
    mongo --port 27017
在macOS上验证默认端口
  1. 启动MongoDB服务

    如果使用Homebrew安装的MongoDB,可以通过以下命令启动MongoDB服务:

    bash 复制代码
    brew services start mongodb/brew/mongodb-community
  2. 验证MongoDB服务状态

    使用lsofnetstat命令确认MongoDB是否监听默认端口27017:

    bash 复制代码
    lsof -i :27017

    或者:

    bash 复制代码
    netstat -an | grep 27017
  3. 连接到MongoDB

    启动MongoDB shell,连接到默认端口27017:

    bash 复制代码
    mongo --port 27017

配置文件和启动参数

MongoDB的配置文件(通常位于/etc/mongod.conf/usr/local/etc/mongod.conf)中也可以指定端口号:

yaml 复制代码
# /etc/mongod.conf

net:
  port: 27017

如果需要使用不同的端口号,可以在配置文件中修改port参数,或者在启动MongoDB实例时使用命令行参数指定:

bash 复制代码
mongod --port <custom_port>

使用Node.js连接到MongoDB默认端口

以下是使用Node.js和MongoDB驱动程序连接到默认端口27017的示例代码:

  1. 安装MongoDB Node.js驱动

    bash 复制代码
    npm install mongodb
  2. 连接到MongoDB并执行操作

    javascript 复制代码
    const { MongoClient } = require('mongodb');
    
    async function main() {
        const uri = "mongodb://localhost:27017";
        const client = new MongoClient(uri, { useUnifiedTopology: true });
    
        try {
            await client.connect();
            console.log("Connected to MongoDB on default port 27017");
    
            const db = client.db('exampledb');
            const collection = db.collection('examplecoll');
    
            // 插入数据
            const insertResult = await collection.insertOne({ name: "Alice", age: 30 });
            console.log('Insert Result:', insertResult);
    
            // 查询数据
            const findResult = await collection.findOne({ name: "Alice" });
            console.log('Find Result:', findResult);
    
        } finally {
            await client.close();
        }
    }
    
    main().catch(console.error);

总结

MongoDB的默认端口号是 27017。通过上述步骤和代码示例,可以在不同操作系统上验证和连接到MongoDB的默认端口。以下是一些关键点:

  1. 启动MongoDB服务:确保MongoDB服务已启动。
  2. 验证端口监听 :使用系统命令(如netstatsslsof等)确认MongoDB正在监听默认端口27017。
  3. 连接到MongoDB:通过MongoDB shell或客户端代码(如Node.js)连接到默认端口27017。

这些步骤可以帮助你确保MongoDB正确安装并运行在默认端口上,从而开始使用MongoDB进行数据管理和操作。

相关推荐
比企谷八幡33 分钟前
一张表在磁盘上长什么样:Heap File 入门
数据库·oracle
流星白龙36 分钟前
【MySQL高阶】11.InnoDB存储引擎
数据库·mysql
wangbing11251 小时前
SQL Server2008 R2版自动备份问题
数据库
Trouvaille ~1 小时前
【Redis篇】Redis 渐进式遍历与数据库管理
数据库·redis·缓存·中间件·数据库管理·后端开发·scan
xcLeigh1 小时前
KES数据库运维监控与故障排查实战
运维·数据库·sql·故障排查·运维监控·kes
GlobalSign数字证书2 小时前
中小企业的 SSL/TLS 证书管理,有更轻量的方案
数据库·网络协议·ssl
梓䈑2 小时前
【MySQL】库的操作(数据库的创建、查看、修改 和 备份)
数据库·mysql
yuzhiboyouye2 小时前
原生 SQL 常用核心语句基础语法
数据库·sql·oracle
我是一颗柠檬2 小时前
【Redis】事务与Lua脚本Day7(2026年)
数据库·redis·后端·lua·database
流星白龙2 小时前
【MySQL高阶】14.MySQL存储结构
android·数据库·mysql