Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to run Mongodb on ubuntu

While trying to access the mongodb I am getting error that connection is failed

  1. while running mongod command I am getting

    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
    

and while I run mongo command, I am getting

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

I dont know what's the issue I have follwe the [https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/] and [https://www.howtoforge.com/tutorial/install-mongodb-on-ubuntu-16.04/] to install the mongo.

Config: I am using UBUNTU 18.04. Any help is appreciated i have gone through many answers and tried all but still facing the same issue.Thanks in advance

After doing a lot of stuff I am getting

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
like image 501
Adesh Kumar Avatar asked Jan 01 '23 05:01

Adesh Kumar


2 Answers

If you face this issue It means you are trying to connect mongo shall without running server on local which give this problem

enter image description here

Some system are not run mongo service automatically on start of machine. So you need to start it manually.

Run this command to run service on every time when you start your machine or whenever you want to use mongo

sudo service mongod start
// or 
sudo systemctl start mongod

Run sudo service mongod status and see what it returns. If it is stopped, run sudo service mongod start to start the mongodb and try connecting to it.

If sudo service mongod start is giving a error then

Create the file /lib/systemd/system/mongod.service with the following content:

[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

If you still persist error please share console error. On start, we may help you better.

like image 125
Ashok Avatar answered Jan 05 '23 18:01

Ashok


As per the error, message the user trying to start the mongod process don't have write access to /data/db folder.

Run the following command to enable the user to write to /data/db folder

sudo chown -R $USER:$USER /data/db

or create a new folder with current users permission and start mongod with --dbpath option

mkdir mydata/db

mongod --dbpath mydata/db
like image 26
Mani Avatar answered Jan 05 '23 17:01

Mani