MongoDB current operations and profiling

Current operations

db.currentOp()

Operations running for over 5 seconds…

db.currentOp().inprog.forEach(function(op) {
    if (op.secs_running > 5) printjson(op);
})

Write operations waiting for a lock

db.currentOp(
  {
    "waitingForLock" : true,
    $or: [
      { "op" : { "$in" : [ "insert", "update", "remove" ] } },
      { "query.findandmodify": { $exists: true } }
    ]
  }
)

Operations that have never yielded

db.currentOp(
   {
     "active" : true,
     "numYields" : 0,
     "waitingForLock" : false
   }
)

Index creation operations

db.currentOp(
    {
      $or: [
        { op: "query", "query.createIndexes": { $exists: true } },
        { op: "insert", ns: /\.system\.indexes\b/ }
      ]
    }
)

Profiling Levels

  • 0 – the profiler is off, does not collect any data.
  • 1 – collects profiling data for slow operations only.
  • 2 – collects profiling data for all database operations.

Checking profiling level

db.getProfilingStatus()

Turn on profiling for operations taking more than 1 second (1000 milliseconds)

db.setProfilingLevel(1,1000)

Turn profiling off

db.setProfilingLevel(0)

5 most recent operations logged in the system.profile table

db.system.profile.find().limit(5).sort( { ts : -1 } ).pretty()

Recent operations that took longer than 5 seconds

db.system.profile.find( { millis : { $gt : 5000 } } ).pretty()

Ten recent operations that took over 15 seconds. Print only important fields.

db.system.profile.find({
 millis: {
 $gt: 15000
 }
}, {
 ns: 1,
 query: 1,
 numYield: 1,
 millis: 1
}).limit(10).pretty();

More examples:

db.system.profile.find({
 millis: {
 $gt: 5000
 },
 ts: {
 $gt: new Date(ISODate().getTime() - 1000 * 60 * 10)
 }
}).limit(5).pretty();

db.system.profile.find({
 millis: {
 $gt: 5000
 },
 ts: {
 $gt: new Date(ISODate().getTime() - 1000 * 60 * 10)
 }
}, {
 op: 1,
 ns: 1,
 query: 1,
 numYield: 1,
 millis: 1,
 ts: 1
}).limit(5).pretty();

To change the size of the system.profile collection, you must:

  1. Disable profiling.
  2. Drop the system.profile collection.
  3. Create a new system.profile collection.
  4. Re-enable profiling.

To create a new system.profile collections that’s 4000000 bytes, use the following sequence of operations

db.setProfilingLevel(0)
db.system.profile.drop()
db.createCollection( "system.profile", { capped: true, size:4000000 } )
db.setProfilingLevel(1)
Advertisements

Linux: Renaming multiple files from command line

How to rename multiple files at a shell prompt under Linux? The rename command is quite useful.

Rename command usage:

rename oldname newname *.files

Some examples:

Rename all *.abc file as *.xyz:

$ rename .abc .xyz *.abc

Remove .txt file extension:

$ rename 's/\.txt$//' *.txt

Convert all uppercase filenames to lowercase:

$ rename 'y/A-Z/a-z/' *

Back up and Restore MySQL database from the command line

Backing up via the command line:

Type the following at the prompt with the appropriate USERNAME and DATABASE name:

mysqldump -u USERNAME -p DATABASE > dump.sql

You will be prompted for your database password and then the DATABASE will be dumped to a plain-text file called dump.sql.

Restoring via the command line:

First drop and recreate the database as needed:

Drop the database

mysqladmin -u USERNAME -p drop DATABASE

Recreate the database

mysqladmin -u USERNAME -p create DATABASE

Import the backup data

mysql -u USERNAME -p DATABASE < dump.sql