MongoDB Plugin Design: Introduction

Revision as of 08:18, 24 November 2010 by Olajideolaolorun (talk | contribs) (Reverted edits by Upazixorys (Talk); changed back to last version by BobS)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

 

UniServer 6-Carbo
MongoDB plugin design.

MongoDB

Introduction

MongoDB is a server in its own right, so instead of defining it as a plugin, perhaps a stand alone version is the best way to proceed.

The other plugins are in reality separate applications, and when I refer to an application, it is a generic term and could be one of the above. This tutorial’s sole purpose is to create a generic set of components (functions) to implement either of the above.

Control

From a control point of view, we need to start and stop MongoDB in either development or production mode. In other words control architecture must cater to no authentication and authentication respectively.

Uniform Server users will be familiar with using default name and password of root. For consistency, the same defaults will be used for the MongoDB plugin.

MongoDB

Interestingly, MongoDB allows you to start the server with or without authentication. This is configurable using a configuration file setting or via a command line parameter.

For scripting, a configuration file is the preferred method of starting MongoDB server.

To read a file:

Command-line has the following format: mongod.exe -f config.ini

We want to run MongoDB in either one of two modes. This can be accomplished using two command lines and two separate configuration files as follows:

Starting the Server

No-authentication:

mongod.exe -f config1.ini

Authentication:

mongod.exe -f config2.ini

dbpath  = ../data/mongodb  # Path to db 
bind_ip = 127.0.0.1        # Localhost
port    = 27017            # Port to use 
noauth  = true             # no authorization
verbose = true 
dbpath  = ../data/mongodb  # Path to db 
bind_ip = 127.0.0.1        # Localhost
port    = 27017            # Port to use
auth    = true             # authorization required
verbose = true             # to disable, comment out.

With the exception of dbpath and auth all parameters are default values.

Strictly speaking, there is no need to include default parameters. They are included because they may change later. Also they serve as a quick reference.

Note: Parameter dbpath uses a relative path. This inherently makes the server portable.


Mongo-client Access

To access the server using the mongo-client requires slightly different command-line formats, as shown blow.

Note: Database we want to initially connect to is admin

No-authentication:

Authentication:

mongo.exe admin
mongo.exe --username root --password root admin


Stopping the Server

Stopping the server requires access to the Admin database. Again to access server using the mongo-client requires slightly different command-line formats as shown blow.

Note: Server can be closed only from the Admin database admin

No-authentication:

mongo.exe  --eval "db.getSisterDB('admin').shutdownServer()"

Authentication:

mongo.exe  --username root --password root admin --eval "db.shutdownServer()"

I have show two methods for selection the admin database, either by specifying it on the command-line or by getting the help method db.getSisterDB('admin') to select it.

Note:

If the server is running as a visible command line process you can use Ctrl-c to shutdown the server. However, the server will be run with its command window hidden. The only way to shutdown server is via the client.

Top

Connecting via a script

Assumes you have installed the PHP extension (also refereed to as the PHP driver)

No-authentication:

$connection = new Mongo();               // connect
$db = $connection->uniform_server;       // select (create) a database

Authentication:

$connection = new Mongo("mongodb://root:root@localhost:27017");
$db = $connection->admin;        // select (create) a database

Note:

A user must exist in the admin database before attempting to use authentication.


Admin name and password

The admin database is a database containing data for running and administering the entire MongoDB server.

Adding a user name and password is easy. Let's assume a fresh install of Mongo:

  • Start MonoDB server
  • Connect to the server using mongo.exe client
  • Select admin database
  • Add user to this database

Enter following into client:

use admin
db.addUser("name", "password")

Authentication takes immediate effect. Further actions require authenticated access. With the current connection you can use explicit authentication by entering this line:

db.authenticate("name", "password")

Note 1: Uniform Server defaults are name=root password=root

Note 2: Currently there is no way to create a user with restrictive privileges. Hence only assign a single user to the Admin database.

Adding users and assigning databases

