johny
07-22-2003, 06:02 AM
Hi, all:
I tried two scripts, which are to use a configuration file to free users from inputing username, password, etc manually.
Here is the file "my.cnf":
# My configuration file
[client]
user=root
password=rochester
Here is the file "mysql_connect2.pl":
#!d:/perl/bin/perl
# Use what needs to be used.
use DBI;
use strict;
# Connect to the database.
my $user;
my $password;
my $dbh = DBI->connect('DBI:mysql:mylibrary;mysql_read_default_file=c:\my.cnf', $user, $password, {RaiseError => 1});
# Report on the success of the connection attempt.
if ($dbh) {
print "Successfully connected to the database! \n";
}
# Disconnect.
$dbh->disconnect;
(I have created a database named "mylibrary" in mysql. )
When I run this script, it has no response and hang there.
(I put the my.cnf file into the root directory of C drive. )
Yet, if I change the DBI->connect line into:
my $dbh = DBI->connect('DBI:mysql:mylibrary;mysql_read_default_file=./my.cnf', $user, $password, {RaiseError => 1});
It can run as well as expected. (I have copied my.cnf into the same folder as this script.)
Sounds strange, right?
I guess it is due to to colon in the string "c:\my.cnf".
But how to solve it? I tried to escape it, e.g. using "c\:\my.cnf", but to no avail.
I run the script again in Win2k with MySQL 3.23.56 installed.
This time, it will pop up an error like this:
***********************************************************
D:\DTemp>mysql_connect2.pl
DBI connect('mylibrary;mysql_read_default_file=c:\my.cnf','',...) failed: Unknown My
SQL Server Host '\my.cnf' (11001) at D:\DTemp\mysql_connect2.pl line 12
***********************************************************
From the error message above, it can be seen that the error is produced by its inability to interprete the colon in the string "c:\my.cnf". The string after this colon is looked as host name of MySQL server.
Because the canonical usage of connect is as follows:
$dbh=DBI->connect('DBI:mysql:database:host', 'user', 'password', {RaiseError => 1})
And in the case of localhost, the host name can be omitted.
How to solve this problem???
thanks,
I tried two scripts, which are to use a configuration file to free users from inputing username, password, etc manually.
Here is the file "my.cnf":
# My configuration file
[client]
user=root
password=rochester
Here is the file "mysql_connect2.pl":
#!d:/perl/bin/perl
# Use what needs to be used.
use DBI;
use strict;
# Connect to the database.
my $user;
my $password;
my $dbh = DBI->connect('DBI:mysql:mylibrary;mysql_read_default_file=c:\my.cnf', $user, $password, {RaiseError => 1});
# Report on the success of the connection attempt.
if ($dbh) {
print "Successfully connected to the database! \n";
}
# Disconnect.
$dbh->disconnect;
(I have created a database named "mylibrary" in mysql. )
When I run this script, it has no response and hang there.
(I put the my.cnf file into the root directory of C drive. )
Yet, if I change the DBI->connect line into:
my $dbh = DBI->connect('DBI:mysql:mylibrary;mysql_read_default_file=./my.cnf', $user, $password, {RaiseError => 1});
It can run as well as expected. (I have copied my.cnf into the same folder as this script.)
Sounds strange, right?
I guess it is due to to colon in the string "c:\my.cnf".
But how to solve it? I tried to escape it, e.g. using "c\:\my.cnf", but to no avail.
I run the script again in Win2k with MySQL 3.23.56 installed.
This time, it will pop up an error like this:
***********************************************************
D:\DTemp>mysql_connect2.pl
DBI connect('mylibrary;mysql_read_default_file=c:\my.cnf','',...) failed: Unknown My
SQL Server Host '\my.cnf' (11001) at D:\DTemp\mysql_connect2.pl line 12
***********************************************************
From the error message above, it can be seen that the error is produced by its inability to interprete the colon in the string "c:\my.cnf". The string after this colon is looked as host name of MySQL server.
Because the canonical usage of connect is as follows:
$dbh=DBI->connect('DBI:mysql:database:host', 'user', 'password', {RaiseError => 1})
And in the case of localhost, the host name can be omitted.
How to solve this problem???
thanks,