https://wiki.uniformserver.com/index.php?title=MongoDB_Plugin_Design:_Client&feed=atom&action=historyMongoDB Plugin Design: Client - Revision history2024-03-29T05:36:15ZRevision history for this page on the wikiMediaWiki 1.41.0https://wiki.uniformserver.com/index.php?title=MongoDB_Plugin_Design:_Client&diff=4312&oldid=prevRic: New page: {{Nav 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-clie...2010-08-01T12:53:38Z<p>New page: {{Nav 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-clie...</p>
<p><b>New page</b></p><div>{{Nav MongoDB Plugin Design}}<br />
'''''Starting MongoDB Client'''''<br />
<br />
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.<br />
<br />
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.<br />
<br />
This page covers two methods connecting from a batch file and a PHP CLI script. <br />
<br />
== Mongo-client ==<br />
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.<br />
<br />
{|<br />
|-<br />
|<br />
'''''No-authentication'':'''<br />
<pre><br />
mongo.exe --port 27017 admin<br />
</pre><br />
|-<br />
|<br />
'''''Authentication'':'''<br />
<pre><br />
mongo.exe --port 27017 --username root --password root admin<br />
</pre><br />
|}<br />
I have shown the two command lines these include parameters we are going to use their values have been set to defaults.<br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
=== Test No-authentication ===<br />
Create a new batch file '''''Run_client_no_auth.bat''''' with the following content: <br />
<pre><br />
:mode con:cols=65 lines=20<br />
TITLE UNIFORM SERVER - Run Mongo Client<br />
COLOR B0<br />
@echo off<br />
cls<br />
cd ..\bin<br />
mongo.exe --port 27017 admin<br />
pause<br />
EXIT<br />
</pre><br />
<br />
==== Test ====<br />
{|<br />
|-valign="top"<br />
|<br />
'''''Run test'':'''<br />
* Start server double click on '''Run_mongo_no_auth.bat'''<br />
* Run client double click on '''''Run_client_no_auth.bat'''''<br />
* Enter exit<br />
* Stop server double click on '''Run_stop_mongo_no_auth.bat''' <br />
Confirm connection as shown on right<br />
|<br />
<pre><br />
MongoDB shell version: 1.4.5-pre-<br />
url: admin<br />
connecting to: 127.0.0.1:27017/admin<br />
type "exit" to exit<br />
type "help" for help<br />
> exit<br />
bye<br />
Press any key to continue . . .<br />
</pre><br />
|}<br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
=== Test Authentication ===<br />
Create a new batch file '''''Run_client_auth.bat''''' with the following content: <br />
<pre><br />
:mode con:cols=65 lines=20<br />
TITLE UNIFORM SERVER - Run Mongo Client<br />
COLOR B0<br />
@echo off<br />
cls<br />
cd ..\bin<br />
mongo.exe --port 27017 --username root --password root admin<br />
pause<br />
EXIT<br />
</pre><br />
'''''[[#top | Top]]'''''<br />
==== Test ====<br />
{|<br />
|-valign="top"<br />
|<br />
'''''Run test'':'''<br />
* Start server double click on '''Run_mongo_auth.bat'''<br />
* Run client double click on '''''Run_client_auth.bat'''''<br />
* Enter exit<br />
* Stop server double click on '''Run_stop_mongo_auth.bat''' <br />
Confirm connection as shown on right<br />
|<br />
<pre><br />
MongoDB shell version: 1.4.5-pre-<br />
url: admin<br />
connecting to: 127.0.0.1:27017/admin<br />
type "exit" to exit<br />
type "help" for help<br />
> exit<br />
bye<br />
Press any key to continue . . .<br />
</pre><br />
|}<br />
'''''[[#top | Top]]'''''<br />
<br />
== Command prompt and client ==<br />
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).<br />
<br />
We already have the code in place to [[MongoDB Plugin Design: Basic Components#Finished test script | '''open a command prompt''']] reproduced below:<br />
{|<br />
|-valign="top"<br />
|<br />
'''''Intro'':'''<br />
<br />
* Function opens a command window.<br />
* Folder is changed to "bin"<br />
<br />
'''''Note'':'''<br />
<br />
* '''Line $cmd3''' Opens a new command-window<br />
* Once open executes a series of command separated by '''&&''' <br />
* 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.<br />
* We can use ths as a working template function. <br />
|<br />
<pre><br />
//=== Open a command window ===================================================<br />
// Opem a cmd window in bin folder <br />
function open_cmd_window(){<br />
$return_wd = getcwd(); // Save current wd <br />
chdir(MONGO_BIN); // Change wd to Mongo Bin<br />
<br />
$base = preg_replace('/\//','\\', MONGO_BIN); // Replace / with \<br />
$cmd1 = "start ";<br />
$cmd2 = "\"UNIFORM SERVER Mongo Command Line\" ";<br />
$cmd3 = "cmd.exe /k \"COLOR B0 && cls && dir\"";<br />
$cmd = $cmd1.$cmd2.$cmd3;<br />
pclose(popen($cmd,'r')); // Start a new process ignore output <br />
<br />
chdir($return_wd); // Restore original wd<br />
}<br />
//=============================================== END Open a command window ===<br />
</pre><br />
|}<br />
'''''[[#top | Top]]'''''<br />
<br />
== Client No Authentication ==<br />
<br />
<br />
=== Test script ===<br />
Modify batch file '''''Run_client_no_auth.bat''''' as shown below <br />
<pre><br />
TITLE UNIFORM SERVER - Mongo Client No Auth<br />
COLOR B0<br />
@echo off<br />
cls<br />
cd ..\..\php<br />
php.exe -c mongo_tutorial_cli.ini ..\mongo_tutorial\a_test\client_no_auth.php<br />
pause<br />
EXIT<br />
</pre><br />
Create a new test script '''''client_no_auth.php''''' with the following content <br />
<pre><br />
//=== Open a client window No Auth ============================================<br />
// Opem a cmd window in bin folder and runs Mongo-client <br />
function client_no_auth(){<br />
$port = get_mongo_port_no_auth(); // Get port from config<br />
$return_wd = getcwd(); // Save current wd <br />
chdir(MONGO_BIN); // Change wd to Mongo Bin<br />
<br />
$cmdx = "mongo.exe --port $port admin";<br />
<br />
$base = preg_replace('/\//','\\', MONGO_BIN); // Replace / with \<br />
$cmd1 = "start ";<br />
$cmd2 = "\"UNIFORM SERVER Mongo Client No Auth\" ";<br />
$cmd3 = "cmd.exe /k \"COLOR B0 && cls && $cmdx\"";<br />
$cmd = $cmd1.$cmd2.$cmd3;<br />
pclose(popen($cmd,'r')); // Start a new process ignore output <br />
<br />
chdir($return_wd); // Restore original wd<br />
}<br />
<br />
//======================================== END Open a client window No Auth ===<br />
</pre> <br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
==== Test ====<br />
{|<br />
|-valign="top"<br />
|<br />
Run Test:<br />
* Run Server (Double click on Run_mongo_no_auth.bat)<br />
* Run "Run_client_no_auth.bat"<br />
* Enter exit<br />
* Run "Run_stop_mongo_no_auth.bat"<br />
With the above confirmed cut and copy function to '''''mongo_db_inc.php'''''<br />
|<br />
<pre><br />
MongoDB shell version: 1.4.5-pre-<br />
url: admin<br />
connecting to: 127.0.0.1:27017/admin<br />
type "exit" to exit<br />
type "help" for help<br />
> exit<br />
bye<br />
</pre><br />
|}<br />
'''''[[#top | Top]]'''''<br />
<br />
== Client Authentication ==<br />
=== Test script ===<br />
Modify batch file '''''Run_client_auth.bat''''' as shown below <br />
<pre><br />
TITLE UNIFORM SERVER - Mongo Client Auth<br />
COLOR B0<br />
@echo off<br />
cls<br />
cd ..\..\php<br />
php.exe -c mongo_tutorial_cli.ini ..\mongo_tutorial\a_test\client_auth.php<br />
pause<br />
EXIT<br />
</pre><br />
Create a new test script '''''client_auth.php''''' with the following content <br />
<pre><br />
//=== Open a client window Auth ===============================================<br />
// Opem a cmd window in bin folder and runs Mongo-client <br />
function client_auth(){<br />
$port = get_mongo_port_no_auth(); // Get port from config<br />
$a_pwd_array = get_name_pwd_array(); // Get Admin name and password from file<br />
$name = $a_pwd_array[0]; // Set name <br />
$password = $a_pwd_array[1]; // Set password<br />
<br />
$return_wd = getcwd(); // Save current wd <br />
chdir(MONGO_BIN); // Change wd to Mongo Bin<br />
<br />
$cmdx = "mongo.exe --port $port --username $name --password $password admin";<br />
<br />
$base = preg_replace('/\//','\\', MONGO_BIN); // Replace / with \<br />
$cmd1 = "start ";<br />
$cmd2 = "\"UNIFORM SERVER Mongo Client Auth\" ";<br />
$cmd3 = "cmd.exe /k \"COLOR B0 && cls && $cmdx\"";<br />
$cmd = $cmd1.$cmd2.$cmd3;<br />
pclose(popen($cmd,'r')); // Start a new process ignore output <br />
<br />
chdir($return_wd); // Restore original wd<br />
}<br />
<br />
//=========================================== END Open a client window Auth ===<br />
</pre><br />
<br />
'''''[[#top | Top]]'''''<br />
==== Test ====<br />
{|<br />
|-valign="top"<br />
|<br />
Run Test:<br />
* Run Server (Double click on Run_mongo_auth.bat)<br />
* Run "Run_client_auth.bat"<br />
* Enter exit<br />
* Run "Run_stop_mongo_auth.bat"<br />
With the above confirmed cut and copy function to '''''mongo_db_inc.php'''''<br />
|<br />
<pre><br />
MongoDB shell version: 1.4.5-pre-<br />
url: admin<br />
connecting to: 127.0.0.1:27017/admin<br />
type "exit" to exit<br />
type "help" for help<br />
> exit<br />
bye<br />
</pre><br />
|}<br />
'''''[[#top | Top]]'''''<br />
<br />
== Summary ==<br />
Above two components (function) complete our main function requirements.<br />
<br />
Remainder of this tutorial looks at [[MongoDB Plugin Design: Support Functions | '''support functions''']].<br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
----<br />
<br />
[[Category: Tutorials]]<br />
[[Category: How To]]<br />
[[Category: Uniform Server 6-Carbo]]</div>Ric