Mysql与PostgreSQL的安装过程省略。

为简便起见,把MySQL和PostgreSQL都安装在一个机器上,然后在此机器上(准确地说是在PostgreSQL运行的机器上)安装mysql_fdw:

首先是下载 mysql_fdw:

http://pgxn.org/dist/mysql_fdw/

mysql_fdw-1.0.1.zip

然后是解压和安装:

[root@server mysql_fdw-1.0.]# pwd
/soft/fdw/mysql_fdw-1.0.
[root@server mysql_fdw-1.0.]# ls
META.json Makefile README mysql_fdw--1.0.sql mysql_fdw.c mysql_fdw.control

运行 make 和 make install,其README文件说得很清楚:

[root@server mysql_fdw-1.0.]# cat README
MySQL FDW for PostgreSQL 9.1+
============================== This PostgreSQL extension implements a Foreign Data Wrapper (FDW) for
the MySQL. This code is experimental, and largely intended as a pet project for me
to experiment with and learn about FDWs in PostgreSQL. By all means use it, but do so entirely at your own risk! You have been
warned! Building
-------- Install MySQL, or just the C client library, and Once that's done, the
extension can be built with: PATH=/usr/local/pgsql/bin/:/usr/local/mysql/bin:$PATH make USE_PGXS=
sudo PATH=/usr/local/pgsql/bin/:/usr/local/mysql/bin:$PATH make USE_PGXS= install (assuming you have PostgreSQL 9.1 in /usr/local/pgsql and MySQL in
/usr/local/mysql). I've tested on Mac OS X 10.7 only, but other *nix's should also work.
I haven't tested on Windows, but the code should be good on MinGW. Limitations
----------- - No attempt is made to pushdown quals to MySQL. - The MySQL connection used to plan queries isn't currently reused
during execution. Usage
----- The following parameters can be set on a MySQL foreign server: address: The address or hostname of the MySQL server.
Default: 127.0.0.1 port: The port number on which the MySQL server is listening.
Default: The following parameter can be set on a MySQL foreign table: database: The name of the MySQL database to query.
Default: NULL query: An SQL query to define the data set on the MySQL server. table: The name of a table (quoted and qualified as required)
on the MySQL table. Note that the query and table paramters are mutually exclusive. Using
query can provide either a simple way to push down quals (which of
course is fixed at definition time), or to base remote tables on
more complex SQL queries. The following parameter can be set on a user mapping for a MySQL
foreign server: username: The username to use when connecting to MySQL
Default <none> password: The password to authenticate to the MySQL server with.
Default: <none> Example
------- -- Install the extension
CREATE EXTENSION mysql_fdw; -- Create the foreign server, a pointer to the MySQL server.
CREATE SERVER mysql_svr
FOREIGN DATA WRAPPER mysql_fdw
OPTIONS (address '127.0.0.1', port ''); -- Create one or more foreign tables on the MySQL server. The first of
-- these maps to a remote table, whilst the second uses an SQL query.
CREATE FOREIGN TABLE employees (
id integer,
name text,
address text)
SERVER mysql_svr
OPTIONS (table 'hr.employees'); CREATE FOREIGN TABLE overtime_2010 (
id integer,
employee_id integer,
hours integer)
SERVER mysql_svr
OPTIONS (query 'SELECT id, employee_id, hours FROM hr.overtime WHERE year = 2010;'); -- Create a user mapping to tell the FDW the username/password to
-- use to connect to MySQL, for PUBLIC. This could be done on a per-
-- role basis.
CREATE USER MAPPING FOR PUBLIC
SERVER mysql_svr
OPTIONS (username 'dpage', password ''); --
Dave Page
dpage@pgadmin.org
[root@server mysql_fdw-1.0.]#
[root@server mysql_fdw-1.0.]# PATH=/usr/local/pgsql/bin/:/usr/local/mysql/bin:$PATH make USE_PGXS=1
gcc -O2 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wformat-security -fno-strict-aliasing -fwrapv -fpic -I/usr/local/mysql/include -I. -I. -I/usr/local/pgsql/include/server -I/usr/local/pgsql/include/internal -D_GNU_SOURCE -c -o mysql_fdw.o mysql_fdw.c
mysql_fdw.c: In function 'mysqlPlanForeignScan':
mysql_fdw.c:: 警告: 'rows' may be used uninitialized in this function
gcc -O2 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wformat-security -fno-strict-aliasing -fwrapv -fpic -shared -o mysql_fdw.so mysql_fdw.o -L/usr/local/pgsql/lib -Wl,-rpath,'/usr/local/pgsql/lib',--enable-new-dtags -L/usr/local/mysql/lib -lmysqlclient -lpthread -lm -lrt -ldl
[root@server mysql_fdw-1.0.]# sudo PATH=/usr/local/pgsql/bin/:/usr/local/mysql/bin:$PATH make USE_PGXS=1 install
/bin/mkdir -p '/usr/local/pgsql/lib'
/bin/mkdir -p '/usr/local/pgsql/share/extension'
/bin/sh /usr/local/pgsql/lib/pgxs/src/makefiles/../../config/install-sh -c -m mysql_fdw.so '/usr/local/pgsql/lib/mysql_fdw.so'
/bin/sh /usr/local/pgsql/lib/pgxs/src/makefiles/../../config/install-sh -c -m ./mysql_fdw.control '/usr/local/pgsql/share/extension/'
/bin/sh /usr/local/pgsql/lib/pgxs/src/makefiles/../../config/install-sh -c -m ./mysql_fdw--1.0.sql '/usr/local/pgsql/share/extension/'
[root@server mysql_fdw-1.0.]#

