• Linux环境下不安装DM数据库,编译dmPython


    一、简介

    dmPython 是 DM 提供的依据 Python DB API version 2.0 中 API 使用规定而开发的数据库访问接口。dmPython 实现这些 API,使 Python 应用程序能够对 DM 数据库进行访问。
    dmPython 通过调用 DM DPI 接口完成 python 模块扩展。在其使用过程中,除 Python标准库以外,还需要 DPI 的运行环境。

    二、使用场景

    应用服务器与数据库服务器分开规划,且应用服务器上不安装数据库,这种情况下安装dmPython

    三、注意事项

    • 建议数据库软件包中自带的dmPython源包,减少摸不着头脑的错误。
    • 编译安装时,它会探测数据库软件目录,最好提前设置好DM_HOME环境变量。
    • 在Linux环境中,python如果是通过yum源安装的,此时记得检查下python-devel开发包是否已安装完毕。
    • dmPython依赖于dpi库,所以Linux中LD_LIBRARY_PATH环境变量记得添上,Windows则拷贝到它默认的加载位置。

    四、具体操作

    1. 拷贝DB服务器相关文件

    推荐使用随同数据库软件包下的相关文件以及驱动包,不推荐使用第三方渠道的下载包或其他数据库版本的目录文件以及驱动包

    拷贝数据库安装目录的三类目录文件到应用服务器上

    • 数据库服务器上$DM_HOME/drivers/python
    • $DM_HOME/bin目录
    • $DM_HOME/include目录

    如:拷贝到应用服务器/data/dmsoft下面。目录结构如下图:
    在这里插入图片描述

    2. 配置环境变量

    ## 查看应用用户的环境变量
    [root@VM-0-17-centos dmPython]# cat ~/.bash_profile
    ## 修改新增用户的环境变量,注意目录最后面不要加/
    [root@VM-0-17-centos dmPython]# vim ~/.bash_profile
    export DM_HOME=/data/dmsoft
    export LD_LIBRARY_PATH=$DM_HOME/bin:$LD_LIBRARY_PATH
    export PATH=$PATH:$HOME/bin:$DM_HOME/bin
    ## 环境变量生效
    [root@VM-0-17-centos dmPython]# source ~/.bash_profile
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3.编译驱动

    [root@VM-0-17-centos dmPython]# cd /data/dmsoft/dmPython
    [root@VM-0-17-centos dmPython]# python setup.py install
    
    • 1
    • 2

    编译驱动期间:警告信息可忽略,只需要关注错误的信息即可
    在这里插入图片描述

    4.验证驱动

    1. pip list

    [root@VM-0-17-centos dmPython]# pip list
    
    
    • 1
    • 2

    在这里插入图片描述

    2. 新建py_conn.py

    [root@VM-0-17-centos dmPython]# vim py_conn.py
    
    #!/usr/bin/python
    #coding:utf-8
    import dmPython
    try:
        conn = dmPython.connect(user='SYSDBA', password='SYSDBA', server='localhost',  port=5236)
        cursor  = conn.cursor()
        print('python: conn success!')
        conn.close()
    except (dmPython.Error, Exception) as err:
        print(err)
    
    [root@VM-0-17-centos dmPython]# python py_conn.py
    python: conn success!
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    五、安装过程中产生的错误

    1. Dameng home (/opt/dmdbms) does not refer to an DM8.1 installation or dmdpi library missing.

    [root@dmdb python]# cd dmPython/
    [root@dmdb dmPython]# python setup.py install
    Traceback (most recent call last):
      File "setup.py", line 97, in <module>
        raise DistutilsSetupError(messageFormat % (userDmHome,DAMENG_VERSION))
    distutils.errors.DistutilsSetupError: Dameng home (/opt/dmdbms) does not refer to an DM8.1 installation or dmdpi library missing.
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    报错原因:当前用户加载不到dmdbms的库文件,需添加环境变量

    ## 查看应用用户的环境变量
    [root@VM-0-17-centos dmPython]# cat ~/.bash_profile
    ## 修改新增用户的环境变量,注意目录最后面不要加/
    [root@VM-0-17-centos dmPython]# vim ~/.bash_profile
    export DM_HOME=/data/dmsoft
    export LD_LIBRARY_PATH=$DM_HOME/bin:$LD_LIBRARY_PATH
    export PATH=$PATH:$HOME/bin:$DM_HOME/bin
    ## 环境变量生效
    [root@VM-0-17-centos dmPython]# source ~/.bash_profile
    	
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    2. error: command ‘gcc’ failed with exit status 1

    [root@dmdb dmPython]# python setup.py install
    running install
    running bdist_egg
    running egg_info
    creating dmPython.egg-info
    writing dmPython.egg-info/PKG-INFO
    writing top-level names to dmPython.egg-info/top_level.txt
    writing dependency_links to dmPython.egg-info/dependency_links.txt
    writing manifest file 'dmPython.egg-info/SOURCES.txt'
    reading manifest file 'dmPython.egg-info/SOURCES.txt'
    writing manifest file 'dmPython.egg-info/SOURCES.txt'
    installing library code to build/bdist.linux-x86_64/egg
    running install_lib
    running build_ext
    building 'dmPython' extension
    creating build
    creating build/temp.linux-x86_64-2.7
    gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -DDM64 -I/home/dmdba/dmdbms/include -I/usr/include/python2.7 -c py_Dameng.c -o build/temp.linux-x86_64-2.7/py_Dameng.o -DBUILD_VERSION=2.3
    In file included from py_Dameng.c:3:0:
    py_Dameng.h:8:20: fatal error: Python.h: No such file or directory
     #include 
                        ^
    compilation terminated.
    error: command 'gcc' failed with exit status 1
    [root@dmdb dmPython]#
    [root@dmdb dmPython]# rpm -qa |grep gcc
    gcc-4.8.5-39.el7.x86_64
    gcc-c++-4.8.5-39.el7.x86_64
    gcc-gfortran-4.8.5-39.el7.x86_64
    libgcc-4.8.5-39.el7.x86_64
    [root@dmdb dmPython]# rpm -q gcc
    gcc-4.8.5-39.el7.x86_64
    [root@dmdb dmPython]#
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    在这里插入图片描述

    报错原因:缺失python-devel包

    [root@dmdb yum.repos.d]# yum install python-devel
    
    • 1

    3.未使用匹配126版本数据库的dmPython包

    In file included from Connection.c:8:0:
    trc.h:45:8: warning: extra tokens at end of #endif directive [enabled by default]
     #endif #_DMPATHON_TRC_H
            ^
    Connection.c:46:47: error: ‘DSQL_ATTR_UKEY_NAME’ undeclared here (not in a function)
     static  udint4      gc_attr_ukey_name       = DSQL_ATTR_UKEY_NAME;
                                                   ^
    Connection.c:47:47: error: ‘DSQL_ATTR_UKEY_PIN’ undeclared here (not in a function)
     static  udint4      gc_attr_ukey_pin        = DSQL_ATTR_UKEY_PIN;
                                                   ^
    Connection.c: In function ‘Connection_Debug_inner’:
    Connection.c:276:5: warning: pointer targets in passing argument 1 of ‘sprintf’ differ in signedness [-Wpointer-sign]
         sprintf(sql_txt, "SP_SET_PARA_VALUE(1, 'SVR_LOG', %d)", debug_type);
    In file included from Connection.c:5:0:
    Connection.c: At top level:
    py_Dameng.h:216:1: warning: ‘PyDecimal_Check’ defined but not used [-Wunused-function]
     PyDecimal_Check(
     ^
    Connection.c:45:16: warning: ‘gc_attr_ssl_pwd’ defined but not used [-Wunused-variable]
     static udint4  gc_attr_ssl_pwd   = DSQL_ATTR_SSL_PWD;
                    ^
    Connection.c:46:21: warning: ‘gc_attr_ukey_name’ defined but not used [-Wunused-variable]
     static  udint4      gc_attr_ukey_name       = DSQL_ATTR_UKEY_NAME;
                         ^
    Connection.c:47:21: warning: ‘gc_attr_ukey_pin’ defined but not used [-Wunused-variable]
     static  udint4      gc_attr_ukey_pin        = DSQL_ATTR_UKEY_PIN;
                         ^
    Connection.c:50:16: warning: ‘gc_attr_trace’ defined but not used [-Wunused-variable]
     static udint4  gc_attr_trace   = DSQL_ATTR_TRACE;
                    ^
    Connection.c:51:16: warning: ‘gc_attr_trace_file’ defined but not used [-Wunused-variable]
     static udint4  gc_attr_trace_file  = DSQL_ATTR_TRACEFILE;
                    ^
    error: command 'gcc' failed with exit status 1
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    处理方法:

    # 换社区源码包编译通过python-126594-20201027.zip
    creating build/lib.linux-x86_64-2.7
    gcc -pthread -shared -Wl,-z,relro build/temp.linux-x86_64-2.7/py_Dameng.o build/temp.linux-x86_64-2.7/row.o build/temp.linux-x86_64-2.7/Cursor.o build/temp.linux-x86_64-2.7/Connection.o build/temp.linux-x86_64-2.7/Environment.o build/temp.linux-x86_64-2.7/Error.o build/temp.linux-x86_64-2.7/Buffer.o build/temp.linux-x86_64-2.7/exLob.o build/temp.linux-x86_64-2.7/exObject.o build/temp.linux-x86_64-2.7/tObject.o build/temp.linux-x86_64-2.7/var.o build/temp.linux-x86_64-2.7/vCursor.o build/temp.linux-x86_64-2.7/vDateTime.o build/temp.linux-x86_64-2.7/vInterval.o build/temp.linux-x86_64-2.7/vLob.o build/temp.linux-x86_64-2.7/vNumber.o build/temp.linux-x86_64-2.7/vObject.o build/temp.linux-x86_64-2.7/vString.o build/temp.linux-x86_64-2.7/vlong.o build/temp.linux-x86_64-2.7/exBfile.o build/temp.linux-x86_64-2.7/vBfile.o build/temp.linux-x86_64-2.7/trc.o -L/home/dmdba/dmdbms/bin -L/usr/lib64 -ldmdpi -lpython2.7 -o build/lib.linux-x86_64-2.7/dmPython.so
    creating build/bdist.linux-x86_64
    creating build/bdist.linux-x86_64/egg
    copying build/lib.linux-x86_64-2.7/dmPython.so -> build/bdist.linux-x86_64/egg
    creating stub loader for dmPython.so
    byte-compiling build/bdist.linux-x86_64/egg/dmPython.py to dmPython.pyc
    creating build/bdist.linux-x86_64/egg/EGG-INFO
    copying dmPython.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
    copying dmPython.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
    copying dmPython.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
    copying dmPython.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
    writing build/bdist.linux-x86_64/egg/EGG-INFO/native_libs.txt
    zip_safe flag not set; analyzing archive contents...
    creating dist
    creating 'dist/dmPython-2.3-py2.7-linux-x86_64.egg' and adding 'build/bdist.linux-x86_64/egg' to it
    removing 'build/bdist.linux-x86_64/egg' (and everything under it)
    Processing dmPython-2.3-py2.7-linux-x86_64.egg
    Copying dmPython-2.3-py2.7-linux-x86_64.egg to /usr/lib64/python2.7/site-packages
    Adding dmPython 2.3 to easy-install.pth file
    
    Installed /usr/lib64/python2.7/site-packages/dmPython-2.3-py2.7-linux-x86_64.egg
    Processing dependencies for dmPython==2.3
    Finished processing dependencies for dmPython==2.3
    [root@dmdb dmPython]#
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    4.ImportError: libdmdpi.so: cannot open shared object file: No such file or directory

    在这里插入图片描述

    报错原因:未是被到dpi文件
    解决办法:添加环境变量

  • 相关阅读:
    MYSQL各种子查询操作总结
    【C++】:日期类实现
    ffmpeg抠图
    SpringCloud(二) 用Eureka做服务注册中心认证
    对字符数组进行冒泡排序
    IDEA软件使用步骤
    微信小程序毕业设计题目计算机维修服务+后台管理系统|前后分离VUE.js
    快递查询工具,一键查物流,派件时效怎么分析
    SAP ABAP ALV 层次顺序表如何使用双表头(Multiple Headers)进行数据输出
    全球都有哪些高光谱遥感卫星?
  • 原文地址:https://blog.csdn.net/qq_35349982/article/details/126889387