MySQL使用federated进行数据同步

1、参考博客:

https://blog.csdn.net/wyb213080286/article/details/38493753/;

https://blog.csdn.net/bufanrong/article/details/84063415?utm_medium=distribute.pc_relevant.none-task-blog-baidujs-2;

https://www.cnblogs.com/hstone16/p/13050481.html。

2、主从库确定(Linux系统):

  在从库添加federated插件,具体操作如下:

   (1)查询插件:以下是FEDERATED插件没有安装或者没有启用的情况;

mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
| CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
| MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
| FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)

   (2)安装插件:

mysql>install plugin federated soname ‘ha_federated.so’;

   (3)执行以下步骤:

      进入mysql文档下 配置/etc/my.conf,设置federated为默认启动

            [root@localhost etc]# vi /etc/my.conf 插入 

[mysqld]
federated

    (4)重启MySQL服务:

[root@localhost etc]# /etc/init.d/mysqld stop
Shutting down MySQL......
[root@localhost etc]# /etc/init.d/mysqld start
Starting MySQL..  

    (5)再次查询插件是否启用(FEDERATED为yes则完成安装、启用):

mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |
| CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
| FEDERATED          | YES     | Federated MySQL storage engine                                 | NO           | NO   | NO         |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+

3、复制主库建表信息:

CREATE TABLE `sys_teacher` (
  `id` varchar(32) CHARACTER SET utf8 NOT NULL COMMENT '主键id',
  `name` varchar(100) CHARACTER SET utf8mb4 DEFAULT NULL COMMENT '讲师名称',
  `photo` varchar(100) CHARACTER SET utf8mb4 DEFAULT NULL COMMENT '照片',
  `sex` varchar(10) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '性别',
  `unit` varchar(200) CHARACTER SET utf8mb4 DEFAULT NULL COMMENT '讲师单位',
  `education` varchar(200) CHARACTER SET utf8mb4 DEFAULT NULL COMMENT '学历',
  `major` varchar(200) CHARACTER SET utf8mb4 DEFAULT NULL COMMENT '专业方向',
  `technical_title` varchar(100) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '职称',
  `rank` varchar(100) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '职级',
  `synopsis` text COLLATE utf8mb4_bin COMMENT '简介',
  `speciality` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '特长',
  `create_by` varchar(64) CHARACTER SET utf8mb4 DEFAULT '' COMMENT '创建者',
  `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  `update_by` varchar(64) CHARACTER SET utf8mb4 DEFAULT '' COMMENT '更新者',
  `update_time` datetime DEFAULT NULL COMMENT '更新时间',
  `phone` varchar(11) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '教师电话',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='讲师';

4、设置从库表信息,进行同步:

   注:主库需要先新建一个远程访问的账号:

创建账号

本地访问:

create user username@localhost identified by 'password';

外网访问:

create user 'username'@'%' identified by 'password';
flush privileges;
账号授权database_name数据库所有权限
grant all privileges on `database_name`.* to 'username'@'%' identified by 'password';
flush privileges;
只读权限
grant select on 数据库名称.* to 'username'@'%' identified by 'password';
flush privileges;
-- select 查询权限,all privileges 所有权限或指定select,delete,update,create,drop权限
-- database1 数据库
-- username 用户账号
-- password 密码
-- % 所有电脑可访问

从库进行同步/备份:

CREATE TABLE `sys_teacher` (
  `id` varchar(32) CHARACTER SET utf8 NOT NULL COMMENT '主键id',
  `name` varchar(100) CHARACTER SET utf8mb4 DEFAULT NULL COMMENT '讲师名称',
  `photo` varchar(100) CHARACTER SET utf8mb4 DEFAULT NULL COMMENT '照片',
  `sex` varchar(10) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '性别',
  `unit` varchar(200) CHARACTER SET utf8mb4 DEFAULT NULL COMMENT '讲师单位',
  `education` varchar(200) CHARACTER SET utf8mb4 DEFAULT NULL COMMENT '学历',
  `major` varchar(200) CHARACTER SET utf8mb4 DEFAULT NULL COMMENT '专业方向',
  `technical_title` varchar(100) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '职称',
  `rank` varchar(100) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '职级',
  `synopsis` text COLLATE utf8mb4_bin COMMENT '简介',
  `speciality` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '特长',
  `create_by` varchar(64) CHARACTER SET utf8mb4 DEFAULT '' COMMENT '创建者',
  `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  `update_by` varchar(64) CHARACTER SET utf8mb4 DEFAULT '' COMMENT '更新者',
  `update_time` datetime DEFAULT NULL COMMENT '更新时间',
  `phone` varchar(11) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '教师电话',
  PRIMARY KEY (`id`)
)  ENGINE=FEDERATED
CONNECTION='mysql://主库访问的用户名:主库访问的密码@主库访问的ip:主库访问的端口号/数据库名称/sys_teacher';

执行以下内容(不清楚作用),如果需要多次同步,只需要执行一次就OK;

mysql> GRANT ALL PRIVILEGES ON *.* TO 'usrname'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;

查询从库所添加表的数据:

mysql> select count(*) from sys_teacher;

注:从表可以删除不需要的字段。

你可能感兴趣的:(MySQL部分数据库表同步,mysql)