perl connection string for DBI (mysql)
Posted: 28. November 2007 21:49
I was getting a this error: Failed to connect to mySQL server: Can't connect to local MySQL server through socket '/tmp/mysql.sock'
I hadn't realized two things:
A) for windows the connection is through tcp/ip and not a socket (I could see this by using "C:\xampp\mysql\bin>mysql -u root" from the windows command line and entering the command "status")
B) "localhost" will not work for a tcp/ip connection, you have to use "127.0.0.1".
The problem is fixed, this is my code:
I hadn't realized two things:
A) for windows the connection is through tcp/ip and not a socket (I could see this by using "C:\xampp\mysql\bin>mysql -u root" from the windows command line and entering the command "status")
B) "localhost" will not work for a tcp/ip connection, you have to use "127.0.0.1".
The problem is fixed, this is my code:
- Code: Select all
use DBI;
my $username = "root";
my $auth = "";
my $driver = "DBI:mysql";
my $database = "dbname";
my $hostname = "127.0.0.1";
my $port = 3306;
my $dsn = "DBI:mysql:host=$hostname:port=$port;database=$database";
my $dbh = DBI->connect($dsn, $username, $auth);
die "Failed to connect to mySQL server: $DBI::errstr\n" unless defined $dbh;