茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1671773 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
PIMG_00264.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

DB研討會 : [轉貼]MySQL Cross Server Select Query

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]MySQL Cross Server Select Query
MySQL Cross Server Select Query

Is it possible to write a cross server select query using MySQL Client. Basically the setup is like follows.
Server IP       Database
---------       --------
1.2.3.4       Test
a.b.c.d       Test

I want to write a query that will select rows from a table in the Test Database on 1.2.3.4 and insert the result in a table into the Test Database on a.b.c.d
My servers are located miles apart so I will be opening a SSH tunnel to connect the two.
Any pointers?



mysqldump could be a solution as mentioned already or you could try using the SELECT ... INTO OUTFILE and then LOAD DATA INFILE ... commands.
MySQL does have the federated storage engine which might be useful to you. Here's some more documentation on it http://dev.mysql.com/doc/refman/5.0/en/federated-storage-engine.html I have to confess that I've not had huge success with it but it might work for you.
The third solution would be to do the work in your application. Read in the results of the SELECT query line by line and INSERT to the other server line by line. You might run into some issues with data types and null handling that way though.
shareimprove this answer



how about using federated tables on one of the servers? create the federated tables based on the remote tables you will use in the query and just run your query as if your database was all local. example below from MySQL site
The procedure for using FEDERATED tables is very simple. Normally, you have two servers running, either both on the same host or on different hosts. (It is possible for a FEDERATED table to use another table that is managed by the same server, although there is little point in doing so.)
First, you must have a table on the remote server that you want to access by using a FEDERATED table. Suppose that the remote table is in the federated database and is defined like this:
CREATE TABLE test_table (
    id     INT(20) NOT NULL AUTO_INCREMENT,
    name   VARCHAR(32) NOT NULL DEFAULT '',
    other  INT(20) NOT NULL DEFAULT '0',
    PRIMARY KEY  (id),
    INDEX name (name),
    INDEX other_key (other)
)
ENGINE=MyISAM
CHARSET=latin1;

The example uses a MyISAM table, but the table could use any storage engine.
Next, create a FEDERATED table on the local server for accessing the remote table:
CREATE TABLE federated_table (
    id     INT(20) NOT NULL AUTO_INCREMENT,
    name   VARCHAR(32) NOT NULL DEFAULT '',
    other  INT(20) NOT NULL DEFAULT '0',
    PRIMARY KEY  (id),
    INDEX name (name),
    INDEX other_key (other)
)
ENGINE=FEDERATED
DEFAULT CHARSET=latin1
CONNECTION='mysql://fed_user@remote_host:9306/federated/test_table';

(Before MySQL 5.0.13, use COMMENT rather than CONNECTION.)



Since the mysql client can only connect to one server at a time, the short answer is No. But there is always a way...
Recent versions of mysqldump support a --where parameter that can be used to limit the data dumped. This means you have a way of running a simple SELECT (i.e. all columns on one table) and producing valid SQL to INSERT it. You can then pipe the output of such a mysqldump command for the source server to a mysql command to the destination server.
You probably want to include a few options like --no-create-info and --no-add-locks on the mysqldump command. Test it until the output is exactly what you want.

原文出處:client - MySQL Cross Server Select Query - Stack Overflow
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]Join tables from two different server(MySql)
Join tables from two different server

I have two different server server1 and server2, now I have db1 in server1 and db2 in server2. I am trying to join these two table in MySQL like this.
Select a.field1,b.field2
FROM  [server1, 3306].[db1].table1 a
Inner Join [server2, 3312].[db2].table2 b
ON a.field1=b.field2

But I am getting error. Is is possible in MYSQL.



Yes, it is possible in MySQL.
There are similar questions asked previously too. You have to use FEDERATED ENGINE to do this. The idea goes like this:
You have to have a federated table based on the table at another remote location to use the way you want. The structure of the table have to exactly same.
CREATE TABLE federated_table (
    id     INT(20) NOT NULL AUTO_INCREMENT,
    name   VARCHAR(32) NOT NULL DEFAULT '',
    other  INT(20) NOT NULL DEFAULT '0',
    PRIMARY KEY  (id),
    INDEX name (name),
    INDEX other_key (other)
)
ENGINE=FEDERATED
DEFAULT CHARSET=latin1
CONNECTION='mysql://fed_user@remote_host:9306/federated/test_table';

[Source Answer]

原文出處:mysql - Join tables from two different server - Stack Overflow
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]Can MySql 5.0 have a view of a table located on another server
Can MySql 5.0 have a view of a table located on another server

Can MySql 5.0 views use tables that are located on another server? What is the syntax for creating such a view?



Federated Tables: http://dev.mysql.com/doc/refman/5.0/en/federated-storage-engine.html
This allows a table to be remotely accessed as if the remote table was a local table. It has its limitations but seems to meet my needs.



Yes, you can create objects in one database that reference another database and even another host. The syntax to do so works something like this:
USE localhost.myDB;
SELECT * FROM host2.db.tableName;


原文出處:Can MySql 5.0 have a view of a table located on another server - Stack Overflow
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]MySQL FEDERATED引擎使用示例, 類似Oracle DBLINK

MySQL FEDERATED引擎使用示例, 類似Oracle DBLINK


MySQL FEDERATED引擎使用示例, 類似Oracle DBLINK

1 引擎說明

本地MySQL資料庫要訪問遠程MySQL資料庫的表中的數據, 必須通過FEDERATED存儲引擎來實現. 有點類似Oracle中的

資料庫鏈接(DBLINK). 要允許這個存儲引擎, 當構建MySQL時使用--with-federated-storage-engine來configure.

當創建一個FEDERATED表的時候, 伺服器在資料庫目錄創建一個表定義文件. 文件由表的名字開始, 並有一個.frm擴展名.

無其它文件被創建, 因為實際的數據在一個遠程資料庫上. 使用FEDERATED表的步驟是非常簡單的. 通常, 運行兩個伺服器,

要麼在同一個主機上, 要麼在不同主機上.

首先, 必須在想要用FEDERATED表訪問的遠程伺服器上有一個表. 假設, 遠程的表在DB_FED資料庫中並且被如下定義:


CREATE TABLE test_table (

id int(20) NOT NULL auto_increment,

name varchar(32) NOT NULL default '',

other int(20) NOT NULL default '0',

PRIMARY KEY (id),

KEY name (name),

KEY other_key (other)

) ENGINE=MyISAM;


接著, 在本地伺服器上為訪問遠程表創建一個FEDERATED表:



CREATE TABLE federated_table (

id int(20) NOT NULL auto_increment,

name varchar(32) NOT NULL default '',

other int(20) NOT NULL default '0',

PRIMARY KEY (id),

KEY name (name),

KEY other_key (other)

) ENGINE=FEDERATED CONNECTION='mysql://remote_user@remote_host:9306/db_fed/test_table';


除了ENGINE選項應該是FEDERATED, 並且CONNECTION表選項是給FEDERATED指明如何連接到遠程伺服器上的連接字元串之外,

這個表的結構必須完全與遠程表的結構相同.

遠程主機信息指明本地伺服器要連接到的遠程伺服器, 資料庫和表信息指明哪一個遠程表要被作為數據文件來用. 在這個例子中.

遠程伺服器被指定來作為遠程主機在9306埠上運行, 所以要啟動伺服器, 讓它監聽9306埠.

在CONNECTION選項中的連接字元串的一般形式如下(密碼和埠號是可選的):

mysql://user_name[:password]@host_name[:port_num]/db_name/tbl_name

這裡有一些連接字元串的例子:

CONNECTION='mysql://username:password@hostname:port/database/tablename'

CONNECTION='mysql://username@hostname/database/tablename'

CONNECTION='mysql://username:password@hostname/database/tablename'

因為任何被用的密碼作為純文本被存在連接字元串中, 它可以被任何使對FEDERATED表使用SHOW CREATE TABLE或SHOW TABLE STATUS的用戶, 或者在INFORMATION_SCHEMA資料庫中查詢TABLES表的用戶看見.

FEDERATED支持及不支持的如下:

·遠程伺服器必須是一個MySQL伺服器. FEDERATED對其它資料庫引擎的支持可能會在將來被添加.

·FEDERATED表指向的遠程表在通過FEDERATED表訪問它之前必須存在.

·一個FEDERATED表指向另一個FEDERATED表是可能的, 但是必須小心不要創建一個循環.

·沒有對事務的支持.

·如果遠程表已經改變, 對FEDERATED引擎而言是沒有辦法知道的. 這個的原因是因為這個表必須象數據文件一樣工作,

除了資料庫其它任何都不會被寫入. 如果有任何對遠程資料庫的改變, 本地表中數據的完整性可能會被破壞.

·FEDERATED存儲引擎支持SELECT,INSERT,UPDATE,DELETE和索引. 它不支持ALTER TABLE,DROP TABLE或任何

其它的數據定義語言語句. 當前的實現不使用預先準備好的語句.

·執行使用SELECT,INSERT,UPDATE和DELETE, 但不用HANDLER.

·FEDERATED表不能對查詢緩存不起作用.

這些限制中的一些在FEDERATED處理機的將來版本可能被消除.

2 使用示例

MySQL實例A(主機192.168.41.196)下, 建立對遠程MySQL實例B(主機192.168.41.197)的表db_fed.tab_test訪問.

1) MySQL實例B建立測試表



mysql> create database db_fed;

mysql> use db_fed

mysql> CREATE TABLE tab_test (

id int(20) NOT NULL auto_increment,

name varchar(32) NOT NULL default '',

PRIMARY KEY (id)

) ENGINE=MyISAM;

mysql> insert into tab_test set name = 'test1';

mysql> insert into tab_test set name = '測試1';

mysql> select * from tab_test;

+----+-------+

| id | name |

+----+-------+

| 1 | test1 |

| 3 | 測試1 |

+----+-------+

--建立一個用戶專門用於遠程federated引擎訪問, 這個用戶的許可權決定了遠程federated表的操作許可權:

mysql> grant select on db_fed.* to u_fed@192.168.41.196 identified by 'iamwangnc';


2) MySQL實例A建立FEDERATED引擎表



--確認FEDERATED引擎已啟用:

mysql> show engines;

...

| FEDERATED | YES | Federated MySQL storage engine | NO | NO | NO |

...

--如果未啟用, 先確保configure MySQL時加了--with-federated-storage-engine選項, 另外還確保啟動mysqld時加了--federated參數,

或者在my.cnf里[mysqld]段後面加了federated參數, 如下, 然後重啟mysqld:

--------------------------------

[mysqld]

federated

--------------------------------

--在某個庫下建立FEDERATED引擎表:

mysql> create database db_test;

mysql> use db_test

mysql> CREATE TABLE tab_test_fed (

id int(20) NOT NULL auto_increment,

name varchar(32) NOT NULL default '',

PRIMARY KEY (id)

) ENGINE=FEDERATED connection = 'mysql://u_fed:iamwangnc@192.168.41.197:3306/db_fed/tab_test';

--測試是否可以訪問:

mysql> select * from tab_test_fed;

+----+-------+

| id | name |

+----+-------+

| 1 | test1 |

| 3 | 測試1 |

+----+-------+

--測試是否可以修改:

mysql> delete from tab_test_fed;

ERROR 1296 (HY000): Got error 10000 'Error on remote system: 1142: DROP command denied to user 'u_fed'@'comp196'

for table 'tab_test'' from FEDERATED



如果本站有幫助到您,請不吝於給一個讚鼓勵!

原文出處: MySQL FEDERATED引擎使用示例, 類似Oracle DBLINK - 數碼維基
前一個主題 | 下一個主題 | 頁首 | | |



Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|