Test Joomla MySQL Connection

I recently ran into an issue with a Joomla site where users couldn’t login with a connection to database error.  I went poking around Google to find this neat test script.  http://joomla-r-us.com/faq/87-mysql-database-connection-test-script

<?php
///////////////////////////////////////////////////
// Joomla-R-Us
//
// Connection test to external MySQL database
//
///////////////////////////////////////////////////

// Configurations below!!

/////////  Bootstrap the Joomla Framework //////////////

define( ‘_JEXEC’, 1 );

define(‘JPATH_BASE’, dirname(__FILE__) );

define( ‘DS’, DIRECTORY_SEPARATOR );

require_once ( JPATH_BASE .DS.’includes’.DS.’defines.php’ );
require_once ( JPATH_BASE .DS.’includes’.DS.’framework.php’ );

/**
* CREATE THE APPLICATION
*
* NOTE :
*/
$mainframe =& JFactory::getApplication(‘site’);

/**
* INITIALISE THE APPLICATION
*
* NOTE :
*/
// set the language
$mainframe->initialise();

JPluginHelper::importPlugin(‘system’);

// trigger the onAfterInitialise events
$mainframe->triggerEvent(‘onAfterInitialise’);

/////////////////////////////////////////////////////

///////////// Configure this ////////////////////////

$hostname     = ‘myhost.mydomain.com’;
$username     = ‘myaccount’;
$password     = ‘mypassword’;
$database     = ‘joomla’;

///////////// End Configure ////////////////////////

// Try connecting to the database

$option = array ();
$option [‘driver’] = ‘mysql’;

$option [‘host’] = $hostname;
$option [‘user’] = $username;
$option [‘password’] = $password;
$option [‘database’] = $database;
$option [‘prefix’] = ”;
$db = JFactory::getDBO ();
$db = & JDatabase::getInstance ($option);
if ( get_class($db) == ‘JDatabaseMySQL’ ) {
echo ‘<b><font color=”green”>OK!</font></b>’;
} else {
echo ‘<b><font color=”red”>FAILED</font></b> : ‘ . $db->message;
}

?>

Obviously I changed the database name, username and password to match my server.

I installed it under my Joolma root and sure enough a great big green OK appeared.  Now what? I asked the maintainers of the website what had changed and nothing but a Joomla component with it’s own database was disabled.  On further investigation, I found the connection to the additional database was failing.  I granted the user access over the database and Viola!  I was able to login to my Joomla site again!

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s