microsoft / WSL

Issues found on WSL
https://docs.microsoft.com/windows/wsl
MIT License
17.44k stars 822 forks source link

MongoDB service failed to start (unrecognized service ) #1822

Closed valorad closed 7 years ago

valorad commented 7 years ago

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6

sudo echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list

sudo apt-get update

sudo apt-get install -y mongodb-org

sudo service mongod start

valorad commented 7 years ago

ah i just saw #796, sorry for the redundancy

aseering commented 7 years ago

Hi @wcxaaa -- you're correct about #796 , but there's another issue here, a typo in your command: The mongodb package is confusing because you do sudo mongod to start it manually, but sudo service mongodb start (note mongodb rather than mongod) to start it using an init script. So your command is missing a b.

valorad commented 7 years ago

@aseering Much appreciated! That works! I just copied and pasted command on official guide not knowing what was going on.

valorad commented 7 years ago

There's one more thing I've found. The name of config file located in /etc should be mongodb.conf instad of mongod.conf.

lobosan commented 7 years ago

I have the same issue after following this tutorial

And when I type mongod --version (not mongodb) I get db version v3.4.3

but when I run sudo service mongod start I get mongod: unrecognized service

aseering commented 7 years ago

@lobosan -- that tutorial unfortunately is incorrect; it has a minor typo that gives you the wrong command. (It's incorrect both for WSL and for regular Ubuntu Linux.) My previous comment contains the correct command, with the typo fixed.

lobosan commented 7 years ago

@aseering I read the whole post and I did try to do that but it didn't work for me and when I run sudo service mongodb start I get the error: mongodb: unrecognized service.

Any other ideas?

lobosan commented 7 years ago

After researching, I solved this problem by doing:

Create folders: ~# mkdir /data/db ~# mkdir /data/db/log

Run command ~# mongod --dbpath data/db --fork --logpath data/db/log

And then run the server ~# mongod

zealseeker commented 7 years ago

Hi @lobosan . Though this way can start mongo by its "executable file, mongod", but it may not be safe and easy enough, I think. Because you have to manually start the service by this command everytime we reboot of the PC. "Service" is more helpful ( both in windows and in Ubuntu). For example, we can stop or restart the service via service mongod stop/restart instead of killing the process manually. However, we still cannot solve the problem why we cannot start it via service mongod start as the tutorial says.

By the way, mongo-org has already given us a config file in /etc/mongod.conf. So we'd better use mongod --fork --config /etc/mongod.conf to make it easier (to config the service).

danwhitston commented 7 years ago

Hullo. Same issue here. service mongod start and sudo mongodb start both give unrecognised service. sudo mongod starts, but then terminates because /data/db isn't found. Creating the db and log directories allows it to start via sudo mongod, but starting it as a service still doesn't work.

thekep7 commented 7 years ago

I had the same problem with unrecognized service.

I did as zealseeker suggested and started with: mongod --fork --config /etc/mongod.conf

xtianus79 commented 7 years ago

Ok you shouldn't have to install the script any differently other than these few changes.... and you shouldn't have to make a directory... it's super odd to have to do it this way and where the break down is occurring I do not know but i will give instructions on how to install everything correctly at this time.

IMHO this is a large pile of hoo-diddy from someone and changes should be made... /rant.

Oh by the way, this is where i am altering the directions from.... https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/

first I purged my mongo so I know everything is from scratch w/ this command: sudo apt-get purge mongodb-org*

  1. sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6
  2. ubuntu 16.04: echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
  3. sudo apt-get update
  4. Not in mongo's directions: but rather something i got from reading above comments: sudo apt-get install mongodb At this point the version installed by mongo is an older version... for me ie.
    mongod --version                                                                                                                             
    db version v2.6.10
  5. sudo apt-get install -y mongodb-org ***Now, everything is installed correctly
    
    mongod --version                                                                                                                             
    db version v3.4.9
    git version: 876ebee8c7dd0e2d992f36a848ff4dc50ee6603e
    OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
    allocator: tcmalloc
    modules: none
    build environment:
    distmod: ubuntu1604
    distarch: x86_64
    target_arch: x86_64

7.  ***Lastly, per the op's @Zx-EvM  && @aseering above states that instead of the start command of mongod you would use the following command to start mongo:
`sudo service mongodb start`

So what does this do in summary?  

It installs mongod DB on an ubuntu 16.0.4 linux Windows subsystem... Updates it... and runs it via the start command... hope this helps someone and everything in this thread was awesome but a little verbose and not to the point which is what this summary is for.  

CP from this thread
https://github.com/Microsoft/BashOnWindows/issues/796
siran commented 7 years ago

As pointed by @xtianus79 and to my surprise, on Ubuntu Xenial doing 4- sudo apt-get install mongodb works like magic

ojoaofernandes commented 7 years ago

Alternatively, you can create the service configuration file as pointed by @maticomp in the Digital Ocean article.

  1. Save this few lines to: /etc/systemd/system/mongodb.service;
  2. Run: [sudo] systemctl start mongodb
    
    [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

reorg-hle commented 6 years ago

I tried @ojoaofernandes' config but that didn't work. I got the error Failed to connect to bus: No such file or directory

So I kind of hacked around it by installing both versions:

apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list

apt update && apt install mongodb-org #installs mongo 3.6.5

apt install mongodb # uninstalls mongo 3.6.5, installs 2.6.10
# this also installs the /etc/init.d/mongodb properly so back it up if you want, though i didn't need to

apt install mongodb-org # uninstalls mongo 2.6.10, installs 3.6.5 again

sudo service mongodb start # now works properly

Windows updated with everything from windows update windows version 1803 (OS Build 17134.48)

ziocleto commented 6 years ago

Same here; one liner:

cp /usr/bin/mongod /etc/init.d/

This is the less hacky solution, but I think something is wrong with the installation as you normally should start daemon as services (*d) not the app itself (mongodb) ( which doesn't even exist on debian form what I'm seeing). There are other oddities as /etc/mongod.conf doesn't get read so you have to create /data/db as default place to save data or force load that config file.

Also if you went for apt-get install mongodb that's an unofficial unsupported debian version. mongodb-org is the "real" one.

Alternatively you can mongod --fork it but ideally it should start as daemon in init.d

I'm installing it on a docker debian 9 stretch container not sure if that causes any issues.

tej12shah commented 6 years ago

screenshot from 2018-07-07 19 40 47 Hello everyone, I'm trying to install mongodb on my ubuntu version 14.04. when I'm trying to import the public key, I'm getting this error. I checked with the network as well, it's not blocked by the firewall.

It'll be a great help if you guys can help me out with this.

Thanks.

therealkenc commented 6 years ago

when I'm trying to import the public key, I'm getting this error

That one is #3286 (not specific to MongoDB)

tej12shah commented 6 years ago

@therealkenc okay. I'm new to this. so can you help me out with this as so what corrections/changes i need to do?

Thanks!

ghost commented 6 years ago

After 6 hours of trying and trying, I can not for my life get MongoDB to start or run properly on Ubuntu 18 inside a docker-container on Codenvy.io. I followed xtianus79 instructions, and MongoDB did re-install and it seem to run when forced to manually start it. But it won't start and function like it suppose to. I followed to official instructions to begin with, but nothing seem to work.

Help would be appreciated! I'm using Codenvy.io (Eclipse/Che)

root@ecdd9f488e89:/# service mongod start
mongod: unrecognized service
root@ecdd9f488e89:/# mongo
MongoDB shell version v4.0.3
connecting to: mongodb://127.0.0.1:27017
2018-10-17T20:24:16.101+0200 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:257:13
@(connect):1:6
exception: connect failed
root@ecdd9f488e89:/# cat /etc/issue
Ubuntu 18.04.1 LTS \n \l
ghost commented 6 years ago

honestly..iv just about had it with this

why cant things be easier XD

!! service mongod start Failed to start mongod.service: Unit mongod.service not found. !!

ravindk89 commented 6 years ago

Hi folks! I'm a Senior Technical Writer at MongoDB and work with the server documentation. We were linked over to this pull, so I'm leaving a comment here.

Please note that the Windows Subsystem for Linux (WSL) is not an officially supported method for installing MongoDB. While Ubuntu 12, 14, and 18 are supported operating systems, we cannot guarantee that they will function as expected through the WSL. It's worth noting that Ubuntu 18 only supports MongoDB 4.0 and later.

As noted on the WSL FAQ, WSL does not guarantee functionality of all Linux applications. Many of the issues here appear to be related to trying to start MongoDB as a service. Please see this comment by @therealkenc who has some workarounds for the missing systemd support required for things to work as documented.

User @Mikeysax posted a gist in #796 with instructions that may work for some users.

We're always happy to see people trying out MongoDB, and would encourage users looking for MongoDB support in particular to check out the MongoDB Google Group for guidance. That said, running MongoDB within the WSL compatibility layer may produce unexpected behavior that we have limited insight or control over. If you want a sandbox to play in, you can always spin up a free MongoDB Atlas cluster as a sandbox to poke around with. Alternatively, spin up a Virtual Machine running a supported OS and install MongoDB using the available instructions.

One last note: Some platforms provide a mongodb package you can install. These are unofficial packages and not maintained by MongoDB. The mongodb-org package is the official package. If you're not running sudo apt-get install mongodb-org (or the equivalent for your desired linux OS), something is probably going to go wrong. Check our installation documentation if you're in doubt!

zffocussss commented 5 years ago

mongodb can not start using systemctl ,but it works with /usr/bin/mongod --config /etc/mongod.conf & mongodb version: 4.0.5

djadomi commented 5 years ago

I'll add this comment here, although it's not got anything to do with WSL, as this thread comes high up in Google results when searching for Debian help.

As others have noted, it's useful to let apt/aptitude install the service, then overwrite it with the mongodb-org packages. Then you just need to unmask the original service. This is how I do it (on Stretch - change repo if necessary):

curl https://www.mongodb.org/static/pgp/server-4.0.asc | apt-key add -
echo 'deb http://repo.mongodb.org/apt/debian stretch/mongodb-org/4.0 main' >  /etc/apt/sources.list.d/mongodb-org-4.0.list
aptitude update
aptitude install mongodb
aptitude install mongodb-org
systemctl unmask mongodb

Aptitude will ask for confirmation to remove the Debian version of MongoDB; answer "yes".

deen804 commented 5 years ago

I removed mongo lock file first sudo rm /var/lib/mongodb/mongod.lock then started the mongod using the following command sudo mongod --fork -f /etc/mongod.conf

guilhermenunes09 commented 5 years ago

As pointed by @xtianus79 and to my surprise, on Ubuntu Xenial doing 4- sudo apt-get install mongodb works like magic

It works on Ubuntu 18 as well. This is the most straight foward way I guess.

Raju commented 5 years ago

try this, it works on MongoDB version 4.20

wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add - && echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list && sudo apt-get update && sudo apt-get install -y mongodb-org

https://github.com/mongodb/mongo/blob/master/debian/init.d copy paste the above links content (init.d content) into a file called mongod in /etc/init.d/, if does not exist create one

then run sudo service mongod start/stop/status

if not working try sudo mongod --dbpath ~/mongodb/are

make sure to create ~/mongodb/db folders

gavindang2911 commented 4 years ago

For me: First install mongodb server by : sudo apt-get install mongodb-server
Then: sudo service mongodb start

This is how I solved the issues

sarvjot commented 2 years ago

@Raju's solution is discussed here as well : https://docs.microsoft.com/en-us/windows/wsl/tutorials/wsl-database#install-mongodb, and fortunately this script worked for me!

alex-maxime commented 2 years ago

@sarvjot this solution did not work for me unfortunately . sudo service mongodb start fail

Standard-IO commented 1 year ago

I had this problem. The mongod command worked fine. The problem was in the mogodb service. At the end I found that somentimes the installer fails to assing the correct owner to some dirs and files that are necessary to start the service under the mongodb user (this user is used in the start service script by default).

So I changed the ownership from these dirs:

sudo chown -R mongodb:mongodb  /var/lib/mongodb
sudo chown -R mongodb:mongodb /var/log/mongodb
sudo chown  mongodb:mongodb /etc/mongod.conf

finally just launch the service with: sudo service /etc/init.d/mongodb

This work in windows 11 with WSL2 ubuntu 20.04.

You can find more information in mongodb community

I hope this helps to someone.

MarvinXu commented 1 year ago

Above doesn't work for me. service simply doesn't work for mongo.

I finnaly made my WSL support systemd, and it worked for me like a charm! https://learn.microsoft.com/en-us/windows/wsl/systemd#how-to-enable-systemd

KiarieWinfred commented 1 year ago

Thank you. This worked like a charm