24

When trying to connect to my database server, i encounter the problem of unknown host:

Warning: mysqli::mysqli() [mysqli.mysqli]: (HY000/2005): Unknown MySQL server host 'xxxxxxxxxxxxx:port' in index.php on line 18

the line 18 is that line where i try to request to connection the the MySQL server:

    $this->db = new mysqli($db_host, $db_user, $db_psw, $db_name);

I host my database on the 1&1 website hosting company.

Scott
  • 4,974
  • 6
  • 35
  • 62
Malloc
  • 15,434
  • 34
  • 105
  • 192

5 Answers5

30

This is usually the case when name resolving doesn't work on the host. If your connect destination is always the same, you might want to use its IP address for connecting instead.

11

If you use this code:

$Mysqli= new mysqli('mysql2.servidoreswindows.net:3306',
                    'usu', 'pass', 'dbname');

you can try to write host without port

That is:

$Mysqli= new mysqli('mysql2.servidoreswindows.net', 'usu', 'pass', 'dbname');
Mikhail
  • 8,692
  • 8
  • 56
  • 82
user3048858
  • 1,036
  • 1
  • 8
  • 5
  • 1
    I was using the Zend Db factory and array('host'=>"host:port") never worked, however array('host'=>"host",'port'=>"port") worked great because http://php.net/manual/en/mysqli.construct.php requires the port as a separate argument. – user3338098 Mar 26 '15 at 17:30
9

Please pay attention with AWS security groups:

In my case I can connect to RDS from my computer through Telnet and I can connect through Mysql Workbench also but I cant connect from my EC2 instance that is within the same VPC as the RDS.

The solution was:

  1. I have created a security group (exampl1) for RDS and assigned to it.
  2. I have created a security group (exampl2) for EC2 and assigned to it.
    and I have assigned the RDS security group (exampl1) to the EC2 too. << this saves me.

Info: If your EC2 has assigned 2 or more security groups, then in the RDS security group inbound source has to create rules as many security groups has your EC2 assigned.

Amazon docs says:
The EC2 instance in the VPC shares the same VPC security group with the DB instance. http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Overview.RDSSecurityGroups.html

Adrian Jandl
  • 2,985
  • 4
  • 23
  • 30
Philip Enc
  • 1,072
  • 15
  • 21
3

I know this is an old question. But I ran into this same issue today, and none of the answers I found were the right one.

The issue in my case ended up being that port 3306 is not open on our firewall, and I was attempting to connect to an internal mysql database from an external server.

I'm not sure why the error it gives you is "Unknown Host" in this case. I would have expected something more like "Unable to connect." or "Connection refused.", but using the exact same code from an internal server worked fine, leading me to this conclusion.

TibTibs
  • 167
  • 4
  • 11
  • Connecting from mysql client, if any connect problem, unknown address exception will be reported. – samuel Feb 09 '15 at 07:48
3

Make sure you're not including the "http://" part. I was using http://example.com in my .env file. When I left it as example.com it worked.

zundi
  • 2,361
  • 1
  • 28
  • 45