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).
Related
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.
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);
I want one website on one server and another website on another server, but only 1 database for both using php and mysql.
is it possible? if yes then how?
Yes. its possible. Normally you dont put your db on distant server. You should keep the db server on same data center so you avail high speed internal network link.
If your websites are in host web1 and web2 and database is in dbhost1, then in web1 and web2 connect to mysql with dbhost1 as host name.
mysqli
$mysqli = new mysqli("dbhost1", "user", "pass");
PDO
$dbh = new PDO('mysql:dbname=dbname;host=dbhost1', "user", "pass");
Legacy mysql extension
mysql_connect("dbhost1", "user", "pass");
Note: Make sure in the database user#web1 (on host web1) or user#web2 (on host web2) has access.
You can connect to a remote database given the correct settings.
Failing that, you could write a service layer that could be called from both servers.
while connecting to mysql database use same database connection credential on both servers.
Yes why not. But for security and performance issues i wouldn't do that.
The question is what do you understand under on another server is the server direct connected over LAN or is the server in another Data center. When the server is in another Data center you can get a lot of traffic and performance issues.
When the server is direkt connected you can change the my.cnf and change the line:
bind-address 127.0.0.1
then the server is reachable from outside. And you should give the user enough rights to connect from outside.
yes, it's possibile.
you have to use a mysql server which is reachable from both servers. and you simply connect with the same host/user/pass from both servers:
define("WEBDOMAIN", "94.145.22.15"); //some fake data
define("DEFDBNAME", "my_db");
define("DEFUSERNAME", "my_user");
define("DEFDBPWD", "my_pass");
mysql_connect( WEBDOMAIN, DEFUSERNAME, DEFDBPWD );
mysql_select_db( DEFDBNAME);
That should be enaugh. You coud have some problems if your hosting providers doesn't allow servers to connect to external servers (sometimes ports are closed).
For security reasons it is common, that You can't connect to a mysql server that has not authorized your hosting server.
So in mysql, You have to allow a user to connect not only from localhost, but also from other host (if You have enought priviliges, You can do it from phpMyAdmin by editing user priviliges afair).
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.
Ok, If you can answer this question, you deserve the nobel peace prize. Anyways, here's the situation.
I'm using Slicehost dot net, and I have 2 slices (two different IPs). One slice is my mail server and the other is my normal website. I'm running Ubuntu (8.04 or 8.10, something like that, it shouldn't matter). What I'm trying to do is access the MySQL database on the Mail server from the other slice. I'm trying to do that with PHP. I really appreciate any help.
mysql_connect()
$resource = mysql_connect('other-server-address.com', 'username', 'password');
The first parameter is the mysql server address.
Server Param
The MySQL server. It can also include
a port number. e.g. "hostname:port" or
a path to a local socket e.g.
":/path/to/socket" for the localhost.
If the PHP directive
mysql.default_host is undefined
(default), then the default value is
'localhost:3306'. In SQL safe mode,
this parameter is ignored and value
'localhost:3306' is always used.
Unless I'm misunderstanding... this setup is pretty common. Any trouble you're having might be related to the following:
Firewall settings
Grant access to the mysql user to connect from the other host
my.ini settings not allowing outside connections
Some other related SO questions:
Connecting to MySQL from other machines
How do I enable external access to MySQL Server?
php access to remote database
How to make mysql accept connections externally
Remote mysql connection
Assuming your mail server is at IP 192.168.1.20 and web server is 192.168.1.30
First of all you need to allow the web server to access the mysql database on your Mail server .
On 192.168.1.20 you run the mysql command and grant access on the database needed to your web server
mysql> grant all on mydb.* to 'someuser'#'192.168.1.30' identified by 'secretpass;
Your PHP code connects to that database with something like:
$conn = mysql_connect('192.168.1.20', 'someuser', 'secretpass');
mysql_connect() returns a link identifier if the connection is successful. Also you have to do is keep the references to both links.
When you want to use which ever link, simply include the link as an argument.
$link1 = mysql_connect($host1, $username1, $password1);
$link2 = mysql_connect($host2, $username2, $password2);
$r = mysql_query(QUERY, $link1);
Simple as that.