Creating a database in Mongo: can't connect, get "connect failed" - mongodb

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:16:25 Error: couldn't connect to server 127.0.0.1 (anon):1154
exception: connect failed
How can I connect to mongo in order to create a new database? Alternatively, can I create a new database from the command line?
Slightly surprisingly, the Mongo docs don't seem to cover how to create a database.
Thanks.

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!

cd /var/lib/mongodb/
remove mongod.lock file from this folder
sudo start mongodb (in console)
mongo (in console)
And it runs fine.

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.

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.

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

Related

How to open a Mongo Atlas backup snapshot locally?

I've enabled automatic backups on Mongo Atlas.
Now I need to view and query a specific snapshot to check some documents?
How can I do it quickly and safely?
Go to Mongo Atlas console, click on your cluster, then go to Backup tab and download your snapshot:
You'll get a .tgz archive. It opens in a popup, so mind your blocker.
Unpack the archive, then run
docker run -it -p 27017:27017 -v /tmp/extracted/snapshot/dir:/data/db mongo
Now you can connect to the snapshot data using a mongo client like MongoDB Compas using default connection (localhost:27017).
The accepted answer did not work for me. Alternative approach:
Download the backup from your Atlas console and extract it.
Then run the following in your Terminal:
mongod --dbpath ~/Downloads/Cluster0-2020-11-20T15-53-03.006Z
Replacing Cluster0... with your extracted folder.
Now you can connect to the snapshot data using a mongo client like MongoDB Compass using default connection (localhost:27017).
You can also define a custom port with --port

How to login to mongodb console in server

I'm trying to create a new collection in an already existing mongodb database on an ubuntu server. I tried running the command mongod but it says the mongod not found
Make sure your mongo server is running:
you can start using command sudo service mongod restart . Once it started just type mongo , It will take you in mongo console.
Try mongo.
mongod is the one that starts the server. mongo is the client that connects to the server.
If your environment variable is set correctly, it should work.
If it still says command not found, try /usr/bin/mongo
You can find more info here: https://docs.mongodb.com/manual/mongo/

Connecting MongoDB to local DB file

I've downloaded the production mongodb and dropped the folder with the data into my documents directory under a folder titled project1. I'm noticing that launching mongo with the following command mongod --port 3000 --dbpath ~/Documents/project/prod/ and then following that up with a mongo --port 3000 isn't causing the shell to find the database. The db itself should be called pord. Im wondering what am I not doing correctly. How does one get MongoDB to use a database that they've downloaded, its looking like specifying the local path isn't working.
The solution is to actually use mongorestore feature. The command is mongorestore -d db_name_here -c collection_name_here ~/path/to/file

MongoDB - mongo command runs but mongod doesn't

I have a MEAN droplet on digital ocean and I've found that when I run the mongo command I connect to test successfully and have access to my other databases, but if I try to run the mongod command I get the following message:
*********************************************************************
ERROR: dbpath (/data/db) does not exist.
Create this directory or give existing directory in --dbpath.
See http://dochub.mongodb.org/core/startingandstoppingmongo
*********************************************************************
How is this possible? I thought mongo was connecting to a specific instance of mongod.
I will create the /data/db folder, but I feel like I might just be ignoring another problem with setup configuration that has allowed this to happen.
/data/db will be the place you store your database data. After you created that folder, you can run mongod as normal.
The mongod is a command to start mongodb server. And mongo is a command line interface to make you community with mongodb server.
So you should start the server -> community with server.

Where is the meteor MongoDB database?

When I create a meteor app, where is the database?
I have an app called leaderboard, but when I run mongo shell and do show dbs I see only local (empty) and test but test doesn't doesn't have the same contents as my leaderboard app. Where does meteor create the Mongo database and how can I access it from mongo shell (so I can load some data into it)?
You need to be running the application with the meteor run command in one session, at which point you can run mongo meteor in another session on the same machine, which will include something like
[kfullert#shotgun ]$ meteor mongo
MongoDB shell version: 2.2.1
connecting to: 127.0.0.1:3002/meteor
At that point, you can use the URL in the "connecting to" line with the standard mongo tools (caveat - you need to be running your project with meteor at the same time, as "meteor run" is what spins up the mongo server for your project
[kfullert#shotgun ]$ mongo 127.0.0.1:3002/meteor
MongoDB shell version: 2.2.3
connecting to: 127.0.0.1:3002/meteor
>
For mongoimport, you'll probably want something like:
[kfullert#shotgun ]$ mongoimport -h 127.0.0.1 --port 3002 -d meteor
Additionally, it may be possible to run mongoimport without meteor running, by using the following switch from your project root directory (untested so beware)
mongoimport --dbpath .meteor/local/db -d meteor
For apps running a local db server, APPDIR/.meteor/local/db
You can connect to your app's mongodb with meteor mongo and then us show collections to list the Meteor.Collections you've created.