MongoDB Tutorial 1: Alternative control

Revision as of 13:04, 1 August 2010 by Ric (talk | contribs) (New page: {{Nav MongoDB Tutorial 1}} '''''MongoDB''''' == Introduction == Hey! What’s this alternative control? It’s a spin-off from testing the main application. I always consider these batch/s...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

 

MongoDB Standalone
UniServer 6-Carbo.

MongoDB

Introduction

Hey! What’s this alternative control? It’s a spin-off from testing the main application. I always consider these batch/scripts worth keeping simply because users may prefer them.

Scenario is to run a batch file that runs a PHP CLI script that in turns runs a specific function (a method).

Requirements

Scripts for alternative control are contained in folder alt_control these scripts reflect functionality of our main program.

We require scripts that perform the following tasks:

  • Start MongoDB
  • Stop MongoDB
  • Run Mongo client
  • Open a command prompt

Four batch files and corresponding PHP scripts are required. These PHP scripts obtain their resources (paths function) from a shared include file located in folder control.

Top

Main include file

During the introduction tutorial we created a file mongo_db_inc.php containing functions required for various application. A sub-set from this file is required for this application.

In folder control create a new file named mongo_db_inc.php from file \mongodb_1\z_temp\mongo_db_inc.php copy the following lines.

$path_array      = explode("\\usr",getcwd());              // Split at folder usr
$mongo_base      = $path_array[0];                         // find drive letter and any sub-folders 
$mongo_base_f    = preg_replace('/\\\/','/', $mongo_base); // Replace \ with /

//=== FOLDERS ===
define("MONGO_BIN",   "$mongo_base_f/usr/local/mongo_tutorial/bin");  // Binaries for MongoDB

//=== FILES ===
define("CONFIG_NO_AUTH_F",   "$mongo_base_f/usr/local/mongo_tutorial/bin/config_no_auth.ini"); 
define("CONFIG_AUTH_F",      "$mongo_base_f/usr/local/mongo_tutorial/bin/config_auth.ini"); 
define("NAME_PWD_F",         "$mongo_base_f/usr/local/mongo_tutorial/bin/mongo_name_password"); 
define("UNISERV_EXE",        "$mongo_base_f/usr/local/mongo_tutorial/bin/uniserv.exe"); // Utility 
define("UNISERV_EXE_B",      $mongo_base.'\usr\local\mongo_tutorial\bin\uniserv.exe');  // Utility 
define("MON_PSKILL_EXE",     "$mongo_base_f/usr/local/mongo_tutorial/bin/pskill.exe");  // Utility 
define("CMD_DUMMY_TXT",      "$mongo_base_f/usr/local/mongo_tutorial/bin/cmd_dummy.txt"); // Temp command fie

It should be obvious all paths are incorrect and need updating added to this are items relating to authentication these need deleting.

First delete authentication related to give:

$path_array      = explode("\\usr",getcwd());              // Split at folder usr
$mongo_base      = $path_array[0];                         // find drive letter and any sub-folders 
$mongo_base_f    = preg_replace('/\\\/','/', $mongo_base); // Replace \ with /

//=== FOLDERS ===
define("MONGO_BIN",   "$mongo_base_f/usr/local/mongo_tutorial/bin");  // Binaries for MongoDB

//=== FILES ===
define("CONFIG_NO_AUTH_F",   "$mongo_base_f/usr/local/mongo_tutorial/bin/config_no_auth.ini"); 
define("UNISERV_EXE",        "$mongo_base_f/usr/local/mongo_tutorial/bin/uniserv.exe"); // Utility 
define("UNISERV_EXE_B",      $mongo_base.'\usr\local\mongo_tutorial\bin\uniserv.exe');  // Utility 
define("MON_PSKILL_EXE",     "$mongo_base_f/usr/local/mongo_tutorial/bin/pskill.exe");  // Utility 

First line splits current working directory at folder usr this requires changing. Choose a folder that is common to folders alt_control and control, in this case use folder mongodb_1

With this split we have a reference $mongo_base that is a path up to folder mongodb_1 hence this folder must be added to the paths being reconstructed.

Top

Paths

After making the changes section looks like this:

$path_array      = explode("\\mongodb_1",getcwd());          // Split at folder usr
$mongo_base      = $path_array[0];                           // find drive letter and any sub-folders 
$mongo_base_f    = preg_replace('/\\\/','/', $mongo_base);   // Replace \ with /

//=== FOLDERS ===
define("MONGO_BIN",   "$mongo_base_f/mongodb_1/bin");        // Binaries for MongoDB

//=== FILES ===
define("CONFIG_NO_AUTH_F",   "$mongo_base_f/mongodb_1/bin/config_no_auth.ini"); 
define("UNISERV_EXE",        "$mongo_base_f/mongodb_1/bin/uniserv.exe"); // Utility 
define("UNISERV_EXE_B",      $mongo_base.'\mongodb_1\bin\uniserv.exe');  // Utility 
define("MON_PSKILL_EXE",     "$mongo_base_f/mongodb_1/bin/pskill.exe");  // Utility 

Top

Command Prompt

From z_temp\mongo_db_inc.php copy function open_cmd_window() to file control\mongo_db_inc.php

In folder alt_control create the following two file with content as shown

Top

Run_cmd.bat

TITLE UNIFORM SERVER - Run Command Prompt
COLOR B0
@echo off
cls

cd ..\php
php.exe -c mongo_tutorial_cli.ini ..\alt_control\run_cmd.php

pause

EXIT

Comments:

  • ..\php Changes working folder. From current folder ..\ move up one folder level and down into folder php
  • php.exe Run PHP
  • -c mongo_tutorial_cli.ini Use this configuration file
  • ..\alt_control\run_cmd.php Move up one folder level and down into folder alt_control and run script run_cmd.php

Note:

After testing you can comment out the pause.

run_cmd.php

<?php

chdir(dirname(__FILE__)); // Change wd to this files location
include_once "../control/mongo_db_inc.php";

open_cmd_window(); // Run command prompt function

?>
  • Change folder to alt_control
  • Include file. Move up one folder level down into folde control and include file mongo_db_inc.php
  • Run function open_cmd_window()

Top

Test

  • Run batch file. Double click on Run_cmd.bat
  • A command window opens displaying the content of folder bin

Above confirms we have a working structure and can add further components.

Top

Support Functions

A user can change server’s working port in the configuration file.

We have a function that determines this

From z_temp\mongo_db_inc.php copy function get_mongo_port_no_auth() to file control\mongo_db_inc.php

We will require several functions hence copy the following:

  • set_mongo_port_no_auth($new_port)
  • start_mongo_no_auth()
  • stop_mongo_no_auth()
  • client_no_auth()
  • mongodb_running()
  • mongo_client_running()

Top

Additional Scripts

Create the following scripts with content as shown

Run_start_mongo.bat


TITLE UNIFORM SERVER - Run Start MongoDB
COLOR B0
@echo off
cls

cd ..\php
php.exe -c mongo_tutorial_cli.ini ..\alt_control\run_start_mongo.php

pause
EXIT

run_start_mongo.php

<?php

chdir(dirname(__FILE__)); // Change wd to this files location
include_once "../control/mongo_db_inc.php";

start_mongo_no_auth(); // Run start mongo function
?>

Run_stop_mongo.bat


TITLE UNIFORM SERVER - Run Stop MongoDB
COLOR B0
@echo off
cls

cd ..\php
php.exe -c mongo_tutorial_cli.ini ..\alt_control\run_stop_mongo.php

pause
EXIT

run_stop_mongo.php

<?php
chdir(dirname(__FILE__)); // Change wd to this files location
include_once "../control/mongo_db_inc.php";

stop_mongo_no_auth() ; // Run command prompt function
?>

Run_client.bat

TITLE UNIFORM SERVER - Run Mongo Client
COLOR B0
@echo off
cls

cd ..\php
php.exe -c mongo_tutorial_cli.ini ..\alt_control\run_client.php

pause
EXIT

run_client.php

<?php
chdir(dirname(__FILE__)); // Change wd to this files location
include_once "../control/mongo_db_inc.php";

client_no_auth() ; // Run command prompt function
?>

Top

Test

  • Start server double click on Run_start_mongo.bat wait a short time
  • Run client double click on Run_client.bat check connection is made. Type exit
  • Stop server double click on Run_stop_mongo.bat wait a short time
  • Run client double click on Run_client.bat check connection fails.

Top

Summary

Above gives us a working server and confirmed functions are working as intended.

Although the batch files are a convenient way to run the server there is a fundamental flaw, lack of user feedback.

To resolve this issue remainder of this tutorial covers a small windows application. This provides user feedback and some extra features.

This MongoDB application provides a good introduction to WinBinder. Next page looks at a basic windows application.

Top