MongoDB Plugin Design: Stop
MongoDB : Introduction | Download | Basic Components | Start | Stop | Client | Support Functions | Command pipe | Mongo-Start
|
|
UniServer 6-Carbo MongoDB plugin design. |
Stopping MongoDB
Previous page covered starting MongoDB either with or without authentication this page covers stopping MongoDB.
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.
Mongo-client
Stopping the server requires access to the Admin database. Mongo's client provides all the functionality and flexibility we require.
Server running mode dictates the command-line format to use in order to shutdown the server:
Note: Server can be closed only from the Admin database admin
No-authentication: mongo.exe --port arg --host arg [db address] --eval "db.shutdownServer()" |
Authentication: mongo.exe --port arg --host arg --username root --password root [db address] --eval "db.shutdownServer()" |
Generally defaults are acceptable however host should be explicitly defined W7 issues. User can change port hence is included likewise name and password. Default db appears to be test hence [db address] will be assigned admin.
Note: If you have been following this tutorial you will by now understand the above two lines will have rattled my cage. Like what are the values, lines are to long, how do you test and prove they work.
To clear one point “--host” is not required it is defined in the configuration file. This parameter is assumed not to change.
However if you have allowed a user to change a parameter test it.
No-authentication
To confirm our proposed method of stopping MongoDB works and correct parameters are being passed a small test batch file is required.
Initially we will test with defaults parameters this gives a command line as shown:
mongo.exe --port 27017 admin --eval "db.shutdownServer()"
Test Batch file
Create a new batch file Run_stop_mongo_no_auth.bat with the following content:
:mode con:cols=65 lines=20 TITLE UNIFORM SERVER - Mongo No Auth COLOR B0 @echo off cls cd ..\bin mongo.exe --port 27017 admin --eval "db.shutdownServer()" pause EXIT
Test
I have explained the sea of errors ignore these. Result of first stop: "connecting to" - Confirms connection was made "server should be down..." - Confirms command executed and server shutdown
These two lines confirm a connection cannot be made and server was shutdown by the first stop. "User Exception 12513:connect failed" "exception: connect failed" |
MongoDB shell version: 1.4.5-pre- url: admin connecting to: 127.0.0.1:27017/admin query failed : admin.$cmd { shutdown: 1.0 } to: 127.0.0.1:27017 server should be down... Press any key to continue . . . MongoDB shell version: 1.4.5-pre- url: admin connecting to: 127.0.0.1:27017/admin JS Error: Error: couldn't connect: couldn't connect to server 127.0.0.1:27017 127.0.0.1:27017 (anon) :954 User Exception 12513:connect failed exception: connect failed Press any key to continue . . . |
Test script
Modify batch file Run_stop_mongo_no_auth.bat as shown below
TITLE UNIFORM SERVER - Mongo No Auth COLOR B0 @echo off cls cd ..\..\php php.exe -c mongo_tutorial_cli.ini ..\mongo_tutorial\a_test\mongo_no_auth.php pause EXIT
Create a new test script stop_mongo_no_auth.php with the following content
<?php chdir(dirname(__FILE__)); // Change wd to this files location include_once "mongo_db_inc.php"; stop_mongo_no_auth(); //=== Stopt Mongo No Auth ===================================================== // Stop mongo server. Uses uniserv.exe to detatch hidden process function stop_mongo_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 // Build command line $cmd1 = "start ".UNISERV_EXE_B." \""; $cmd2 = "mongo.exe "; $cmd3 = "--port $port "; $cmd4 = 'admin --eval \"db.shutdownServer()\"'; $cmd5 = "\""; $cmd=$cmd1.$cmd2.$cmd3.$cmd4.$cmd5; //print $cmd; // Test code exec($cmd,$dummy,$return); // Run command chdir($return_wd); // Restore original wd } //================================================== END Stop Mongo No Auth === ?> |
Note 1: We obtain port directly from configuration file Test:
Confirmed: With the above confirmed cut and copy function to mongo_db_inc.php |
Authentication
To confirm our proposed method of stopping MongoDB works and correct parameters are being passed a small test batch file is required.
Initially we will test with defaults parameters this gives a command line as shown:
mongo.exe --port 27017 --username root --password root admin --eval "db.shutdownServer()"
Test batch file
Create a new batch file Run_stop_mongo_auth.bat with the following content:
:mode con:cols=65 lines=20 TITLE UNIFORM SERVER - Mongo No Auth COLOR B0 @echo off cls cd ..\bin mongo.exe --port 27017 --username root --password root admin --eval "db.shutdownServer()" pause EXIT
Create Admin user
Create Amin User
|
MongoDB shell version: 1.4.5-pre- url: test connecting to: test type "exit" to exit type "help" for help > use admin switched to db admin > db.addUser("root", "root") { "user" : "root", "readOnly" : false, "pwd" : "2a8025f0885adad5a8ce0044070032b3" } > exit bye Press any key to continue . . . |
Test
Quick check
- Run "Run_stop_mongo_auth.bat"
- Open Windows Task Manger - Check mongod.exe is no longer listed
Complete check
- Run Server (Double click on Run_mongo_auth.bat)
- Open Windows Task Manger - Check mongod.exe is listed
- Run "Run_stop_mongo_no_auth.bat"
- Open Windows Task Manger - Check mongod.exe is listed
- Run "Run_stop_mongo_auth.bat"
- Open Windows Task Manger - Check mongod.exe is no longer listed
Above test confirms only a authenticated user in Admin is allowed to shutdown server.
Test script
Modify batch file Run_stop_mongo_auth.bat as shown below
:mode con:cols=65 lines=20 TITLE UNIFORM SERVER - Mongo Auth COLOR B0 @echo off cls cd ..\..\php php.exe -c php-cli.ini ..\mongo\a_test\stop_mongo_auth.php pause EXIT
Create a new test script stop_mongo_auth.php with the following content
<?php chdir(dirname(__FILE__)); // Change wd to this files location include_once "mongo_db_inc.php"; stop_mongo_auth(); //=== Stop Mongo Auth ======================================================== // Stop mongo server. Uses uniserv.exe to detatch hidden process function stop_mongo_auth(){ $port = get_mongo_port_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 // Build command line $cmd1 = "start ".UNISERV_EXE_B." \""; $cmd2 = "mongo.exe "; $cmd3 = "--port $port "; $cmd4 = "--username $name "; $cmd5 = "--password $password "; $cmd6 = 'admin --eval \"db.shutdownServer()\"'; $cmd7 = "\""; $cmd=$cmd1.$cmd2.$cmd3.$cmd4.$cmd5.$cmd6.$cmd7; //print $cmd; // Test code exec($cmd,$dummy,$return); // Run command chdir($return_wd); // Restore original wd } //===================================================== END Stop Mongo Auth === ?>
Test
Repeat these tests:
- Run Server (Double click on Run_mongo_auth.bat)
- Open Windows Task Manger - Check mongod.exe is listed
- Run "Run_stop_mongo_no_auth.bat"
- Open Windows Task Manger - Check mongod.exe is listed
- Run "Run_stop_mongo_auth.bat"
- Open Windows Task Manger - Check mongod.exe is no longer listed
With the above confirmed cut and copy function to mongo_db_inc.php
Summary
Our basic components (functions) now contain support for stopping Mongo server with or without authentication.
To set a name and password we ran Mongo’s client using batch file Run_client.bat this effectively runs the client directly.
Our main application requires client to be run in either of the two modes using PHP CLI functions covered on next page.