简体   繁体   English

未知的 MySQL 服务器主机

[英]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:第 18 行是我尝试请求连接 MySQL 服务器的那一行:

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

I host my database on the 1&1 website hosting company.我在1&1网站托管公司托管我的数据库。

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.如果您的连接目标始终相同,则您可能希望使用其 IP 地址进行连接。

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:请注意 AWS 安全组:

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.在我的情况下,我可以通过 Telnet 从我的计算机连接到 RDS,也可以通过 Mysql Workbench 连接,但我无法从与 RDS 位于同一 VPC 内的 EC2 实例连接。

The solution was:解决方案是:

  1. I have created a security group (exampl1) for RDS and assigned to it.我为 RDS 创建了一个安全组(exampl1)并分配给它。
  2. I have created a security group (exampl2) for EC2 and assigned to it.我为 EC2 创建了一个安全组(exampl2)并分配给它。
    and I have assigned the RDS security group (exampl1) to the EC2 too.并且我也将 RDS 安全组(exampl1)分配给了 EC2。 << 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.信息:如果您的 EC2 已分配 2 个或更多安全组,则在 RDS 安全组中,入站源必须创建规则,因为您的 EC2 已分配了多个安全组。

Amazon docs says:亚马逊文档说:
The EC2 instance in the VPC shares the same VPC security group with the DB instance. VPC 中的 EC2 实例与数据库实例共享同一个VPC 安全组。 http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Overview.RDSSecurityGroups.html 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.我的问题最终是我们的防火墙上没有打开端口 3306,我试图从外部服务器连接到内部 mysql 数据库。

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.确保您不包括“http://”部分。 I was using http://example.com in my .env file.我在.env文件中使用了http://example.com When I left it as example.com it worked.当我将其保留为example.com时,它起作用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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