Creating a database in Mongo: can't connect, get “connect failed”

后端 未结 5 826
刺人心
刺人心 2020-12-13 19:02

I want to create a new database in Mongo. However, I\'m having trouble connecting:

:~$ mongo
MongoDB shell version: 1.6.5
connecting to: test
Tue Dec 21 18:         


        
相关标签:
5条回答
  • 2020-12-13 19:37

    You'll need to run mongod (the daemon) before you can use mongo (the client), it's easiest to just run it in another shell; These should be in your path if mongo is installed correctly. After that the docs should get you through creating and editing dbs and collections.

    0 讨论(0)
  • 2020-12-13 19:47

    Just try following commands in given order :

    sudo rm /var/lib/mongodb/mongod.lock
    
    sudo mongod --repair
    
    sudo service mongodb start
    
    sudo service mongodb status
    
    0 讨论(0)
  • 2020-12-13 19:59

    First you'll need to run mongod on one terminal. Then fire up another terminal and type mongo. This shall open the mongo shell. You also need to create /data/db/ where mongo will store your databases.

    0 讨论(0)
  • 2020-12-13 20:00

    In order to open Mongo JavaScript shell, a Listener should be initialized first.

    So, first run mongod.exe before running mongo.exe. Both are in the same location(/bin).

    There is no separate commands to create a db in mongodb. Just type "use dbname;" in console. Now you have created a db of the name 'dbname'. Now, if you type 'show databases' you cannot see the db name you just created. Because, mongo will not create any db, util you create collection and insert a document into that collection.

    Hope this is useful to you!

    0 讨论(0)
  • 2020-12-13 20:00
    1. cd /var/lib/mongodb/

    2. remove mongod.lock file from this folder

    3. sudo start mongodb (in console)

    4. mongo (in console)

      And it runs fine.

    0 讨论(0)
提交回复
热议问题