Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
mongo_management [2020/12/16 09:35] – [Index] andonovj | mongo_management [2020/12/16 09:42] (current) – [Index] andonovj | ||
---|---|---|---|
Line 9: | Line 9: | ||
===Start=== | ===Start=== | ||
- | <sxh bash> | + | <Code:bash> |
[root@tain-cx-mdb1 scripts]# ./ | [root@tain-cx-mdb1 scripts]# ./ | ||
about to fork child process, waiting until server is ready for connections. | about to fork child process, waiting until server is ready for connections. | ||
forked process: 67867 | forked process: 67867 | ||
child process started successfully, | child process started successfully, | ||
- | </sxh> | + | </Code> |
Where the script contains: | Where the script contains: | ||
**startMongoDB** | **startMongoDB** | ||
- | <sxh bash> | + | <Code:bash> |
#!/bin/bash | #!/bin/bash | ||
sudo /bin/mongod --auth -f / | sudo /bin/mongod --auth -f / | ||
- | </sxh> | + | </Code> |
and the contents of the configuration file is: | and the contents of the configuration file is: | ||
**mongo.conf** | **mongo.conf** | ||
- | <sxh bash> | + | <Code:bash> |
port = 9005 | port = 9005 | ||
logpath = / | logpath = / | ||
Line 36: | Line 36: | ||
smallfiles = true | smallfiles = true | ||
maxConns = 16000 | maxConns = 16000 | ||
- | </sxh> | + | </Code> |
The database can also be shutdown as follows: | The database can also be shutdown as follows: | ||
Line 42: | Line 42: | ||
===Stop=== | ===Stop=== | ||
- | <sxh bash> | + | <Code:bash> |
~/ | ~/ | ||
- | </sxh> | + | </Code> |
or you can do it manually using | or you can do it manually using | ||
- | <sxh bash> | + | <Code:bash> |
mongo admin -u ' | mongo admin -u ' | ||
- | </sxh> | + | </Code> |
Line 58: | Line 58: | ||
- | <sxh bash> | + | <Code:bash> |
prompt = function() { | prompt = function() { | ||
user = db.runCommand({connectionStatus: | user = db.runCommand({connectionStatus: | ||
Line 78: | Line 78: | ||
| | ||
Username IP Address Port Database | Username IP Address Port Database | ||
- | </sxh> | + | </Code> |
Line 90: | Line 90: | ||
Server status will give you some rough overview of how the memory is allocated: | Server status will give you some rough overview of how the memory is allocated: | ||
- | <sxh bash> | + | <Code:bash> |
> db.serverStatus().mem | > db.serverStatus().mem | ||
{ " | { " | ||
Line 119: | Line 119: | ||
Call ReleaseFreeMemory() to release freelist memory to the OS (via madvise()). | Call ReleaseFreeMemory() to release freelist memory to the OS (via madvise()). | ||
Bytes released to the OS take up virtual address space but no physical memory. | Bytes released to the OS take up virtual address space but no physical memory. | ||
- | </sxh> | + | </Code> |
So you can see how much memory your database is storing. | So you can see how much memory your database is storing. | ||
The storage can be checked as follows: | The storage can be checked as follows: | ||
- | <sxh bash> | + | <Code:bash> |
> db.getCollectionNames().map(name => ({totalIndexSize: | > db.getCollectionNames().map(name => ({totalIndexSize: | ||
Line 141: | Line 141: | ||
{ " | { " | ||
{ " | { " | ||
- | </sxh> | + | </Code> |
Of course you can investigate it further with: | Of course you can investigate it further with: | ||
- | <sxh bash> | + | <Code:bash> |
> db.rounds.stats().indexSizes | > db.rounds.stats().indexSizes | ||
{ | { | ||
Line 158: | Line 158: | ||
} | } | ||
> | > | ||
- | </sxh> | + | </Code> |
===Get Storage Size for each Colleciton=== | ===Get Storage Size for each Colleciton=== | ||
Line 191: | Line 191: | ||
- | <sxh bash> | + | <Code:bash> |
> db.files.chunks.stats() | > db.files.chunks.stats() | ||
{ | { | ||
Line 201: | Line 201: | ||
" | " | ||
" | " | ||
- | </sxh> | + | </Code> |
On our example, this collection " | On our example, this collection " | ||
Line 216: | Line 216: | ||
==Linux Shell== | ==Linux Shell== | ||
- | <sxh bash> | + | <Code:bash> |
mongod --repair --repairpath /mnt/vol1 | mongod --repair --repairpath /mnt/vol1 | ||
- | </sxh> | + | </Code> |
==Mongo Shell== | ==Mongo Shell== | ||
- | <sxh bash> | + | <Code:bash> |
db.repairDatabase() | db.repairDatabase() | ||
- | </sxh> | + | </Code> |
or | or | ||
- | <sxh bash> | + | <Code:bash> |
db.runCommand({repairDatabase: | db.runCommand({repairDatabase: | ||
- | </sxh> | + | </Code> |
RepairDatabase needs free space equivalent to the data in your database and an additional 2GB more. It can be run either from the system shell or from within the mongo shell. Depending on the amount of data you have, it may be necessary to assign a sperate volume for this using the --repairpath option. | RepairDatabase needs free space equivalent to the data in your database and an additional 2GB more. It can be run either from the system shell or from within the mongo shell. Depending on the amount of data you have, it may be necessary to assign a sperate volume for this using the --repairpath option. | ||
Line 237: | Line 237: | ||
he compact command works at the collection level, so each collection in your database will have to be compacted one by one. This completely rewrites the data and indexes to remove fragmentation. In addition, if your storage engine is WiredTiger, the compact command will also release unused disk space back to the system. You're out of luck if your storage engine is the older MMAPv1 though; it will still rewrite the collection, but it will not release the unused disk space. Running the compact command places a block on all other operations at the database level, so you have to plan for some downtime. | he compact command works at the collection level, so each collection in your database will have to be compacted one by one. This completely rewrites the data and indexes to remove fragmentation. In addition, if your storage engine is WiredTiger, the compact command will also release unused disk space back to the system. You're out of luck if your storage engine is the older MMAPv1 though; it will still rewrite the collection, but it will not release the unused disk space. Running the compact command places a block on all other operations at the database level, so you have to plan for some downtime. | ||
- | <sxh bash> | + | <Code:bash> |
db.runCommand({compact:' | db.runCommand({compact:' | ||
- | </sxh> | + | </Code> |
===Example=== | ===Example=== | ||
So let's check it in action with WiredTiger: | So let's check it in action with WiredTiger: | ||
- | <sxh bash> | + | <Code:bash> |
> db.files.chunks.storageSize() | > db.files.chunks.storageSize() | ||
394018263040 | 394018263040 | ||
Line 252: | Line 252: | ||
162991509504 | 162991509504 | ||
> | > | ||
- | </sxh> | + | </Code> |
WoW, we saved more than half of the space :) | WoW, we saved more than half of the space :) | ||
Line 269: | Line 269: | ||
The export can be done wherever you can, in our case we will create a new directory for that backup: | The export can be done wherever you can, in our case we will create a new directory for that backup: | ||
- | <sxh bash> | + | <Code:bash> |
[root@localhost mongo]# mkdir -p / | [root@localhost mongo]# mkdir -p / | ||
[root@localhost mongo]# cd /app/data | [root@localhost mongo]# cd /app/data | ||
Line 285: | Line 285: | ||
drwxr-xr-x. 4 root root 33 Sep 10 15:22 . | drwxr-xr-x. 4 root root 33 Sep 10 15:22 . | ||
drwxr-xr-x. 6 root root 62 Sep 10 15:25 backup <- The backup (export) location | drwxr-xr-x. 6 root root 62 Sep 10 15:25 backup <- The backup (export) location | ||
- | </sxh> | + | </Code> |
The export is done very easy: | The export is done very easy: | ||
- | <sxh bash> | + | <Code:bash> |
[root@localhost backup]# mongodump | [root@localhost backup]# mongodump | ||
2019-09-10T15: | 2019-09-10T15: | ||
Line 303: | Line 303: | ||
2019-09-10T15: | 2019-09-10T15: | ||
2019-09-10T15: | 2019-09-10T15: | ||
- | </sxh> | + | </Code> |
===Modify the mongo config file=== | ===Modify the mongo config file=== | ||
We have to modify the config file so it will point to the new folder and with the correct engine: | We have to modify the config file so it will point to the new folder and with the correct engine: | ||
- | <sxh bash> | + | <Code:bash> |
# Where and how to store data. | # Where and how to store data. | ||
storage: | storage: | ||
Line 315: | Line 315: | ||
enabled: true | enabled: true | ||
engine: wiredTiger <- The New engine | engine: wiredTiger <- The New engine | ||
- | </sxh> | + | </Code> |
P.S. Disable the authentication for now, since it will be problem later: | P.S. Disable the authentication for now, since it will be problem later: | ||
- | <sxh bash> | + | <Code:bash> |
security: | security: | ||
authorization: | authorization: | ||
- | </sxh> | + | </Code> |
===Restart the mongod=== | ===Restart the mongod=== | ||
Feel free to restart the mongo however you want :) | Feel free to restart the mongo however you want :) | ||
- | <sxh bash> | + | <Code:bash> |
[root@localhost mongo]# mongo -u adminDBA -p password123 localhost: | [root@localhost mongo]# mongo -u adminDBA -p password123 localhost: | ||
MongoDB shell version v4.0.12 | MongoDB shell version v4.0.12 | ||
Line 381: | Line 381: | ||
> | > | ||
bye | bye | ||
- | </sxh> | + | </Code> |
Line 387: | Line 387: | ||
If the authentication isn't disabled, that will create a problem since in the config file the authentication is enabled, but the user (which is in the admin database in our case) cannot be used. So ensure the authentication is disabled: | If the authentication isn't disabled, that will create a problem since in the config file the authentication is enabled, but the user (which is in the admin database in our case) cannot be used. So ensure the authentication is disabled: | ||
- | <sxh bash> | + | <Code:bash> |
[root@localhost mongo]# mongorestore / | [root@localhost mongo]# mongorestore / | ||
2019-09-10T15: | 2019-09-10T15: | ||
Line 400: | Line 400: | ||
2019-09-10T15: | 2019-09-10T15: | ||
2019-09-10T15: | 2019-09-10T15: | ||
- | </sxh> | + | </Code> |
===Verify the data== | ===Verify the data== | ||
We can verify the data, for some reason mongo was showing me 0000, even though there was data in the databases: | We can verify the data, for some reason mongo was showing me 0000, even though there was data in the databases: | ||
- | <sxh bash> | + | <Code:bash> |
[root@localhost mongo]# mongo | [root@localhost mongo]# mongo | ||
MongoDB shell version v4.0.12 | MongoDB shell version v4.0.12 | ||
Line 464: | Line 464: | ||
{ " | { " | ||
{ " | { " | ||
- | </sxh> | + | </Code> |
Line 477: | Line 477: | ||
====Index==== | ====Index==== | ||
- | Indexes | + | Indexes |
+ | |||
+ | Indexes | ||
+ | |||
+ | The following diagram illustrates a query that selects and orders the matching documents using an index: | ||
+ | |||
+ | {{:mongoindexoverview.jpg? | ||
===Index types=== | ===Index types=== | ||
Line 540: | Line 546: | ||
- | ====Practice==== | + | ====Management==== |
Let's see how to get what indexes a collection has and how to create or re-create and index: | Let's see how to get what indexes a collection has and how to create or re-create and index: | ||
Line 621: | Line 627: | ||
If you have a JSON file " | If you have a JSON file " | ||
- | <sxh bash> | + | <Code:bash> |
[root@tbp-mts-mdb02 ~]# / | [root@tbp-mts-mdb02 ~]# / | ||
MongoDB shell version v4.2.2 | MongoDB shell version v4.2.2 | ||
Line 631: | Line 637: | ||
{ " | { " | ||
{ " | { " | ||
- | </sxh> | + | </Code> |