This will come as a surprise especially if you are a MySQL admin. Creating a new user and assigning a database is similar to the above.

  • Start MongoDB server
  • Connect to the server using mongo.exe client with admin name and password
  • Select or create new database
  • Add user to this database

For example enter following into client:

use newdatabase
db.addUser("fred", "fred123")

Display existing users for a database

To display existing users for a database

Select the database and type this command:

db.system.users.find()

Changing Passwords

If a user already exists, all that is required is to run addUser to change their password. Hence to change the default Admin requires:

use admin
db.addUser("root", "new_password")


Command-line parameters

To view command-line parameters accepted by command-line tools: from a command prompt, type the application name followed by –help

For example:

mongod.exe --help

Allowed options:

General options:
  -h [ --help ]             show this usage information
  --version                 show version information
  -f [ --config ] arg       configuration file specifying additional options
  -v [ --verbose ]          be more verbose (include multiple times for more
                            verbosity e.g. -vvvvv)
  --quiet                   quieter output
  --port arg                specify port number
  --logpath arg             file to send all output to instead of stdout
  --logappend               append to logpath instead of over-writing
  --bind_ip arg             local ip address to bind listener - all local ips
                            bound by default
  --dbpath arg (=/data/db/) directory for datafiles
  --directoryperdb          each database will be stored in a separate
                            directory
  --repairpath arg          root directory for repair files - defaults to
                            dbpath
  --cpu                     periodically show cpu and iowait utilization
  --noauth                  run without security
  --auth                    run with security
  --objcheck                inspect client data for validity on receipt
  --quota                   enable db quota management
  --quotaFiles arg          number of files allower per db, requires --quota
  --appsrvpath arg          root directory for the babble app server
  --nocursors               diagnostic/debugging option
  --nohints                 ignore query hints
  --nohttpinterface         disable http interface
  --rest                    turn on simple rest api
  --noscripting             disable scripting engine
  --noprealloc              disable data file preallocation
  --smallfiles              use a smaller default file size
  --nssize arg (=16)        .ns file size (in MB) for new databases
  --diaglog arg             0=off 1=W 2=R 3=both 7=W+some reads
  --sysinfo                 print some diagnostic system information
  --upgrade                 upgrade db if needed
  --repair                  run repair on all dbs
  --notablescan             do not allow table scans
  --syncdelay arg (=60)     seconds between disk syncs (0 for never)
  --profile arg             0=off 1=slow, 2=all
  --slowms arg (=100)       value of slow for profile and console log
  --maxConns arg            max number of simultaneous connections
  --install                 install mongodb service
  --remove                  remove mongodb service
  --service                 start mongodb service
 ......

Top

mongo.exe --help

usage: mongo.exe [options] [db address] [file names (ending in .js)]
db address can be:
  foo                   foo database on local machine
  192.169.0.5/foo       foo database on 192.168.0.5 machine
  192.169.0.5:9999/foo  foo database on 192.168.0.5 machine on port 9999
options:
  --shell               run the shell after executing files
  --nodb                don't connect to mongod on startup - no 'db address'
                        arg expected
  --quiet               be less chatty
  --port arg            port to connect to
  --host arg            server to connect to
  --eval arg            evaluate javascript
  -u [ --username ] arg username for authentication
  -p [ --password ] arg password for authentication
  -h [ --help ]         show this usage information
  --version             show version information

file names: a list of files to run.
            files have to end in .js
            and will exit after unless --shell is specified

Summary

Development mode (no authentication) is really a fallback mode only used to gain access to the server should something drastic happen. It can be used for production, but only on a trusted network.

A production server (authentication) in the context of Uniform Server means the server is pre-configured with a default name/password; this must be the first thing a user changes for security purposes.

This admin user will create new users and assign them to databases. More specifically, each application on the server should be assigned a unique database user.

This page serves as a quick reference, providing enough detail to create components (functions) required for our application.

If you wish to follow this tutorial, download and install the tutorial plugin as explained on the next page.

Top