简体   繁体   English

MySQL连接失败。 无法连接到“ http”上的MySQL服务器(4)

[英]MySQL connect failed. Can't connect to MySQL server on 'http' (4)

I try to connect my android application using JSON Parser to the web hosting. 我尝试将使用JSON Parser的android应用程序连接到虚拟主机。 But whenever I try to connect (even just open using url in the browser) I will get the error message. 但是无论何时我尝试连接(甚至只是使用浏览器中的url打开),我都会收到错误消息。

<?php
  $dbHost = 'http://sql4.000webhost.com/localhost';
  $dbUser = 'a6410240_cbetTD';
  $dbPass = 'xxxxxx';
  $dbName = 'a6410240_cbetTD';

  $conn = mysql_connect ($dbHost, $dbUser, $dbPass) or die ('MySQL connect failed. ' . mysql_error());
  mysql_select_db($dbName,$conn);
?>

This is my database.php file. 这是我的database.php文件。 The full error message is 完整的错误消息是

Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'http' (4) in /home/a6410240/public_html/database.php on line 8.

I have tried change the $conn but still it didn't worked for me. 我曾尝试更改$ conn,但仍然对我没有用。

Thanks 谢谢

If your database and application is on same server then use "locahost" in $dbhost . 如果您的数据库和应用程序在同一服务器上,则在$ dbhost中使用“ locahost”

And if your database and application is on different servers then you need to use IP address or hostname in $dbhost and the database user should be added on database server provided with required privileges. 而且,如果数据库和应用程序位于不同的服务器上,则需要在$ dbhost中使用IP地址或主机名 ,并且应将数据库用户添加到具有所需特权的数据库服务器上。

The problem you are having was already mentioned in one of the comments, this one to be precise. 您所遇到的问题已在其中一项评论中提到,确切地说, 这是一条评论。

For your solution to work, all you need to do is omit the part http:// at the beginning and probably /localhost at the end. 为了使您的解决方案生效,您需要做的就是在开头省略http:// ,在结尾省略/localhost

The host is only the domain you are referring to. 主机只是您要引用的域。 In this case sql4.000webhost.com . 在这种情况下为sql4.000webhost.com With /localhost you tried to already connect to a database, although your configured database is supposed to be a6410240_cbetTD . 使用/ localhost,您已尝试连接到数据库,尽管您配置的数据库应该是a6410240_cbetTD

MySQL use TCP port 3306 by default ($dbport) and hostname or IP address ($dbhost). MySQL默认使用TCP端口3306($ dbport)和主机名或IP地址($ dbhost)。 For LAMP (Linux-Apache-MySQL-php) you can find a lot of tutorials. 对于LAMP(Linux-Apache-MySQL-php),您可以找到很多教程。 Usually MySQL server listens internal port (which can't be reached via Internet) for security purposes. 通常,出于安全目的,MySQL服务器会侦听内部端口(无法通过Internet访问)。 If you familiar with docker, you can simply download examples of LAMP solutions from hub.docker.com. 如果您熟悉docker,则可以直接从hub.docker.com下载LAMP解决方案的示例。

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

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