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.
Related
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.
I've migrated a Database from cPanel to cPanel through Backup tool, But the problem is whenever i try to connect to this database for example using this TEST php code
<?php
# Fill our vars and run on cli
# $ php -f db-connect-test.php
$dbname = 'imadoulh_test';
$dbuser = 'xxxxx';
$dbpass = 'xxxxx';
$dbhost = 'localhost';
$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";
}
the response is This page isn’t working with HTTP ERROR 500
and the MySQL log says Got an error reading communication packets
I did try many ways to fix it but unfortunately.
Here's what I tried:
1. optimize and repair tables
2. add some code to /etc/my.conf file such as
max_allowed_packet=2684354569944
open_files_limit=10000
default-storage-engine=MyISAM
innodb_file_per_table=1
default_authentication_plugin=mysql_native_password
dealing with this article:
https://dba.stackexchange.com/questions/19135/mysql-error-reading-communication-packets
Error 500 from http means your web program -- your php program -- failed trying to send out that page. But 500 can tell us absolutely nothing about why it failed. So, moving along... You seem to be getting communication errors between php and mysql.
I believe the key words in your question are from cPanel to cPanel. That suggests you are migrating your application from server to server and maybe even from hosting-vendor to hosting-vendor.
You have imported your MySQL data from backups onto a new server with a different IP address. So, you now have two different database instances. And, it's possible you have two different instances of your php program.
So now you're troubleshooting these questions:
Can my new php code talk to my new database server? From your question it's hard to tell whether the answer is "no" or "sometimes, but unreliably."
Does my new php code run at the same hosting vendor as its MySQL database? In network engineer lingo the question is "are they on the same datacenter network?"
Is there any chance I'm trying to hit the new database from the old code, or the old database from the new code?
Some (but not all) technical support specialists at hosting companies know about this kind of thing. Call for help, but be sure the first two words out of your mouth are "I'm migrating" so they know it's a new customer and a one-time setup issue.
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.
I've set up a local web server on my home network by setting up a LAMP on my Raspberry Pi. When doing my PHP scripts and connecting to MySQL database I have been using localhost. Now I'm messing about with accessing the server outside of my home network so I've set-up a domain name to access the server.
But what I didn't realise is my code still uses the localhost to log into the MySQL database. So I decided to change it to my domain name and now it states that the database cannot be found so I'm assuming it is not logging on and therefore no database is being selected.
PHP Script:
<?php
//thermReading1.php
$servername = "myservername.com"; //example URL
$username = "user";
$password = "password";
$dbname = "database";
//create connection
$db_handle = mysql_connect($servername,$username,$password);
$db_found = mysql_select_db($dbname,$db_handle);
$thermID = "'T00'";
$userID = "'1'";
if($db_found)
{
$sql = "SELECT * FROM tblTempReading"
$result = mysql_query($sql);
while($db_field = mysql_fetch_assoc($result))
{
echo "<p>" . $db_field['temp'] . "°C</p>";
echo "<p>" . $db_field['tempTimeStamp'] . "</p>";
}
}
else
{
echo "<p>Database NOT Found</p>";
}//endif
mysql_close($db_handle);
?>
Is this a problem with logging into MySQL via the server instead of localhost or would it be firewall issues (I've set up port forwarding on my router to access the server)?
Any help is appreciated, I'm new to this web server stuff.
localhost is a domain name to refer to the local machine.
If the web server and the MySQL database server are on the same server, you can leave localhost as "remote" database host.
In fact, when you're running a script on a server, all addresses that have to be resolved from DNS are solved from internal DNS server and not from the client DNS.
It does not matter from where you make the request to the server. If the IP address of the machine that runs PHP is the same of the database server, the address of the database server is localhost, relative to the web server.
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