Today is: 12 January, 2012
Check todays hot topics

Mass SQL - Perform mysql query across multiple replicated databases

In large environments it becomes difficult to ensure your data is always in sync. I've noticed that the troubleshooting process is often flawed at certain organizations. People tend take a 10 mile wide top down approach to troubleshooting every problem that comes across their desk. In conjunction with a few other tools, this can help you isolate data anomalies.

There are certain dangers to running this script. It goes without saying that this should never be public facing unless you want bots/people hammering your databases with connect attempts.

It's also important to note that this script does not make any attempt to sanitize your data (AGAIN: THIS SCRIPT WILL NOT SANITIZE YOUR INPUT). This is so important that I've decided to include a warning in page itself. If you do any expensive mysql queries you will destroy your databases one at a time. This will likely leave your site non-operable, so use caution.

<?php
 
echo "<p>\n";
echo "<font size=\"2\">\n";
echo "This tool will reach out to every database in it's configuration with your<br />\n";
echo "Login credentials. Use it carefully. It can help you and it can hurt you.<br/>\n";
echo "<font color=\"red\">\n";
echo "!!! For Redundancy, do <b>*NOT*</b> do things like cartesian joins !!!\n";
echo "</font>\n";
echo "</p>\n";
 
$database = array(
    "db1" => "database1.example.com",
    "db2" => "database2.example.com",
    "db3" => "database3.example.com",
    "db4" => "database4.example.com",
    "db5" => "database5.example.com",
    "db6" => "database6.example.com");
 
$dbname = 'production_db' // Change this to the database name to query against.
 
function do_sql($dbhost, $dbuser, $dbpass, $query) {
    $dsn = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
    if($dsn) {
        mysql_select_db($dbname) or die(mysql_error());
        $someshit = mysql_query($query) or die(mysql_error());
        echo "<table style=\"width:75%;\">\n";
        echo "  <tr>\n";
        echo "    <td>$dbhost</td>\n";
        while($row = mysql_fetch_array($someshit, MYSQL_ASSOC)){
            foreach ($row as $col_value) {
                echo "\t<td>$col_value</td>\n";
            }
        }
        echo "  </tr>\n";
        echo "</table>\n";
        mysql_free_result($someshit);
        mysql_close($dsn);
    }
}
 
function query_builder() {
    echo " <div id=\"sql\">\n";
    echo "   <form action=\"index.php\" method=\"post\"><br />\n";
    echo "     Username: <input name=\"sql_user\" type=\"text\" size=\"20\"/>\n";
    echo "     Password: <input name=\"sql_pass\" type=\"password\" size=\"20\"/><br />\n";
    echo "     Query: <br /> \n<textarea cols=\"80\" rows=\"10\" name=\"sql_query\" size=\"500\" maxlength=\"300\"></textarea><br />\n";
    echo "     <input type=\"submit\" value=\"submit\"/><br />\n";
    echo "   </form>\n";
    echo " </div>\n";
}
 
if(empty($_POST)) {
    query_builder();
    } else {
        $sql_fields = array('sql_user', 'sql_pass', 'sql_query');
        $timestamp = date("l, F j, Y, g:i a");
        foreach($sql_fields as $unique) {
            if(!isset($_POST[$unique])) {
                query_builder();
            } else {
                $sql_fields[$unique] = $_POST[$unique];
            }
        }
        $sql_fields['sql_query'] = stripslashes( $sql_fields['sql_query'] );
        foreach ($database as $thisone) {
            do_sql($thisone, $sql_fields['sql_user'], $sql_fields['sql_pass'], $sql_fields['sql_query']);
        }
    }
 
echo "<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><font color=\"white\">.</font>\n";
echo "<!-- Templated -->\n";
?>

AttachmentSize
index.php.txt2.74 KB