mysql数据导入odoo_odoo中连接mysql数据库

本文介绍了如何在Odoo中利用PostgreSQL的foreign data wrapper (mysql_fdw) 连接和导入MySQL数据库数据。首先,详细说明了安装和配置mysql_fdw的步骤,包括创建服务器对象、用户映射和外部表。然后,展示了在Odoo中创建模型以操作MySQL数据,重点是设置_auto为False。最后提到了在Amazon RDS中使用mysql_fdw的解决方案,通过在独立EC2上部署mysql_fdw并使用postgresql_fdw进行转发。
摘要由CSDN通过智能技术生成

以前以为odoo中只能连接postgresql数据库,并且只能连接一个数据库,今天发现,得益于postgresql的强大,我们可以再odoo中嵌入式地加载其他数据库的数据,就像本地库中的数据一样。

这其中的关键就在于postgresql中的foreign data wrapper的概念,利用fdw,我们可以连接mysql,sqlserver,oracle等市面上常见的数据库。

下面,我们就来说一下,如何利用fdw来实现这一目的。

先决条件

安装mysql_fdw

github 下载 mysql_fdw https://github.com/EnterpriseDB/mysql_fdw

首先运行 pg_config 如果报错,安装如下插件

1You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a client-side application.

安装:

sudo apt-get install libpq-dev

编译

然后make之

make USE_PGXS=1

如果报如下的错误:

1

2Makefile:47: /usr/lib/postgresql/9.5/lib/pgxs/src/makefiles/pgxs.mk: No such file or directory

Makefile:52: *** PostgreSQL 9.3, 9.4, 9.5, 9.6 or 10beta is required to compile this extension. Stop.

解决方法:

1

2sudo apt-get install postgresql-server-dev-all

sudo apt-get install postgresql-common

centos 用如下命令安装:

1

2yum install postgresql-server

yum install postgresql95-devel

安装 foreign data wrapper

make USE_PGXS=1 install

创建mysql_fdw for postgresql– load extension first time after install

CREATE EXTENSION mysql_fdw;

– create server object

1

2

3CREATE SERVER mysql_server

FOREIGN DATA WRAPPER mysql_fdw

OPTIONS (host '127.0.0.1', port '3306');

– create user mapping

1

2

3CREATE USER MAPPING FOR postgres

SERVER mysql_server

OPTIONS (username 'foo', password 'bar');

– create foreign table

1

2

3

4

5

6CREATE FOREIGN TABLE warehouse(

warehouse_id int,

warehouse_name text,

warehouse_created datetime)

SERVER mysql_server

OPTIONS (dbname 'db', table_name 'warehouse');

至此,应该可以在pg数据库查询到mysql server中的数据了。

二、在odoo中使用FDW连接Mysql数据库

新建对象kite_runner1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19class kite_user(models.Model):

_name = "kite.user"

_auto = False

_table = "kite_user"

def init(self):

self.env.cr.execute(

'''

create foreign table if not exists kite_user (

id int,

account varchar,

password varchar

)

server kite options(dbname 'db_scversion', table_name 'user')

'''

)

account = fields.Char(u'账号')

password = fields.Char(u'密码')

这里边的关键点在于,_auto要设置为False,这样odoo就不会傻乎乎地再去创建kite_user这个表了(odoo并不识别pg中的foreign table,它傻*似地只认普通表和视图),然后_table指定表名,这里的表名要跟已经存在的foreign table名一致。

新建了model,再给model建立好视图,就能像操作本地数据一样的方便操作mysql数据库了。

PS:参考资料中说,要自己添加create_uid,create_date,write_uid,write_date等默认字段,但是实际实现的过程中,我并没有实现这些字段,但也是成功的,只是缺少了这些字段,可能会在数据追查方面有所损失,如果需要,读者请自行实现。

三、在Amazon RDS中使用mysql_fdw

当前,Amazon Postgresql 所支持的扩展中并不包含 mysql_fdw,想要在rds中使用,就必须另辟蹊径。

这里提供一种方法,在一台独立的ec2中安装mysql_fdw,然后再rds中安装postgresql_fdw,使用postgresql_fdw外链到mysql_fdw,即利用postgresql_fdw当作中间转发跳板,经过二次转发到达mysql数据库完成我们的目的。

参考资料:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值