Advertisement
flycat

Show MySQL Database size

Nov 8th, 2011
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.46 KB | None | 0 0
  1. SELECT table_schema "Data Base Name",
  2. sum( data_length + index_length ) / 1024 /
  3. 1024 "Data Base Size in MB",
  4. sum( data_free )/ 1024 / 1024 "Free Space in MB"
  5. FROM information_schema.TABLES
  6. GROUP BY table_schema ;
  7.  
  8. <html><head><title>mysql database size</title></head><body>  
  9. <h1>mysql database size</h1>  
  10. <?php  
  11. function file_size_info($filesize) {  
  12.  $bytes = array('KB', 'KB', 'MB', 'GB', 'TB'); # values are always displayed  
  13. if ($filesize < 1024) $filesize = 1; # in at least kilobytes.  
  14. for ($i = 0; $filesize > 1024; $i++) $filesize /= 1024;  
  15.  $file_size_info['size'] = ceil($filesize);  
  16.  $file_size_info['type'] = $bytes[$i];  
  17.  return $file_size_info;  
  18. }  
  19. $db_server = 'put your server here';  
  20. $db_user = 'put your mysql user here';  
  21. $db_pwd = 'put your password here';  
  22. $db_name = 'put your db name here';  
  23. $db_link = @mysql_connect($db_server, $db_user, $db_pwd)  
  24.  or exit('Could not connect: ' . mysql_error());  
  25. $db = @mysql_select_db($db_name, $db_link)  
  26.  or exit('Could not select database: ' . mysql_error());  
  27. // Calculate DB size by adding table size + index size:  
  28. $rows = mysql_query("SHOW TABLE STATUS");  
  29. $dbsize = 0;  
  30. while ($row = mysql_fetch_array($rows)) {  
  31.  $dbsize += $row['Data_length'] + $row['Index_length'];  
  32. }  
  33. print "database size is: $dbsize bytes<br />";  
  34. print 'or<br />';  
  35. $dbsize = file_size_info($dbsize);  
  36. print "database size is: {$dbsize['size']} {$dbsize['type']}";  
  37. ?>  
  38. </body></html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement