【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的服务确实是启动了:
在这里插入图片描述
感谢您的阅读~

这个错误通常表示 MongoDB 服务器无法在指定的时间内连接到本地主机(127.0.0.1)的默认端口(27017)。出现这种情况可能有几个原因: 1. MongoDB 服务器未正确启动:首先,请确保您已经成功启动了 MongoDB 服务器。您可以尝试在终端或命令提示符中运行以下命令来启动 MongoDB 服务器: ``` mongod ``` 如果服务器已经在运行,您将看到一些启动日志和端口信息。 2. 端口冲突:另一个常见的问题是端口冲突,可能是因为该端口已被其他程序占用。请确保在启动 MongoDB 服务器之前没有其他程序在使用 27017 端口。您可以尝试更改 MongoDB 的监听端口,例如使用 `--port` 参数指定一个不被占用的端口号。 3. 防火墙阻止连接:如果您的计算机上启用了防火墙,可能会阻止 MongoDB 服务器的连接。请确保您的防火墙配置允许 MongoDB 的入站和出站连接。您可以尝试暂时禁用防火墙来测试是否是防火墙引起的问题。 4. 数据库文件损坏:如果 MongoDB 数据库文件损坏或出现其他问题,可能会导致启动失败。您可以尝试删除 MongoDB 数据目录中的文件并重新启动服务器。请注意,这将删除所有现有的数据库和数据,因此请确保您已经备份了重要的数据。 5. 确保 MongoDB 客户端和服务器版本匹配:如果您使用的是较新版本的 MongoDB 客户端,但连接的是较旧版本的 MongoDB 服务器,可能会导致连接问题。请确保您使用的 MongoDB 客户端和服务器版本相匹配。 请根据您的具体情况逐一排查这些可能的问题,希望能解决您的连接问题!如果问题仍然存在,请提供更多详细信息,以便我能够更好地帮助您。
评论 24
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值