How to connect to database knowing only frontend URL? - php

I have a URL from frontend access to the database.
https://db.blabla.com
Is this enough information to connect programmatically to the database? I'm trying with things like this (php):
<?php
$username = "xxx";
$password = "xxx";
$hostname = "db.blabla.com";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
?>
And it seems at least to recognize that the host exists, but it says timed out connection.
It seems to use tcp://
I also did a random test with java and jdbc:mysql:// but same problem.
I don't have an idea what the protocol and port are supposed to be... is it possible to guess it, maybe trial and error...?
Thanks
Edit: Could file ending .do for server files give a hint for the type of database used?
Edit 2: This is not a php question, I just put php example. It's about how (if possible) generally connect.

It isn't enough information. You have no idea what the backend is doing. There might be a database, there might not be. It might be MySQL, it might not be. It might be on the same host, it might not be. It might have the same passwords as the front end, it shouldn't.
Any DBA worth his salt will have configured the database so only the host running the front end (and maybe a few other authorised hosts) can access it. Any decent network admin will have ensured that those hosts are the only ones that the firewall will allow access to the database server as well.

Related

Connecting to a server's local host (PHP programming)

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 :)

Using IP address instead of 'localhost' as hostname in mysqli connection

I am perhaps being a bit overly cautious asking this and apologies if it has been but I want to be as secure as possible.
Is it just as secure to use my hosting accounts IP address instead of localhost when I connect to the database via my mysqli connection below? Would the speed of connection be dramatically affected at all?
$hostname = "localhost";
$database = "wwwcapco_crm";
$username = "wwwcapco_user";
$password = "cKUsaf#&^0";
$connect = new MySQLi($hostname, $username, $password, $database);
if ($connect->connect_errno) { echo "Failed to connect to MySQL: (" . $mysqli- >connect_errno . ") " . $mysqli->connect_error; }
The main reason I ask is because at the moment I am exporting my live website database and importing it into my local website testing enviroment (xampp). This takes ages and I just thought it may be a better idea to connect directly to the live database.
Thanks
Is it just as secure to use my hosting accounts IP address instead of localhost when I connect to the database via my mysqli connection below?
No. See the documentation:
All other information is transferred as text, and can be read by anyone who is able to watch the connection. If the connection between the client and the server goes through an untrusted network
Would the speed of connection be dramatically affected at all?
Since you are getting the data from a computer at a remote site instead of locally — yes, it will.
local website testing enviroment (xampp). This takes ages and I just thought it may be a better idea to connect directly to the live database
Risking your live data by exposing it to your test code is a very bad idea.

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.

MySQL remote access trouble?

I have a database set up on a godaddy server. It is configured to allow remote access, and there are a couple of websites I'm running which need to access this data. It works when accessed from another godaddy site, and I can connect from my development environment both at work and home. We recently set up hosting with mydomain.com.
Here is the code block that triggered it:
function connect(){
$servername = "XX.XX.XXX.XX";
$dbusername = "databaseusername";
$dbpassword = "mahpassword";
$dbname = "databasename";
try{
$newMysql = new PDO("mysql:host=".$servername.";dbname=".$dbname, $dbusername, $dbpassword);
}
catch(PDOException $e){
echo 'connection Failed: '. $e->getMessage();
die;
}
}
and now I'm getting this error message on the new site:
connection Failed: SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'XX.XX.XXX.XX' (111)
The only problems I can think of is that either for some reason there are a limited number of IP addresses the MySQL database will connect to by default (which seems squirrely), I'm getting blocked by a firewall on the MySQL server (again.. doesn't make sense to me), or there is some setting on the mydomain hosting server disallowing remote requests (?)
I'm new to this kind of thing, so I'm open to any suggestions. I could probably just set up another database on the new site, but I don't want the hassle of keeping them synchronized if I don't need to. What might be wrong? Are there any workarounds?
[edit]
connected to remote database via console (mysql -h XX.XX.XXX.XX ...), the privileges were found under the information_schema database, a quick select * from SCHEMA_PRIVILEGES and select * from USER_PRIVILEGES shows that 'databaseusername'#'%' has sufficient privileges. Not that it helped me any, but maybe it'll help someone down the road.
[/edit]
As it has been more than a year since I asked this question, I suppose I need to answer it just to close it.
It turns out that godaddy had blocked mydomain.com servers via firewall ("Remote access" was limited). so in order to accomplish what I wanted to do, I had to copy and store the database on both sites.

how to connect to database on another server

Could I have my php scripts on server A and connect to the MySQL database on server B?
If yes, how it would be done? Thanks in advance
its simple all thise above techniques are quite complicated
suppose you have database on server B and website on server A(say it has IP 192.234.12.1)
on cpanel whitelist the IP of server B
and create a new user having sufficient privileges in database (say this user is test)
then create this user as test#192.234.12.1
Yes.
The same way you access the localhost on the same server, you change the database host to the external one. This is more a configuration issue, you need to grant your database user remote access to your MySQL, you also need to make sure your firewall allows connections on the MySQL port.
Example on Debian: http://www.debianhelp.co.uk/remotemysql.htm
Yes it can be done.
Find out the IP address of the server A where your scripts will be uploaded. Do not forget to change the localhost to the ip address of the Server B in mysql_connect() or mysqli_connect() method.
Now go the control panel of the Server B where your Database is.
In the control panel's Homepage go the databases section and click the Remote MYSQL option.
Then add the Ip address of the Server A and click on add host.
Now you can access to the database in Server B while your scripts are running in Server A.
Mind you the fetched result will be slow cause it is getting data from database that is located on another server.
Your welcome
Just don't the hostname of the other box for the connection. Details depend on the extension you're using:
$mysql = mysql_connect($host, $user, $pass);
$mysqli = new mysqli($host, $user, $password, $schema);
$pdo = new PDO("mysql:host=$host", $user, $pass);
Make sure that the user is allowed to access by the MySQL server (CREATE USER) and check that there's no firewall in the way.
That is all what you need .
(Even you can have your scripts on server A, your web server on server B and your database on server C ...)
Have a look here:
http://us2.php.net/manual/en/function.mysql-connect.php
You can either pass in the server hostname as an argument, or configure in php.ini.
I was having similar challenges but here is what work for me:
To connect to server B from server A, First, you need to allow remote MySQL access hosts in cPanel (Server B), Home -> Databases -> Remote MySQL and also whitelist the IP in the firewall (That is IP Address of B server). Then the following php db connection should work.
$db_connect = mysqli_connect("serverB.com", "dbuser", "dbpassword", "dbname");
// Evaluate the connection
if (mysqli_connect_errno()) {
echo mysqli_connect_error();
exit();
}else{
//successful connection
echo "Yes, successful";
}
Its a perfect solution for connecting another database from other servers.
$dbserverName = "191.238.0.2";
$dbUsername = "lauranco_L2L";
$dbPassword = "SL92TIW5T96L";
$dbName = "lauranco_siteBits";
Good old thread.
Still - of all the answers appearing here, nothing addresses about the security.
It is highly insecure to open up the mysql port to outside the server.
The most secure option is to keep the mysql port open to one and only localhost in all servers.
And have another php running inside the second server, make it create the desired output and deliver the same to your php (running in the first server).

Categories