我怎样才能使用PHP中的两个不同的服务器的MySQL数据库? [英] How can I use MySQL database from two different server in PHP?

查看:155
本文介绍了我怎样才能使用PHP中的两个不同的服务器的MySQL数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在后端使用PHP和MySQL制作一个web应用程序。我想知道如何使用两台MySQL服务器来获取数据。因为我打算为应用程序使用两个域。所以,当我的第一个SQL数据库将达到其极限,我将需要使用另一台服务器。那么我怎样才能从所有服务器获取数据?我将使用用户注册和登录系统,然后其他文件信息和所有。那么对我有什么建议?你认为我应该使用单个服务器上的用户细节和其他服务器上的文档的其他细节吗?

我的问题可能是混淆,所以让我试着解释更多。

请参阅我将使用两个域。 Domain1和Domain2



人们可以从两个域注册来访问这两个应用程序。 (如谷歌的应用程序,例如gmail,youtube,adsense,adwords和所有)

Domain1将缩短网址,Domain2将允许管理文件。 Domain2也会生成缩短的链接来下载这些文件。所以人们可以从Domain1上传文件,并生成缩短的网址,例如Domain1 / f / 123456。类似的东西,所以任何建议的应用程序?

解决方案

您可以轻松使用两个(或更多)应用程序中的一个数据库。您只需要主机名即可从任何地方访问数据库。例如,如果你的数据库主机是whatever.secureserver.net,只要把它放在连接参数中就可以了:

  $ connection = mysql_connect ('whatever.secureserver.net','username','password'); 

您可以在两个三,四个等..访问相同数据库的不同应用程序但是,您必须确保数据库允许远程连接,如果应用程序对于数据库来说是远程的。



如果要使用两个数据库(db1和db2在这个例子中)在一个应用程序中,您可以执行以下操作:

如果数据库在同一台服务器上:

  $ connection1 = mysql_connect('whatever.secureserver.net','username','password'); 
$ db1_selected = mysql_select_db('db1',$ connection1);
$ db2_selected = mysql_select_db('db2',$ connection1);

如果数据库位于不同的服务器上:

  $ connection1 = mysql_connect('whatever.secureserver.net','username','password'); 
$ connection2 = mysql_connect('somethingelse.secureserver.net','username','password');
$ db1_selected = mysql_select_db('db1',$ connection1);
$ db2_selected = mysql_select_db('db2',$ connection2);

当然,处理数据库连接有一个更好的方法,但我选择了这个详细的答案,明白地为你拼写。


I am trying to make a webapp using PHP and MySQL in backend. I want to know that how can I use two MySQL servers to fetch data. Because I am going to use two domains for apps. So when my first sql database will get to its limit, I will need to use another server. So how can I fetch data from all servers? I am gonna use user signup and login system and then other their document information and all. So any suggestion for me? Do you think that I should use user details on a single server and other details of documents on another server?

My question might be confusing so let me try to explain more.

See, I am going to use two domains. Domain1 and Domain2

People will be able to register from both domains to access both apps. (like google's apps e.g. gmail, youtube, adsense, adwords and all)

Domain1 will make shorten urls, Domain2 will allow to manage files. And also Domain2 will generate shorten link to download those files. so people can upload files from Domain1 and it will generate shorten urls like Domain1/f/123456. Something like that, so any suggestion for apps?

解决方案

You can easily use one database from two (or many more) apps. You just need the host name to access the database from anywhere. For example if your database host is whatever.secureserver.net just put that in the connection parameters like so:

$connection = mysql_connect('whatever.secureserver.net', 'username', 'password');

You can do that in two three, four ..etc. different apps accessing the same database. You do however have to make sure that the database allows remote connections if the apps are remote in relation to the database.

If you want to use two databases (db1 and db2 in this example) in one app you can do the following -

If the databases are on the same server:

$connection1 = mysql_connect('whatever.secureserver.net', 'username', 'password');    
$db1_selected = mysql_select_db('db1', $connection1);
$db2_selected = mysql_select_db('db2', $connection1);

If the databases are on different servers:

$connection1 = mysql_connect('whatever.secureserver.net', 'username', 'password');  
$connection2 = mysql_connect('somethingelse.secureserver.net', 'username', 'password');    
$db1_selected = mysql_select_db('db1', $connection1);
$db2_selected = mysql_select_db('db2', $connection2);

There is a more elegant way of handling database connections of course but I chose this verbose answer so it is plainly spelled out for you.

这篇关于我怎样才能使用PHP中的两个不同的服务器的MySQL数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