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

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

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.

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.

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.

Related

Connecting MySQL database to website with PHP

As the title indicates I'm trying to link my MySQL database to my website using php.
I'm using GoDaddy hosting for the MySQL database and I got this code directly from them and it's still not working for some reason, giving the error below.
Here's my code :
<?php
$hostname = "trdlibrary.db.6253425.hostedresource.com";
$username = "*******";
$password = "*******";
$dbname = "trdlibrary";
$connect = mysqli_connect($hostname, $username, $password, $dbname, 3306) OR DIE ("Unable to connect to database! Please try again later or contact an administrator for help.");
?>
Here's the error :
Warning: mysqli_connect(): (HY000/2002): A connection attempt failed
because the connected party did not properly respond after a period of
time, or established connection failed because connected host has
failed to respond. in C:\xampp\htdocs\searchengine\dbconnect.php on
line 6 Unable to connect to database! Please try again later or
contact an administrator for help.
Can anyone see the issue?.. or is this something I need to contact GoDaddy's support line about.
-- EDIT --
I forgot to mention that I am running the web server off my local machine using XAMPP (so I can run the PHP scripts) but the MySQL server is off-site with GoDaddy, so 'localhost' won't work currently.
-- EDIT #2 --
So I learned that sadly, remote access of MySQL databases to GoDaddy requires a more expensive and 'higher tier' hosting package from them. So I just answered my own question, thanks to those who replied.
You have to setup remote access to databases on Godaddy. You can refer to this page to see how to enable it.

Access a MySql Database from PHP (not on localhost)

