How to backup and Restore Mongo Db on Linux

Mongo Dump and Restore on Linux

[root@linuxhelp ~]# mongo
MongoDB shell version v4.2.1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("53fabe52-b45d-4762-9163-9aff4fb3a10c") }
MongoDB server version: 4.2.1
Server has startup warnings: 
2019-12-10T00:54:33.662+0530 I  CONTROL  [initandlisten] 
2019-12-10T00:54:33.662+0530 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-12-10T00:54:33.662+0530 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-12-10T00:54:33.662+0530 I  CONTROL  [initandlisten] 
2019-12-10T00:54:33.662+0530 I  CONTROL  [initandlisten] 
2019-12-10T00:54:33.662+0530 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-12-10T00:54:33.662+0530 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-12-10T00:54:33.662+0530 I  CONTROL  [initandlisten] 
2019-12-10T00:54:33.662+0530 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-12-10T00:54:33.662+0530 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-12-10T00:54:33.662+0530 I  CONTROL  [initandlisten] 
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

> show dbs
admin    0.000GB
config   0.000GB
linuxdb  0.000GB
local    0.000GB
test     0.000GB
> use linuxdb
switched to db linuxdb

> show users
{
	"_id" : "linuxdb.linuxuser",
	"userId" : UUID("51fc70ee-a124-4176-90ce-064ae7a3b1d5"),
	"user" : "linuxuser",
	"db" : "linuxdb",
	"roles" : [
		{
			"role" : "readWrite",
			"db" : "linuxdb"
		}
	],
	"mechanisms" : [
		"SCRAM-SHA-1",
		"SCRAM-SHA-256"
	]
}
{
	"_id" : "linuxdb.testuser",
	"userId" : UUID("1db23a44-1135-492b-8ad5-f33691e93d4b"),
	"user" : "testuser",
	"db" : "linuxdb",
	"roles" : [
		{
			"role" : "readWrite",
			"db" : "linuxdb"
		}
	],
	"mechanisms" : [
		"SCRAM-SHA-1",
		"SCRAM-SHA-256"
	]
}

> show collections
product
test
> 
bye

Using the following command can take dump of the database

[root@linuxhelp ~]# mongodump --port 27017 -u "linuxuser" -p "password" --db=linuxdb –out=backup/mongodb10
2019-12-10T01:11:50.404+0530	writing linuxdb.test to 
2019-12-10T01:11:50.404+0530	writing linuxdb.product to 
2019-12-10T01:11:50.407+0530	done dumping linuxdb.test (1 document)
2019-12-10T01:11:50.472+0530	done dumping linuxdb.product (1 document)

Enter into the directory

[root@linuxhelp ~]# cd backup/mongodb10

List out the files

[root@linuxhelp mongodb10]# ls -la
total 0
drwxr-xr-x 3 root root 21 Dec 10 01:11 .
drwxr-xr-x 3 root root 23 Dec 10 01:11 ..
drwxr-xr-x 2 root root 98 Dec 10 01:11 linuxdb

Open the db

[root@linuxhelp mongodb10]# cd linuxdb
 [root@linuxhelp linuxdb]# ll
total 16
-rw-r--r-- 1 root root  51 Dec 10 01:11 product.bson
-rw-r--r-- 1 root root 163 Dec 10 01:11 product.metadata.json
-rw-r--r-- 1 root root  51 Dec 10 01:11 test.bson
-rw-r--r-- 1 root root 160 Dec 10 01:11 test.metadata.json

Restore the database to another database

[root@linuxhelp ~]# mongorestore --port 27017 --db=sampledb backup/mongodb10/
2019-12-10T01:25:52.006+0530	the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead
2019-12-10T01:25:52.006+0530	building a list of collections to restore from backup/mongodb10 dir
2019-12-10T01:25:52.006+0530	don't know what to do with subdirectory "mongodb10/linuxdb", skipping...
2019-12-10T01:25:52.006+0530	0 document(s) restored successfully. 0 document(s) failed to restore.

Enter into the mongo shell by using the credentials

 [root@linuxhelp ~]# mongo -u sampleuser -p --authenticationDatabase sampledb
MongoDB shell version v4.2.1
Enter password: 
connecting to: mongodb://127.0.0.1:27017/?authSource=sampledb&compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("51150f13-b4e4-47e8-95c2-fc4bd7daf482") }
MongoDB server version: 4.2.1
Server has startup warnings: 
2019-12-10T00:54:33.662+0530 I  CONTROL  [initandlisten] 
2019-12-10T00:54:33.662+0530 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-12-10T00:54:33.662+0530 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-12-10T00:54:33.662+0530 I  CONTROL  [initandlisten] 
2019-12-10T00:54:33.662+0530 I  CONTROL  [initandlisten] 
2019-12-10T00:54:33.662+0530 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-12-10T00:54:33.662+0530 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-12-10T00:54:33.662+0530 I  CONTROL  [initandlisten] 
2019-12-10T00:54:33.662+0530 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-12-10T00:54:33.662+0530 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-12-10T00:54:33.662+0530 I  CONTROL  [initandlisten] 
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

> show collections
product
sample
test

This Is the method to take dump and restore database on monogdb

Tag : Mongodb CentOS
Comment
indiandefence
Jun 09 2020
Hi,
my database is huge and its failing with
error "Failed: can't create session: could not connect to server: server selection error: server selection timeout
current topology: Type: Single"

How To increase the timeout for mongodb?
Add a comment
FAQ
Q
What is MongoDB atlas?
A
MongoDB Atlas is a fully-managed cloud database developed by the same people that build MongoDB. Atlas handles all the complexity of deploying, managing, and healing your deployments on the cloud service provider of your choice (AWS, Azure, and GCP). Follow the links below to get started. Deploy your first free cluster
Q
What are MongoDB and use of it?
A
MongoDB is a document-oriented database that stores data in JSON-like documents with the dynamic schema. It means you can store your records without worrying about the data structure such as the number of fields or types of fields to store values. MongoDB documents are similar to JSON objects
Q
Does MongoDB support SQL?
A
Not directly, no. However, MongoDB does support a rich query language of its own. For examples on using MongoDB’s query language, see MongoDB CRUD Operations
Q
How to add a repo for MongoDB on Linuxmint?
A
wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | apt-key add -
Q
Is mongodb .repo aleady exists on centos?
A
No, you need to create and add the configuration