Home Database Mysql Tutorial 使用mysql federated引擎构建MySQL分布式数据库访问层_MySQL

使用mysql federated引擎构建MySQL分布式数据库访问层_MySQL

Jun 01, 2016 pm 01:30 PM
app Database access Connect to the database

bitsCN.com

使用mysql federated引擎构建MySQL分布式数据库访问层

 

本文所描述的 federated属于 MySQL的一种特殊引擎,利用它可将本地数据表映射至远程 MySQL 数据表,从而就可以解决应用程序中繁多的跨机器连接数据库问题,拓扑图如下:

使用mysql federated引擎构建MySQL分布式数据库访问层_MySQL

如此就可以构造出一个统一的数据访问入口,就大大提高了整个数据库系统的可维护性。 Federated引擎是基于表级别的,只能将本地数据表定义为 Federated 引擎并映射至远程实体表,无法实现基于库级别的整体映射。

 

在本文中,我们将启用Federated 引擎的数据库访问入口服务器称为本地数据库,而将本地数据表对应的远程数据表,称之为实体表。

 

本地数据库需要启用Federated 引擎支持,而远程数据表无须 Federated 引擎支持。 Federated 引擎表使用标准的 MySQL 客户端协议与远程数据库建立 TCP 连接。

 

创建Federated 表的过程:

 

 以root 登录远程 MySQL ,上创建合适的访问账号 grant all on DB1.* to 'federated'@'%' identified by 'federated'; flush privileges;

在远程MySQL 找到对应实体表的创建命令(如果是新表,请先建立好数据表,再执行此命令) 假设在远程mysql 上有库名 DB1, 表名 tag, 执行以下命令找到远程表的结构: show create table DB1.tag 输出: CREATE TABLE tag ( id int(10) unsigned NOT NULL AUTO_INCREMENT, name varchar(128) NOT NULL, frequency int(10) unsigned NOT NULL DEFAULT '1', PRIMARY KEY (id) ) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8

 

假设我们要将远程的DB1.tag 映射至本地 DB.TableA 表上。那么我们应该保持本地虚拟表与远程实体表结构一致(结构可以有所差异,但会造成使用,管理上的麻烦)。根据远程实体表的创建命令,创建本地虚拟表 ( 结构部分完全一样,创建表选项有所差异 ) :

 

登录本地Mysql 服务器,创建相应的数据库及表: create database DB; use DB; CREATE TABLE TableA ( id int(10) unsigned NOT NULL AUTO_INCREMENT, name varchar(128) NOT NULL, frequency int(10) unsigned NOT NULL DEFAULT '1', PRIMARY KEY (id) ) ENGINE=federated connection="mysql://federated:federated@127.0.0.1 :3306/DB1/tag";

 

这时,即建立好了federated 虚拟表,实际上本地 MySQL 只创建了表定义文件 , 而没有数据文件。我们对本地虚拟表的数据修改,均会发送到远程机器上执行。

 

本地虚拟表名与远程表名,可不相同。

 

经过测试,这个引擎的一些额外特点: 1. 本地虚拟表与远程实体表之间是 TCP 长连接,并且是多个客户端利用的。所以不用担心因频繁建立连接带来的网络开销。 2. 本虚拟表表与远程实体表之间的网络连接断开后,当对虚拟表发起查询时,它会尝试重新连接远程实体表,所以我们不用担心网络连接断开造成的永久中断问题。 3. 如果无时间未对本地虚拟表作任何操作,虚拟表与实体表之间的连接将在远程主机的 wait_timeout 秒后自动断开,当对虚拟表发起查询时,连接又会重新建立。

 

一些注意事项: 1. 对本地虚拟表的结构修改,并不会修改远程表的结构 2.  truncate 命令,会清除远程表数据 3. drop命令只会删除虚拟表,并不会删除远程表 4. 不支持 alter table 命令 目前使用federated 最大的缺点: 1. select count(*), select * from limit M, N 等语句执行效率非常低,数据量较大时存在很严重的问题,但是按主键或索引列查询,则很快,如以下查询就非常慢(假设 id 为主索引) select id from db.tablea where id >100 limit 10 ; 而以下查询就很快: select id from db.tablea where id >100 and id

 

如果虚拟虚拟表中字段未建立索引,而实体表中为此字段建立了索引,此种情况下,性能也相当差。但是当给虚拟表建立索引后,性能恢复正常。

 

类似 where name like "str%" limit 1 的查询,即使在 name 列上创建了索引,也会导致查询过慢,是因为 federated引擎会将所有满足条件的记录读取到本,再进行 limit 处理。

 

这几个问题已经严重影响了federated 在实际环境中的应用。

bitsCN.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Unable to save changes to Photos app error in Windows 11 Unable to save changes to Photos app error in Windows 11 Mar 04, 2024 am 09:34 AM

