Linux下通过python访问MySQL、Oracle、SQL Server数据库的方法
本文档主要描述了Linux下python数据库驱动的安装和配置,用来实现在Linux平台下通过python访问MySQL、Oracle、SQLServer数据库。
其中包括以下几个软件的安装及配置:
unixODBC
FreeTDS
pyodbc
cx_Oracle
欢迎转载,请注明作者、出处。
作者:张正
QQ:176036317
如有疑问,欢迎联系。
本文档主要描述了Linux下python数据库驱动的安装和配置,用来实现在Linux平台下通过python访问MySQL、Oracle、SQLServer数据库。
一、前提概述...
二、驱动安装...
1.unixODBC安装...
2.FreeTds安装...
3.pyodbc安装...
4.cx_Oracle安装...
三、驱动配置...
四、连接测试...
一、前提概述
pyodbc是一个Python模块,能够让python使用ODBC连接来自Windows,Linux,OS/X等系统中的大部分数据库。pyodbc依赖于unixODBC和FreeTDS,因此需要先安装unixODBC和FreeTDS。(FreeTDS是一个开源的C程序库,它可以实现在Linux系统下访问操作SQLServer数据库)
cx_Oracle是一个用来连接并操作Oracle数据库的Python扩展模块,支持包括Oracle9.210.2以及11.1等版本。
二、驱动安装
1.unixODBC安装
a.下载unixODBC安装包:unixODBC-2.3.2.tar.gz
下载地址:http://www.linuxfromscratch.org/blfs/view/cvs/general/unixodbc.html
b.解压:tar-zxfunixODBC-2.3.2.tar.gz
c.安装:cdunixODBC-2.3.2
./configure--enable-gui=no
make
makeinstall
NOTES:
编译安装三个步骤之间,可通过echo$?查看退出状态,为0则正常,可进行下一步操作。(MYSQL驱动可安装MySQL-python.x86_64)
2.FreeTds安装
下载FreeTds安装包:freetds-stable.tgz
下载地址:http://www.freetds.org/
tar-zxvffreetds-stable.tgz
cdfreetds-0.91
./configure--prefix=/etc/freetds--with-tdsver=7.0--enable-msdblib--with-gnu-ld --enable-shared--enable-static
make
makeinstall
同样可通过echo$?查看退出状态,安装完后可查看freeTds相关版本及安装信息:
cdsrc/apps/
./tsql-C
[root@localhostapps]#./tsql-C Compile-timesettings(establishedwiththe"configure"script) Version:freetdsv0.91 freetds.confdirectory:/usr/local/etc MSdb-libsourcecompatibility:no Sybasebinarycompatibility:no Threadsafety:yes iconvlibrary:yes TDSversion:7.1 iODBC:no unixodbc:yes SSPI"trusted"logins:no Kerberos:no
3.pyodbc安装
下载pyodbc-3.0.6.zip
下载地址:https://www.nhooo.com/softs/453989.html
unzippyodbc-3.0.6.zip
cdpyodbc-3.0.6
pythonsetup.pybuild
pythonsetup.pyinstall
NOTES:安装时build遇到以下错误,而且gcc相关包已经安装完全
/root/software/pyodbc-3.0.6/src/connection.h:27:error:‘uintptr_t'doesnotnameatype error:command'gcc'failedwithexitstatus1
查看python版本,发现其为python2.4,通过官网下载页面信息对比,可知pyodbc-3.0.6不支持python2.4,重新下载pyodbc-2.1.7安装即可。
4.cx_Oracle安装
4.1.下载:cx_oracle:cx_Oracle-5.1.2-10g-py27-1.x86_64.rpm
http://sourceforge.net/projects/cx-oracle/files/
NOTES:
需要下载对应版本的驱动
4.2 oracleinstantclient安装:
gunzip10201_client_linux_x86_64.cpio.gz
cpio-idmv<10201_client_linux_x86_64.cpio
在~/.bash_profile中添加oracle相关配置:
exportORACLE_BASE=/opt/oracle exportORACLE_HOME=$ORACLE_BASE/client_10g exportLD_LIBRARY_PATH=$ORACLE_HOME:$LD_LIBRARY_PATH
建立oracle用户和文件目录
groupadddba useradd-gdbaoracle su-oracle mkdir-p/opt/oracle/client_10g
配置.bash_profile
回到root用户:
mvclient/home/oracle/
[root@localhostsoftware]#chown-Roracle:dba/home/oracle/client
su-oracle进行安装:
cd/home/oracle/client/response viinstantClient.rsp
修改以下参数:
UNIX_GROUP_NAME=dba ORACLE_HOME=/opt/oracle/client_10g ORACLE_HOME_NAME=OraClient10ghome1 cd../ ./runInstaller-silent-noconfig-responseFile/home/oracle/client/response/instantClient.rsp pythonsetup.pybuild pythonsetup.pyinstall
4.3安装cx_Oracle:
rpm-ivhcx_oracle:cx_Oracle-5.1.2-10g-py27-1.x86_64.rpm
三、驱动配置
1.配置freetds:
vi/etc/freetds/etc/freetds.conf
textsize=10240000
clientcharset=UTF-8
2.配置freetds驱动:
vifreetds.ini
[FreeTDS]
Description =0.91withprotocolv7.0
Driver =/etc/freetds/lib/libtdsodbc.so
odbcinst-i-d-ffreetds.ini
3.配置数据源:
vidb.ini
[SQLSERVER]
Driver=FreeTDS
Description=SQLSERVER
Trace =No
Server =192.168.16.13
Port =1433
Database=master
#此处需要配置SQLServer数据的连接信息
odbcinst-i-s-fdb.ini
cat/root/.odbc.ini
[SQLSERVER]
Driver=FreeTDS
Description=SQLSERVER
Trace=No
Server=192.168.16.13
Port=1433
Database=master
NOTES:
若见到以下错误:
importcx_Oracle Traceback(mostrecentcalllast): File"",line1,in? ImportError:libclntsh.so.10.1:cannotopensharedobjectfile:Nosuchfileordirectory
则需要找到对应的文件建立软链接,如:
ln-s/opt/oracle/db_10g/lib/libclntsh.so.10.1/usr/lib64/libclntsh.so.10.1
四、连接测试
1.前提说明:
本处提供的连接测试,仅仅是一个简单的连通并发起查询,以证明驱动的正确安装和配置。
可先进行import查看驱动安装是否有误:
[root@localhostetc]#python Python2.4.3(#1,Apr142011,20:41:59) [GCC4.1.220080704(RedHat4.1.2-50)]onlinux2 Type"help","copyright","credits"or"license"formoreinformation. >>>importMySQLdb#测试MySQL >>>importcx_Oracle#测试Oracle >>>importpyodbc #测试pyodbc,可访问SQLServer >>>
若驱动正确安装,执行import操作就不会报错。
2.MySQL连接测试:
[root@localhostsoftware]#catmysql_test.py importMySQLdb sql='showdatabases'; conn=MySQLdb.connect(host='localhost',user='test',passwd='test',db='test',port=3306) cur=conn.cursor() cur.execute(sql) printcur.fetchone() [root@localhostsoftware]#pythonmysql_test.py ('information_schema',)
3.Oracle连接测试:
[root@localhostsoftware]#cattest.py importtime importcx_Oracle sql2="select*fromtab"; connstr="system/oracle@192.168.43.130:1521/orcl" conn=cx_Oracle.connect(connstr) cur=conn.cursor() aa=cur.execute(sql2) printcur.fetchone() printcur.rowcount print"connectionstringis:",conn.dsn print"thedatabaseversionis:",conn.version [root@localhostsoftware]#pythontest.py ('SYSCATALOG','SYNONYM',None) 1 connectionstringis:192.168.43.130:1521/orcl thedatabaseversionis:10.2.0.1.0
4.SQLServer连接测试
[root@localhostsoftware]#cattest_sqlserver.py importpyodbc dd=sqlserver('DRIVER=FreeTDS;SERVER=192.168.16.13;PORT=1433;DATABASE=master;UID=dba_test_tmp;PWD=nimeia;TDS_Version=8.0;',"selectgetdate()as'时间'")
最后附上完整的word版:pythonsjkqdaz(jb51.net)