【MongoDB】 Failed to connect to 127.0.0.1:27017, reason: Connection refused

由于项目需要,在一台虚拟机上安装了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" }, driver: { name: "MongoDB Internal Client", version: "3.4.0" }, os: { type: "Linux", name: "CentOS Linux release 7.5.1804 (Core) ", architecture: "x86_64", version: "Kernel 3.10.0-862.el7.x86_64" } }
2018-09-27T21:14:00.424+0800 I NETWORK  [thread1] connection accepted from 192.168.20.29:10351 #2 (2 connections now open)
2018-09-27T21:14:00.424+0800 I -        [conn2] end connection 192.168.20.29:10351 (2 connections now open)
2018-09-27T21:14:00.969+0800 I NETWORK  [thread1] connection accepted from 192.168.20.29:10386 #3 (2 connections now open)
2018-09-27T21:14:00.969+0800 I NETWORK  [thread1] connection accepted from 192.168.20.29:10387 #4 (3 connections now open)
2018-09-27T21:14:03.063+0800 I NETWORK  [thread1] connection accepted from 192.168.20.29:10392 #5 (4 connections now open)
2018-09-27T21:14:03.063+0800 I NETWORK  [thread1] connection accepted from 192.168.20.29:10393 #6 (5 connections now open)
2018-09-27T21:14:03.063+0800 I -        [conn6] end connection 192.168.20.29:10393 (5 connections now open)
2018-09-27T21:14:03.469+0800 I -        [conn5] end connection 192.168.20.29:10392 (4 connections now open)
2018-09-27T21:16:31.073+0800 I NETWORK  [thread1] connection accepted from 192.168.21.247:1138 #7 (4 connections now open)
2018-09-27T21:16:31.714+0800 I INDEX    [conn7] build index on: NCRE3.01.files properties: { v: 2, key: { filename: 1, uploadDate: 1 }, name: "filename_1_uploadDate_1", ns: "NCRE3.01.files" }
2018-09-27T21:16:31.714+0800 I INDEX    [conn7] 	 building index using bulk method; build may temporarily use up to 500 megabytes of RAM
2018-09-27T21:16:31.765+0800 I INDEX    [conn7] build index done.  scanned 0 total records. 0 secs
2018-09-27T21:16:31.765+0800 I COMMAND  [conn7] insert NCRE3.system.indexes ninserted:1 numYields:0 locks:{ Global: { acquireCount: { r: 1, w: 1 } }, Database: { acquireCount: { W: 1 } }, Collection: { acquireCount: { w: 1 } } } 550ms
2018-09-27T21:16:32.627+0800 I INDEX    [conn7] build index on: NCRE3.01.chunks properties: { v: 2, unique: true, key: { files_id: 1, n: 1 }, name: "files_id_1_n_1", ns: "NCRE3.01.chunks" }
2018-09-27T21:16:32.627+0800 I INDEX    [conn7] 	 building index using bulk method; build may temporarily use up to 500 megabytes of RAM
2018-09-27T21:16:32.994+0800 I INDEX    [conn7] build index done.  scanned 0 total records. 0 secs
2018-09-27T21:16:32.994+0800 I COMMAND  [conn7] insert NCRE3.system.indexes ninserted:1 numYields:0 locks:{ Global: { acquireCount: { r: 1, w: 1 } }, Database: { acquireCount: { W: 1 } }, Collection: { acquireCount: { w: 1 } } } 1216ms
2018-09-27T21:16:35.895+0800 I -        [conn7] end connection 192.168.21.247:1138 (4 connections now open)
^C2018-09-27T21:17:17.799+0800 I CONTROL  [signalProcessingThread] got signal 2 (Interrupt), will terminate after current cmd ends
2018-09-27T21:17:17.799+0800 I NETWORK  [signalProcessingThread] shutdown: going to close listening sockets...
2018-09-27T21:17:17.799+0800 I NETWORK  [signalProcessingThread] closing listening socket: 6
2018-09-27T21:17:17.799+0800 I NETWORK  [signalProcessingThread] closing listening socket: 7
2018-09-27T21:17:17.799+0800 I NETWORK  [signalProcessingThread] removing socket file: /tmp/mongodb-27017.sock
2018-09-27T21:17:17.801+0800 I NETWORK  [signalProcessingThread] shutdown: going to flush diaglog...
2018-09-27T21:17:17.801+0800 I FTDC     [signalProcessingThread] Shutting down full-time diagnostic data capture
2018-09-27T21:17:17.803+0800 I STORAGE  [signalProcessingThread] WiredTigerKVEngine shutting down
2018-09-27T21:17:19.283+0800 I STORAGE  [signalProcessingThread] shutdown: removing fs lock...
2018-09-27T21:17:19.283+0800 I CONTROL  [signalProcessingThread] now exiting
2018-09-27T21:17:19.283+0800 I CONTROL  [signalProcessingThread] shutting down with code:0

出现这样的界面,就是成功了。然后不要关闭这个界面,重新打开一个窗口来启动:

[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] 

出现这样的界面,代表问题解决了~
此时,访问网址,发现MongoDB的服务确实是启动了:
在这里插入图片描述
感谢您的阅读~

你可能感兴趣的:(➤数据库)