尝试访问mongodb时,出现连接失败的错误
在运行我得到的mongod
命令时
2019-02-25T11:05:05.870+0530 I CONTROL [initandlisten] MongoDB
starting : pid=2580 port=27017 dbpath=/data/db 64-bit host=gten
2019-02-25T11:05:05.870+0530 I CONTROL [initandlisten] db version
v3.6.3
2019-02-25T11:05:05.870+0530 I CONTROL [initandlisten] git
version: 9586e557d54ef70f9ca4b43c26892cd55257e1a5
2019-02-25T11:05:05.870+0530 I CONTROL [initandlisten] OpenSSL
version: OpenSSL 1.1.0g 2 Nov 2017
2019-02-25T11:05:05.870+0530 I CONTROL [initandlisten] allocator:
tcmalloc
2019-02-25T11:05:05.870+0530 I CONTROL [initandlisten] modules:
none
2019-02-25T11:05:05.870+0530 I CONTROL [initandlisten] build
environment:
2019-02-25T11:05:05.871+0530 I CONTROL [initandlisten]
distarch: x86_64
2019-02-25T11:05:05.871+0530 I CONTROL [initandlisten]
target_arch: x86_64
2019-02-25T11:05:05.871+0530 I CONTROL [initandlisten] options:
{}
2019-02-25T11:05:05.962+0530 I STORAGE [initandlisten] exception
in initAndListen: IllegalOperation: Attempted to create a lock
file on a read-only directory: /data/db, terminating
2019-02-25T11:05:05.962+0530 I CONTROL [initandlisten] now
exiting
2019-02-25T11:05:05.962+0530 I CONTROL [initandlisten] shutting
down with code:100
当我运行mongo
命令时,我得到了
MongoDB shell version v4.0.6
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
2019-02-25T11:16:44.802+0530 E QUERY [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:343:13
@(connect):1:6
exception: connect failed
我不知道我遇到了[https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/]和[https://www.howtoforge.com/tutorial/install-mongodb-on-ubuntu-16.04/] 安装mongo。
配置: 我正在使用UBUNTU 18.04。 感谢您的帮助,我已经回答了很多问题,并尝试了所有问题,但仍然面临同样的问题。谢谢
做了很多事情后,我得到了
2019-02-25T18:52:27.197+0530 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2019-02-25T18:52:27.200+0530 I CONTROL [initandlisten] MongoDB starting : pid=27390 port=27017 dbpath=/data/db 64-bit host=gten
2019-02-25T18:52:27.200+0530 I CONTROL [initandlisten] db version v4.0.6
2019-02-25T18:52:27.200+0530 I CONTROL [initandlisten] git version: caa42a1f75a56c7643d0b68d3880444375ec42e3
2019-02-25T18:52:27.200+0530 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.1.0g 2 Nov 2017
2019-02-25T18:52:27.200+0530 I CONTROL [initandlisten] allocator: tcmalloc
2019-02-25T18:52:27.200+0530 I CONTROL [initandlisten] modules: none
2019-02-25T18:52:27.200+0530 I CONTROL [initandlisten] build environment:
2019-02-25T18:52:27.200+0530 I CONTROL [initandlisten] distmod: ubuntu1804
2019-02-25T18:52:27.200+0530 I CONTROL [initandlisten] distarch: x86_64
2019-02-25T18:52:27.200+0530 I CONTROL [initandlisten] target_arch: x86_64
2019-02-25T18:52:27.200+0530 I CONTROL [initandlisten] options: {}
2019-02-25T18:52:27.200+0530 E STORAGE [initandlisten] Failed to set up listener: SocketException: Address already in use
2019-02-25T18:52:27.200+0530 I CONTROL [initandlisten] now exiting
2019-02-25T18:52:27.200+0530 I CONTROL [initandlisten] shutting down with code:48
答案 0 :(得分:3)
根据错误,尝试启动mongod进程的用户没有对/ data / db文件夹的写访问权。
运行以下命令以使用户能够写入/ data / db文件夹
sudo chown -R $USER:$USER /data/db
或使用当前用户权限创建一个新文件夹,并使用--dbpath选项启动mongod
mkdir mydata/db
mongod --dbpath mydata/db
答案 1 :(得分:3)
如果您遇到此问题,则意味着您尝试连接mongo 在本地运行服务器会出现此问题
某些系统在计算机启动时未自动运行mongo服务。 因此,您需要手动启动它。
每次启动计算机或使用mongo时,运行此命令即可运行服务
sudo service mongod start
// or
sudo systemctl start mongod
运行sudo service mongod status
并查看返回的内容。
如果停止了
运行sudo service mongod start
以启动mongodb并尝试连接到它。
如果sudo service mongod start
输入错误,则
使用以下内容创建文件/lib/systemd/system/mongod.service
:
[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target
[Service]
User=mongodb
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf
[Install]
WantedBy=multi-user.target
如果仍然存在错误,请共享控制台错误。一开始,我们可能会为您提供更好的帮助。
答案 2 :(得分:0)
尝试这个。这可能会有帮助。
sudo chmod 777 /data/db