检查一下 mysql_fdw.so文件,是否出现在 /usr/local/pgsql/lib 目录下。

然后,分别启动mysql和 postgresql:

[root@server ~]# mysqld_safe &
[]
[root@server ~]# :: mysqld_safe Logging to '/usr/local/mysql/data/server.gao.err'.
:: mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
[root@server ~]# su - postgres
[postgres@server ~]$ pwd
/home/postgres
[postgres@server ~]$ cd /usr/local/pgsql
[postgres@server pgsql]$ ./bin/pg_ctl -D ./data start
server starting
[postgres@server pgsql]$ LOG: database system was shut down at -- :: CST
LOG: autovacuum launcher started
LOG: database system is ready to accept connections

然后,在mysql客户端,执行以下命令来创建表以及访问该表的用户:

[root@server ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.13 MySQL Community Server (GPL) Copyright (c) 2000, 2013, 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> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A Database changed
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec) mysql> select host,user from mysql.user;
+------------+--------+
| host | user |
+------------+--------+
| % | usrabc |
| 127.0.0.1 | root |
| ::1 | root |
| localhost | |
| localhost | root |
| server.gao | |
| server.gao | root |
+------------+--------+
7 rows in set (0.08 sec) mysql> select * from example;
ERROR 1146 (42S02): Table 'mysql.example' doesn't exist
mysql> CREATE TABLE example ( id INT,data VARCHAR(100) );
Query OK, 0 rows affected (0.05 sec) mysql> quit
Bye
[root@server ~]# mysql -uusrabc -p
Enter password:
ERROR 1045 (28000): Access denied for user 'usrabc'@'localhost' (using password: YES)
[root@server ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.6.13 MySQL Community Server (GPL) Copyright (c) 2000, 2013, 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 'usrabc'@'localhost' identified by 'usrabc';
Query OK, 0 rows affected (0.00 sec) mysql> GRANT SELECT, INSERT, DELETE,UPDATE ON *.* TO 'usrabc'@'localhost';
Query OK, 0 rows affected (0.00 sec) mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec) mysql>

然后,开始在PostgreSQL端,建立FDW:

[postgres@server pgsql]$ ./bin/psql
psql (9.1.2)
Type "help" for help. postgres=# CREATE EXTENSION mysql_fdw;
ERROR: extension "mysql_fdw" already exists
postgres=# CREATE SERVER mysql_svr FOREIGN DATA WRAPPER mysql_fdw OPTIONS (address '127.0.0.1', port '');
CREATE SERVER postgres=# CREATE FOREIGN TABLE example (id INT,data VARCHAR(100) ) SERVER mysql_svr
postgres-# OPTIONS (table 'mysql.example');
CREATE FOREIGN TABLE
postgres=#
postgres=# CREATE USER MAPPING FOR PUBLIC SERVER mysql_svr OPTIONS (username 'usrabc', password 'usrabc');
CREATE USER MAPPING
postgres=#
postgres=# select * from example;
ERROR: failed to connect to MySQL: Access denied for user 'usrabc'@'localhost' (using password: YES)
postgres=#
postgres=# select * from example;
id | data
----+------
(0 rows) postgres=#

此时,在mysql端,增加数据:

mysql> insert into mysql.example values(123,'');
Query OK, 1 row affected (0.01 sec) mysql> select * from example;
ERROR 1046 (3D000): No database selected
mysql> select * from mysql.example;
+------+-------+
| id | data |
+------+-------+
| 123 | 11111 |
+------+-------+
1 row in set (0.00 sec) mysql>

然后,在psql端验证:

postgres=# select * from example;
id | data
-----+-------
123 | 11111
(1 row) postgres=#

结束

