I am trying to use a simple PHP connection to connect remotely to a MySQL database. I have searched many threads and still receiving error 'Connection failed: Can't connect to MySQL server on 'my IP address' (111 "Connection refused")'
I can access the server remotely using the wan IP address no problem i.e. The software files. I am using port forwarding to do that on port 80.
My connection is very simple
<?php
$servername = "my ip address";
$username = "user";
$password = "password";
$dbname = "company";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, name FROM table";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
I have tried the following;
Creating a new user with all privileges in phpMyAdmin and using % as host
Allow from all in phpmyadmin.conf and HTTP.conf (don't think this is the problem)
Adding port forwarding on router to 3306
Disabled all firewalls on local machine
Adding bind-address = 0.0.0.0 to my.ini file (restarting the server)
A lot of thread suggest that the default bind-address is set to localhost or 127.0.0.1 but in my.ini under [mysqld] all I have from default is
default_authentication_plugin=mysql_native_password
port = 3306
I am using WAMP version 3.1.9 64 bit version
Separate your concerns.
First, verify that another machine on your local network can access the database directly at the 3306 port. This will verify that the server is working, that the log in is correct, and that the permissions are correct, and that any firewall settings have been addressed.
After all is working correctly, deal with accessing it via WAN. It's unclear if you're attempting to do this "from inside the house", ie, using your own external WAN ip address from yourself, there are multiple ways this can fail. It can work, but that's additional setting/debugging. So ensure you're dealing with a machine that's actually outside your network for this section of the testing.
As it stands, somewhat impossible to know where the trouble lies.
After trying everything I set up another WAMP installation on a separate machine and connected to a separate network. I could get it to connect to the remote MYSQL on my original machine. It appears that the PHP script was blocked by cPanel before the request was even sent. The link here https://docs.cpanel.net/knowledge-base/general-systems-administration/how-to-configure-your-firewall-for-cpanel-services/84/ shows that cPanel does not allow outbound connection on port 3306. Unfortunately I can't access settings on my shared hosting to change this.
Related
This question already has answers here:
Why shouldn't I use mysql_* functions in PHP?
(14 answers)
Closed 4 years ago.
I have a mySQL database hosted locally using WAMP, I went through all the procedures to access it remotely and seem to have succeeded in that I can put the IP address into my phone, see WAMP and access phpmyAdmin etc.
My issue is that I want to test the database connection using the script below from my website (ultimately I want to use a WP plugin to get info off the DB and then close it).
However I am not fully sure what the parameters would be.
Apache server is on port 80 and mysql is on port 3306. I have setup port forwarding on those ports and tested them using http://www.canyouseeme.org/
Therefore would my host be myIP (using What is myIP) : 80 or 3306.
I have tried both and they fail. Would my user name be root or Root#Host
Like I say ultimately I want to test this, connect to the database and then use the parameters in a WordPress plugin to copy data from my locally hosted DB to my site's db.
Please advise.
Thank you.
<?php
# Fill our vars and run on cli
# $ php -f db-connect-test.php
$dbname = 'name';
$dbuser = 'user';
$dbpass = 'pass';
$dbhost = 'host';
$connect = mysql_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'");
mysql_select_db($dbname) or die("Could not open the db '$dbname'");
$test_query = "SHOW TABLES FROM $dbname";
$result = mysql_query($test_query);
$tblCnt = 0;
while($tbl = mysql_fetch_array($result)) {
$tblCnt++;
#echo $tbl[0]."<br />\n";
}
if (!$tblCnt) {
echo "There are no tables<br />\n";
} else {
echo "There are $tblCnt tables<br />\n";
}
Make sure your router is allowing the port forwarding. If the (ip address) xxx.xxx.xxx.xxx:80 is not showing anything then, ports are not being forwarded properly. Here are some check you could make to see where the problem lies:
See if it's working on the local machine at 127.0.0.1 (most likely it will work in your situation).
Check on other devices on your network such as using your smartphone go to your machines local IP address and see if that works.
If both of the above work, then you need to make sure your router is port forwarding the correct IP Address and Ports.
I've looked through the others that use these words. Not applicable. I have two machines plugged into the same switch with IP's of 192.168.0.7 and .10. 7 is the client. 10 is the server. I've checked netstat on the server and it is showing:
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 192.168.0.10:3306 0.0.0.0:* LISTEN
I can ping, sftp and so forth in both directions from either machine. The PHP itself is as follows:
$host = "192.168.0.10";
$user = "web";
$pass = redacted;
$db = "appliance";
// open connection
$connection = new mysqli($host, $user, $pass, $db);
if ($connection->connect_errno) {
echo "Error: Failed to make a MySQL connection, here is why: \n";
echo "Errno: " . $connection->connect_errno . "\n";
echo "Error: " . $connection->connect_error . "\n";
exit;
}
I get this at the browser (3rd machine - same network):
Error: Failed to make a MySQL connection, here is why: Errno: 2002
Error: No route to host
I've even tried adding the explicit port argument. Same error.
I don't even know where to look at this point.
Can You execute this code in both CLI and HTTP environment?
If apache has problem, therefore in cli there was no problem.
Check if the firewall on your MySQL server is blocking port 3306. For me it caused the 'No route to host' error.
check your connection to remote and check remote's firewall.
and check bind address in remote my.ini
I am running mysql on a linux server. I have the linux server IP and mysql runs on localhost on that linux server. Now how do I access mysql running on localhost server remotely?
I can do this by ssh into linux server then running mysql, but how would I do it with in a php script? For example...
<?php
$servername = "localhost";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername;dbname=myDB", $username,
$password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
(this script only connects to mysql directly which is not possible remotely)
In my script I would need to access linux server then mysql server unless there is a way to access mysql directly (remotely). How is this achievable?
You need change bind address in my.cnf and restart your mysql service.
Official documentation:
https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_bind_address
If the address is 0.0.0.0, the server accepts TCP/IP connections on all server host IPv4 interfaces.
By default all connections restricted localhost.
In order to access your mysql instance from your server; you need to install workbench (or some other mySql explorer) on your development machine.
Create a connection to mysql instance using server ip address as host name and provide username and password to open connection. If every thing goes well you will see your databases in explorer.
I have a MySQL server hosted in my Godaddy server. I want to access that database from my EC2 instance. However, I am unable to do so. I have added the public IP of my EC2 instance to the remote MySQL access hosts.
Things I have tried:
By using my Godaddy hosted website name as my server name.
Using my Godaddy hosted website IP address as my server name.
Every time I try to connect, It gives me the error:
Connection failed: Unknown MySQL server host '<hostname>:3306' (11)
Script I used:
<?php
$servername = "<hostname>:3306";
$username = "<username>";
$password = "<password>";
$dbname = "<dbname>";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
PS: I can successfully connect my MySQL workbench and a locally hosted server to my Godaddy database.
Try removing :3306 from $servername
Make sure the arguments are correct. mysqli() needs port as separate argument as described here
According to godaddy you need to enable Allow Direct Database Access for remotely connections.
Link here!
I have two servers, for the sake for my questions, lets name the first server A, and the second server B. Server A is a magento server that has a lot of user data, and server B is a basic web page.
I want to create an analytics dashboard in server B, that uses the SQL databases of server A.
Now the problem is having a remote connection from server B to server A.
I try to connect to server A, using this code in server B:
$servername = "104.2*****"; // Server A
$username = "root";
$password = "******";
$dbname = "magento";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
I get this error:
Connection failed: Can't connect to MySQL server on '104.2******'
Can someone please tell me what I'm doing wrong?
You may need to create a ssh tunnel to connect to a remote server hosted database if the settings on the database server do now allow unsecure connections which is likely why you're getting that error.
http://kb.mediatemple.net/questions/133/Tunnel+local+MySQL+server+through+SSH#gs
It might be that there is a firewall restriction on the server you want to connect to. The MySQL port connection might be restricted.
Try to connect from the server you run this script (or another server) to the ip/port of the server you want to connect to.
If you have access to a linux/windows console, try running:
telnet 104.2***** 3306
3306 is the default port for MySQL and telnet should work.