简体   繁体   中英

Unknown MySQL server host

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.

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.

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');

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM