我怎样才能使用PHP中的两个不同的服务器的MySQL数据库? [英] How can I use MySQL database from two different server in PHP?
问题描述
我的问题可能是混淆,所以让我试着解释更多。
请参阅我将使用两个域。 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屋!