2017-06-20 95 views
-5

我使用两个域名('domain1','domain2')和2个服务器('服务器A'和'服务器B')。 'domain1'是主域。 'domain2',我用作后端,用于存储数据。所以数据将被存储在服务器B中。当我在浏览器中使用'domain1'时,它将显示存储在'服务器B'中的数据。我没有使用'服务器A'。如何在不同服务器上连接数据库?

我已经做了一些代码。请通过它,让我知道我必须做的纠正。代码如下,

//these hostname,dbname,user and password from server B,(is this correct?) 
$hostname = "1.1.1.1" ; 
$database = "db_B"  ; 
$user  = "u_B"  ; 
$pasword = "u_pas"  ; 
$conn = mysqli_connect($hostname,$user,$pasword,$database) ; 
if (!$conn) 
{ 
    die ("Error connecting to the database because : " . 
    mysqli_error($conn)) ; 
} 
+0

尝试用服务器B的IP地址和默认端口3306 –

+4

[连接到远程MySQL服务器使用PHP]的可能的复制(https://stackoverflow.com/questions/1935314/connecting -to-remote-mysql-server-using-php) –

+0

了解Rest API ...它是两台服务器连接的安全方式.. – pAsh

回答

0
In domain1 config.php file i have written the following code. It's working for me. 

$servername = "domain2.in" ; 
$database  = "db_B"   ;//server B's database name 
$user   = "u_B"   ;//server B's database's(db_B) user name 
$pasword  = "u_pas"  ;//server B's database's(db_B) password 

$conn = mysqli_connect($servername,$user,$pasword,$database) ; 
if (!$conn) 
{ 
    die ("Error connecting to the database because : " . 
    mysqli_error($conn)) ; 
} 

after this, add "server A"'s Remote MySQL hostname in "server B"'s Remote MySQL. 
相关问题