Tutorial :perl script to connect to mysql server port 3307



Question:

I am trying to connect to a mysql server which is running at port 3307. How do I connect to the server? I do not see any other way to specify port. I am using like this:

#!/usr/bin/perl  use Mysql;    $host = "localhost";  $database = "abc";  $tablename = "def";  $user = "uuu";  $pw = "ppp";      $connect = Mysql->connect($host, $database, $user, $pw) or die "Cannot connect to MySQL server\n";  

I want to use MySQL package and not DBI.

Thank you.


Solution:1

You are mistaken. You want to use DBI and not Mysql. The Mysql module became obsolete 12 years ago, when it was replaced with a compatibility module that's just a wrapper around DBI. Even the compatibility module has been removed from the current distribution; you have to install an old DBD::mysql just to get it (it last shipped in DBD-mysql 3.0008, released back in 2006).

#!/usr/bin/perl    use strict;  use DBI;    my $host = "localhost";  my $database = "abc";  my $port = 3307;  my $tablename = "def";  my $user = "uuu";  my $pw = "ppp";    my $dbh = DBI->connect("DBI:mysql:database=$database;host=$host;port=$port",                         $user, $pw)    or die "Cannot connect to MySQL server\n";  


Solution:2

Try specifying host like localhost:3307


Solution:3

For me...Following seems to be working ...

#!/usr/bin/perl    use strict;  use DBI;    my $host = "rajeshk-W7";  my $database = "rajesh";  my $port = 3307;  my $tablename = "def";  my $user = "rajesh";  my $pw = "rajesh123";  #my $dbh = DBI->connect("DBI:mysql:rajesh:rajeshk-W7","rajesh","rajesh123") or die "Cannot connect to MySQL server\n";    my $dbh = DBI->connect("DBI:mysql:database=$database;host=$host",$user, $pw) or die "Cannot connect to MySQL server\n";  

where i dint mentioned port. When i add port, its unable to connect.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »