In MySQL, I have two different databases -- let's call them A and B.
Database A resides on server server1, while database B resides on server server2.
Both servers {A, B} are physically close to each other, but are on different machines and have different connection parameters (different username, different password etc).
In such a case, is it possible to perform a connection between a table that is in database A, to a table that is in database B of different servers?
If so, how do I go about it, programatically, in php? (I am using php, MySQLDB to separately interact with each one of the databases).
The only way I can think of, is by opening 2 separate connections (i.e. instantiate 2 PDO objects) with all the different parameters, use 2 queries to query all the data you need into PHP, and then work with that on PHP.
You can make two separate MySQL connections in PHP, do two queries to the two tables, and then work with the results in PHP.
Another option, since the servers are physically close, is to setup the one or both servers to replicate the needed database/tables to each other. You can look here for more on MySQL replication:
http://dev.mysql.com/doc/refman/5.6/en/replication.html
Related
How can I connect two tables from different databases using PostgreSQL? My first database is called "payments_details" and my second one is called "insurance". Also I want to display and highlight the id's they don't have in common using php, is that possible?
Databases are isolated from each other, you cannot access data from different databases with one SQL statement. That is not a bug, but a design feature.
There are three ways to achieve what you want:
Don't put the data in different databases, but in different schemas in one database. It is a common mistake for people who are more experienced with MySQL to split up data that belong to one application in multiple databases and then try to join them. This is because the term database in MySQL is roughly equivalent to what in (standard) SQL is called a schema.
If you cannot do the above, e.g. because the data really belong to different applications, you can use the PostgreSQL foreign data wrapper. This enables you to access tables from a different database (or even on a different machine) as if they were local tables. You'll have to write your statements more carefully, because complicated queries can sometimes be inefficient if large amounts of data have to me transferred between the databases.
You can use dblink, which is an older and less comfortable interface than foreign data wrappers, but can allow you to do things that you could not do otherwise, like call a remote function.
I was wondering does anyone know if it is possible in Zend to run one query which will connect to two different databases on two different servers and combine the results in one result set?
You can federate one database in the other one, via a wrapper. You reference table via nicknames, and then you execute a query normally, as both tables were in the same database (joining, sorting, etc.)
Federation is free between DB2 databases or with Informix (because is from IBM). If you want to federate another data source (Oracle, Excel, flat files), you have to buy that separately.
With federation, you do not need to do the join at application level, but at database level.
I'm currently coding a php website. however one of the requirement is to have it connect to two database concurrently and does the same action on the two database. so if one fail, the another will continue. Is it possible for this to work? server in php with mysql
You should look into replication
I am using one db file to connect to the database. However, i need to show something else from another db database on this site. I can't use two db connect files. So, how do i go about display one websites content on another website with two different connections.
For one thing, you don't need separate database connections to the same server. Simply specify the database name before each table name in your SQL statements. For example:
SELECT foo, bar FROM db.table;
rather than:
SELECT foo, bar FROM table;
For another, you shouldn't be storing the database connection in a global for just the sort of reason you're running into. I suspect you're using the outdated mysql extension, which uses an implicit DB connection resource whenever one isn't explicit. Switch to PDO and create a simple connection manager class. One big advantage to PDO is it supports prepared statements, which can be safer and more performant than what the mysql extension provides. If you need a PDO tutorial, try "Writing MySQL Scripts with PHP and PDO"
If it is a separate db server you might need to look into the mysql federated engine which allows you to link to databases on separate servers as if they were on the same host.
I'm wondering how slow it's going to be switching between 2 databases on every call of every page of a site. The site has many different databases for different clients, along with a "global" database that is used for some general settings. I'm wondering if there would be much time added for the execution of each script if it has to connect to the database, select a DB, do a query or 2, switch to another DB and then complete the page generation. I could also have the data repeated in each DB, I just need to mantain it (will only change when upgrading).
So, in the end, how fast is mysql_select_db()?
Edit: Yes, I could connect to each DB separately, but as this is often the slowest part of any PHP script, I'd like to avoid this, especially since it's on every page. (It's slow because PHP has to do some kind of address resolution (be it an IP or host name) and then MySQL has to check the login parameters both times.)
Assuming that both databases are on the same machine, you don't need to do the mysql_select_db. You can just specify the database in the queries. For example;
SELECT * FROM db1.table1;
You could also open two connections and use the DB object that is returned from the connect call and use those two objects to select the databases and pass into all of the calls. The database connection is an optional parameter on all of the mysql db calls, just check the docs.
You're asking two quite different questions.
Connecting to multiple database instances
Switching default database schemas.
MySQL is known to have quite fast connection setup time; making two mysql_connect() calls to different servers is barely more expensive than one.
The call mysql_select_db() is exactly the same as the USE statement and simply changes the default database schema for unqualified table references.
Be careful with your use of the term 'database' around MySQL: it has two different meanings.