MongoDB Basics
MongoDB is a document oriented data base.
How to install mongoDB in ubunthu?
1. Download mongoDB 32/64bit version from http://www.mongodb.org/downloads
If you are a developer and you are going to store data less than 2GB then go for 32 bit version. Otherwise go for 64 bit version.
will give you the OS bit version.
32 bit:
Linux krishnakumar 3.13.0-35-generic #62-Ubuntu SMP Fri Aug 15 01:58:01 UTC 2014 i686 i686 i686 GNU/Linux
64 bit:
Linux discworld 2.6.38-8-generic #42-Ubuntu SMP Mon Apr 11 03:31:50 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux
Mine is 32 bit version so I have downloaded 32 bit version into downloads directory
2. Extract it into the folder.
krishnakumar@krishnakumar:~$ tar -xvf ~/Downloads/mongodb-linux-x86_64-2.6.7.tgz
krishnakumar@krishnakumar:~/Documents/mongodb-linux-i686-2.6.7/bin$ ./mongod
But this command does not succeed and gives below message.
ERROR: dbpath (/data/db) does not exist.
By default all the data data files will be stored into /data/db folder but I have not created it.
So, one way to avoid this error is, just create the above folder structure
krishnakumar@krishnakumar:~/Documents/mongodb-linux-i686-2.6.7/bin$ ./mongod
Started mongoClient
krishnakumar@krishnakumar:~/Documents/mongodb-linux-i686-2.6.7/bin$ ./mongo
otherwise while starting we can provide our own path to store files as like below.
Use the parameter --dbpath
krishnakumar@krishnakumar:~/Documents/mongodb-linux-i686-2.6.7/bin$ mongod --dbpath ~/Documents/MongoDBFiles/
By default it will point to test database and if you wish to create own database use "use" command.
I have added a student collection with some basic details as below.
How to take backup in mongodb?MongoDB is a document oriented data base.
How to install mongoDB in ubunthu?
1. Download mongoDB 32/64bit version from http://www.mongodb.org/downloads
If you are a developer and you are going to store data less than 2GB then go for 32 bit version. Otherwise go for 64 bit version.
krishnakumar@krishnakumar:~$ uname -a
will give you the OS bit version.
32 bit:
Linux krishnakumar 3.13.0-35-generic #62-Ubuntu SMP Fri Aug 15 01:58:01 UTC 2014 i686 i686 i686 GNU/Linux
64 bit:
Linux discworld 2.6.38-8-generic #42-Ubuntu SMP Mon Apr 11 03:31:50 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux
Mine is 32 bit version so I have downloaded 32 bit version into downloads directory
2. Extract it into the folder.
krishnakumar@krishnakumar:~$ tar -xvf ~/Downloads/mongodb-linux-x86_64-2.6.7.tgz
Now this has been extracted and move into the directory which you wish.
Here I am moving to Documents directory.
krishnakumar@krishnakumar:~/Downloads$ mv mongodb-linux-i686-2.6.7 ~/Documents/ krishnakumar@krishnakumar:~/Downloads$ cd ~/Documents/mongodb-linux-i686-2.6.7/bin/We can start mongodb service by giving below command
krishnakumar@krishnakumar:~/Documents/mongodb-linux-i686-2.6.7/bin$ ./mongod
But this command does not succeed and gives below message.
ERROR: dbpath (/data/db) does not exist.
By default all the data data files will be stored into /data/db folder but I have not created it.
So, one way to avoid this error is, just create the above folder structure
krishnakumar@krishnakumar:~/Documents/MongoDBFiles$ mkdir -p /data/db mkdir: cannot create directory ‘/data’: Permission denied krishnakumar@krishnakumar:~/Documents/MongoDBFiles$ sudo su root [sudo] password for krishnakumar: root@krishnakumar:/home/krishnakumar/Documents/MongoDBFiles# cd root@krishnakumar:~# mkdir -p data/db root@krishnakumar:~# mkdir -p /data/db root@krishnakumar:~# chmod 777 /data/dbI do not have permission to create the above mentioned folder structure, so I changed to root and created the folder and changed the folder permission. Now this starts with out error as below
krishnakumar@krishnakumar:~/Documents/mongodb-linux-i686-2.6.7/bin$ ./mongod
Started mongoClient
krishnakumar@krishnakumar:~/Documents/mongodb-linux-i686-2.6.7/bin$ ./mongo
otherwise while starting we can provide our own path to store files as like below.
Use the parameter --dbpath
krishnakumar@krishnakumar:~/Documents/mongodb-linux-i686-2.6.7/bin$ mongod --dbpath ~/Documents/MongoDBFiles/
By default it will point to test database and if you wish to create own database use "use" command.
Sun Jan 18 16:55:20.457 [initandlisten] > use student switched to db student > show collections >I have switched to student database and try to get the collections in the student db but there is no collection exists.
I have added a student collection with some basic details as below.
> use student
switched to db student
> show collections
> db.student.save({"name":"Krishnakumar","age":"27","address":[{"Street":"1st street","city":"erode"}]}
... )
> db.student.find()
{ "_id" : ObjectId("54bb996230e9091442d80903"), "name" : "Krishnakumar", "age" : "27", "address" : [ { "Street" : "1st street", "city" : "erode" } ] }
> db.student.find().pretty()
{
"_id" : ObjectId("54bb996230e9091442d80903"),
"name" : "Krishnakumar",
"age" : "27",
"address" : [
{
"Street" : "1st street",
"city" : "erode"
}
]
}
>
pretty() is a method which gives collection value as readable structure.mongodump is the command used to take backup of the database.
Syntax: mongodump --host<host name> --port <port> Ex: krishnakumar@krishnakumar:~/Documents/mongodb-linux-i686-2.6.7/bin$ ./mongodump --host localhost --port 27017 connected to: localhost:27017 2015-01-18T17:36:01.483+0530 all dbs 2015-01-18T17:36:01.484+0530 DATABASE: student to dump/student 2015-01-18T17:36:01.485+0530 student.system.indexes to dump/student/system.indexes.bson 2015-01-18T17:36:01.485+0530 1 documents 2015-01-18T17:36:01.485+0530 student.student to dump/student/student.bson 2015-01-18T17:36:01.486+0530 1 documents 2015-01-18T17:36:01.486+0530 Metadata for student.student to dump/student/student.metadata.json 2015-01-18T17:36:01.486+0530 DATABASE: admin to dump/admin krishnakumar@krishnakumar:~/Documents/mongodb-linux-i686-2.6.7/bin$
We can get dump of particular collection in the db as like below.
Syntax: mongodump --hostwe can view the backup files in dump directory<host name> --port <port> --collection <collection name> --db <DB> Ex: krishnakumar@krishnakumar:~/Documents/mongodb-linux-i686-2.6.7/bin$ ./mongodump --host localhost --port 27017 --collection student --db student connected to: localhost:27017 2015-01-18T17:39:01.720+0530 DATABASE: student to dump/student 2015-01-18T17:39:01.728+0530 student.student to dump/student/student.bson 2015-01-18T17:39:01.730+0530 1 documents 2015-01-18T17:39:01.730+0530 Metadata for student.student to dump/student/student.metadata.json krishnakumar@krishnakumar:~/Documents/mongodb-linux-i686-2.6.7/bin$
krishnakumar@krishnakumar:~/Documents/mongodb-linux-i686-2.6.7/bin$ ls bsondump mongo mongodump mongofiles mongooplog mongorestore mongostat dump mongod mongoexport mongoimport mongoperf mongos mongotop krishnakumar@krishnakumar:~/Documents/mongodb-linux-i686-2.6.7/bin$ cd dump/ admin/ student/we have created student collection and after mongodump command we are able to see dump files inside student directory.
How to take a backup to the custom directory and not in default directory?
--out parameter krishnakumar@krishnakumar:~/Documents/mongodb-linux-i686-2.6.7/bin/dump/student$ ./mongodump --host localhost --port 27017 --collection student --db student --out ~/Documents/MongoDBFiles/Backup for non local databases:
Syntax: mongodump --host <host> --port <port> --username <username> --password <password> --out /Documents/backup/mongodumpRestore dump:
mongodb dump can be restored using mongorestore command
Syntax: mongorestore --port <port_number> <backup path>Ex: krishnakumar@krishnakumar:~/Documents/mongodb-linux-i686-2.6.7/bin$ ./mongorestore --port 27017 dump/ admin/ student/ krishnakumar@krishnakumar:~/Documents/mongodb-linux-i686-2.6.7/bin$ ./mongorestore --port 27017 dump/student/ connected to: 127.0.0.1:27017 2015-01-18T17:53:05.480+0530 dump/student/student.bson 2015-01-18T17:53:05.480+0530 going into namespace [student.student] Restoring to student.student without dropping. Restored data will be inserted without raising errors; check your server log 1 objects found 2015-01-18T17:53:05.492+0530 Creating index: { key: { _id: 1 }, name: "_id_", ns: "student.student" } krishnakumar@krishnakumar:~/Documents/mongodb-linux-i686-2.6.7/bin$
Restored.




No comments :
Post a Comment