https://wiki.uniformserver.com/index.php?title=MongoDB_Plugin_Design:_Start&feed=atom&action=historyMongoDB Plugin Design: Start - Revision history2024-03-29T11:58:17ZRevision history for this page on the wikiMediaWiki 1.41.0https://wiki.uniformserver.com/index.php?title=MongoDB_Plugin_Design:_Start&diff=4310&oldid=prevRic: New page: {{Nav MongoDB Plugin Design}} '''''Starting MongoDB''''' Starting MongoDB is very easy this page covers two methods from a batch file and a PHP CLI script. Our main application requires M...2010-08-01T12:51:41Z<p>New page: {{Nav MongoDB Plugin Design}} '''''Starting MongoDB''''' Starting MongoDB is very easy this page covers two methods from a batch file and a PHP CLI script. Our main application requires M...</p>
<p><b>New page</b></p><div>{{Nav MongoDB Plugin Design}}<br />
'''''Starting MongoDB'''''<br />
<br />
Starting MongoDB is very easy this page covers two methods from a batch file and a PHP CLI script. Our main application requires Mongo be run either with or without authentication and hidden in the background.<br />
<br />
== Manually stopping MongoDB ==<br />
For testing we will manually stop MongoDB using the mongo-client.<br />
Create a new batch file Run_client.bat with the following ontents:<br />
{|<br />
|-<br />
|<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<br />
pause<br />
EXIT<br />
</pre><br />
|<br />
* Changes directory to folder bin <br />
* Run mongo-client mongo.exe<br />
|}<br />
To stop MongoDB type the following into client:<br />
* use admin<br />
* db.shutdownServer()<br />
* exit <br />
'''''[[#top | Top]]'''''<br />
== Starting MongoDB from a batch file ==<br />
To start MongoDB from a batch file<br />
<br />
Create a new batch file Run_mongo_no_auth.bat with the following ontents:<br />
{|<br />
|-<br />
|<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 />
mongod.exe -f config_no_auth.ini<br />
pause<br />
EXIT<br />
</pre><br />
|<br />
* Changes directory to folder bin <br />
* Run mongo-server mongod.exe<br />
* With following configuration file -f config_no_auth.ini<br />
|}<br />
'''''[[#top | Top]]'''''<br />
=== Test ===<br />
Running the above batch files is equivalent to running commands from a command prompt.<br />
{|<br />
|-valign="top"<br />
|<br />
'''''Start MongoDb'''''<br />
<br />
* Double click Run_mongo_no_auth.bat<br />
Result shown on right. For clarity some details have been removed.<br />
<br />
What is important is the last line “listening on port 28017” this confirms server is running.<br />
<br />
|<br />
<pre><br />
Accessing: local for the first time<br />
query local.system.namespaces ntoreturn:0 reslen:36 nreturned:0 15ms<br />
enter repairDatabases<br />
uniform_server<br />
Accessing: uniform_server for the first time<br />
done repairDatabases<br />
db version v1.4.5-pre-, pdfile version 4.5<br />
git version: f07303e44fbfaab5d9eb014aaaec8cb3b64a4c48<br />
waiting for connections on port 27017<br />
web admin interface listening on port 28017<br />
</pre><br />
|-valign="top"<br />
|<br />
'''''Start Mongo-client'''''<br />
<br />
* Double click Run_client.bat<br />
Result shown on right.<br />
<br />
What is important you will see a flashing cursor “>|” this confirms mongo-client has connected to the server and is ready to receive commands. <br />
|<br />
<pre><br />
MongoDB shell version: 1.4.5-pre-<br />
url: test<br />
connecting to: test<br />
type "exit" to exit<br />
type "help" for help<br />
><br />
</pre><br />
|}<br />
We have both server and client running next test cleanly shuts down the sever.<br />
{|<br />
|-valign="top"<br />
|<br />
'''''From client close server'''''<br />
<br />
Enter the following command:<br />
<br />
* use admin<br />
* db.shutdownServer()<br />
* exit<br />
<br />
You will notice several error message these I believe are irrelevant! <br />
<br />
Ignoring the errors what is important is the “bye” message.<br />
<br />
This indicates the command-line client has not fallen over. It received the exit command and terminated with no errors.<br />
<br />
|<br />
<pre><br />
MongoDB shell version: 1.4.5-pre-<br />
url: test<br />
connecting to: test<br />
type "exit" to exit<br />
type "help" for help<br />
> use admin<br />
switched to db admin<br />
> db.shutdownServer()<br />
query failed : admin.$cmd { shutdown: 1.0 } to: 127.0.0.1<br />
server should be down...<br />
trying reconnect to 127.0.0.1<br />
reconnect 127.0.0.1 failed couldn't connect to server 127.0.0.1<br />
127.0.0.1:27017<br />
MessagingPort say send() errno:0 No error 127.0.0.1:27017<br />
JS Error: Error: error doing query: unknown (anon):1284<br />
> exit<br />
bye<br />
Press any key to continue . . .<br />
</pre><br />
|-valign="top"<br />
|<br />
'''''Server Response'''''<br />
<br />
Of real importance is the server’s response to command db.shutdownServer()<br />
<br />
It has received this command and initiated a clean server shutdown. <br />
<br />
Up to line '''''dbexit: really exiting now''''' there are no errors this confirms server was cleanly shutdown.<br />
<br />
<br />
'''''What about those errors!'''''<br />
<br />
I think it’s a cause of overzealous error reporting. Client has sent a shutdown command, which is dully executed by the server and shuts down. The client is still expecting a response from a killed server!<br />
<br />
Probably needs sorting by Mongo’s development team.<br />
<br />
From a users point of view it’s not a issue '''''data integrity is maintained'''''.<br />
|<br />
<pre><br />
flushing mmap took 0ms<br />
flushing mmap took 0ms<br />
flushing mmap took 0ms<br />
<br />
flushing mmap took 0ms<br />
run command admin.$cmd { shutdown: 1.0 }<br />
Accessing: admin for the first time<br />
terminating, shutdown command received<br />
dbexit:<br />
shutdown: going to close listening sockets...<br />
going to close listening socket: 1876<br />
going to close listening socket: 1884<br />
Listener: accept() returns -1 errno:0 No error<br />
shutdown: going to flush oplog...<br />
MiniWebServer: accept() returns -1 errno:0 No error<br />
shutdown: going to close sockets...<br />
shutdown: waiting for fs preallocator...<br />
shutdown: closing all files...<br />
closeAllFiles() finished<br />
dbexit: really exiting now<br />
ERROR: Client::~Client _context should be NULL: conn<br />
Press any key to continue . . .<br />
</pre><br />
|}<br />
'''''[[#top | Top]]'''''<br />
<br />
== Starting MongoDB from a batch file hidden ==<br />
We want to run MongoDB hidden in the background easiest way to to this is to use Uniform Server’s utility uniserv.exe.<br />
<br />
Modify batch file Run_mongo_no_auth.bat as shown below:<br />
{|<br />
|-<br />
|<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 />
start uniserv.exe "mongod.exe -f config_no_auth.ini"<br />
pause<br />
EXIT<br />
</pre><br />
|<br />
* start uniserv.exe - Runs Uniform Server’s utility<br />
* This runs mongod.exe<br />
* Because we are passing parameters to mongod.exe separated by spaces it needs to be enclosed in quotes <br />
|}<br />
'''''[[#top | Top]]'''''<br />
=== Test ===<br />
{|<br />
|-valign="top"<br />
|<br />
Start MongoDB<br />
* Doube click on Run_mongo_no_auth.bat<br />
|<br />
<pre><br />
Nothing to see running in background<br />
</pre><br />
|-valign="top"<br />
|<br />
'''''Start Mongo-client'''''<br />
* Double click Run_client.bat <br />
Enter the following command:<br />
* use admin<br />
* db.shutdownServer()<br />
* exit <br />
<br />
Confirms MongoDB was running and client can be closed.<br />
|<br />
<pre><br />
MongoDB shell version: 1.4.5-pre-<br />
url: test<br />
connecting to: test<br />
type "exit" to exit<br />
type "help" for help<br />
> use admin<br />
switched to db admin<br />
> db.shutdownServer()<br />
query failed : admin.$cmd { shutdown: 1.0 } to: 127.0.0.1<br />
server should be down...<br />
trying reconnect to 127.0.0.1<br />
reconnect 127.0.0.1 failed couldn't connect to server 127.0.0.1<br />
127.0.0.1:27017<br />
MessagingPort say send() errno:0 No error 127.0.0.1:27017<br />
JS Error: Error: error doing query: unknown (anon):1284<br />
> exit<br />
bye<br />
Press any key to continue . . .<br />
</pre><br />
|-valign="top"<br />
|<br />
'''''Restart Mongo-client'''''<br />
* Double click Run_client.bat <br />
<br />
Cannot connect confirms MongoDB was shutdown.<br />
|<br />
<pre><br />
MongoDB shell version: 1.4.5-pre-<br />
url: test<br />
connecting to: test<br />
JS Error: Error: couldn't connect: couldn't connect<br />
to server 127.0.0.1 127.0.0.1:27017 (anon):952<br />
User Exception 12513:connect failed<br />
exception: connect failed<br />
Press any key to continue . . .<br />
</pre><br />
|}<br />
<br />
'''''[[#top | Top]]'''''<br />
== Starting MongoDB from a PHP CLI script ==<br />
Above test has proven we can start MongoDB hidden. We can converts the batch file to a PHP CLI script.<br />
<br />
First step is to add a back slash to all quotes as shown<br />
<pre><br />
start uniserv.exe \"mongod.exe -f config_no_auth.ini\"<br />
</pre><br />
Slit line into smaller manageable parts:<br />
<pre><br />
start uniserv.exe \"<br />
mongod.exe<br />
-f config_no_auth.ini\"<br />
</pre><br />
Our include file contains a constant to config_no_auth.ini <br />
<pre><br />
define("CONFIG_NO_AUTH_F", "$mongo_base_f/usr/local/mongo_tutorial/bin/config_no_auth.ini"); <br />
</pre><br />
We need to define a constant for uniserv.exe as follows:<br />
<pre><br />
define("UNISERV_EXE", "$mongo_base_f/usr/local/mongo_tutorial/bin/uniserv.exe"); <br />
</pre> <br />
Add the above to mongo_db_inc.php<br />
<br />
Add the two constants to above and rebuild the command line string as follows: <br />
<pre><br />
$cmd1 = "start ".UNISERV_EXE." \"";<br />
$cmd2 = "mongod.exe ";<br />
$cmd3 = "-f ".CONFIG_NO_AUTH_F."\"";<br />
$cmd=$cmd1.$cmd2.$cmd3;<br />
</pre><br />
'''''[[#top | Top]]'''''<br />
=== Update batch file ===<br />
Update batch file Run_mongo_no_auth.bat to have the following content<br />
<pre><br />
TITLE UNIFORM SERVER - Run cmd<br />
COLOR B0<br />
@echo off<br />
cls<br />
<br />
cd ..\..\php<br />
php.exe -c mongo_tutorial_cli.ini ..\mongo_tutorial\a_test\mongo_no_auth.php<br />
<br />
pause<br />
<br />
EXIT<br />
</pre><br />
<br />
=== New Script ===<br />
Create a new script '''''mongo_no_auth.php''''' with the following content:<br />
{|-<br />
|<br />
<pre><br />
<?php<br />
chdir(dirname(__FILE__)); // Change wd to this files location<br />
include_once "mongo_db_inc.php";<br />
<br />
start_mongo_no_auth();<br />
<br />
//=== Start Mongo No Auth =====================================================<br />
// Start mongo server. Uses uniserv.exe to detatch hidden process<br />
<br />
function start_mongo_no_auth(){<br />
$return_wd = getcwd(); // Save current wd <br />
chdir(MONGO_BIN); // Change wd to Mongo Bin<br />
<br />
// Build command line<br />
$cmd1 = "start ".UNISERV_EXE." \"";<br />
$cmd2 = "mongod.exe ";<br />
$cmd3 = "-f ".CONFIG_NO_AUTH_F."\"";<br />
$cmd=$cmd1.$cmd2.$cmd3;<br />
//print $cmd; // Test code<br />
<br />
exec($cmd,$dummy,$return); // Run command <br />
chdir($return_wd); // Restore original wd<br />
}<br />
//================================================= END Start Mongo No Auth ===<br />
?><br />
</pre><br />
|<br />
* First operation of the function is to switch working directory to “bin” folder<br />
* The command line is assembled as explained.<br />
* This command is executed as a detached process <br />
* Finally original working directory is restored. <br />
<br />
|}<br />
'''''[[#top | Top]]'''''<br />
==== Testing ====<br />
* Run test script by double clicking on Run_mongo_no_auth.bat<br />
* Run Run_client.bat check server can be stopped<br />
After confirming server can be run and closed copy the function to include file.<br />
<br />
In our test script modify the function to run server with authentication as shown below:<br />
<pre><br />
//=== Start Mongo Auth =======================================================<br />
// Start mongo server. Uses uniserv.exe to detatch hidden process<br />
<br />
function start_mongo_auth(){<br />
$return_wd = getcwd(); // Save current wd <br />
chdir(MONGO_BIN); // Change wd to Mongo Bin<br />
<br />
// Build command line<br />
$cmd1 = "start ".UNISERV_EXE." \"";<br />
$cmd2 = "mongod.exe ";<br />
$cmd3 = "-f ".CONFIG_AUTH_F."\"";<br />
$cmd=$cmd1.$cmd2.$cmd3;<br />
//print $cmd; // Test code<br />
<br />
exec($cmd,$dummy,$return); // Run command <br />
chdir($return_wd); // Restore original wd<br />
}<br />
//==================================================== END Start Mongo Auth ===<br />
</pre><br />
Change line start_mongo_no_auth(); to start_mongo_auth();<br />
<br />
'''''Test'''''<br />
<br />
* Run test script by double clicking on Run_mongo_no_auth.bat<br />
* Run Run_client.bat check server can be stopped<br />
After confirming server can be run and closed copy the function to include file.<br />
<br />
'''''Note'':'''<br />
<br />
We have not set an admin account hence there is nothing to authenticate the client can connect and close the server. <br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
== Final test scripts and batch files ==<br />
Before moving on ensure you have the following four files with content as shown. They are used throughout the remainder of this tutorial.<br />
=== Run_mongo_no_auth.bat ===<br />
<pre><br />
TITLE UNIFORM SERVER - Mongo 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\mongo_no_auth.php<br />
pause<br />
EXIT<br />
</pre><br />
<br />
=== mongo_no_auth.php ===<br />
<pre><br />
<?php<br />
chdir(dirname(__FILE__)); // Change wd to this files location<br />
include_once "mongo_db_inc.php";<br />
start_mongo_no_auth();<br />
?><br />
</pre><br />
=== Run_mongo_auth.bat ===<br />
<pre><br />
TITLE UNIFORM SERVER - Mongo Auth<br />
COLOR B0<br />
@echo off<br />
cls<br />
cd ..\..\php<br />
php.exe -c mongo_tutorial_cli.ini ..\mongo_tutorial\a_test\mongo_auth.php<br />
pause<br />
EXIT<br />
</pre><br />
<br />
=== mongo_auth.php ===<br />
<pre><br />
<?php<br />
chdir(dirname(__FILE__)); // Change wd to this files location<br />
include_once "mongo_db_inc.php";<br />
start_mongo_auth();<br />
?><br />
</pre><br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
<br />
== Summary ==<br />
Our basic components (functions) are slowly increasing we now have support for starting Mongo server with or without authentication.<br />
<br />
For testing we had to stop the server manually [[MongoDB Plugin Design: Stop | '''next page''']] covers stopping the server using a PHP CLI script.<br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
----<br />
<br />
[[Category: Tutorials]]<br />
[[Category: How To]]<br />
[[Category: Uniform Server 6-Carbo]]</div>Ric