MongoDB Plugin Design: Client

 

UniServer 6-Carbo
MongoDB plugin design.

Starting MongoDB Client

Having started MongoDB in one of two modes (with or without authentication) we want a user who wishes to use the Mongo-client to have the ability to seamlessly connect to it.

We will require two methods catering for each mode. A user can change the server port and admin name and password these need to be included in our two functions.

This page covers two methods connecting from a batch file and a PHP CLI script.

Mongo-client

Mongo's client uses a command-line driven interface. Server running status dictates what parameters we need to pass to the client in oerder to connect to the server.

No-authentication:

mongo.exe  --port 27017 admin

Authentication:

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

I have shown the two command lines these include parameters we are going to use their values have been set to defaults.

Top

Test No-authentication

Create a new batch file Run_client_no_auth.bat with the following content:

:mode con:cols=65 lines=20
TITLE UNIFORM SERVER - Run Mongo Client
COLOR B0
@echo off
cls
cd ..\bin
mongo.exe  --port 27017 admin
pause
EXIT

Test

Run test:

  • Start server double click on Run_mongo_no_auth.bat
  • Run client double click on Run_client_no_auth.bat
  • Enter exit
  • Stop server double click on Run_stop_mongo_no_auth.bat

Confirm connection as shown on right

MongoDB shell version: 1.4.5-pre-
url: admin
connecting to: 127.0.0.1:27017/admin
type "exit" to exit
type "help" for help
> exit
bye
Press any key to continue . . .

Top

Test Authentication

Create a new batch file Run_client_auth.bat with the following content:

:mode con:cols=65 lines=20
TITLE UNIFORM SERVER - Run Mongo Client
COLOR B0
@echo off
cls
cd ..\bin
mongo.exe  --port 27017 --username root --password root admin
pause
EXIT

Top

Test

Run test:

  • Start server double click on Run_mongo_auth.bat
  • Run client double click on Run_client_auth.bat
  • Enter exit
  • Stop server double click on Run_stop_mongo_auth.bat

Confirm connection as shown on right

MongoDB shell version: 1.4.5-pre-
url: admin
connecting to: 127.0.0.1:27017/admin
type "exit" to exit
type "help" for help
> exit
bye
Press any key to continue . . .

Top

Command prompt and client

The above test file opens a command window and runs the Mongo-client our application require we mimic the above using a PHP CLI script (function).

We already have the code in place to open a command prompt reproduced below:

Intro:

  • Function opens a command window.
  • Folder is changed to "bin"

Note:

  • Line $cmd3 Opens a new command-window
  • Once open executes a series of command separated by &&
  • The last one dir is not required hence if replaced with a command line to open Mongo's client will provide us with a solution.
  • We can use ths as a working template function.
//=== Open a command window ===================================================
// Opem a cmd window in bin folder 
function open_cmd_window(){
  $return_wd = getcwd();                    // Save current wd 
  chdir(MONGO_BIN);                         // Change wd to Mongo Bin

       $base = preg_replace('/\//','\\', MONGO_BIN); // Replace / with \
       $cmd1 = "start  ";
       $cmd2 = "\"UNIFORM SERVER Mongo Command Line\" ";
       $cmd3 = "cmd.exe /k \"COLOR B0 && cls && dir\"";
       $cmd  = $cmd1.$cmd2.$cmd3;
       pclose(popen($cmd,'r'));             // Start a new process ignore output  
 
  chdir($return_wd);                        // Restore original wd
}
//=============================================== END Open a command window ===

Top

Client No Authentication

Test script

Modify batch file Run_client_no_auth.bat as shown below

TITLE UNIFORM SERVER - Mongo Client No Auth
COLOR B0
@echo off
cls
cd ..\..\php
php.exe -c mongo_tutorial_cli.ini ..\mongo_tutorial\a_test\client_no_auth.php
pause
EXIT

Create a new test script client_no_auth.php with the following content

//=== Open a client window No Auth ============================================
// Opem a cmd window in bin folder and runs Mongo-client 
function client_no_auth(){
  $port = get_mongo_port_no_auth();         // Get port from config
  $return_wd = getcwd();                    // Save current wd 
  chdir(MONGO_BIN);                         // Change wd to Mongo Bin

  $cmdx = "mongo.exe  --port $port admin";

       $base = preg_replace('/\//','\\', MONGO_BIN); // Replace / with \
       $cmd1 = "start  ";
       $cmd2 = "\"UNIFORM SERVER Mongo Client No Auth\" ";
       $cmd3 = "cmd.exe /k \"COLOR B0 && cls && $cmdx\"";
       $cmd  = $cmd1.$cmd2.$cmd3;
       pclose(popen($cmd,'r'));             // Start a new process ignore output  
 
  chdir($return_wd);                        // Restore original wd
}

//======================================== END Open a client window No Auth ===

Top

Test

Run Test:

  • Run Server (Double click on Run_mongo_no_auth.bat)
  • Run "Run_client_no_auth.bat"
  • Enter exit
  • Run "Run_stop_mongo_no_auth.bat"

With the above confirmed cut and copy function to mongo_db_inc.php

MongoDB shell version: 1.4.5-pre-
url: admin
connecting to: 127.0.0.1:27017/admin
type "exit" to exit
type "help" for help
> exit
bye

Top

Client Authentication

Test script

Modify batch file Run_client_auth.bat as shown below

TITLE UNIFORM SERVER - Mongo Client Auth
COLOR B0
@echo off
cls
cd ..\..\php
php.exe -c mongo_tutorial_cli.ini ..\mongo_tutorial\a_test\client_auth.php
pause
EXIT

Create a new test script client_auth.php with the following content

//=== Open a client window Auth ===============================================
// Opem a cmd window in bin folder and runs Mongo-client 
function client_auth(){
  $port = get_mongo_port_no_auth();    // Get port from config
  $a_pwd_array = get_name_pwd_array(); // Get Admin name and password from file
  $name = $a_pwd_array[0];             // Set name 
  $password =  $a_pwd_array[1];        // Set password

  $return_wd = getcwd();                    // Save current wd 
  chdir(MONGO_BIN);                         // Change wd to Mongo Bin

  $cmdx = "mongo.exe  --port $port --username $name --password $password admin";

       $base = preg_replace('/\//','\\', MONGO_BIN); // Replace / with \
       $cmd1 = "start  ";
       $cmd2 = "\"UNIFORM SERVER Mongo Client Auth\" ";
       $cmd3 = "cmd.exe /k \"COLOR B0 && cls && $cmdx\"";
       $cmd  = $cmd1.$cmd2.$cmd3;
       pclose(popen($cmd,'r'));             // Start a new process ignore output  
 
  chdir($return_wd);                        // Restore original wd
}

//=========================================== END Open a client window Auth ===

Top

Test

Run Test:

  • Run Server (Double click on Run_mongo_auth.bat)
  • Run "Run_client_auth.bat"
  • Enter exit
  • Run "Run_stop_mongo_auth.bat"

With the above confirmed cut and copy function to mongo_db_inc.php

MongoDB shell version: 1.4.5-pre-
url: admin
connecting to: 127.0.0.1:27017/admin
type "exit" to exit
type "help" for help
> exit
bye

Top

Summary

Above two components (function) complete our main function requirements.

Remainder of this tutorial looks at support functions.

Top