Thursday, August 23, 2018

MongoDB Administration

To check all databases:-

MongoDB Enterprise > show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
MongoDB Enterprise >

Connect to specific DB :-

MongoDB Enterprise > use MongoDB
switched to db MongoDB

To check the db connection:-

MongoDB Enterprise > db
MongoDB
MongoDB Enterprise >


To check the DB Stats :

> db.stats()
{
        "db" : "db2",
        "collections" : 3,
        "objects" : 4,
        "avgObjSize" : 47,
        "dataSize" : 188,
        "storageSize" : 16384,
        "numExtents" : 3,
        "indexes" : 1,
        "indexSize" : 8176,
        "fileSize" : 67108864,
        "nsSizeMB" : 16,
        "dataFileVersion" : {
                "major" : 4,
                "minor" : 5
        },
        "ok" : 1
}
>
>

To create a capped collection:

> db.createCollection("mycoll", {capped: true, size:100000});
{ "ok" : 1 }


To check the collection is capped or not:

> db.mycoll.isCapped()
true
>

To Convert a Collection to Capped Collection

> db.createCollection("doccollect4")
{ "ok" : 1 }


> db.doccollect4.isCapped()
false
>

> db.runCommand({"convertToCapped":"doccollect4",size:200000,max:10000});
{ "ok" : 1 }


To check all collections

> show collections

TEST
doccollect4
mycoll

system.indexes

how to insert a document to the collection :

MongoDB Enterprise > db.doccollect.insert({"title" : "MongoDB doc1", "description" : "MongoDB document 1", "by" : "Vimal", "tags" : [  "mongodb",  "NoSQLDB",  "DocDB",  "database" ], "likes" : 100 });
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > db.doccollect.insert({"title" : "MongoDB doc1", "description" : "MongoDB document 1", "by" : "ashok", "tags" : [  "mongodb",  "NoSQLDB",  "DocDB",  "database" ], "likes" : 100 });
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > db.doccollect.insert({"title" : "MongoDB doc1", "description" : "MongoDB document 1", "by" : "ashok", "tags" : [  "mongodb",  "NoSQLDB",  "DocDB",  "database" ], "likes" : 100 });
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > db.doccollect.insert({"title" : "casandra doc1", "description" : "NoSQLDB", "by" : "Ashok", "tags" : [  "NoSQL",  "DocDB",  "NotOnlySQL" ], "likes" : 100 });
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise >


To find the document from the doccollect :-

MongoDB Enterprise > db.doccollect.find();
{ "_id" : ObjectId("5b7e43714b924fa09c96849d"), "title" : "MongoDB doc1", "description" : "Mongodb Book", "by" : "Ashok", "tags" : [ "NoSQL" ], "likes" : 100 }
{ "_id" : ObjectId("5b7e44764b924fa09c96849e"), "title" : "MongoDB doc1", "description" : "MongoDB document 1", "by" : "Vimal", "tags" : [ "mongodb", "NoSQLDB", "DocDB", "database" ], "likes" : 100 }
{ "_id" : ObjectId("5b7e44aa4b924fa09c96849f"), "title" : "MongoDB doc1", "description" : "MongoDB document 1", "by" : "ashok", "tags" : [ "mongodb", "NoSQLDB", "DocDB", "database" ], "likes" : 100 }
{ "_id" : ObjectId("5b7e44aa4b924fa09c9684a0"), "title" : "MongoDB doc1", "description" : "MongoDB document 1", "by" : "ashok", "tags" : [ "mongodb", "NoSQLDB", "DocDB", "database" ], "likes" : 100 }
{ "_id" : ObjectId("5b7e44aa4b924fa09c9684a1"), "title" : "casandra doc1", "description" : "NoSQLDB", "by" : "Ashok", "tags" : [ "NoSQL", "DocDB", "NotOnlySQL" ], "likes" : 100 }
MongoDB Enterprise >

To View the document in a formatted way by using pretty() command:

