postgresql添加mysql_fdw测试过程
请先确认已经安装好mysql_fdw,如果没有配置好点这:https://www.cnblogs.com/ohsolong/p/13041989.html
1.切换至postgres用户,输入密码登录 。注:如果安装了postgresql系统会自动创建该用户
su postgres
2.登录postgres数据库。注:安装postgresql时默认生成的数据库, 也可以登录自己创建的数据库,这里为了方便直接登录默认提供的数据库。
$ psql
psql (10.12 (Ubuntu 10.12-0ubuntu0.18.04.1))
Type "help" for help.
postgres=#
2.接下来按照官方文档流程使用mysql_fdw,官方文档点这:https://pgxn.org/dist/mysql_fdw/
使用的命令说明在postgresql文档中,点这:http://postgres.cn/docs/10/postgres-fdw.html
第一步:加载mysql_fdw扩展
CREATE EXTENSION mysql_fdw;
第二步:创建连接外部数据库的连接对象
CREATE SERVER mysql_server
FOREIGN DATA WRAPPER mysql_fdw
OPTIONS (host '127.0.0.1', port '3306');
第三步:创建用户映射,postgresql用户映射mysql服务器的用户(postgresql数据库用户:postgres; mysql数据库用户:foo)
CREATE USER MAPPING FOR postgres
SERVER mysql_server
OPTIONS (username 'foo', password 'bar');
第四步:创建与mysql中对应的外部表。注:表名称,列名一致,对应列数据类型可以不同但能够转换(依赖与数据库本身)
CREATE FOREIGN TABLE warehouse(
warehouse_id int,
warehouse_name text,
warehouse_created TIMESTAMPZ)
SERVER mysql_server
OPTIONS (dbname 'db', table_name 'warehouse');
在postgresql中向mysql外部数据库插入数据,发现出出错。
postgres=# insert into warehouse values (1,'ups',now());
ERROR: failed to connect to MySQL: Access denied for user 'foo'@'localhost' (using password: YES)
3.上面出错的原因是我没有安装mysql_server,
以及没有在mysql中添加上面映射的 foo 用户,
以及没有创建名为db的数据库并在其中创建warehouse表,接下来进行配置。
第一步:安装mysql_server,启动mysql服务,切换至root用户登录mysql
安装数据库
$ sudo apt install mysql-server
启动数据库
$ service mysql start
登录mysql并创建远程用户
$ su root
# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.7.30-0ubuntu0.18.04.1 (Ubuntu)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
create user foo identified by 'bar';
Query OK, 0 rows affected (0.00 sec)
给用户授权
mysql> grant all privileges on *.* to 'foo'@'%'identified by 'bar' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)
创建数据库
mysql> create database db;
Query OK, 1 row affected (0.00 sec)
计入数据库
mysql> user db;
创建表
mysql> create table warehouse( warehouse_id int, warehouse_name text, warehouse_created timestamp);
Query OK, 0 rows affected (0.03 sec)
4.在postgresql上向外表插入数据,发现没有创建表的唯一索引
postgres=# insert into warehouse values (1, 'ups', now());
ERROR: first column of remote table must be unique for INSERT/UPDATE/DELETE operation
mysql表中创建唯一索引
mysql> create unique index warehouse_index on warehouse(warehouse_id);
5.最后测试
postgresql插入数据 mysql中查询成功(INSERT INTO)
postgres=# insert into warehouse values (1,'ups',now());
INSERT 0 1
postgres=#
mysql> select * from warehouse;
+--------------+----------------+---------------------+
| warehouse_id | warehouse_name | warehouse_created |
+--------------+----------------+---------------------+
| 1 | ups | 2020-06-04 15:41:51 |
+--------------+----------------+---------------------+
1 row in set (0.00 sec) mysql>
postgresql更新数据 mysql中查询成功(UPDATE)
postgres=# update warehouse set warehouse_name = 'ups_new' ,warehouse_created = now() where warehouse_id=1;
UPDATE 1
postgres=#
mysql> select * from warehouse;
+--------------+----------------+---------------------+
| warehouse_id | warehouse_name | warehouse_created |
+--------------+----------------+---------------------+
| 1 | ups_new | 2020-06-04 16:53:01 |
+--------------+----------------+---------------------+
1 row in set (0.00 sec) mysql>
postgresql删除数据 mysql中查询成功(DELETE)
postgres=# delete from warehouse where warehouse_id=1;
DELETE 1
postgres=#
mysql> select * from warehouse;
Empty set (0.00 sec) mysql>
done
第一次使用外部数据功能,看了一些文档,总的来说不难。吧整个流程整理了一下,以后用到有迹可循,也希望能够帮助到大家。
postgresql添加mysql_fdw测试过程的更多相关文章
- 【PostgreSQL】PostgreSQL添加新服务器连接时,报错“Server doesn't listen ”,已解决。
PostgreSQL添加新的服务器连接时,报错:
- 给PostgreSQL添加MySQL的unix_timestamp与from_unixtime函数
MySQL的2个常用函数unix_timestamp()与from_unixtime PostgreSQL并不提供,但通过PostgreSQL强大的扩展性可以轻松的解决问题. 话说远在天边,尽在眼前, ...
- PostgreSQL通过mysql_fdw访问MySQL数据库
Mysql与PostgreSQL的安装过程省略. 为简便起见,把MySQL和PostgreSQL都安装在一个机器上,然后在此机器上(准确地说是在PostgreSQL运行的机器上)安装mysql_fdw ...
- postgresql添加字段
ALTER TABLE jiangan_config ADD COLUMN article text NOT NULL DEFAULT ''; pg_dump -s database_name -t ...
- postgresql 添加uuid扩展
去年用EF Core做数据迁移到psql数据库的时候遇到了缺失uuid的错误,当时帅气的脸蛋突然懵逼了.现记录一下 以备参考. 环境:Centos7.2 psql Xshell Xshell连接C ...
- Postgresql添加/删除触发器示例
-- 定义触发器 CREATE TRIGGER "vss_after_insert_flow_201702" AFTER INSERT ON "public". ...
- postgresql+slony-i安装配置主从
slon软件下载地址:slony1-1.2.6 http://slony.info/downloads/1.2/source/ postgresql下载地址: http://www.postgresq ...
- 安装Postgresql
p.MsoNormal,li.MsoNormal,div.MsoNormal { margin: 0cm; margin-bottom: .0001pt; line-height: 150%; fon ...
- postgresql总结
这篇博客主要对PostgreSQL进行总结,内容偏基础. 这里先附上一个PostgreSQL的中文资源:PostgreSQL 8.1 中文文档.英文不好的同学可以看看这个. 安装PostgreSQL ...
随机推荐
- vue-项目的整体增删改查
Dept: package com.example.demo.gs; public class Dept { private int id; private String name; private ...
- 常用CSS样式属性
01.常用样式 1.1.background背景 设置元素背景的样式 background,更好的衬托内容. 属性 描述 值 background 背景属性简写.支持多组背景设置,逗号,隔开 back ...
- 开箱即用 yyg-cli(脚手架工具):快速创建 vue3 组件库和vue3 全家桶项目
1 yyg-cli 是什么 yyg-cli 是优雅哥开发的快速创建 vue3 项目的脚手架.在 npm 上发布了两个月,11月1日进行了大升级,发布 1.1.0 版本:支持创建 vue3 全家桶项目和 ...
- KMP算法,匹配字符串模板(返回下标)
//KMP算法,匹配字符串模板 void getNext(int[] next, String t) { int n = next.length; for (int i = 1, j = 0; i & ...
- OpenCvSharp的安装和使用
OpencvSharp是opencv的C#版本,使用习惯了opencv的人学起OpenCvSharp会很容易上手,看了网上很多的安装方式,最后我感觉还是自己去下载安装包的方式最简单,通过Nuget的方 ...
- JavaScript代码是怎么在浏览器里面运行起来的?
JavaScript代码是怎么在浏览器里面运行的?下面简单探索一下 浏览器内核 浏览器内核(Rendering Engine),常见的叫法如:排版引擎.解释引擎.渲染引擎,现在流行称为浏览器内核. 浏 ...
- Nginx 安装篇-1.19.9版本源码安装
系统环境:CentOS 8.5 64位 [开始安装](此步骤引自网络教程) https://www.cnblogs.com/torchstar/p/16027538.html 教程比较详细,一步步操作 ...
- 2022年Kubernetes CKA 认证真题解析完整版
第一题 RBAC授权问题权重: 4% 设置配置环境:[student@node-1] $ kubectl config use-context k8s Context为部署管道创建一个新的Cluste ...
- pyqt5制作俄罗斯方块小游戏-----源码解析
一.前言 最近学习pyqt5中文教程时,最后一个例子制作了一个俄罗斯方块小游戏,由于解释的不是很清楚,所以源码有点看不懂,查找网上资料后,大概弄懂了源码的原理. 二.绘制主窗口 将主窗口居中,且设置了 ...
- Jmeter 定时器之同步定时器(Synchronizing Timer)
性能测试中需要模拟多用户并发测试,此时需要用到同步定时器(Synchronizing Timer).如下图,模拟用户组的数量设置20,相当于20个用户(线程)并发 名词解释: 名称:定时器名称,可根据 ...