Errors connecting to MySQL database from alternate domain - php

I have two domains on two separate servers, foo.com and bar.com.
I have a website and MySQL database setup on foo that I want to migrate to bar, but bar doesn't have MySQL.
As a solution, I'm moving all the files across but leaving the database on foo, and connecting to it remotely.
I'm currently connecting like so:
connect.php
$hostname = 'database.foo.com';
$username = 'username';
$password = 'password';
$dbname = 'database';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
}
catch(PDOException $e){
echo($e->getMessage());
}
This works fine on foo.com but when I migrate this file to bar.com I get this error:
SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'database.foo.com' (110)
I can have the two sites, foo.com and bar.com open in two windows, and one will work while the other doesn't - despite the fact that they are both connecting (or trying to connect) to the same database.
Why is this happening and how can this be rectified?

The database server is not configured to listen on a network port accessible to the other server.
There are several reasons why this might be.
It is typical to only listen on local sockets and not use TCP/IP at all since that is more efficient and secure.
The port might be firewalled off from the other machine.
The server might only be accessible on the local network and foo.com might be a different network with no route from bar.com to database.foo.com.

suppose you have database on server B and website on server A(say it has IP 192.234.23.12)
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.23.12

You need to enable the Remote MySQL access on foo.com by going to,
foo's cpanel > databases > remote mysql, and adding the bar's domain to white list it.
That should work.

Related

Can I use my mysql database on one host and my server files on another?

I have rented a hosting provider which gives me MySQL database. I would like to locally connect to that database using PHP. Please see below $dbServer property.
$dbServer = "localhost"; // I'm talking about this one
$dbUsername = "username";
$dbPassword = "blabla";
$dbName = "db_name";
How I could connect to that MySQL? What kind of URL should I put?
Let's say my domain from hosting company is - www.randomdomain.tld
Can I somehow change localhost to an actual host link so it would work on any website?
Maybe. You would have to ensure all of these are true:
MySQL is configured to listen on a network interface you can route to from the other computer. If it only listens on localhost (with the loopback network interface or a UNIX socket) then it won't be accessible.
The network port it listens to must not be firewalled so that the other computer cannot access it.
The credentials used to connect to the database must allow access from the computer you are connecting from. (It is not usually to allow access to dbuser#localhost).
And if yes, where I should get that information?
localhost just means "this computer".
You know a hostname you can access it from because you typed it into your browser's address bar in order to access PHPMyAdmin over HTTP.
Yes, you can do that.
In CPanel there is an option called Remote MySQL. Your hosting provider must allow this option. You can find the documentation here

How can I write data to a database table on another server?

I am using the mysqli_connect($host, $user, $pass, $db) line to connect to another server through php. I am trying to write to a MySQL database.
I have used the server's IP [A.B.C.D] on the $host variable. However I cannot write to that database, and the script does not run completely.
I am using the root user and password to connect, and the user list in phpmyadmin shows that anyhost(%), 127.0.0.1 and localhost can connect with the root username (I think that's what it means). Anyway, as I am using root, I don't think this is a user privilege problem.
I have tried putting a # before 'bind-address=127.0.0.1'.
I have tried editing my.cnf according to suggestions from a lot of forums.
I have not installed firewall in any of the servers that I'm using, so unless Debian 7 64-bit comes with pre-installed firewall that blocks certain ports, I don't think that is the problem either.
Isn't there an easy way to establish connection between multiple servers? :-/
Your remote mysql server must accept remote connection from your IP address.
After you make this setup you can access remote mysql server like this:
mysqli_connect($remote_host, $remote_user, $pemote_pass, $remote_db);

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

Connect database from other computer

I'm using PHP with MySQL database. The PCs are having a network to each other. My problem is I want to connect to the MySQL database on another computer. I want to store data on that MySQL database from another computer. How could i possibly do on this? Thanks so much for any suggestions.
The MySQL server must be configured to accept connections externally, and its firewall must be configured to allow incoming connections on that port (TCP port 3306). This may or may not already be set up.
You must also account for this in the MySQL permissions as follows.
Often, when setting up your MySQL permissions, you'll set user access rights only for #'localhost'. You'll need to make sure that both the user account and its granted permissions are set for the appropriate hostname or IP address you will be connection from. For example, you could create a new authorised user with:
GRANT ALL PRIVILEGES ON somedatabase.* TO someuser#'somehostname' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
You have to do all of this before you can connect to that server remotely with PHP, using something like this:
mysql_connect('mysqlservername', 'someuser', 'password');
Point mysql_connect() to use the other computer's name / IP address:
$server = '192.168.0.3';
$user = "foo";
$password = "bar";
$conn = mysql_connect($server, $user, $password);
You'll need to make sure the DB in the other PC has enough rights to connect from a different host - i.e. your computer.
Set up MySQL as normal on that computer. Then, simply:
<?php mysql_connect('IP of 2nd computer', 'username', 'password'); ?>

Categories