MongoDB Enterprise > db.doccollect.find().pretty();
{
        "_id" : ObjectId("5b7e43714b924fa09c96849d"),
        "title" : "MongoDB doc1",
        "description" : "Mongodb Book",
        "by" : "Ashok",
        "tags" : [
                "NoSQL"
        ],
        "likes" : 100
}
{
        "_id" : ObjectId("5b7e44764b924fa09c96849e"),
        "title" : "MongoDB doc1",
        "description" : "MongoDB document 1",
        "by" : "Vimal",
        "tags" : [
                "mongodb",
                "NoSQLDB",
                "DocDB",
                "database"
        ],
        "likes" : 100
}
{
        "_id" : ObjectId("5b7e44aa4b924fa09c96849f"),
        "title" : "MongoDB doc1",
        "description" : "MongoDB document 1",
        "by" : "ashok",
        "tags" : [
                "mongodb",
                "NoSQLDB",
                "DocDB",
                "database"
        ],
        "likes" : 100
}
{
        "_id" : ObjectId("5b7e44aa4b924fa09c9684a0"),
        "title" : "MongoDB doc1",
        "description" : "MongoDB document 1",
        "by" : "ashok",
        "tags" : [
                "mongodb",
                "NoSQLDB",
                "DocDB",
                "database"
        ],
        "likes" : 100
}
{
        "_id" : ObjectId("5b7e44aa4b924fa09c9684a1"),
        "title" : "casandra doc1",
        "description" : "NoSQLDB",
        "by" : "Ashok",
        "tags" : [
                "NoSQL",
                "DocDB",
                "NotOnlySQL"
        ],
        "likes" : 100
}
MongoDB Enterprise >



TO SHUTDOWN MONGODB server (mongod core processes) ###############################################################################

> db.shutdownServer();
shutdown command only works with the admin database; try 'use admin'
> 
> 

> use admin
switched to db admin
> 
> 
> db.shutdownServer();
2018-07-17T10:42:33.547+0700 DBClientCursor::init call() failed
server should be down...
2018-07-17T10:42:33.550+0700 trying reconnect to 127.0.0.1:27017
2018-07-17T10:42:33.550+0700 reconnect 127.0.0.1:27017 failed couldn't connect to server 127.0.0.1:27017
> 
> 
> exit
bye

[dsmongousr@localhost db]$ mongo
MongoDB shell version: 2.5.2
connecting to: test
2018-07-17T10:42:58.802+0700 Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:145
exception: connect failed
[dsmongousr@localhost db]$

-- ##############################################################################################################################################################

-TO START MONGODB server (mongod core processes)  ###############################################################################

