HowTo – Check Your MySQL DB Size And Ability To Connect

The following script will allow you to keep track of your MySql Database size.

This is important for those people who have limits on the size of their databases on their leased hosting.

If you are running a WordPress install watching your database size will give you the option to clean out parts of your older files or tell you when you are about to hit your hosts limit and prepare to look for better hosting.

If you areĀ  trying to install a Blog like WordPress or another application that relies on PHP and MySQL to store data and you are getting an error about the connection then this script will tell you quickly if PHP is able to make the connection to your Database or if maybe you need to look at your dbdriver setup in your php.ini file.

Use this script only for testing and remove it from your site when not in use.

Place the following code into a file named mydbsize.php and put it in your website root.

EditĀ  to set your DatabaseName DbUsername and Password.

If you find you can not connect make sure your database permissions are correct.

If you can connect and WordPress is not connecting make sure you are using a version of MySql that is compatible with WordPress.

<html><head><title>MySQL Database Check</title></head>
<div style="width:400px;border:1px solid;padding:10px">
<span style="font-size:24px;font-weight:bold">MySQL Database Check</span> <br/>
<?php # database values
$username = "USERNAME";
$databasename = "DATABASENAME";
$password = "PASSWORD";
$server = "localhost";

<?php function file_size_info($filesize) {$bytes = array(‘KB’, ‘KB’, ‘MB’, ‘GB’, ‘TB’);
if ($filesize < 1024) $filesize = 1;
for ($i = 0; $filesize > 1024; $i++) $filesize /= 1024;
$file_size_info[‘size’] = ceil($filesize);
$file_size_info[‘type’] = $bytes[$i];
return $file_size_info; } $db_server = $server; $db_user = $username ; $db_pwd = $password; $db_name = $databasename;
$db_link = @mysql_connect($db_server, $db_user, $db_pwd)
or exit(‘Could not connect: <br>’ . mysql_error()); $db = @mysql_select_db($db_name, $db_link) or exit(‘Could not select database:<br> ‘ . mysql_error());
$rows = mysql_query("SHOW table STATUS"); $dbsize = 0;
while ($row = mysql_fetch_array($rows)) {$dbsize += $row[‘Data_length’] + $row[‘Index_length’]; }

print "<strong>Connected to Server:</strong> $server<br />";
print "<strong>Database:</strong> $databasename<br />";
print "<strong>UserName:</strong> $username<br />";
print "<strong>Password:</strong> $password<br />";
print "<hr>";
print "<strong>Database Size:</strong> $dbsize bytes<br />";
$dbsize = file_size_info($dbsize); print "<strong>About:</strong> {$dbsize[‘size’]} {$dbsize[‘type’]}";

Remember to remove this script from your webhost after you are done.