1. ホーム
  2. MongoDB

[MongoDB] 127.0.0.1:27017 への接続に失敗しました、理由。接続が拒否されました

2022-02-11 09:09:34
<パス

プロジェクトの必要性から仮想マシンにMongoDBをインストールしたのですが、起動時に以下のエラーが発生します。

[root@localhost bin]# . /mongo
MongoDB shell version v3.4.0
connecting to: mongodb://127.0.0.1:27017
2018-09-27T21:11:14.779+0800 W NETWORK [main] Failed to connect to 127.0.0.1:27017, reason: Connection refused
2018-09-27T21:11:14.780+0800 E QUERY [main] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:234:13
@(connect):1:6
exception: connect failed


上記の理由は、dataディレクトリにあるmongod.lockファイルに問題がある可能性があります。これは、コマンドを使って修正することができます。

[root@localhost mongodb]# . /bin/mongod --repair


しかし、私のようなせっかちな人間には、そのディレクトリに行ってファイルを削除するだけです。
再修正する場合、または削除後、MongoDB:を再起動します。

. /mongod --dbpath=/usr/local/mongodb/data/db/


すると、次のように表示される。

2018-09-27T21:12:16.441+0800 I CONTROL [initandlisten] MongoDB starting : pid=6748 port=27017 dbpath=/usr/local/mongodb/data/db/ 64-bit host= localhost.localdomain
2018-09-27T21:12:16.441+0800 I CONTROL [initandlisten] db version v3.4.0
2018-09-27T21:12:16.441+0800 I CONTROL [initandlisten] git version: f4240c60f005be757399042dc12f6addbc3170c1
2018-09-27T21:12:16.441+0800 I CONTROL [initandlisten] allocator: tcmalloc
2018-09-27T21:12:16.441+0800 I CONTROL [initandlisten] modules: none
2018-09-27T21:12:16.441+0800 I CONTROL [initandlisten] build environment:
2018-09-27T21:12:16.441+0800 I CONTROL [initandlisten] distarch: x86_64
2018-09-27T21:12:16.441+0800 I CONTROL [initandlisten] target_arch: x86_64
2018-09-27T21:12:16.441+0800 I CONTROL [initandlisten] options: { storage: { dbPath: "/usr/local/mongodb/data/db/" }
2018-09-27T21:12:16.482+0800 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=1383M,session_max=20000,eviction=( threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=( close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2018-09-27T21:12:17.294+0800 I CONTROL [initandlisten] 
2018-09-27T21:12:17.294+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-09-27T21:12:17.294+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-09-27T21:12:17.294+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-09-27T21:12:17.294+0800 I CONTROL [initandlisten] 
2018-09-27T21:12:17.294+0800 I CONTROL [initandlisten] 
2018-09-27T21:12:17.294+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-09-27T21:12:17.294+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-09-27T21:12:17.295+0800 I CONTROL [initandlisten] 
2018-09-27T21:12:17.295+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-09-27T21:12:17.295+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-09-27T21:12:17.295+0800 I CONTROL [initandlisten] 
2018-09-27T21:12:17.483+0800 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/usr/local/mongodb/data/db /diagnostic.data'
2018-09-27T21:12:17.769+0800 I INDEX [initandlisten] build index on: admin.system.version properties: { v: 2, key: { version: 1 }, name: " incompatible_with_version_32", ns: "admin.system.version" }
2018-09-27T21:12:17.769+0800 I INDEX [initandlisten] building index using bulk method; build may temporarily use up to 500 megabytes of RAM
2018-09-27T21:12:17.793+0800 I INDEX [initandlisten] build index done. scanned 0 total records. 0 secs
2018-09-27T21:12:17.794+0800 I COMMAND [initandlisten] setting featureCompatibilityVersion to 3.4
2018-09-27T21:12:17.794+0800 I NETWORK [thread1] waiting for connections on port 27017
2018-09-27T21:13:22.539+0800 I NETWORK [thread1] connection accepted from 127.0.0.1:51708 #1 (1 connection now open)
2018-09-27T21:13:22.540+0800 I NETWORK [conn1] received client metadata from 127.0.0.1:51708 conn1: { application: { name: "MongoDB Shell& quot; }, driver: { name: "MongoDB Internal Client", version: "3.4.0" }, os: { type: "Linux", name: "CentOS Linux release 7.5.1804 (Core) &quo

この画面が表示されたら成功です。その後、この画面を閉じずに、新しいウィンドウを開いて開始してください。

[root@localhost ~]# cd /usr/local/mongodb
[root@localhost mongodb]# . /bin/mongo
MongoDB shell version v3.4.0
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.0
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	http://docs.mongodb.org/
Questions? Try the support group
	http://groups.google.com/group/mongodb-user
Server has startup warnings: 
2018-09-27T21:12:17.294+0800 I CONTROL [initandlisten] 
2018-09-27T21:12:17.294+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-09-27T21:12:17.294+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-09-27T21:12:17.294+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-09-27T21:12:17.294+0800 I CONTROL [initandlisten] 
2018-09-27T21:12:17.294+0800 I CONTROL [initandlisten] 
2018-09-27T21:12:17.294+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-09-27T21:12:17.294+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-09-27T21:12:17.295+0800 I CONTROL [initandlisten] 
2018-09-27T21:12:17.295+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-09-27T21:12:17.295+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-09-27T21:12:17.295+0800 I CONTROL [initandlisten] 


このような画面は、問題が解決されたことを意味します〜。
この時点で、URLにアクセスすると、MongoDBサービスが確かに稼働していることがわかります。

お読みいただきありがとうございました。