If you encounter the Unable to save changes error while using the Photos app for image editing in Windows 11, this article will provide you with solutions. Unable to save changes. An error occurred while saving. Please try again later. This problem usually occurs due to incorrect permission settings, file corruption, or system failure. So, we’ve done some deep research and compiled some of the most effective troubleshooting steps to help you resolve this issue and ensure you can continue to use the Microsoft Photos app seamlessly on your Windows 11 device. Fix Unable to Save Changes to Photos App Error in Windows 11 Many users have been talking about Microsoft Photos app error on different forums

Learn how to use the Boundless Notes app on iPhone and iPad Learn how to use the Boundless Notes app on iPhone and iPad Nov 18, 2023 pm 01:45 PM

What is Boundless Notes on iPhone? Like the iOS17 Diary app, Boundless Notes is a productivity app with tons of creative potential. It’s a great place to turn ideas into reality. You can schedule projects, brainstorm ideas, or create mood boards so you never run out of space to express your ideas. The app allows you to add photos, videos, audios, documents, PDFs, web links, stickers, and more anywhere on an unlimited canvas. Many of the tools in Boundless Notes (like brushes, shapes, and more) will be familiar to anyone who uses iWork apps like Keynote or Notes. Real-time collaboration with colleagues, teammates, and group project members is also easy because Freeform allows

Photos cannot open this file because the format is not supported or the file is corrupted Photos cannot open this file because the format is not supported or the file is corrupted Feb 22, 2024 am 09:49 AM

In Windows, the Photos app is a convenient way to view and manage photos and videos. Through this application, users can easily access their multimedia files without installing additional software. However, sometimes users may encounter some problems, such as encountering a "This file cannot be opened because the format is not supported" error message when using the Photos app, or file corruption when trying to open photos or videos. This situation can be confusing and inconvenient for users, requiring some investigation and fixes to resolve the issues. Users see the following error when they try to open photos or videos on the Photos app. Sorry, Photos cannot open this file because the format is not currently supported, or the file

How to connect Apple Vision Pro to PC How to connect Apple Vision Pro to PC Apr 08, 2024 pm 09:01 PM

The Apple Vision Pro headset is not natively compatible with computers, so you must configure it to connect to a Windows computer. Since its launch, Apple Vision Pro has been a hit, and with its cutting-edge features and extensive operability, it's easy to see why. Although you can make some adjustments to it to suit your PC, and its functionality depends heavily on AppleOS, so its functionality will be limited. How do I connect AppleVisionPro to my computer? 1. Verify system requirements You need the latest version of Windows 11 (Custom PCs and Surface devices are not supported) Support 64-bit 2GHZ or faster fast processor High-performance GPU, most

How to solve application startup error 0xc000012d problem How to solve application startup error 0xc000012d problem Jan 02, 2024 pm 12:53 PM

When a friend's computer is missing certain files, the application cannot start normally with error code 0xc000012d. In fact, it can be solved by re-downloading the files and installing them. The application cannot start normally 0xc000012d: 1. First, the user needs to download ".netframework". 2. Then find the download address and download it to your computer. 3. Then double-click on the desktop to start running. 4. After the installation is completed, return to the wrong program location and open the program again.

MS Paint not working properly in Windows 11 MS Paint not working properly in Windows 11 Mar 09, 2024 am 09:52 AM

Microsoft Paint not working in Windows 11/10? Well, this seems to be a common problem and we have some great solutions to fix it. Users have been complaining that when trying to use MSPaint, it doesn't work or open. Scrollbars in the app don't work, paste icons don't show up, crashes, etc. Luckily, we've collected some of the most effective troubleshooting methods to help you resolve issues with Microsoft Paint app. Why doesn't Microsoft Paint work? Some possible reasons why MSPaint is not working on Windows 11/10 PC are as follows: The security identifier is corrupted. hung system

Fix caa90019 Microsoft Teams error Fix caa90019 Microsoft Teams error Feb 19, 2024 pm 02:30 PM

Many users have been complaining about encountering error code caa90019 every time they try to log in using Microsoft Teams. Even though this is a convenient communication app, this mistake is very common. Fix Microsoft Teams Error: caa90019 In this case, the error message displayed by the system is: "Sorry, we are currently experiencing a problem." We have prepared a list of ultimate solutions that will help you resolve Microsoft Teams error caa90019. Preliminary steps Run as administrator Clear Microsoft Teams application cache Delete settings.json file Clear Microsoft from Credential Manager

How to use multi-threads to concurrently access the database in C# development How to use multi-threads to concurrently access the database in C# development Oct 09, 2023 pm 11:29 PM

How to use multi-threads to access the database concurrently in C# development In C# development, multi-threads concurrent access to the database is a common requirement. Using multi-threading can improve the efficiency of database operations, but you also need to pay attention to issues such as thread safety and database connection management. This article will introduce how to use multi-threading to access the database concurrently in C# and provide specific code examples. Create a database connection Before using multi-threads to access the database concurrently, you first need to create a database connection. Normally, we use Sql provided by ADO.NET

See all articles