0

I am having trouble connecting to my SQL database pointing to the domain. My connect.php returns the following warning:

Warning: mysqli::mysqli() [mysqli.mysqli]: (HY000/1042): Can't get hostname for your address

connect.php

<?php
    $server = "agenciaeficacia.com.br";
    $user = "my-user";
    $pass = "my-password";
    $db = "my-db";

    // CRIA CONEXÃO
    $conexao = new mysqli($server, $user, $pass, $db);

    // CHECA CONEXÃO
    if ($conexao->connect_error) {
        echo "Falha ao conectar com o banco de dados.";
    }   

    date_default_timezone_set('America/Sao_Paulo');
?>

The weird thing is that the connection works on localhost, but not when I upload it to the server (unless I use localhost instead of my domain). I have already tried the skip-name-resolve solution, but it didn't work.

Is there another way to solve this problem?

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Matheus Souza
  • 334
  • 1
  • 3
  • 10
  • [checkout this link](http://stackoverflow.com/questions/13769504/mysqlimysqli-hy000-2002-cant-connect-to-local-mysql-server-through-sock)... – pritesh Jan 28 '16 at 14:20
  • 1
    Does the remote server allow connections that come from outside? Afaik per default the server only accepts `localhost`. – Nidhoegger Jan 28 '16 at 14:21
  • @Nidhoegger Yes, it allows. I have always done my connections this way and I've never had this problem before. – Matheus Souza Jan 28 '16 at 14:23
  • 2
    mysql's probably trying to do a reverse dns lookup on the IP you're connecting from, and failing. if your account in mysql is defined as `user@example.com` instead of `user@x.x.x.x` (ip address), then this rerverse DNS lookup **MUST** succeed for mysql to allow you in. – Marc B Jan 28 '16 at 14:24
  • 1
    Try with ip address instead of domain name. – GuRu Jan 28 '16 at 14:24

3 Answers3

0

I had a problem like this some time back and it only worked when I disabled DNS lookup. If this is an option for you then you can change my.cnf or my.ini

[mysqld]

skip-name-resolve

GuRu
  • 1,880
  • 3
  • 23
  • 32
mukundi
  • 49
  • 5
0

See this: https://serverfault.com/questions/174242/error-connecting-remote-mysql-server-error-1042-hy000-cant-get-hostname-for NB: "...in this case, you can use only IP numbers in the MySQL grant tables." So probably you have hostnames in grant tables and your mysql server does not have DNS resolver working ..

Community
  • 1
  • 1
0

That error resolved for me when I turned TCP/IPv6 off on my network adapter.

I was going to change the my.ini/my.cnf, but all the other stations were working fine with the same setting on the server, so after trying to use the IP address for the host and still getting that error, I was looking at the network adapter settings through the Control Panel and unchecked TCP/IPv6 (we don't use it), not even thinking it would have an effect, but it did and that station started to connect just fine.

Jerry
  • 1