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

落花浮王杯 提交于 2019-12-11 08:48:41

问题


I try to connect my android application using JSON Parser to the web hosting. But whenever I try to connect (even just open using url in the browser) I will get the error message.

<?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. 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.

Thanks


回答1:


If your database and application is on same server then use "locahost" in $dbhost.

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.




回答2:


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.

The host is only the domain you are referring to. In this case sql4.000webhost.com. With /localhost you tried to already connect to a database, although your configured database is supposed to be a6410240_cbetTD.




回答3:


MySQL use TCP port 3306 by default ($dbport) and hostname or IP address ($dbhost). For LAMP (Linux-Apache-MySQL-php) you can find a lot of tutorials. Usually MySQL server listens internal port (which can't be reached via Internet) for security purposes. If you familiar with docker, you can simply download examples of LAMP solutions from hub.docker.com.



来源:https://stackoverflow.com/questions/39632402/mysql-connect-failed-cant-connect-to-mysql-server-on-http-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!