Cross-database association query method in MySQL

Cross-database association query method in MySQL

Business scenario: querying tables in different databases

For example, the tables to be associated are: Table A in database A on machine A && Table B in database B on machine B.

In this case, it is impossible to execute "select A.id,B.id from A left join B on ~~~;", but the business requirements are immutable and the database design is immutable, which is a pain in the ass. .

Solution: Create a table B in database A on machine A. . .

I'm not kidding you, we use the method of building tables based on MySQL's federated engine.

Example of table creation statement:

CREATE TABLE `table_name`(......) ENGINE = FEDERATED CONNECTION = 'mysql://[username]:[password]@[location]:[port]/[db-name]/[table-name]'

Prerequisite: Your MySQL must support the federated engine (execute show engines; to see whether it is supported).

If there is a FEDERATED engine, but Support is NO, it means that your MySQL has installed this engine but has not enabled it. Add a line of federated to the end of the my.cnf file and restart MySQL.

If there is no FEDERATED line at all, it means that your MySQL engine is not installed, and you can't play happily. It's best to ask your operation and maintenance to fix it, because the next step is relatively large, and I don't know how to do it;

Explanation: The table created by the FEDERATED engine only has a local table definition file, and the data file exists in the remote database. This engine can be used to implement remote data access functions similar to DBLINK under Oracle. That is to say, this table creation method will only create a table structure file of Table B in Database A. The table index, data and other files are still in Database B on Machine B. It is equivalent to just creating a shortcut to Table B in Database A.

So, the pain in the balls is gone. .

A few points to note:

1. The local table structure must be exactly the same as the remote one.

2. The remote database is currently limited to MySQL

3. No support for transactions

4. Table structure modification is not supported

Other netizens added:

CREATE TABLE IF NOT EXISTS `logintoken` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`type` char(1) NOT NULL DEFAULT '0',
`loginName` varchar(20) DEFAULT NULL,
`token` varchar(2000) DEFAULT NULL,
`tokenExpiredTime` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)ENGINE=FEDERATED CONNECTION='mysql://root:[email protected]:3306/zysso/logintoken';

To use the remote 5.12 logintoken table, you only need to enable FEDERATED yourself. 5.12 does not need to enable it.

You may also be interested in:
  • Example of cross-database query in MySQL
  • Example of MySQL remote cross-database joint query

<<:  Vue project code splitting solution

>>:  How to use yum to configure lnmp environment in CentOS7.6 system

Recommend

Quick understanding and example application of Vuex state machine

Table of contents 1. Quick understanding of conce...

Example code for CSS pseudo-classes to modify input selection style

Note: This table is quoted from the W3School tuto...

Detailed tutorial on installing CUDA9.0 on Ubuntu16.04

Preface: This article is based on the experience ...

How to build a deep learning environment running Python in Docker container

Check virtualization in Task Manager, if it is en...

How to design a web page? How to create a web page?

When it comes to understanding web design, many p...

Mysql queries the transactions being executed and how to wait for locks

Use navicat to test and learn: First use set auto...

Webservice remote debugging and timeout operation principle analysis

WebService Remote Debugging In .NET, the remote d...

Common methods of Vue componentization: component value transfer and communication

Related knowledge points Passing values ​​from pa...

Complete steps to quickly configure HugePages under Linux system

Preface Regarding HugePages and Oracle database o...

How to view the database installation path in MySQL

We can view the installation path of mysql throug...

Full analysis of MySQL INT type

Preface: Integer is one of the most commonly used...

A brief discussion on the role and working principle of key in Vue3

What is the function of this key attribute? Let’s...

MySQL's conceptual understanding of various locks

Optimistic Locking Optimistic locking is mostly i...

Example of how to create and run multiple MySQL containers in Docker

1. Use the mysql/mysql-server:latest image to qui...

The pitfalls and solutions caused by the default value of sql_mode in MySQL 5.7

During normal project development, if the MySQL v...