Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect MySQL database to several domains

I have two websites with different domains and I have to share data between the two, such as user registered on first website will be login on the second website too.

Is it possible to connect the second website to the first website's MySQL database? I.e. I have two websites www.example1.com and www.example2.com. Is it possible for example2.com to connect to database of example1.com?

I use the following PHP code which works fine for example1.com but not on example2.com:

    $conn =mysql_connect("HOST","USERNAME","PASS") or die("cannot connect with db").mysql_error();
    $dbname=mysql_select_db("DATABASE_NAME") or die("Error selecting db").mysql_error();

On example2.com, the connection fails with error message

cannot connect with db....

Could you please explain me what is the configuration required for such type of connection and what are the things which may stop this connection?

like image 909
Rajendra Khabiya Avatar asked Jun 05 '13 14:06

Rajendra Khabiya


2 Answers

if the 2 domains are hosted in the same server then no problem at it all you may connect with the database with as much scripts as you want .
if they are in different servers from your database server you can do this but you have allow remote connection for each script IP

like image 70
Dr.Neo Avatar answered Oct 12 '22 13:10

Dr.Neo


It depends on where you host your database and your website. Some webhostings allow only local database connection. So make sure that either both websites are on the same server or that your settings allow you to connect from your PHP code to the remote database and the database allows remote connection.

If you have command-line access to your webserver, check that you can connect using mysql command - see this answer.

like image 44
Mifeet Avatar answered Oct 12 '22 13:10

Mifeet