[dsmongousr@localhost ~]$ whoami
dsmongousr
[dsmongousr@localhost ~]$ . .bash_profile
[dsmongousr@localhost ~]$
[dsmongousr@localhost ~]$ cd $MONGO_HOME
[dsmongousr@localhost ~]$ cat start.sh
nohup mongod --dbpath /data/db --fork --logpath /data/log/mongo.log
[dsmongousr@localhost ~]$ nohup mongod --dbpath /data/db --fork --logpath /data/log/mongo.log
nohup: appending output to `nohup.out''
[dsmongousr@localhost ~]$ tail -200f nohup.out
about to fork child process, waiting until server is ready for connections.
forked process: 2848
child process started successfully, parent exiting
about to fork child process, waiting until server is ready for connections.
forked process: 2863
ERROR: child process failed, exited with error number 100
about to fork child process, waiting until server is ready for connections.
forked process: 3001
child process started successfully, parent exiting

[dsmongousr@localhost ~]$ mongo
MongoDB shell version: 2.5.2
connecting to: test
Server has startup warnings:
2018-07-17T10:44:55.089+0700 [initandlisten]
2018-07-17T10:44:55.089+0700 [initandlisten] ** NOTE: This is a development version (2.5.2) of MongoDB.
2018-07-17T10:44:55.089+0700 [initandlisten] **       Not recommended for production.
2018-07-17T10:44:55.089+0700 [initandlisten]
> 
> show dbs
db2      0.078GB
local    0.078GB
ashokdb  0.078GB
> 

> use ashokdb
switched to db ashokdb
> show collections
doccollect
system.indexes
users10
> 

--##############################################################################################################################

How to connect to MongoDB Server

How to connect to MongoDB:


run .bash_profile file from mongodb user account then run mongod command to initialize the mongodb service 

[mdashok@mongodb4 bin]$ mongod
2018-08-23T09:56:30.370+0530 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2018-08-23T09:56:30.579+0530 I CONTROL  [initandlisten] MongoDB starting : pid=3843 port=27017 dbpath=/data/db 64-bit host=mongodb4
2018-08-23T09:56:30.579+0530 I CONTROL  [initandlisten] db version v4.0.0
2018-08-23T09:56:30.579+0530 I CONTROL  [initandlisten] git version: 3b07af3d4f471ae89e8186d33bbb1d5259597d51
2018-08-23T09:56:30.579+0530 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
2018-08-23T09:56:30.579+0530 I CONTROL  [initandlisten] allocator: tcmalloc
2018-08-23T09:56:30.579+0530 I CONTROL  [initandlisten] modules: enterprise
2018-08-23T09:56:30.579+0530 I CONTROL  [initandlisten] build environment:
2018-08-23T09:56:30.579+0530 I CONTROL  [initandlisten]     distmod: rhel62
2018-08-23T09:56:30.579+0530 I CONTROL  [initandlisten]     distarch: x86_64
2018-08-23T09:56:30.579+0530 I CONTROL  [initandlisten]     target_arch: x86_64
2018-08-23T09:56:30.579+0530 I CONTROL  [initandlisten] options: {}
2018-08-23T09:56:30.613+0530 W STORAGE  [initandlisten] Detected unclean shutdown - /data/db/mongod.lock is not empty.
2018-08-23T09:56:30.671+0530 I STORAGE  [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2018-08-23T09:56:30.671+0530 W STORAGE  [initandlisten] Recovering data from the last clean checkpoint.
2018-08-23T09:56:30.671+0530 I STORAGE  [initandlisten]
2018-08-23T09:56:30.671+0530 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2018-08-23T09:56:30.671+0530 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2018-08-23T09:56:30.672+0530 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=1355M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
2018-08-23T09:56:31.659+0530 I STORAGE  [initandlisten] WiredTiger message [1534998391:659516][3843:0x7f4a80b14a80], txn-recover: Main recovery loop: starting at 2/5504
2018-08-23T09:56:31.659+0530 I STORAGE  [initandlisten] WiredTiger message [1534998391:659801][3843:0x7f4a80b14a80], txn-recover: Recovering log 2 through 3
2018-08-23T09:56:31.711+0530 I STORAGE  [initandlisten] WiredTiger message [1534998391:711621][3843:0x7f4a80b14a80], txn-recover: Recovering log 3 through 3
2018-08-23T09:56:31.753+0530 I STORAGE  [initandlisten] WiredTiger message [1534998391:753249][3843:0x7f4a80b14a80], txn-recover: Set global recovery timestamp: 0
2018-08-23T09:56:31.818+0530 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
2018-08-23T09:56:31.865+0530 I CONTROL  [initandlisten]
2018-08-23T09:56:31.865+0530 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-08-23T09:56:31.865+0530 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-08-23T09:56:31.865+0530 I CONTROL  [initandlisten]
2018-08-23T09:56:31.865+0530 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2018-08-23T09:56:31.865+0530 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server.
2018-08-23T09:56:31.866+0530 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP
2018-08-23T09:56:31.866+0530 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2018-08-23T09:56:31.866+0530 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2018-08-23T09:56:31.866+0530 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2018-08-23T09:56:31.866+0530 I CONTROL  [initandlisten]
2018-08-23T09:56:31.867+0530 I CONTROL  [initandlisten]
2018-08-23T09:56:31.867+0530 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-08-23T09:56:31.867+0530 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-08-23T09:56:31.867+0530 I CONTROL  [initandlisten]
2018-08-23T09:56:31.867+0530 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-08-23T09:56:31.867+0530 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-08-23T09:56:31.867+0530 I CONTROL  [initandlisten]
2018-08-23T09:56:32.047+0530 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2018-08-23T09:56:32.061+0530 I NETWORK  [initandlisten] waiting for connections on port 27017


Open new session then run "mongo" command :-

[mdashok@mongodb4 ~]$ . .bash_profile
[mdashok@mongodb4 ~]$ mongo
MongoDB shell version v4.0.0
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.0
Server has startup warnings:
2018-08-23T09:56:30.671+0530 I STORAGE  [initandlisten]
2018-08-23T09:56:30.671+0530 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2018-08-23T09:56:30.671+0530 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2018-08-23T09:56:31.865+0530 I CONTROL  [initandlisten]
2018-08-23T09:56:31.865+0530 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-08-23T09:56:31.865+0530 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-08-23T09:56:31.865+0530 I CONTROL  [initandlisten]
2018-08-23T09:56:31.865+0530 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2018-08-23T09:56:31.865+0530 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server.
2018-08-23T09:56:31.866+0530 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP
2018-08-23T09:56:31.866+0530 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2018-08-23T09:56:31.866+0530 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2018-08-23T09:56:31.866+0530 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2018-08-23T09:56:31.866+0530 I CONTROL  [initandlisten]
2018-08-23T09:56:31.867+0530 I CONTROL  [initandlisten]
2018-08-23T09:56:31.867+0530 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-08-23T09:56:31.867+0530 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-08-23T09:56:31.867+0530 I CONTROL  [initandlisten]
2018-08-23T09:56:31.867+0530 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-08-23T09:56:31.867+0530 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-08-23T09:56:31.867+0530 I CONTROL  [initandlisten]
MongoDB Enterprise > show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

Step by Step YugabyteDB 2.11 (Open Source) Distributed DB - Multi-node Cluster Setup on RHEL

Scope - ·        Infrastructure planning and requirement for installation of the multi-node cluster database ·        Prerequisites Software...