http://www.perlmonks.org?node_id=1072907

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 127.0.0.1?

        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 (127.0.0.1) 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.

      Karl

      «The Crux of the Biscuit is the Apostrophe»