PHP local host to URL - php

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.

Related

Remote connection from cPanel to MySQL on WAMP

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 want to test connection to MySQL database hosted locally - remotely [duplicate]

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.

How to access mysql server within a server?

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.

Unable to connect mysql and php in Lamp on online server

I am using XAMPP on my computer. My website is running fine on it. Now I uploaded my files to server using scp. Now I tried to change my connection parameter. I change it
<?php
$host ="104.236.227.173";
$username = "****";
$password = "******";
$database = "****";
#session_start();
?>
The host is the server on which my website is running.
Mysite
But online I am unable to connect to database.
I installed Lamp online.
I have changed settings in my.conf. But it does not work.
I have used the following command too
GRANT ALL ON `database`.* TO username#'%' IDENTIFIED BY 'password';
Please help me to solve this.
As shared using 'localhost' instead the IP address should do the trick. You're accessing the mysql on the same server as the php script is running. Unless the mysql server is configured to be available via IP address, 'localhost' is default behaviour. Glad it helped!

How do I connect MySQL server

I have created three EC2 instances, two of instances are web servers and one instance is a MySQL server. I would like to connect to the MySQL server and retrieve data. I was wondering how I can send a SQL query to the MySQL server.
<?php
$server = "localhost";
$username = "username";
$password = "password";
$database_name = "dbname";
$dbconnect = mysql_connect($server, $username, $password);
if(!$dbconnect)
{
//connection failed to the host
echo "-1";
exit;
}
if(!mysql_select_db($database_name))
{
//cannot connect to database
echo "-2";
exit;
}
?>
I used this php script to connect to the local mysql server. If I want to connect to the remote MySQL server on EC2 instance then do I just simply replace the server address to the IP address (elastic IP address) of the EC2 instance that running MySQL server?
Instead of using the Elastic IP address, use the DNS name associated with the Elastic IP address. It will resolve to the internal IP address associated with the current instance mapped to the Elastic IP address. This saves you in latency and cost.
Here's an article I wrote that describes this approach:
Using Elastic IP to Identify Internal Instances on Amazon EC2
http://alestic.com/2009/06/ec2-elastic-ip-internal
You'll also need to make sure that your MySQL database is listening on 0.0.0.0 instead of 127.0.0.1. Check that your MySQL database server is not accessible from the Internet.
not sure what programming language you want to use, but here is a Getting Started Guide for PHP, but it is just a matter of setting the IP

Categories