You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
db.copyDatabase('old_name', 'new_name');# Copy the database to a new database
use <old_name># Switch to the database you wish to deletedb.dropDatabase(); # Destroys the selected database
# mongo --host <ipadress>>rs.initiate()
> rs.add("mongo_1_2")
> rs.add("mongo_1_3")
> rs.addArb("mongo_1_A")
>rs.status()
create a registered user to protect the collection agains public access
The following command must be executed on the master of the cluster. Not a slave
# mongo --host <ipadress>> use admin
> db.createUser(
{ user: "adminUser",
pwd: "adminPassword",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
})
Create a keyfile for the inter-cluster authentication
# openssl rand -base64 741 > /opt/mongo/mongodb.key# chmod 600 /opt/mongo/mongodb.key# chown mongod:mongod /opt/mongo/mongodb.key
Then copy this keyfile on all the cluster members.
Edit /etc/mongod.conf and activate security by adding the flowing lines
#security:
security:
authorization: enabled
keyFile: /opt/mongo/mongodb.key
Then restart mongod :# systemctl restart mongod
Log in mongo and add the clusterAdmin right to the user
# mongo --host <ipadress>> use admin
> db.auth("adminUser", "adminPassword" )
> db.grantRolesToUser("adminUser",["clusterAdmin"])
You can verify your user setting with the following command>db.system.users.find()
Now we can create a collection in the cluster and add a user able to access it
> use myNewCollection
> db.createUser({user:"newUser", pwd:"newUserPassword",
roles:[{role:"readWrite", db:"myNewCollection"}] })
You can quit and try to login on the different cluster node
> use myNewCollection
> db.auth("newUser", "newUserPassword" )
The last step is to connect to your application. As an exemple with SpringBoot, if you want to setup a mongoDB cluster you can set the propertie file like this:
spring.data.mongodb.uri=mongodb://newUser:NewUserPassword@<ipadress1>:27017,<ipadress2>:27017,<ipadress3>:27017/newCollection
The hosts executing the springboot application need to have /etc/hosts file knowing the mongodb cluster members:
<ipadress1> mongo_1_1
<ipadress2> mongo_1_2
<ipadress3> mongo_1_3
use local
show collections
You should see an oplog.rs collection.
db.oplog.rs.find().pretty()
special capped collection that keeps a rolling record of all operations that modify the data stored in the databases
idempotent
default oplog size (for Unix and Windows systems):
Storage Engine Default Oplog Size Lower Bound Upper Bound
In-memory 5% of physical memory 50MB 50GB
WiredTiger 5% of free disk space 990MB 50GB
MMAPv1 5% of free disk space 990MB 50GB
mkdir oplogBackup
cd oplogBackup
mongodump -d=local -c=oplog.rs
| field | optional | description || ----- | ------ | ------ || ts | N | BSON Timestamp. Often converted to 64bit INT for JSON || h | Y | Random hash|| v | N | oplog protocol version. default 1. || op | N | Type of op. one of `i`, `u`, `d`, `n`, `c`|| ns | N | BSON Namespacestring. Serialised as db.collection || o | N | Operation applied. object || o2 | Y | Object 2. Additional information, usually operand || fromMigrate | Y | Boolean. Indicates if this operation is part of chunk migration between shards |
source: https://github.com/mongodb/mongo/blob/master/src/mongo/db/repl/oplog_entry.idl
show collections
db.getCollectionNames()
// get the collection statistics
db.<collectionName>.stats()
db.printCollectionStats()
// latency statistics for read, write operations including average time taken for reads, writes
// and related umber of operations performed
db.<collectionName>.latencyStats()
// get collection size for data and indexes
db.<collectionName>.dataSize() // size of the collection
db.<collectionName>.storageSize() // total size of document stored in the collection
db.<collectionName>.totalSize() // total size in bytes for both collection data and indexes
db.<collectionName>.totalIndexSize() // total size of all indexes in the collection
# backup all databases
$ mongodump -v -u username -p strongpassword --port 27018
# backup single database
$ mongodump -v -u username -p strongpassword --port 27018 --db=app
# mongo shell: if you want to restore an existing db and keep the old version around> db.copyDatabase('app', 'app_20200428')
# restore all databases from backup
$ mongorestore -v -u username -p strongpassword --host="rs1/node1:27021,node2:27021,node3:27021" --dir ./restore/
# restore db app from backup with new name app_dev
$ mongorestore -v -u username -p strongpassword --db app_dev --dir dump/app
# restore a collection from backup (e.g. 'sessions' collection)
$ mongorestore -v -u username -p strongpassword --host="rs1/node1:27021,node2:27021,node3:27021" --drop --db=app --collection=sessions ./restore/app/sessions.bson
# mongo shell: check if the old and restored dbs match> use app
>db.stats()
mongodump --db <database> \
-c <collection>