MongoDB Plugin Design: Client
|
MongoDB : Introduction | Download | Basic Components | Start | Stop | Client | Support Functions | Command pipe | Mongo-Start
|
|
| 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.
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:
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 . . . |
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
Test
|
Run test:
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 . . . |
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:
Note:
|
//=== 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 ===
|
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 ===
Test
|
Run Test:
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 |
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 ===
Test
|
Run Test:
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 |
Summary
Above two components (function) complete our main function requirements.
Remainder of this tutorial looks at support functions.