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.
Related
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 was just wondering if it was possible for certain databases to block SQL accesses through mysql_connect. I recently downloaded XAmpp and created a SQL database using my own computer as a local host and wrote a PHP file that accessed that database.
However, when I tried to change the username and host settings so that Xampp would run the PHP files that would then connect to the external SQL database of a free hosting site that I'm trying to use (biz.co.nf), I got the following error message:
Warning: mysql_connect(): Connection refused in /Applications/XAMPP/xamppfiles/htdocs/...
Right now I'm thinking either:
My login credentials to that database are incorrect, or
Somehow the host blocks SQL accesses from external users, but if I were to load my php code into the server using FileZilla, it should work okay?
Please let me know if this is the case.
I'm also fairly certain I have the right login credentials.
Here's what I have:
$host = "fdb13.biz.nf";
$username = "1764941_login";
$password = ________;
$db_name = "1764941_Login";!
$tbl_name = "Members";
//Connect to server
mysql_connect($host, $username, $password)or die("Cannot Connect!");
mysql_select_db($db_name)or die("Cannot select Database");
with my server settings according to my website...
Of course, I omitted my password.
Connections are refused from remote locations to MySQL for security reasons. You can add your IP Address to enable MySQL to listen from your database by following the instructions in this link - Cyberciti biz tips on enabling remote access to mysql database server In your case, add the IP address of the location where your PHP script.
I would also say that
mysql_connect
is deprecated in PHP. Use mysqli instead. Check the comments in the link - PHP original MySQL API
I'm just an amateur when it comes to PHP programming and I was hoping if you could help me in this problem of mine. I just started learning php last week and my problem is that i cant find a way on how to connect to a server computer's local host. basically the code in my global.php is this:
<?php
error_reporting(0);
session_start();
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "carlog";
mysql_connect($dbhost, $dbuser, $dbpass) or die (mysql_error());
mysql_select_db($dbname) or die (mysql_error());
?>
so i'm connected to the server computer via a viritual router. I was hoping that the client computer could connect to the server's local host. I tried changing the "localhost" to the server's IP add but nothing happened. the client computer wasn;t able to connect to the server (only to its own localhost)
I would be really happy if anyone of you could help me in this problem of mine. thank you
Which version of PHP are you running? This method is deprecated. I recommend you to check out how to set-up a PDO connection. http://php.net/manual/en/pdo.connections.php
Try ping in your terminal and check if you can reach it. Use this IP in your code.
Hiding all the errors isnt helping you debugging your issue.
Use:
error_reporting(E_ALL);
and check the output
First of all, if any errors are being thrown right now, the error_reporting(0); at the top is preventing the system from telling you what it's seeing.
Secondly, as others have pointed out, please try and see if you can use PDO or MySQLi instead of the mysql_connect() family, as it is indeed deprecated and scheduled for removal from PHP in the future.
Thirdly, ensure that MySQL server is running on the default port of 3306, has no firewall closing it off, is running, etc, on the remote server :)
Okay, this is something I've been banging my head on for a few weeks now, so just bear with me. When I set up my postgres database, I wanted to connect to it only through the local Unix socket. Just like how I set up my MySQL database, and subsequent PHP scripts I wrote for it. Thus, the bottom of my pg_hba.conf file looked like this:
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all md5
# IPv4 local connections:
#host all all 127.0.0.1/32 md5
# IPv6 local connections:
#host all all ::1/128 md5
I was, and still am, able to interface with my db via the psql command-line utility, and do anything I need to do without issue. However, when I tried to write a simple PHP script to connect to the db, it would always fail:
<?php
$dbhost = "localhost";
$dbname = "postgres";
$dbuser = "postgres";
$dbpassword = "####";
$conn_string = "host=$dbhost dbname=$dbname user=$dbuser password=$dbpassword";
$dbconn = pg_connect($conn_string);
if($dbconn)
{
echo "<p>Win!</p>";
}
else
{
echo "<p>Connect failed!</p>";
exit();
}
pg_close($dbconn);
?>
I was absolutely dumbstruck for weeks as to why PHP wouldn't establish a simple Unix socket connection, especially after writing quite a few scripts with the mysqli library for my MySQL db that used local connections! However, I finally un-commented the line associated with the loop-back address, and like magic it suddenly worked! This indicates to me that when 'localhost' is passed as the host parameter of pg_connect, it uses the loop-back interface, rather than just the local Unix socket.
It's not the end of the world, but is there a way to use PHP in a way so that it connects via a local Unix socket, or does it not support this type of connection with postgresql?
Set host in the connection string to the value of the directory that holds the unix socket.
So if the socket is at /var/run/postgresql/psql.sock then try:
pg_connect("host=/var/run/postgresql dbname=..etc...");
Other comments on the PHP docs page for pg_connect indicate that you can also leave out the host key/value completely and it will work - but I haven't tried.:
http://php.net/manual/en/function.pg-connect.php
Following the post on Connecting mysql remotely via free host,
the remote access still fails.
My PHP script is stored in Xtreemhost and intends to access the MySQL database on FreeMySQL.net. If FreeMySQL.net supports remote accesss to MySQL databases (which is true), there should be no problem with my PHP script run on another free web hosting server.
Can anyone tell me what is wrong with my remote access to my MySQL database?
Thank you.
<?php
// $server = "example.com";
$server = "sql.example.com";
$username = "tinfanide";
$password = "MyPassword";
$database_name = "tinfanide";
$connect = mysql_connect($server,$username,$password) or die(mysql_error());
mysql_select_db($database_name,$connect);
mysql_close($connect);
?>
The crux of the problem of mine is that I thought I had a SQL server which allows external connection but actually not. Now I've applied for a SQL server that supports the feature and the problem with the PHP script is resolved.