there's this server:
http://phpmyadmin.pvdata.fr/index.php ,
I can connect to it knowing the username and password and Server Choice.
Once I connect to it, it shows under MySql the following information: (I'm Translating from French)
Server: sql6 (sql6 via TCP/IP)
Server Version: 5.0.92-87
Protocol Version: 10
Username: pvdata#10.5.1.3
Character for MySql: UTF-8 Unicode (utf8)
So I tried using the following code in order to connect to the Database:
<?php
// Create connection
$username = "pvdata";
$password = "xxxxxxxx";
$hostname = "10.5.1.3";
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
?>
But it didn't work, it's saying: "A connection attempt failed because the connected party did not properly respond after a period of time".
What am I doing wrong?
Go to your phpMyAdmin installation and look for the file config.inc.php. Open that file, and check which hostname / ip is being used for SQL6. That is the IP you should use for your mysql_connect()
This is most likely a firewall issue. Most 3rd party hosts don't allow you to access MySQL from 'the outside'.
You can do this if you manage the MySQL server yourself or if you have a host that doesn't deny you to access the database from another machine, but in regular web hosting, this is not common.
You need to search, google helps a lot, also you need to connect to an ip/domain and port of your MySQL server.
For this, you need to know what is the port of your mysql server.
Look this answer from link
<?php
mysql_connect("mysite.com:3306", "admin", "1admin") or die(mysql_error());
echo "Connected to MySQL<br />";
?>
to check if you have coneccion, try in console:
shell> mysql --host=remote.example.com --port=3306 //3306 is the
defaul port for MySQL
http://dev.mysql.com/doc/refman/5.0/en/connecting.html

My code only connect to localhost mysql server but not for remote server

When trying to connect to remote Mysql server I get this error :
php_network_getaddresses: getaddrinfo failed: Hôte inconnu. (translation: Host unknown).
con.php :
<?php
$db_host = 'http://xxx.xxx.xxx.xxx';
$db_username = 'xxx';
$db_password = 'xxx';
$db_name = 'xxx';
$con = mysql_connect($db_host,$db_username,$db_password);
$select_db = mysql_select_db($db_name,$con);
?>
When connecting to localhost all goes fine, but for remote connection it didn't work.
$db_host = 'xxx.xxx.xxx.xxx'; Instead
You can't use the protocol HTTP for Mysql since they both run on different ports / use different protocols.
Further more HTTP (web service) = 80 Mysql (your database) = 3306 just to note as mentioned by BenM this is the default port. In most instance this is not changed. However you change its port to any other port (like you can with any other service).
And in addition please take a look at PDO since Mysql_ functions are going to be deprecated and are also dangerous to use in new systems and in old ones as well.
Take a look at one of my answers on how to set up PDO
Edit 1
As you mentioned on benM's answer in a comment, "No connection could be made because the target machine actively refused it."
so now if you have root access on the actual server the database is hosted on then you have to check if the server (Example in my case is ubuntu server)
run this command :
sudo netstat -pan | grep mysql
Also take a look at this picture : Pretty much it shows the TCP port, if it iS Listening or not listing (here it is listening)
again this is assuming you have root access to the actual linux / (whatever server) I am only proficient at ubuntu but can assist you with other server information as well.
You cannot connect to MySQL using the HTTP protocol (namely because of port conflicts). The parameter should be an IP string. Remove the http protocol as follows:
$db_host = 'xxx.xxx.xxx.xxx';
$db_username = 'xxx';
$db_password = 'xxx';
$db_name = 'xxx';
$con = mysql_connect($db_host, $db_username, $db_password);
$select_db = mysql_select_db($db_name, $con);
You should also note that MySQL can be configured to prevent anything other than localhost access on user accounts. Ensure that the user you're using is able to access the database from any domain.
Finally, the mysql_* family of functions are now being deprecated, and you should avoid using them for new code. Instead, check out MySQLi or PDO.

PHP Localhost Application connect to server database

I want to ask about how to connect my localhost application (C:xampp/htdocs/myproject) to database at my server host (www.someweb.somedomain)?
Am I possible to do that? If it is, how to connect it at my php config? Right now my config (server.php) is:
<?php
$host = "my web IP public:3306";
$user = "root";
$pass = "";
$db = "dispatcherDB";
$conn = mysql_connect($host, $user, $pass) or die ("Cant connect to mySQL");
mysql_select_db($db);
?>
what I got:
Warning: mysql_connect(): No connection could be made because the target machine actively refused it. in C:\xampp\htdocs\XMS\server.php on line 7
So, what must I filled for $host? I'm trying using website IP, it still can't connect. Maybe there's someone here have experience at this problem?
Sorry for my bad English
If you have cPanel access to the remote server then you need to mention that from which ip addresses it should allow access to MySQL..
In cPanel you will get Remote MySQL under heading Databases:
Clicking Remote MySQL will give you the option to add hosts from where you want to allow connections to your MySQL server:
Also, you can check localhost without specifying port number as value of $host..
Probably Mysql server is not allowed root access from remote servers.
you could check this post

Connecting From PHP Server to MySQL Server on Amazon AWS

I have two servers setup on Amazon AWS. One server is running PHP and the other has MySQL. I can connect to the MySQL database through my terminal and MySQL Query Browser.
I am trying to get a connection between the PHP and MySQL servers.
Here is the PHP Code that I am using (works for "localhost" databases)
$dbbase = 'mydb';
$dbuser = 'myuser'; // Your MySQL username
$dbpass = 'mypassword'; // ...and password
$dbhost = 'localhost:3306'; // Internal IP for MYSQL Server
// connect to database
$dblink = mysql_connect($dbhost, $dbuser, $dbpass)
or die ("System Down");
mysql_select_db($dbbase, $dblink)
or die ("Database Down");
It is my understanding that I should be able to route this an internal AWS traffic, but at this point I will take anything that works and build from there.
Here is what I have done:
Added the ip of the PHP server to the Security Group for MySQL(3306) permissions
Tried to use the internal, external, and private IPs/DNSs of the MySQL Server as the $dbhost variable
Created myuser#% (wildcard) on thy MySQL server
Any ideas or tips would be much appreciated.
I had the same issue - turns out MySQL extension for PHP is NO longer included in PHP5!
"In PHP 5 (updated PHP 5.0.4), the following changes exist. Built in: DOM, LibXML, Iconv, SimpleXML, SPL and SQLite. And the following are no longer built in: MySQL and Overload."
Source: http://php.net/manual/en/install.windows.extensions.php
I've got this working.
I think the big trick was to add this rule to the Security Group:
Type: MySQL
Source: 10.0.0.0/8
My understanding is that 10.0.0.0/8 covers all internal amazon IPs. I think you could tighten this up, but it is possible for the internal IP of your servers to change, so that would need to be managed.
Then in my PHP script I used the Private DNS of my MySQL Server. It should look something like this: ip-10-10-100-100.ec2.internal:3306
In the end, I think that is everything that I did.

Categories