PostgreSQL通过mysql_fdw访问MySQL数据库的更多相关文章

  1. java文件来演示如何访问MySQL数据库

    java文件来演示如何访问MySQL数据库. 注:在命令行或用一个SQL的前端软件创建Database. 先创建数据库: CREATE DATABASE SCUTCS; 接着,创建表: CREATE ...

  2. PHP访问MySql数据库介绍

    在网站后台,经常要与数据库打交道.本文介绍如何使用XAMPP来管理MySql数据库及如何用PHP来访问MySql数据库. 一.使用XAMPP来管理MySql数据库 首先使用XAMPP打开MySql的管 ...

  3. C#连接、访问MySQL数据库

    一.准备工具 visual stuido(本示例使用visual studio 2010) MySql.Data.dll mysql_installer_community_V5.6.21.1_set ...

  4. 在Eclipse中使用JDBC访问MySQL数据库的配置方法

    在Eclipse中使用JDBC访问MySQL数据库的配置方法 分类: DATABASE 数据结构与算法2009-10-10 16:37 5313人阅读 评论(10) 收藏 举报 jdbcmysql数据 ...

  5. C#访问MySQL数据库(winform+EF)

    原文:C#访问MySQL数据库(winform+EF) 以前都是C#连接SQLServer,现在MySQL也比较火了,而且是开源跨平台的,这里连接使用一下,主要是体会一下整个流程,这里使用的是winf ...

  6. Spring Boot入门(六):使用MyBatis访问MySql数据库(注解方式)

    本系列博客记录自己学习Spring Boot的历程,如帮助到你,不胜荣幸,如有错误,欢迎指正! 本篇博客我们讲解下在Spring Boot中使用MyBatis访问MySql数据库的简单用法. 1.前期 ...

  7. html页面通过http访问mysql数据库中的内容,实现用户登录的功能

    需求: 通过html编写用户登录页面,页面内容包括用户名.密码和登录按钮,点击登录后访问login.php文件,使用按钮默认的submit提交用户名和密码,在login.php中访问mysql数据库, ...

  8. 关于利用PHP访问MySql数据库的逻辑操作以及增删改查实例操作

    PHP访问MySql数据库 <?php //造连接对象$db = new MySQLi("localhost","root","",& ...

  9. C#访问MySQL数据库的方法

    C#访问MySQL数据库的方法 (1)首先需要下载C#访问MySQL数据库的ADO.NET驱动程序 下载地址为: http://dev.mysql.com/downloads/connector/ne ...

随机推荐

  1. Charles使用笔记

    Charles本身其实是一款十分强大且易用的代理软件,最近用的比较多,大致整理了一下自己用到的一些东西.   Charles的主要作用 1.查看网络请求,手动分析数据 2.代理接口,模拟数据      ...

  2. javascript对象和函数的几种常见写法

    /** * Created by chet on 15/12/17. */ var En= function (button,func) { //dosth,不能return alert(button ...

  3. TCP (传输控制协议)

    http://baike.baidu.com/item/TCP/33012?fr=aladdin TCP(Transmission Control Protocol 传输控制协议)是一种面向连接的.可 ...

  4. java @XmlTransient与@Transient区别

    1.@XmlTransient a.@XmlTransient 注解解决 JavaBean 属性名称与字段名称之间的名称冲突,或者用于防止字段/属性的映射 b.阻止将 JavaBean 属性映射到 X ...

  5. ZooKeeper学习之路 (四)ZooKeeper开发环境eclipse配置

    一.eclipse中配置zookeeper开发环境 1)将zookeeper eclipse plugin中的6个jar包放到eclipse安装目录下的plugins文件中,重启eclipse (2) ...

  6. Word 2019 for mac更新喽!

    新的Word2019添加了许多的新功能,提供了新的文档处理方式,如改进的数字笔功能.焦点模式.学习工具和翻译,为用户提供了用于创建专业而优雅的高效文档工具,帮助用户节省时间,并得到优雅美观的结果,有效 ...

  7. selenium + python自动化测试unittest框架学习(二)

    1.unittest单元测试框架文件结构 unittest是python单元测试框架之一,unittest测试框架的主要文件结构: File >report >all_case.py &g ...

  8. Linux下使用FIO测试磁盘的IOPS

    FIO是测试IOPS的非常好的工具,用来对硬件进行压力测试和验证,支持13种不同的I/O引擎,包括:sync,mmap, libaio, posixaio, SG v3, splice, null, ...

  9. Linux BLE 基于 树莓派

    1.参考资料:Linux(RaspberryPi)上使用BLE低功耗蓝牙 使用bluez协议栈方法有用 2.Linux下Bluez的编程实现 3.和菜鸟一起学linux之bluez学习记录2 4.BL ...

  10. 通达信k线颜色设置

    通达信的k线函数没有颜色选项.如果想要画颜色可以使用STICKLINE函数来覆盖当前k线这样也是可以满足需求. 第一步画针 STICKLINE(条件 , L , H , 0 , 0 ) , 颜色; 第 ...