PCCS MySQLDatabase Admin Tool version 1.3.4


/ -> mysqldb_optimize.php

1  <?php
2 
3      /**
4      MySQL Database Optimizer 1.0.0: Optimizes all tables of a given
5                                      MySQL database.
6      Copyright (C) 2000 Jeremy Brand
7                          email: jeremy
8                          web: http://www.nirvani.net/jeremy/
9 
10      This program is free software; you can redistribute it and/or modify
11      it under the terms of the GNU General Public License as published by
12      the Free Software Foundation; either version 2 of the License, or
13      (at your option) any later version.
14 
15      This program is distributed in the hope that it will be useful,
16      but WITHOUT ANY WARRANTY; without even the implied warranty of
17      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18      GNU General Public License for more details.
19 
20      You should have received a copy of the GNU General Public License
21      along with this program; if not, write to the Free Software
22      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA USA
23      **/
24      /**
25      Modified by Chauncey Thorn.
26      **/
27 
28      /**
29       ** THIS SCRIPT PERFORMS TABLE LOCKING AND SAFELY OPTIMIZE ALL TABLES
30       ** WITHIN THE GIVEN DATABASE.
31       **/
32 
33      /**
34       ** THIS IS TO BE RAN ON THE COMMAND LINE.
35       ** THE FIRST ARGUMENT TO THE SCRIPT IS THE DATABASE NAME
36       ** OF WHICH YOU WANT TO OPTIMIZE.
37       **
38       ** EXAMPLE:
39       ** shell> php -q this_script.php my_database_name
40       **/
41 
42      /** CONFIG CHANGE HERE **/
43      /*************************************** *********** **/
44      /** IP or hostname of MySQL server **/ // $db_host = '127.0.0.1';
45      /** MySQL user name **/ // $db_user = 'mysql';
46      /** MySQL password **/ // $db_pass = 'mysql_password';
47      /** Program start delay in seconds **/ $start_delay = 30;
48      
49 
50      /** PROGRAM STARTS HERE **/
51      /*******************************/
52     
53     // Added by CT
54      
55  // updated 10-23-2000
56  // -- moving to a single file for global include files
57    
58  require('mysqldb_app_includes.php');
59 
60     common_header($str_mainTitle);
61 
62  if($action == "optimizedb"):
63 
64      set_time_limit(0);
65 
66      function format_time($seconds)
67      {
68        $hour = $seconds / 3600;
69        $total_time = $seconds - ($hour*3600);
70        $min = $seconds / 60;
71        $sec = $seconds % 60;
72        $format = sprintf("%02d",$hour).":".sprintf("%02d",$min).":".sprintf("%02d",$sec);
73        return $format;
74      }
75 
76      // mysql_connect($db_host, $db_user, $db_pass) or die(mysql_error() . "\n\n");
77      mysql_select_db($dbname) or die(mysql_error() . "\n\n");
78 
79      print "\n";
80      print "WARNING: YOUR DATABASE WILL BE UNACCESSIBLE DURING THE OPTIMIZE.<P>\n";
81      print "STARTING OPTIMIZE ON DATABASE '" . $dbname . "' in $start_delay SECONDS.<BR>\n"; flush();
82      print "CTRL-C TO ABORT.<BR>\n\n"; flush();
83 
84      for ($i=0; $i<$start_delay; $i++)
85      {
86        print "."; flush();
87        sleep(1);
88      }
89      print "\n"; flush();
90 
91      $q = "SHOW TABLES";
92      $r = mysql_query($q);
93 
94      $q = "LOCK TABLES";
95 
96      while($row = mysql_fetch_row($r))
97      {
98        $table[] = $row[0];
99        $q .= " " . $row[0]." WRITE,";
100      }
101      $q = substr($q,0,strlen($q)-1);
102      mysql_query($q);
103 
104 
105      print "THE DATABASE '".$dbname."' IS LOCKED FOR READ/WRITE<BR>.\n\n";
106 
107      $t1 = time();
108      while(list($key, $val) = each($table))
109      {
110        $b1 = time();
111        $q = "OPTIMIZE TABLE $val";
112        print $q . "<BR>"; flush();
113        mysql_query($q) or die("QUERY: \"$q\" " . mysql_error() . "\n\n");
114        $b2 = time();
115        $table_time = $b2 - $b1;
116        print "\t\t(TIME ELAPSED: " . format_time($table_time). ")\n"; flush();
117      }
118      $q = "UNLOCK TABLES";
119      mysql_query($q);
120      print "\n";
121      print "<P>THE DATABASE '".$dbname."' IS NOW UNLOCKED.<BR>\n\n";
122     
123      $t2 = time();
124      $total_time = $t2 - $t1;
125 
126      print "TOTAL TIME ELAPSED: " . format_time($total_time) . "<BR>\n\n"; flush();
127      print "<A HREF=mysqldb_admin.php>BACK to Databases</A>";
128      log_this("DATABASE ACTION: $dbname OPTIMIZED","dblog.txt");
129 
130      exit();
131 
132  endif;
133 
134  ?>


Generated: Sat Jan 27 15:40:36 2001 Generated by PHPXref 0.1.2
PCCS-Linux.COM ::ource Advocate Articles catalogue
2000 2002