在Linux中,MongoDB是一种流行的NoSQL数据库管理系统。它可以运行在各种操作系统上,包括Linux。在使用MongoDB时,我们需要确保它已经启动并正常运行。本文将介绍如何在Linux中查看MongoDB服务是否启动以及如何查看MongoDB的状态。
一、查看MongoDB服务是否启动
1. 使用命令行工具
在Linux中,我们可以使用命令行工具来检查MongoDB服务是否已经启动。首先打开终端窗口,然后输入以下命令:
```
sudo systemctl status mongod
```
如果MongoDB服务正在运行,则会显示类似于以下内容的输出:
```
● mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
Active: active (running) since Tue -06-22 16:23:32 CST; 3 days ago
Docs:
Main PID: 1099 (mongod)
Tasks: 23 (limit: 4915)
Memory: 141.7M
CGroup: /system.slice/mongod.service
└─1099 /usr/bin/mongod --config /etc/mongod.conf
```
2. 检查MongoDB日志文件
另一种方法是检查MongoDB的日志文件。默认情况下,MongoDB的日志文件位于/var/log/mongodb/mongod.log。我们可以使用以下命令查看日志文件:
```
sudo tail -f /var/log/mongodb/mongod.log
```
如果MongoDB服务正在运行,则会显示类似于以下内容的输出:
```
-06-25T09:41:28.547+0800 I CONTROL [initandlisten] MongoDB starting : pid=1099 port=27017 dbpath=/var/lib/mongodb 64-bit host=ubuntu
-06-25T09:41:28.547+0800 I CONTROL [initandlisten] db version v4.4.6
-06-25T09:41:28.547+0800 I CONTROL [initandlisten] git version: 72e66213c2c3eab37d9358d5e78ad7f5c1d0d0d7
-06-25T09:41:28.547+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.1.1f 31 Mar
-06-25T09:41:28.547+0800 I CONTROL [initandlisten] allocator: tcmalloc
-06-25T09:41:28.547+0800 I CONTROL [initandlisten] modules: none
-06-25T09:41:28.547+0800 I CONTROL [initandlisten] build environment:
-06-25T09:41:28.547+0800 I CONTROL [initandlisten] distmod: ubuntu2004
-06-25T09:41:28.547+0800 I CONTROL [initandlisten] distarch: x86_64
-06-25T09:41:28.547+0800 I CONTROL [initandlisten] target_arch: x86_64
-06-25T09:41:28.547+0800 I CONTROL [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1" }, processManagement: { timeZoneInfo: "/usr/share/zoneinfo" }, storage: { dbPath: "/var/lib/mongodb", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } }
-06-25T09:41:28.548+0800 I STORAGE [initandlisten] WiredTigerKVEngine loaded the metadata for the column family: (no column family specified). Please note that the cached metadata is not necessarily in sync with the file system metadata.
-06-25T09:41:28.549+0800 I STORAGE [initandlisten] WiredTigerKVEngine starting recovery from checkpoint for unclean shutdown
-06-25T09:41:28.549+0800 I STORAGE [initandlisten] WiredTigerKVEngine recoveryTimestampTs: Timestamp(1624585283, 1)
-06-25T09:41:28.549+0800 I STORAGE [initandlisten] WiredTigerKVEngine recoveryCheckpoint: 34920
-06-25T09:41:28.549+0800 I STORAGE [initandlisten] WiredTigerKVEngine (WTCheckpointThread) WT_CHECKPOINT [WT_VERB_RECOVERY_PROGRESS] Progress: 0.00% (0/34920)
-06-25T09:41:28.550+0800 I STORAGE [initandlisten] WiredTigerKVEngine (WTCheckpointThread) WT_CHECKPOINT [WT_VERB_RECOVERY_PROGRESS] Progress: 1.75% (612/34920)
...
```
二、查看MongoDB的状态
1. 使用mongo shell
在Linux中,我们可以通过MongoDB自带的mongo shell来查看MongoDB的状态。首先打开
网友留言: