2

I have a production server that creates different databases everyday in a MySql server.

i need to write a perl script that would run at the end of the day and do the following.

connect to the mysql server and get the list of all the names of the databases it has. then connect to each of them and check the data that was stored.

the only thing i wasn't able to do with DBI and DBD::mysql was read the database names from the mysql server.

i wasn't able to find any DBI function that would work without connecting to a database first.

any help/example/URL would be very helpful :-)

1
  • In Sybase, there's a standard "master" database where info like that is stored. E.g. for your purpose master..sysdatabases. Not sure about MySql Commented Dec 6, 2010 at 13:56

2 Answers 2

4
use DBI;
use strict;
use warnings;
my $dbh = DBI->connect('dbi:mysql:information_schema', $ENV{'USER'}, $ENV{'PASSWD'}, { 'RaiseError' => 1 } );
my $databases = $dbh->selectcol_arrayref('show databases');

use Data::Dumper;
print Dumper $databases;
__END__
$VAR1 = [
      'information_schema',
      'mysql',
      'your_database_1',
      'your_database_2'
    ];

Only databases authorized to the user you connect with will be shown.

Sign up to request clarification or add additional context in comments.

1 Comment

THANK YOU :-) that worked , i have added it to my code and it works like a charm :-) thank you again.
4

Try using INFORMATION_SCHEMA database - that database name always exists and thus can be hard-coded into DBI.

To be more specific, the equivalent to SHOW DATABASES is:

SELECT SCHEMA_NAME AS `Database`
FROM INFORMATION_SCHEMA.SCHEMATA

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.