Your Mother alerted me to a
neat trick for keeping MySQL connection parameters in a safe place by using the
mysql_read_default_file connection option.
A couple of points to note:
-
You can omit the name of the database from the data source name (dsn) but you need the trailing colon.
-
In the connect call the two undefs are needed (on the remote server at least, it worked ok locally without them).
See below for version info.
my $my_cnf = 'path_to_somewhere_safe/my_cnf.cnf';
my $dsn =
"DBI:mysql:;" .
"mysql_read_default_file=$my_cnf";
my $dbh = DBI->connect(
$dsn,
undef,
undef,
{RaiseError => 1}
) or die "DBI::errstr: $DBI::errstr";
Note there is no connection info in the script at all. It's tucked away nicely in a cnf file. For example:
# my_cnf.cnf
[client]
host = hostname
database = my_db
user = my_usr
password = my_pwd
The DBD::mysql docs give an indication on how to use option files but more can be found on the MySQL site:
To see which options are available:
There is also an interesting article at:
The last link points out that you need the two undef parameters in the connect statement.
Version info:
MySQL -> 4.0.17 (remote)
MySQL -> 4.1.15 (local)
DBI 1.32 (remote)
DBI 1.49 (local)
DBD::MySQL 2.0419 (remote)
DBD::MySQL 3.0002 (local)
Hope this is of some use and many thanks to Your Mother for the pointer.
John
update:
The 'interesting article' linked to above should be credited to gmax (see below).
++ to gmax
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.