請先確認已經安裝好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
第一次使用外部數據功能,看了一些文檔,總的來說不難。吧整個流程整理了一下,以后用到有跡可循,也希望能夠幫助到大家。