Mongodb how to connect and import data - mongodb

I installed mongodb in Mac by homebrew. Then I tried to start it by mongodb. Failed. Then I start it by mongod --dbpath /data/db. Then it is waiting forever. How to start mongodb?
2015-08-22T16:08:52.619+0100 I CONTROL [initandlisten] build info: Darwin mimac 14.4.0 Darwin Kernel Version 14.4.0: Thu May 28 11:35:04 PDT 2015; root:xnu-2782.30.5~1/RELEASE_X86_64 x86_64 BOOST_LIB_VERSION=1_49
2015-08-22T16:08:52.619+0100 I CONTROL [initandlisten] allocator: system
2015-08-22T16:08:52.619+0100 I CONTROL [initandlisten] options: { storage: { dbPath: "/data/db/" } }
2015-08-22T16:08:52.628+0100 I NETWORK [initandlisten] waiting for connections on port 27017

To start mongodb automatically you can use command:
ln -sfv /usr/local/opt/mongodb/*.plist ~/Library/LaunchAgents
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist;

mongod is the daemon (i.e the mongoDB server itself). you should run it, as you did.
after there is a running server, you can:
run the mongo shell, with the mongo command. this would give you an interface to the databases you have, such as querying and inserting data.
to import an external file into mongo database, you should use the mongoimport command line tool, which is provided with mongodb.
mongoimport -d database_name -c collection_name < filename.json
mongoimport can import json, csv and tsv files.

Related

mongodump error connecting to db server in localhost

It may be a very simple/obvious answer but I've tried running the following variations of the same command to backup a local mongo database and they all fail by returning:
Failed: error connecting to db server: no reachable servers
These are the commands:
mongodump --host localhost --port 27017 --db mydbname --collection mycollection
mongodump -h localhost:27017 --db mydbname --collection mycollection -u user -p passw --out <folder path>
mongodump -h localhost:27017 --db mydbname --collection mycollection --out <folder path>
mongodump --port 27017 --db mydbname --collection mycollection --out <folder path>
mongod instance was running on a separate terminal window while I tried them all
I JOURNAL [initandlisten] journal dir=/data/db/journal
I JOURNAL [initandlisten] recover : no journal files present, no recovery needed
I JOURNAL [durability] Durability thread started
I CONTROL [initandlisten] MongoDB starting : pid=88451 port=27017 dbpath=/data/db 64-bit host=Diegos-MBP.home
I CONTROL [initandlisten]
I CONTROL [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
I JOURNAL [journal writer] Journal writer thread started
I CONTROL [initandlisten] db version v3.0.6
I CONTROL [initandlisten] git version: nogitversion
I CONTROL [initandlisten] build info: Darwin yosemitevm.local 14.5.0 Darwin Kernel Version 14.5.0: Wed Jul 29 02:26:53 PDT 2015; root:xnu-2782.40.9~1/RELEASE_X86_64 x86_64 BOOST_LIB_VERSION=1_49
I CONTROL [initandlisten] allocator: system
I CONTROL [initandlisten] options: {}
I NETWORK [initandlisten] waiting for connections on port 27017
I NETWORK [initandlisten] connection accepted from 127.0.0.1:55910 #1 (1 connection now open)
I cannot find mongodb.conf in /etc/mongod.conf. I installed mongo with homebrew. Maybe it is related to an authentication issue?
I just ran into the same issue and it appears that using 127.0.0.1 instead of localhost in the host setting worked...
Like :
mongodump -h 127.0.0.1:27017 -d demo
If you are using a replicat set (like "myset"), use this instead :
mongodump -h myset/127.0.0.1:27017 -d demo
Hope this helps...

MongoDB 3.0.1 mongodump error

I need to create a dumpfile of a MongoDB db using 'mongodump'.
ubuntu-0864947#ubuntu-vm:~$ mongo
MongoDB shell version: 3.0.1
connecting to: test
Server has startup warnings:
2015-03-30T14:26:08.806+0200 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2015-03-30T14:26:08.807+0200 I CONTROL [initandlisten]
2015-03-30T14:26:08.807+0200 I CONTROL [initandlisten]
2015-03-30T14:26:08.807+0200 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2015-03-30T14:26:08.808+0200 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2015-03-30T14:26:08.808+0200 I CONTROL [initandlisten]
> mongodump
2015-04-30T15:56:00.719+0200 E QUERY ReferenceError: mongodump is not defined
at (shell):1:1
> mongodump RotterdamHaven
2015-04-30T15:56:15.970+0200 E QUERY SyntaxError: Unexpected identifier
> use RotterdamHaven
switched to db RotterdamHaven
> mongodump RotterdamHaven
2015-04-30T16:26:13.742+0200 E QUERY SyntaxError: Unexpected identifier
Can anyone please tell me what I am doing wrong here?
Thanks!
mongodump is an executable like mongo, you need to execute it in the OS shell, not in the MongoDB shell:
ubuntu-0864947#ubuntu-vm:~$ mongodump <arguments>
Just Go to bin Directory of Mongodb (mycase : c:\mongodb\bin)
Then,
Run : following Command
mongodump --db database_name
Then go to Dump Directory Created inside Bin Folder
There you'll find folder of your named database and inside that
.bson & .json file would be there just keep it safe .bson is your backup file
Cheers

MongoDB starts up, but commands don't respond at all

Using an old 32bit machine to play w mongo. Got it installed, starts up normally, but nothing I type in to the console does any good.
2015-02-24T23:45:35.996-0500 [initandlisten] db version v2.6.7
2015-02-24T23:45:35.996-0500 [initandlisten] git version: a7d57ad27c382de82e9cb93bf983a80fd9ac9899
2015-02-24T23:45:35.996-0500 [initandlisten] build info: Linux ip-10-80-47-117 2.6.18-194.el5xen #1 SMP Tue Mar 16 22:08:06 EDT 2010 i686 BOOST_LIB_VERSION=1_49
2015-02-24T23:45:35.997-0500 [initandlisten] allocator: system
2015-02-24T23:45:35.997-0500 [initandlisten] options: { storage: { dbPath: "/home/Chris/data/db" } }
2015-02-24T23:45:36.040-0500 [initandlisten] waiting for connections on port 27017
db
use ccc-db
db.currentOp()
2015-02-24T23:46:36.038-0500 [clientcursormon] mem (MB) res:53 virt:206
2015-02-24T23:46:36.038-0500 [clientcursormon] mapped:80
2015-02-24T23:46:36.038-0500 [clientcursormon] connections:0
I tried to connect from another machine as well and couldn't get anywhere, but really that's not surprising since I can't set up any mongo users.
Looks like you ran mongod and started typing commands in the same terminal window.
If you run mongod in a terminal simply then mongod spits out the loggging information on the screen. This way the mongod process will keep running until you stop it you can just press Ctrl+C or you close the terminal window.
You might have done something like this -
$ mongod --dbpath /home/Chris/data/db
And, then you tried to type in the commands on the same window. Remember, you need to first connect to this running mongod process through a client - mongo. Only then you can send commands to the running mongod process.
So, open another Terminal window and run the mongodb client, ie. mongo first. Like this -
$ mongo
MongoDB shell version: 2.6.7
connecting to: localhost:27017/test
> use ccc-db
> db.currentOp()

how to set mongod --dbpath

very new to mongodb and databases in general. whenever i run mongo i receive this error message: ​​​
MongoDB shell version: 2.4.9
connecting to: test
Thu Jan 30 13:03:33.170 Error: couldn't connect to server 127.0.0.1:27017
at src/mongo/shell/mongo.js:145
exception: connect failed
running mongod i see this:
Thu Jan 30 13:13:36.588 [initandlisten] MongoDB starting : pid=29408 port=27017 dbpath=/usr/local/var/mongodb 64-bit host=Kimis-MacBook-Air-2.local
Thu Jan 30 13:13:36.588 [initandlisten]
Thu Jan 30 13:13:36.588 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
Thu Jan 30 13:13:36.588 [initandlisten] db version v2.4.9
Thu Jan 30 13:13:36.588 [initandlisten] git version: nogitversion
Thu Jan 30 13:13:36.588 [initandlisten] build info: Darwin minilionvm.local 11.4.2 Darwin Kernel Version 11.4.2: Thu Aug 23 16:25:48 PDT 2012; root:xnu-1699.32.7~1/RELEASE_X86_64 x86_64 BOOST_LIB_VERSION=1_49
Thu Jan 30 13:13:36.588 [initandlisten] allocator: tcmalloc
Thu Jan 30 13:13:36.588 [initandlisten] options: { bind_ip: "127.0.0.1", config: "/usr/local/etc/mongod.conf", dbpath: "/usr/local/var/mongodb" }
Thu Jan 30 13:13:36.591 [initandlisten] journal dir=/usr/local/var/mongodb/journal
Thu Jan 30 13:13:36.591 [initandlisten] recover begin
Thu Jan 30 13:13:36.591 [initandlisten] recover lsn: 108155770
Thu Jan 30 13:13:36.591 [initandlisten] recover /usr/local/var/mongodb/journal/j._0
Thu Jan 30 13:13:36.591 [initandlisten] journal file version number mismatch got:4147 expected:4149. if you have just upgraded, recover with old version of mongod, terminate cleanly, then upgrade.
Thu Jan 30 13:13:36.592 [initandlisten] dbexception during recovery: 13536 journal version number mismatch 16711
Thu Jan 30 13:13:36.592 [initandlisten] exception in initAndListen: 13536 journal version number mismatch 16711, terminating
Thu Jan 30 13:13:36.592 dbexit:
Thu Jan 30 13:13:36.592 [initandlisten] shutdown: going to close listening sockets...
Thu Jan 30 13:13:36.592 [initandlisten] shutdown: going to flush diaglog...
Thu Jan 30 13:13:36.592 [initandlisten] shutdown: going to close sockets...
Thu Jan 30 13:13:36.592 [initandlisten] shutdown: waiting for fs preallocator...
Thu Jan 30 13:13:36.592 [initandlisten] shutdown: lock for final commit...
Thu Jan 30 13:13:36.592 [initandlisten] shutdown: final commit...
Thu Jan 30 13:13:36.592 [initandlisten] shutdown: closing all files...
Thu Jan 30 13:13:36.592 [initandlisten] closeAllFiles() finished
Thu Jan 30 13:13:36.592 [initandlisten] shutdown: removing fs lock...
Thu Jan 30 13:13:36.592 dbexit: really exiting now
when i manually set my mongo dpath to mongod --dbpath /data/db (as should be the default on installation) and keep it running in terminal everything runs fine. but once i close it, everything breaks again. my question is:
why is my dbpath set to /usr/local/var/mongodb?
how do i fix this error so mongo works on my machine?
i'm assuming i either need to permanently set the dbpath to /data/db or reconfigure something so it works with the dbpath as /usr/local/var/mongodb
i fairly new to unix commands as well so not entirely sure how to fix this error.
thank you for any suggestions!
so, digging through the mongodb documentation i read this:
"Unless specified, mongod will look for data files in the default /data/db directory. (Windows systems use the \data\db directory.) If you installed using a package management system. Check the /etc/mongodb.conf file provided by your packages to see the configuration of the dbpath."
when i took a look at the files within /etc/ there is no mongodb.conf file... however, i do see /usr/local/etc/mongod.conf... so if i understand this correctly, i should create a file called /etc/mongodb.conf and set the dbpath to /data/db.
do i need to delete the stuff within /usr/local/ as well?
i think this has something to do with how my PATH are setup... could someone explain to me how to fix this in unix so i won't have this problem?
thanks again!
Have only tried this on Mac:
Create a data directory in the root folder of your app
cd into your wherever you placed your mongo directory when you installed it
run this command:
mongod --dbpath ~/path/to/your/app/data
You should be good to go!
You can set dbPath in the mongodb.conf file:
storage:
dbPath: "/path/to/your/database/data/db"
It's a YAML-based configuration file format (since Mongodb 2.6 version), so pay attention no tabs only spaces, and space after ": "
usually this file located in the *nix systems here: /etc/mongodb.conf
So then just run
$ mongod -f /etc/mongodb.conf
And mongod process will start...
(on the Windows something like)
> C:\MongoDB\bin\mongod.exe -f C:\MongoDB\mongod.conf
First you will have a config file in /etc/mongodb.conf, therefore this sounds like a homebrew install which will use some more standardized paths. The whole /data/db/ thing is referenced in a lot of manual install documentation.
So basically from your log the server is not running, it's shutting down, so there is nothing for the shell to connect to. Seems like you have had some unclean shutdowns/restarts which has led to the inconsistency.
Clear the files in the journal /usr/local/var/mongodb/journal/ on your config.
Also:
sudo rm /var/lib/mongodb/mongod.lock
Just in case, even though that part looks clean. And then restart.
For me it must have:
mongod --dbpath=/whatever/data/path
Create a directory db in home, inside db another directory data
cd
mkdir db
cd db
mkdir data
then type this command--
mongod --dbpath ~/db/data
mongod --port portnumber --dbpath /path_to_your_folder
By default portnumber is 27017 and path is /var/lib/mongodb
You can set your own port number and path where you want to keep all your database.
You could also configure mongod to run on start up so that it is automatically running on start up and the dbpath is set upon configuration. To do this try:
mongod --smallfiles --config /etc/mongod.conf
The --smallfiles tag is there in case you get an error with size. It is, of course, optional. Doing this should solve your problem while also automating your mongodb setup.
sudo mongod --dbpath ~/data
This made it work for me.
very simple:
sudo chown mongodb:mongodb /var/lib/mongodb/mongod.lock
Scenario: MongoDB(version v4.0.9).
Set custom folder(with name: myCustomDatabases), where to store databases.
In custom folder(with name: myCustomDatabases), have to create database (with name: newDb).
Resolve:
Create custom folder(with name: myCustomDatabases):
D:>md myCustomDatabases
Run 'mongod --dbpath' with path to custom folder(with name: myCustomDatabases):
mongod --dbpath "D:\myCustomDatabases"
From another 'cmd' run 'mongo':
D:>mongo
3.1. Show all databases, stored in custom folder(with name: myCustomDatabases):
>show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
3.2. Use database with name newDb:
> use newDb
switched to db newDb
3.3. Show all databases, stored in custom folder(with name: myCustomDatabases):
>show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
!!! Noticed, that newDb is NOT in the list !!!
3.4. Have to create a collection with a document,
which will create the database newDb.
> db.Cats.insert({name: 'Leo'})
WriteResult({ "nInserted" : 1 })
The insert({name: 'Leo'}) operation creates:
the database newDB and the collection Cats, because they do not exist.
3.5. Now the new created database newDb will be displayed in the list.
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
newDb 0.000GB
3.6. Now in custom folder D:\myCustomDatabases, have database newDb.
Windows environment, local machine.
I had an error
[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 ::
No connection could be made because the target machine actively refused it. :
After some back and forth attempts I decided
to check Windows "Task Manager". I noticed that MongoDB process is stopped.
I made it run. Everything starts working as expected.
You can use --dbpath flag in order to provide it with the parameters i.e, the directory address. I found it useful for Windows 10 OS.
With the default settings it is usually not required.
Go to the directory you would like your data to be saved in.
Create a folder called "mongodb" and within it create another directory named "data"
Navigate back to the "mongodb" directory in the terminal window and place the following line of mongod --dbpath=data --bind_ip 127.0.0.1
Run this command each time you want to start up the mongodb database
With the terminal command mongod --dbpath /YOUR/PATH/TO/DATA/ I need to type it everytime I want to start MongoDB. Somehow setting it in the mongo.conf didn't load the path properly. I found a solution to avoid having to type the long command every time by adding an alias in ~/.zshrc
Shut down MongoDB (while mongo is running type in Terminal: use admin press enter and on a next line db.shutdownServer().
Close the Terminal window.
Open ~/.zshrc or when your macOS is using bash-shell open ~/.bash_profile
Add in the file: alias -g mongod="MongoDB --dbpath /YOUR/PATH/TO/DATA/
Save your .zshrc or .bash_profile file
Open Terminal
Type source ~/.zshrc or for bash-shell users: source ~/.bash_profile
Type mongod
MongoDB should now be running with the right path and connection. Terminal can be closed now. To check whether mongo is running open Terminal, type mongo which gives you the interface in Terminal or type top and check mongo in the list of applications.

Error while connecting mongodb shell [duplicate]

when i setup mongodb in my ubuntu , i try : ./mongo it show this error :
couldn't connect to server 127.0.0.1 shell/mongo.js
so what can i do ,
thanks
Manually remove the lockfile: sudo rm /var/lib/mongodb/mongod.lock
Run the repair script: sudo -u mongodb mongod -f /etc/mongodb.conf --repair
Please note the following:
You must run this command as the mongodb user. If you run it as root,
then root will own files in /var/lib/mongodb/ that are necessary to
run the mongodb daemon and therefore when the daemon trys to run
later as the mongodb user, it won't have permissions to start. In
that case you'll get this error: Unable to create / open lock file
for lockfilepath: /var/lib/mongodb/mongod.lock errno:13 Permission
denied, terminating.
On Ubuntu, you must specify the configuration file /etc/mongodb.conf
using the -f flag. Otherwise it will look for the data files in the
wrong place and you will see the following error: dbpath (/data/db/)
does not exist, terminating.
sudo rm /var/lib/mongodb/mongod.lock
sudo -u mongodb mongod -f /etc/mongodb.conf --repair
sudo service mongodb start
Here is all, sometimes, it takes a little while to start mongo after performing these operations.
Trying running $mongod
If you get en error such as
MongoDB shell version: 2.0.5
connecting to: test
Fri Jun 1 11:20:33 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84
exception: connect failed
hisham-agil:~ hisham$ mongod
mongod --help for help and startup options
Fri Jun 1 11:24:47 [initandlisten] MongoDB starting : pid=53452 port=27017 dbpath=/data/db/ 64-bit host=hisham-agil.local
Fri Jun 1 11:24:47 [initandlisten] db version v2.0.5, pdfile version 4.5
Fri Jun 1 11:24:47 [initandlisten] git version: nogitversion
Fri Jun 1 11:24:47 [initandlisten] build info: Darwin gamma.local 11.3.0 Darwin Kernel Version 11.3.0: Thu Jan 12 18:48:32 PST 2012; root:xnu-1699.24.23~1/RELEASE_I386 i386 BOOST_LIB_VERSION=1_49
Fri Jun 1 11:24:47 [initandlisten] options: {}
Fri Jun 1 11:24:47 [initandlisten] exception in initAndListen: 10296 dbpath (/data/db/) does not exist, terminating
Fri Jun 1 11:24:47 dbexit:
Fri Jun 1 11:24:47 [initandlisten] shutdown: going to close listening sockets...
Fri Jun 1 11:24:47 [initandlisten] shutdown: going to flush diaglog...
Fri Jun 1 11:24:47 [initandlisten] shutdown: going to close sockets...
Fri Jun 1 11:24:47 [initandlisten] shutdown: waiting for fs preallocator...
Fri Jun 1 11:24:47 [initandlisten] shutdown: lock for final commit...
Fri Jun 1 11:24:47 [initandlisten] shutdown: final commit...
Fri Jun 1 11:24:47 [initandlisten] shutdown: closing all files...
Fri Jun 1 11:24:47 [initandlisten] closeAllFiles() finished
Fri Jun 1 11:24:47 dbexit: really exiting now
Then you've run into a basic startup error that is pretty common.
By default mongod will try to use /data/db for its database files, which in this case, does not exist.
You can't start
mongo
until you handle
mongod.
Try creating those directories and make sure they are writable by the same user that is running the mongod process.
**See similar question-- Getting an error, "Error: couldn't connect to server 127.0.0.1 shell/mongo.js" & when trying to run mongodb on mac osx lion
This is actually not an error... What happens here is that Mongo relies on a daemon in order to run the local database server, so in order to "fire up" the mongo server in your shell, you have to start the mongo service first.
For Fedora Linux (wich is the Distro I use) You have to run these commands:
1 sudo service mongod start
2 mongo
And there you have it! the server is going to run. Now, If you want Mongo service
to Start when the system boots then you have to run:
sudo chkconfig --levels 235 mongod on
And that's all! If you do that, now in the shell you just have to type mongo in order
to start the server but that's pretty much it, the problem is you have to start the SERVICE first and then the SERVER :)
P.S. The commands I posted might work on other linux distros as well, not just in fedora... In case not maybe you have to tweak some words depending on the distro you're using ;)
I got the same problem when I tried to install mongo. I got Error as,
Error
"Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84"
Solution:
First install mongod by using:
sudo apt-get install mongodb-server
Then type
mongod --dbpath /mongo/db
Then
sudo rm /var/lib/mongodb/mongod.lock
Then
sudo -u mongodb mongod -f /etc/mongodb.conf --repair
Thank You
You need to delete the lockfile mongod.lock or /var/lib/mongodb/mongod.lock on ubuntu, then you need to run mongod.exe or service mongodb start on ubuntu first, then run mongo.exe or mongo on ubuntu.
Either your mongod is not running (check using "ps" command) or it is listening on some outside IP address and not on localhost. So first check the process list if 'mongod' is running. If yes, check with "netstat -nap" for the related port.
Of course you can start mongod on the console manually or even look into the mongod logfile
(if there is one configured...depending on how you installed mongod).
First you have to make sure that all the files and directories in your /var/lib/mongodb/ folder (or whichever folder dbpath points to) belong to the mongodb user and mongodb group.
cd /var/lib/mongodb/
sudo chown mongodb filename.*
sudo chgrp mongodb filename.*
sudo chown -R mongodb directory
sudo chgrp -R mongodb directory
(Replace filename and directory with their respective names)
Then you can remove the lock, repair the database and restart the daemon as other people already mentioned:
sudo rm /var/lib/mongodb/mongod.lock
sudo -u mongodb mongod -f /etc/mongodb.conf --repair
sudo service mongodb start
First start your mongo server by
Users-MacBook-Pro:csv1 Admin$ mongod
all output going to: /usr/local/var/log/mongodb/mongo.log
Then open another terminal window and open shell
Users-MacBook-Pro:csv1 Admin$ mongo
Also check that your root partition has enough space to start mongod.
df -h /
You'll see smth like this on mongod launch:
Mon Aug 12 17:02:59.159 [initandlisten] recover : no journal files present, no recovery needed
Mon Aug 12 17:02:59.159 [initandlisten]
Mon Aug 12 17:02:59.159 [initandlisten] ERROR: Insufficient free space for journal files
Mon Aug 12 17:02:59.159 [initandlisten] Please make at least 3379MB available in /var/lib/mongodb/journal or use --smallfiles
Mon Aug 12 17:02:59.159 [initandlisten]
Mon Aug 12 17:02:59.159 [initandlisten] exception in initAndListen: 15926 Insufficient free space for journals, terminating
Mon Aug 12 17:02:59.159 dbexit:
Mon Aug 12 17:02:59.159 [initandlisten] shutdown: going to close listening sockets...
On Ubuntu, try this:
sudo invoke-rc.d mongodb start
It could be combination of $PATH and Permission issue.
Try following steps given below:
Update your $PATH variable to point to your MongoDB bin file. In my case brew install MongoDB to this folder:
/usr/local/Cellar/mongodb/2.4.6/
In order to update your $PATH variable, do following:
$ sudo vi /etc/paths
Then, press ‘i’ to insert text in Vi and append the your MongoDB path to the end of the ‘paths’ file and restart the terminal.
/usr/local/Cellar/mongodb/2.4.6/bin
Use ‘Esc : w q’ to save and exit from Vi editor.
Use echo to display your path variable:
$ echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/Cellar/mongodb/2.4.6/bin
Now try to check the Mongo version, if you get following, then you are on the right track!
$ mongo --version
MongoDB shell version: 2.4.6
Now we need to create the database directory. I used the default ‘/data/db’ location suggested in MongoDB docs. I also created a log directory to avoid any permission issues while Mongo tries to create any logs. Change ownership and that will do the job.
$ sudo mkdir /data/db
$ sudo mkdir /data/log
$ whoami
username
$ chown -R username /data
Now, we will create a default config file for MongoDB to be provided for the first time we run ‘mongod’ command. Now, I will also like to point out that ‘mongod’ will start a service, which will listen for incoming data connections. This is similar having ‘$service mysqld start’ executed.Let’s go ahead and create the config file. Please keep in mind that I have created very basic config file. However, you can add many other variables to configure MongoDB. This is the first time I am playing with MongoDB, so I just know as much as I read on MongoDB docs!I created ‘mongodb.conf’.
$ sudo vi /etc/mongodb.conf
Add following:
fork = true
port = 27017
quiet = true
dbpath = /data/db
logpath = /data/log/mongod.log
logappend = true
journal = true
Please note that the default port for MongoDB server is 27017. Use your own path for dbpath and logpath you created in Step – 5. Don’t forget to close and save the conf file.
Now we are all set to start our MongoDB service. Open two instances of Terminal.In Terminal 1, type in:
$ sudo mongod -f /etc/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3516
all output going to: /data/log/mongod.log
child process started successfully, parent exiting
If you get above message, then know that you have successfully started your Mongod service.
Now, to connect to it, in Terminal 2 type following:
$mongo test
MongoDB shell version: 2.4.6
connecting to: test
Server has startup warnings:
Tue Sep 3 16:55:43.527 [initandlisten]
Tue Sep 3 16:55:43.527 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
>
Ignore the warnings, but you are successfully connected to the ‘test’ database! Cool!
That's all. I applied this solution, when I tried to install copy of MongoDB on my Mac for the first time. See if this help you too.
For detailed post you can go here - http://arcanebytes.com/2013/09/03/mongodb-installation-on-mac-os-x/#comment-1036112094.
I hope it helps!
Cheers,
Chinmay
I solved this problem on ubuntu 12.04 by following steps:
1) sudo rm /var/log/mongodb
2) sudo rm /var/lib/mongodb
3) I removed mongo and then installed it again
4) sudo service mongodb restart
and All is Well
For Ubuntu:
Just Open the terminal and enter the below command.
You just have to restart your mongoDB.
sudo service mongodb restart