Linux下通過python訪問MySQL、Oracle、SQL Server數(shù)據(jù)庫的方法
本文檔主要描述了Linux下python數(shù)據(jù)庫驅(qū)動(dòng)的安裝和配置,用來實(shí)現(xiàn)在Linux平臺(tái)下通過python訪問MySQL、Oracle、SQL Server數(shù)據(jù)庫。
其中包括以下幾個(gè)軟件的安裝及配置:
unixODBC
FreeTDS
pyodbc
cx_Oracle
歡迎轉(zhuǎn)載,請(qǐng)注明作者、出處。
作者:張正
QQ:176036317
如有疑問,歡迎聯(lián)系。
本文檔主要描述了Linux下python數(shù)據(jù)庫驅(qū)動(dòng)的安裝和配置,用來實(shí)現(xiàn)在Linux平臺(tái)下通過python訪問MySQL、Oracle、SQL Server數(shù)據(jù)庫。
一、前提概述...
二、驅(qū)動(dòng)安裝...
1. unixODBC安裝...
2. FreeTds安裝...
3. pyodbc安裝...
4. cx_Oracle安裝...
三、驅(qū)動(dòng)配置...
四、連接測(cè)試...
一、前提概述
pyodbc是一個(gè)Python模塊,能夠讓python使用 ODBC 連接來自Windows, Linux, OS/X等系統(tǒng)中的大部分?jǐn)?shù)據(jù)庫。pyodbc依賴于unixODBC和FreeTDS,因此需要先安裝unixODBC和FreeTDS。(FreeTDS是一個(gè)開源的C程序庫,它可以實(shí)現(xiàn)在Linux系統(tǒng)下訪問操作SQL Server數(shù)據(jù)庫)
cx_Oracle 是一個(gè)用來連接并操作 Oracle 數(shù)據(jù)庫的 Python 擴(kuò)展模塊, 支持包括 Oracle 9.2 10.2 以及 11.1 等版本。
二、驅(qū)動(dòng)安裝
1. unixODBC安裝
a.下載unixODBC安裝包:unixODBC-2.3.2.tar.gz
下載地址:http://www.linuxfromscratch.org/blfs/view/cvs/general/unixodbc.html
b.解壓:tar -zxf unixODBC-2.3.2.tar.gz
c.安裝:cd unixODBC-2.3.2
./configure --enable-gui=no
make
make install
NOTES:
編譯安裝三個(gè)步驟之間,可通過echo $?查看退出狀態(tài),為0則正常,可進(jìn)行下一步操作。(MYSQL驅(qū)動(dòng)可安裝MySQL-python.x86_64)
2. FreeTds安裝
下載FreeTds安裝包:freetds-stable.tgz
下載地址:http://www.freetds.org/
tar -zxvf freetds-stable.tgz
cd freetds-0.91
./configure --prefix=/etc/freetds --with-tdsver=7.0 --enable-msdblib --with-gnu-ld --enable-shared --enable-static
make
make install
同樣可通過echo $?查看退出狀態(tài),安裝完后可查看freeTds相關(guān)版本及安裝信息:
cd src/apps/
./tsql -C
[root@localhost apps]# ./tsql -C Compile-time settings (established with the "configure" script) Version: freetds v0.91 freetds.conf directory: /usr/local/etc MS db-lib source compatibility: no Sybase binary compatibility: no Thread safety: yes iconv library: yes TDS version: 7.1 iODBC: no unixodbc: yes SSPI "trusted" logins: no Kerberos: no
3. pyodbc安裝
下載pyodbc-3.0.6.zip
下載地址:http://www.dbjr.com.cn/softs/453989.html
unzip pyodbc-3.0.6.zip
cd pyodbc-3.0.6
python setup.py build
python setup.py install
NOTES:安裝時(shí)build 遇到以下錯(cuò)誤,而且gcc相關(guān)包已經(jīng)安裝完全
/root/software/pyodbc-3.0.6/src/connection.h:27: error: ‘uintptr_t' does not name a type
error: command 'gcc' failed with exit status 1
查看python版本,發(fā)現(xiàn)其為python 2.4,通過官網(wǎng)下載頁面信息對(duì)比,可知pyodbc-3.0.6不支持python 2.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:
需要下載對(duì)應(yīng)版本的驅(qū)動(dòng)
4.2 oracle instant client 安裝:
gunzip 10201_client_linux_x86_64.cpio.gz
cpio -idmv < 10201_client_linux_x86_64.cpio
在~/.bash_profile中添加oracle相關(guān)配置:
export ORACLE_BASE=/opt/oracle
export ORACLE_HOME=$ORACLE_BASE/client_10g
export LD_LIBRARY_PATH=$ORACLE_HOME:$LD_LIBRARY_PATH
建立oracle用戶和文件目錄
groupadd dba
useradd -g dba oracle
su - oracle
mkdir -p /opt/oracle/client_10g
配置.bash_profile
回到root用戶:
mv client /home/oracle/
[root@localhost software]# chown -R oracle:dba /home/oracle/client
su - oracle 進(jìn)行安裝:
cd /home/oracle/client/response
vi instantClient.rsp
修改以下參數(shù):
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
python setup.py build
python setup.py install
4.3 安裝cx_Oracle:
rpm -ivh cx_oracle:cx_Oracle-5.1.2-10g-py27-1.x86_64.rpm
三、驅(qū)動(dòng)配置
1. 配置freetds:
vi /etc/freetds/etc/freetds.conf
text size = 10240000
client charset = UTF-8
2. 配置freetds驅(qū)動(dòng):
vi freetds.ini
[FreeTDS]
Description = 0.91 with protocol v7.0
Driver = /etc/freetds/lib/libtdsodbc.so
odbcinst -i -d -f freetds.ini
3. 配置數(shù)據(jù)源:
vi db.ini
[SQLSERVER]
Driver = FreeTDS
Description = SQLSERVER
Trace =No
Server =192.168.16.13
Port =1433
Database=master
#此處需要配置SQL Server數(shù)據(jù)的連接信息
odbcinst -i -s -f db.ini
cat /root/.odbc.ini
[SQLSERVER]
Driver=FreeTDS
Description=SQLSERVER
Trace=No
Server=192.168.16.13
Port=1433
Database=master
NOTES:
若見到以下錯(cuò)誤:
import cx_Oracle
Traceback (most recent call last):
File "", line 1, in ?
ImportError: libclntsh.so.10.1: cannot open shared object file: No such file or directory
則需要找到對(duì)應(yīng)的文件建立軟鏈接,如:
ln -s /opt/oracle/db_10g/lib/libclntsh.so.10.1 /usr/lib64/libclntsh.so.10.1
四、連接測(cè)試
1. 前提說明:
本處提供的連接測(cè)試,僅僅是一個(gè)簡(jiǎn)單的連通并發(fā)起查詢,以證明驅(qū)動(dòng)的正確安裝和配置。
可先進(jìn)行import 查看驅(qū)動(dòng)安裝是否有誤:
[root@localhost etc]# python
Python 2.4.3 (#1, Apr 14 2011, 20:41:59)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb #測(cè)試MySQL
>>> import cx_Oracle #測(cè)試Oracle
>>> import pyodbc #測(cè)試pyodbc,可訪問SQL Server
>>>
若驅(qū)動(dòng)正確安裝,執(zhí)行import操作就不會(huì)報(bào)錯(cuò)。
2. MySQL連接測(cè)試:
[root@localhost software]# cat mysql_test.py import MySQLdb sql='show databases'; conn=MySQLdb.connect(host='localhost',user='test',passwd='test',db='test',port=3306) cur=conn.cursor() cur.execute(sql) print cur.fetchone() [root@localhost software]# python mysql_test.py ('information_schema',)
3.Oracle連接測(cè)試:
[root@localhost software]# cat test.py import time import cx_Oracle sql2="select * from tab"; connstr="system/oracle@192.168.43.130:1521/orcl" conn=cx_Oracle.connect(connstr) cur=conn.cursor() aa=cur.execute(sql2) print cur.fetchone() print cur.rowcount print "connection string is : ",conn.dsn print "the database version is: ",conn.version [root@localhost software]# python test.py ('SYSCATALOG', 'SYNONYM', None) 1 connection string is : 192.168.43.130:1521/orcl the database version is: 10.2.0.1.0
4.SQL Server連接測(cè)試
[root@localhost software]# cat test_sqlserver.py
import pyodbc
dd=sqlserver('DRIVER=FreeTDS;SERVER=192.168.16.13;PORT=1433;DATABASE=master;UID=dba_test_tmp;PWD=nimeia;TDS_Version=8.0;',"select getdate() as '時(shí)間'")
最后附上完整的word版:pythonsjkqdaz(jb51.net)
- Python3.7 pyodbc完美配置訪問access數(shù)據(jù)庫
- 詳解js文件通過python訪問數(shù)據(jù)庫方法
- 對(duì)Python通過pypyodbc訪問Access數(shù)據(jù)庫的方法詳解
- Python使用pyodbc訪問數(shù)據(jù)庫操作方法詳解
- Python輕量級(jí)ORM框架Peewee訪問sqlite數(shù)據(jù)庫的方法詳解
- Python的Tornado框架實(shí)現(xiàn)異步非阻塞訪問數(shù)據(jù)庫的示例
- python訪問mysql數(shù)據(jù)庫的實(shí)現(xiàn)方法(2則示例)
- python使用MySQLdb訪問mysql數(shù)據(jù)庫的方法
- Python訪問純真IP數(shù)據(jù)庫腳本分享
- 在Linux中通過Python腳本訪問mdb數(shù)據(jù)庫的方法
- Shell、Perl、Python、PHP訪問 MySQL 數(shù)據(jù)庫代碼實(shí)例
- python訪問純真IP數(shù)據(jù)庫的代碼
- 使用Python通過oBIX協(xié)議訪問Niagara數(shù)據(jù)的示例
相關(guān)文章
Python+gensim實(shí)現(xiàn)文本相似度分析詳解
這篇文章主要介紹了Python+gensim實(shí)現(xiàn)文本相似度分析詳解,在開發(fā)中我們會(huì)遇到進(jìn)行文本相似度分析的需求,計(jì)算文本相似度,用于鑒別文章是否存在抄襲,需要的朋友可以參考下2023-07-07NetWorkX使用方法及nx.draw()相關(guān)參數(shù)解讀
這篇文章主要介紹了NetWorkX使用方法及nx.draw()相關(guān)參數(shù)解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12Python?如何實(shí)現(xiàn)批量轉(zhuǎn)換視頻音頻的采樣率
這篇文章主要分享一個(gè)python代碼,可以將多個(gè)視頻中的音頻轉(zhuǎn)化為相同采樣率的視頻,具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下2021-11-11Python matplotlib圖例放在外側(cè)保存時(shí)顯示不完整問題解決
這篇文章主要介紹了Python matplotlib圖例放在外側(cè)保存時(shí)顯示不完整問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07