Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

connect to DBD::MYSQL

by fionbarr (Friar)
on Jan 31, 2014 at 21:00 UTC ( #1072907=perlquestion: print w/replies, xml ) Need Help??

fionbarr has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to connect to MySQL with this code:
my $hostname = "localhost"; my $db_name = "test"; my $dsn = "DBI:mysql:host=$hostname; database=$db_name"; my $dbh = (DBI->connect($dsn, "ttt", "nancy613", {PrintError => 0, RaiseError => 1} ));
I am getting this error: 'Unknown MYSQL server host ' localhost' <11004>'

Replies are listed 'Best First'.
Re: connect to DBD::MYSQL
by Tux (Canon) on Jan 31, 2014 at 21:06 UTC
    1. Please remove your credentials (or replace them with something stupid)
    2. Is localhost present in /etc/hosts?

    Enjoy, Have FUN! H.Merijn
      thanks....the credentials are already 'stupid'. This is a windows7 machine.

        Did you try to replace localhost with

        edit (added question): Is the error really 'Unknown MYSQL server host ' localhost' <11004>', as in '<space>localhost? Could it be your code is not actually what you pasted, and the is a space before $hostname?

        Enjoy, Have FUN! H.Merijn
Re: connect to DBD::MYSQL
by Generoso (Prior) on Feb 02, 2014 at 06:31 UTC

    Think you are missing the port 3306.

    my $port1='3306'; my $host1="localhost"; my $service1="test2"; my $userid1="xxx"; my $passwd1="yyy"; my $connectionInfo1="DBI:mysql:database=$service1:host=$host1:$port1";
Re: connect to DBD::MYSQL
by FloydATC (Deacon) on Feb 01, 2014 at 09:38 UTC

    If MySQL is running on the local machine, there are actually two ways you can connect to it; either via TCP/IP on the localhost address ( or via a named pipe.

    ...Ofcourse, this doesn't actually explain the error you're getting, but it may help you bypass it: 4.2.2. Connecting to the MySQL Server

    The article seems to suggest you may need to tweak things a bit on Windows to use named pipes on that platform.

    -- FloydATC

    Time flies when you don't know what you're doing

      ..."...Ofcourse, this doesn't actually explain the error you're getting"

      It doesn't, indeed. But perhaps this issue has something to do with avian carriers? I remember you already mentioned this.


      «The Crux of the Biscuit is the Apostrophe»

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1072907]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2022-05-20 20:30 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (76 votes). Check out past polls.