3 # This is mysql-table-sniper, a program to help remove tables from a MySQL server.
5 # This program is copyright (c) 2007 Baron Schwartz, baron at xaprb dot com.
6 # Feedback and improvements are welcome.
8 # THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
9 # WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
10 # MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
12 # This program is free software; you can redistribute it and/or modify it under
13 # the terms of the GNU General Public License as published by the Free Software
14 # Foundation, version 2; OR the Perl Artistic License. On UNIX and similar
15 # systems, you can issue `man perlgpl' or `man perlartistic' to read these
18 # You should have received a copy of the GNU General Public License along with
19 # this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20 # Place, Suite 330, Boston, MA 02111-1307 USA.
23 use warnings FATAL => 'all';
26 use English qw(-no_match_vars);
28 use List::Util qw(max);
30 our $VERSION = '@VERSION@';
32 $OUTPUT_AUTOFLUSH = 1;
34 # ############################################################################
35 # Get configuration information.
36 # ############################################################################
40 $PROGRAM_NAME helps you remove tables from a MySQL server.
42 If possible, database options are read from your .my.cnf file.
43 For more details, please read the documentation:
51 # ############################################################################
52 # Get ready to do the main work.
53 # ############################################################################
55 my @databases = @{$dbh->selectcol_arrayref('SHOW DATABASES')};
59 foreach my $database ( @databases ) {
61 # Ignore databases as instructed. Also ignore INFORMATION_SCHEMA and skip
62 # databases caused by lost+found directories created in the root of ext3
63 # filesystems; they are not really databases.
65 ( $opts{d} && !exists($opts{d}->{$database}) )
66 || $database =~ m/^(information_schema|lost\+found)$/mi
67 || exists $opts{g}->{$database};
69 my @tables = @{$dbh->selectcol_arrayref('SHOW TABLES FROM `' . $database . '`')};
70 next DATABASE unless @tables;
74 # Get a list of active connections
75 my $processes = $dbh->selectall_hashref("show processlist", 'Id');
77 foreach my $db ( @{ $dbh->selectcol_arrayref('show databases') } ) {
78 my @tables = @{ $dbh->selectcol_arrayref("show tables from $db") };
79 foreach my $tbl ( @tables ) {
81 # We only want tables whose name ends in digits NOT preceded by other
82 # digits (for example, barontest_2006_12_06 should not be dropped).
83 my ( $process ) = $tbl =~ m/\D_(\d+)$/;
87 # If the process doesn't exist anymore, the table isn't in use.
88 if ( !exists($processes->{$process} ) ) {
89 print "Dropping table $db.$tbl\n" if $ENV{RKGDEBUG};
90 $dbh->do("drop table if exists $db.$tbl");
96 foreach my $table ( @tables ) {
97 next TABLE if exists $opts{n}->{$table};
99 my $ddl = ($dbh->selectrow_array("SHOW CREATE TABLE `$database`.`$table`"))[1];
100 next TABLE if $ddl =~ m/^CREATE ALGORITHM/;
106 # ############################################################################
108 # ############################################################################
110 # ############################################################################
112 # ############################################################################
118 mysql-stale-table-sniper - Find and possibly remove stale MySQL tables.
124 =head1 SYSTEM REQUIREMENTS
126 You need the following Perl modules: DBI and DBD::mysql.
130 This program is copyright (c) 2007 Baron Schwartz, baron at xaprb dot com.
131 Feedback and improvements are welcome.
133 THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
134 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
135 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
137 This program is free software; you can redistribute it and/or modify it under
138 the terms of the GNU General Public License as published by the Free Software
139 Foundation, version 2; OR the Perl Artistic License. On UNIX and similar
140 systems, you can issue `man perlgpl' or `man perlartistic' to read these
143 You should have received a copy of the GNU General Public License along with
144 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
145 Place, Suite 330, Boston, MA 02111-1307 USA.
149 Baron Schwartz, baron at xaprb dot com.