将MySQL数据库连接到多个域

时间:2021-05-12 11:14:58

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?

是否可以将第二个网站连接到第一个网站的MySQL数据库?即我有两个网站www.example1.com和www.example2.com。 example2.com是否可以连接到example1.com的数据库?

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

我使用以下PHP代码,它适用于example1.com但不适用于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

在example2.com上,连接失败并显示错误消息

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?

能否请您解释一下这种连接所需的配置是什么,可以阻止这种连接的是什么?

2 个解决方案

#1


3  

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

如果2个域都托管在同一个服务器上,那么就没有任何问题,您可以使用尽可能多的脚本连接数据库。如果它们与数据库服务器位于不同的服务器中,则可以执行此操作,但允许每个脚本IP进行远程连接

#2


1  

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.

这取决于您托管数据库和网站的位置。某些虚拟主机只允许本地数据库连接。因此,请确保两个网站位于同一服务器上,或者您的设置允许您从PHP代码连接到远程数据库,并且数据库允许远程连接。

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

如果您具有对Web服务器的命令行访问权限,请检查是否可以使用mysql命令进行连接 - 请参阅此答案。

#1


3  

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

如果2个域都托管在同一个服务器上,那么就没有任何问题,您可以使用尽可能多的脚本连接数据库。如果它们与数据库服务器位于不同的服务器中,则可以执行此操作,但允许每个脚本IP进行远程连接

#2


1  

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.

这取决于您托管数据库和网站的位置。某些虚拟主机只允许本地数据库连接。因此,请确保两个网站位于同一服务器上,或者您的设置允许您从PHP代码连接到远程数据库,并且数据库允许远程连接。

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

如果您具有对Web服务器的命令行访问权限,请检查是否可以使用mysql命令进行连接 - 请参阅此答案。