简体   繁体   English

我可以使用MySQL中的两个数据库执行查询吗?

[英]Can I execute query using two databases in MySQL?

I've never tried this but I'm currently considering database design and wanted to know if its possible to run SQL Queries against two separate MySQL Databases on the same or different Server/Cluster? 我从来没有尝试过这个,但我正在考虑数据库设计,并想知道是否可以在相同或不同的服务器/集群上针对两个单独的MySQL数据库运行SQL查询?

Also what are speed issues with these types of configurations (cross database)? 这些类型的配置(交叉数据库)的速度问题是什么?

Yes , why not, you can use same query for different MySql Database, all you need is their authentication to access that particular database. 是的 ,为什么不,您可以对不同的MySql数据库使用相同的查询,您只需要对其进行身份验证即可访问该特定数据库。 I mean to say, the username and password for each database. 我的意思是说,每个数据库的usernamepassword Like in php we use these script to connect our database. 像在php我们使用这些脚本来连接我们的数据库。

<?php
$hostname = 'localhost';  //server       
$dbname   = 'myphp';   //database
$username = 'root';             //username
$password = '';                 //password
mysql_connect($hostname, $username, $password) or DIE('Connection to host is failed, perhaps the service is down!');
mysql_select_db($dbname) or DIE('Database name is not available!');
?>  

So, you need to create different connection script and change them accordingly. 因此,您需要创建不同的连接脚本并相应地更改它们。 As you see,in my connection script hostname is the server location (generally it is an IP address, where your server is), currently it is set to localhost (you can change it to 127.0.0.1), since my computer itself serve as a server. 如您所见,在我的连接脚本中, hostname是服务器位置(通常是服务器所在的IP地址),当前它设置为localhost (您可以将其更改为127.0.0.1),因为我的计算机本身就是一台服务器。 So, you need to change the server details only (if database, username and password are same for all). 因此,您只需要更改服务器详细信息(如果所有数据库,用户名和密码都相同)。

Now, coming to next part of your question ie speed issue, so i would say, it depends on the server, how much time it takes to respond. 现在,进入问题的下一部分即速度问题,我想说,这取决于服务器,响应需要多长时间。 There are many factors (like number of user, server respond time and etc) that affects the speed issue. 有许多因素(如用户数,服务器响应时间等)会影响速度问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM