Date :20140213
Auth: Jin
参考
http://blog.sina.com.cn/s/blog_757b0e130101erl5.html
http://dev.mysql.com/doc/refman/5.1/zh/storage-engines.html#federated-storage-engine
一、问题描述
1.目前应用情况
在mysql 3304实例中有phpcms,anquanzuo两个库,
phpcms有数据写入,anquanzu没有数据写入
anquanzu有一个视图表carseats_article 为phpcms库的三个表v9_news,v9_news_data,v9_category组合的视图
3304-anquanzuo>show create table carseats_articleG
*************************** 1. row ***************************
View: carseats_article
Create View: CREATE ALGORITHM=UNDEFINED DEFINER=`dbselect`@`192.168.201.201` SQL SECURITY DEFINER VIEW `carseats_article` AS select `a`.`id` AS `id`,`a`.`title` AS `title`,`a`.`catid` AS `module_id`,`a`.`thumb` AS `img`,`c`.`catname` AS `module_name`,`b`.`content` AS `content`,`a`.`username` AS `publish_user`,`a`.`status` AS `is_del`,`a`.`listorder` AS `order`,`a`.`updatetime` AS `update_time`,`a`.`inputtime` AS `add_time`,`b`.`copyfrom` AS `copyfrom`,`a`.`keywords` AS `keywords`,`a`.`islink` AS `islink`,`a`.`url` AS `url` from ((`phpcms`.`v9_news` `a` join `phpcms`.`v9_news_data` `b`) join `phpcms`.`v9_category` `c`) where ((`a`.`catid` = `c`.`catid`) and (`a`.`id` = `b`.`id`))
character_set_client: utf8
collation_connection: utf8_general_ci
1 row in set (0.00 sec)
2、数据迁移
phpcms 迁移到mmm集群
anquanzuo 迁移至NDB Cluster (两台数据节点245,246,管理节点247)
3,问题
迁移anquanzuo库时无法创建carseats_article,因为phpcms库不在一个实例了
4、解决办法
以前SQL Server工作有用到DB link远程库
而mysql支持远程表
1)在phpcms创建需要的视图
2)在anquanzuo上创建远程表
二、创建mmm上phpcms的视图
到mmm的master服务器上
$ jump 121
# mysql -h 127.0.0.1 -P 3304 -u root -pjpasswd2011 phpcms
1、创建视图
DROP VIEW IF EXISTS `v_carseats_article`;
CREATE VIEW `v_carseats_article` AS
SELECT
`a`.`id` AS `id`,
`a`.`title` AS `title`,
`a`.`catid` AS `module_id`,
`a`.`thumb` AS `img`,
`c`.`catname` AS `module_name`,
`b`.`content` AS `content`,
`a`.`username` AS `publish_user`,
`a`.`status` AS `is_del`,
`a`.`listorder` AS `order`,
`a`.`updatetime` AS `update_time`,
`a`.`inputtime` AS `add_time`,
`b`.`copyfrom` AS `copyfrom`,
`a`.`keywords` AS `keywords`,
`a`.`islink` AS `islink`,
`a`.`url` AS `url`
FROM ((`v9_news` AS `a` JOIN `v9_news_data` AS `b`) JOIN `v9_category` AS `c`)
WHERE ((`a`.`catid` = `c`.`catid`) AND (`a`.`id` = `b`.`id`));
确认视图可以查询数据
3304-phpcms>select id,title from v_carseats_article limit 1;
+-------+-----------------------------------------+
| id | title |
+-------+-----------------------------------------+
| 96325 | 孩子们,圣诞老人真的存在! |
+-------+-----------------------------------------+
1 row in set (0.00 sec)
2、授权访问
grant select on phpcms.* to 'dbselect'@'192.168.201.%' identified by 'dmlpasswd'
注:线上已经有权限
三、修改NDB cluster支持FEDERATED [已准备好]
1、确实是否已经支持
$ jump 245
$ jump 246
mysql -S /mysql/dev/data4/mysql.sock -pjpasswd2013
mysql> select version();
+------------+
| version() |
+------------+
| 5.5.30-log |
+------------+
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| CSV | YES | CSV storage engine | NO | NO | NO |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
使用show engines 命令查看数据库是否已支持FEDERATED引擎:
Support 的值有以下几个:
YES 支持并开启
DEFAULT 支持并开启, 并且为默认引擎
NO 不支持
DISABLED 支持,但未开启
2、修改246
$ jump 247
1)确认信息
[root@JQ-pdt-host-247 ~]# ndb_mgm -e show
Connected to Management Server at: localhost:1186
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=20 @192.168.201.245 (mysql-5.5.30 ndb-7.2.12, Nodegroup: 0, Master)
id=21 @192.168.201.246 (mysql-5.5.30 ndb-7.2.12, Nodegroup: 0,)
[ndb_mgmd(MGM)] 1 node(s)
id=10 @192.168.201.247 (mysql-5.5.30 ndb-7.2.12)
[mysqld(API)] 4 node(s)
id=30 @192.168.201.245 (mysql-5.5.30 ndb-7.2.12) ### 这里
id=31 @192.168.201.246 (mysql-5.5.30 ndb-7.2.12) ### 这里
id=32 (not connected, accepting connect from any host)
id=33 (not connected, accepting connect from any host)
2)停止从
[root@JQ-pdt-host-247 ~]# ndb_mgm ndb_mgm
ndb_mgm> 21 STOP
或者
[root@JQ-pdt-host-246 ~]# mysqld_multi report 3
Reporting MySQL servers
MySQL server from group: mysqld3 is running
[root@JQ-pdt-host-246 ~]# mysqld_multi stop 3 --password=jpasswd2011
[root@JQ-pdt-host-246 ~]# mysqld_multi report 3
Reporting MySQL servers
MySQL server from group: mysqld3 is not running
3)修改配置
[root@JQ-pdt-host-246 ~]# vim /etc/my.cnf
删除默认参数
skip-federated
打开
[mysqld3]
federated
4)启动
[root@JQ-pdt-host-246 ~]# mysqld_multi report 3
Reporting MySQL servers
MySQL server from group: mysqld3 is running
5)确认生效
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| ndbcluster | DEFAULT | Clustered, fault-tolerant tables | YES | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| ndbinfo | YES | MySQL Cluster system information storage engine | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| FEDERATED | YES | Federated MySQL storage engine | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| InnoDB | YES | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
3、修改245
jump 245
同样的方法修改245
root@JQ-pdt-host-245 ~]# mysql -h127.0.0.1 -P3303 -uroot -pjpasswd2011
确认
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| ndbcluster | DEFAULT | Clustered, fault-tolerant tables | YES | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| ndbinfo | YES | MySQL Cluster system information storage engine | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| FEDERATED | YES | Federated MySQL storage engine | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| InnoDB | YES | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
11 rows in set (0.00 sec)
四、数据迁移
1、导出数据
cd /home/backup
mysqldump -h192.168.201.210 -P3304 anquanzuo > anquanzuo_source_20140213.sql
2、修改数据并导入
cp anquanzuo_source_20140213.sql anquanzuo_ndb_20140213.sql
sed -i s/MyISAM/ndbcluster/ anquanzuo_source_20140213.sql
sed -i s/InnoDB/ndbcluster/anquanzuo_source_20140213.sql
删除anquanzuo_ndb_20140213.sql文件中 carseats_article创建语句,或者前面dump前就先删除 carseats_article视图
这不如果没有依赖前面的远程表则可以先做
mysql> use anquanzuo;
Database changed
mysql> source ./anquanzuo_ndb_20140213.sql;
ERROR 1146 (42S02): Table 'phpcms.v9_news' doesn't exist
清理不是很干净
mysql> show tables;
+----------------------------+
| Tables_in_anquanzuo |
+----------------------------+
| carseats_evaluation_report |
| carseats_goods |
| carseats_topic |
| carseats_topic_comment |
+----------------------------+
4 rows in set (0.01 sec)
把原来的远程表删了
把下面-- Final view structure for view `carseats_article`
也删除
后发现还报错phpcms.v9_news
这个步骤放在前面在创建远程表
确认246是否同步
3、准备远程表
-- 在anquanzuo上调整,注意调整ip
--drop table carseats_article;
mysql> create database anquanzuo;
mysql> use anquanzuo;
Database changed
create table carseats_article (
id mediumint(8) unsigned,
title varchar(80),
module_id smallint(5) unsigned,
img varchar(100),
module_name varchar(30),
content mediumtext,
publish_user char(20),
is_del varchar(2),
`order` int(10) unsigned,
update_time int(10) unsigned,
add_time int(10) unsigned,
copyfrom varchar(100),
keywords varchar(255),
islink tinyint(1) unsigned,
url varchar(255)
) ENGINE=FEDERATED CONNECTION='mysql://dbselec:dmlpasswd@192.168.201.121:3304/phpcms/v_carseats_article';
ip选择vip或者其他ip都可以
mysql> show tables;
+---------------------+
| Tables_in_anquanzuo |
+---------------------+
| carseats_article |
+---------------------+
1 row in set (0.00 sec)
查询
mysql> select id,title from carseats_article limit 1;
+-------+-----------------------------------------+
| id | title |
+-------+-----------------------------------------+
| 96325 | 孩子们,圣诞老人真的存在! |
+-------+-----------------------------------------+
1 row in set (45.47 sec)
另外一台246确认
[root@JQ-pdt-host-246 ~]# mysql -h127.0.0.1 -P3303 -uroot -pjpasswd2011 anquanzuo
mysql> show tables;
Empty set (0.00 sec)
可以见FEDERATED 存储引擎是不同步的
因为查询时两台都有可能查询到,所以246这台也要创建;
注意:
root@JQ-pdt-host-246 ~]# ll /data/mysql/data3/anquanzuo/
total 16
-rw-rw---- 1 mysql mysql 9068 Feb 13 16:59 carseats_article.frm
-rw-rw---- 1 mysql mysql 61 Feb 13 16:50 db.opt
除了ENGINE选项应该是FEDERATED, 并且CONNECTION表选项是给FEDERATED指明如何连接到远程服务器上的连接字符串之外, 这个表的结构必须完全与远程表的结构相同.
五、总结
1、开启支持FEDERATED存储引擎,默认不知道
2、NDB cluster中不会同步远程表,因为不是NDBcluster存储引擎,所以每个SQL节点都要创建一次远程表,不然当使用到那个节点时候表不存在
3、远程表或视图需要先存在
4、FEDERATED支持及不支持的如下:
·远程服务器必须是一个MySQL服务器. FEDERATED对其它数据库引擎的支持可能会在将来被添加.
·FEDERATED表指向的远程表在通过FEDERATED表访问它之前必须存在.
·一个FEDERATED表指向另一个FEDERATED表是可能的, 但是必须小心不要创建一个循环.
·没有对事务的支持.
·如果远程表已经改变, 对FEDERATED引擎而言是没有办法知道的. 这个的原因是因为这个表必须象数据文件一样工作,
除了数据库其它任何都不会被写入. 如果有任何对远程数据库的改变, 本地表中数据的完整性可能会被破坏.
·FEDERATED存储引擎支持SELECT,INSERT,UPDATE,DELETE和索引. 它不支持ALTER TABLE,DROP TABLE或任何
其它的数据定义语言语句. 当前的实现不使用预先准备好的语句.
·执行使用SELECT,INSERT,UPDATE和DELETE, 但不用HANDLER.
·FEDERATED表不能对查询缓存不起作用.
这些限制中的一些在FEDERATED处理机的将来版本可能被消除.