linux+Python+mssql安装笔记

linux+mssql安装笔记

0.升级python2.7
tar -vxf
./configure
make
make install

3.安装ez_setup
解压缩
tar -vxf
python ez_setup.py

easy_install

2.安装Cython
easy_install cython
解压缩
tar -vxf
python setup.py install


1.安装freetds
wget ftp://https://www.360docs.net/doc/1b11616494.html,/pub/freetds/stable/freetds-stable.tgz

解压缩
tar -vxf freetds-stable.tgz

cd freetds-0.91

可以看帮助 ./configure --help
编译
./configure --prefix=/etc/freetds --with-tdsver=7.1 --enable-msdblib --with-gnu-ld --enable-shared --enable-static

///./configure --prefix=/etc/freetds --with-tdsver=8.0 --enable-msdblib --enable-dbmfix --with-gnu-ld --enable-shared --enable-static

编译
make

make install


测试
/etc/freetds/bin/tsql -H 202.198.3.31 -p 1433 -U sa -P 654321
locale is "en_US.UTF-8"
locale charset is "UTF-8"
1> q
2> use sql2001
select * from syslog
go
1>quit

vi ~/.bashrc

# user path
export PATH=$PATH:/etc/freetds/bin
(可以添加 export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/etc/freetds/lib 从而生效lib也可以修改/etc/ld.so.conf.d)
cd /etc/ld.so.conf.d
vi freetds.conf

/etc/freetds/lib

以root权限执行
ldconfig

reboot 更新变量后

查看版本信息
tsql -C
Compile-time settings (established with the "configure" script)
Version: freetds v0.91
freetds.conf directory: /etc/freetds/etc
MS db-lib source compatibility: yes
Sybase binary compatibility: no
Thread safety: yes
iconv library: yes
TDS version: 7.1
iODBC: no
unixodbc: no
SSPI "trusted" logins: no
Kerberos: no


4.安装pymssal
解压缩
tar xvf pymssql-2.1.0.tar.gz

tar -vxf
出现错误
gcc -pthread -shared build/temp.linux-i686-2.7/_mssql.o -L/usr/local/lib -lsybdb -lrt -o build/lib.linux-i686-2.7/_mssql.so
/usr/bin/ld: cannot find -lsybdb

对pymssql-2.1.0源码进行修改

cd /pymssql-2.1.0/freetds/nix_32
rm -fr include
rm -fr lib

ln -s /etc/freetds/include include
ln -s /etc/freetds/lib lib

ll

查看ld的库
ldconfig -p | grep libsy

仍然出错!!!

没办法直接copy freetds的头文件和lib文件到python头库和用户类库


cp /etc/freetds/include/*.h /usr/local/python27/include/python2.7/
cp /etc/freetds/lib/* /usr/local/lib/


python setup.py install

相关链接
https://www.360docs.net/doc/1b11616494.html,/p/pymssql/wiki/Compilation

https://www.360docs.net/doc/1b11616494.html,/







#7 VL.Var...@https://www.360docs.net/doc/1b11616494.html,
subject: my issue fixed

problem was: old lib found in /usr/lib/libsybdb.so.5 (old freetds lib version 0.64)

how it may discovered: enable freetds logging by $ export TDSDUMP=/tmp/freetds.log and very carefully examine results. In my case I wondered that freetds says me my server not found even its have in freetds.conf file, and first l

ine of trace file says what freetds version used

How to fix: carefully remove old freetds version (e.g. /usr/lib/libsybdb.so.5 file),
compile last freetds (I use ./configure --with-tdsver=7.1 --enable-msdblib)
ensure resulting lib be found in ldconfig path ( I symlinked /usr/local/lib/libsybdb.so.5 to /usr/lib/libsybdb.so.5 ( command is "ln -s /usr/local/lib/libsybdb.so.5 /usr/lib/libsybdb.so.5" and rerun ldconfig). Check ldconfig cache for currect lib:
ldconfig -p | grep libsybdb.so.5
libsybdb.so.5 (libc6) => /usr/lib/libsybdb.so.5
Grab last pymssql tarball, untar it
cd to freetds/nix_(your arch) in my case - pymssql-2.0.0b1-dev-20111019/freetds/nix_32
remove include and lib dirs, create symlinks to include and lib directory to freetds source dir. In my case result sounds like:
# ls -la
lrwxrwxrwx 1 root root 18 Oct 24 18:27 include -> /usr/local/include
lrwxrwxrwx 1 root root 14 Oct 24 18:27 lib -> /usr/local/lib
(I compile freetds without any --predix options)
Ensure you using correct python, cyphon and easy install modules
I use:
PATH=/opt/python2.7/bin:$PATH python setup.py clean
PATH=/opt/python2.7/bin:$PATH python setup.py build
PATH=/opt/python2.7/bin:$PATH python setup.py install

After that import pymssql and other code works fine. As additional you can set TDSVER and FREETDS enviroment variable as explained at https://www.360docs.net/doc/1b11616494.html,/userguide/envvar.htm for ensure using correct freetds.conf file (my freetds.conf located in /usr/local/etc directory)
Oct 25, 2011 #8 rsyr...@https://www.360docs.net/doc/1b11616494.html,
Thanks for the follow-up report, glad you got it fixed. FYI, w/ pymssql you should not need a freetds.conf file.

Resolving as fixed since we pushed new source to address the missing x86 pre-compiled libraries.
Status: Fixed
Dec 6, 2011 #9 m.maly...@https://www.360docs.net/doc/1b11616494.html,
In tarball pymssql-2.0.0b1-dev-20111019.tar.gz, 64-bit build still has this problem. Possibly files in freetds/nix_64/* were not updated:

nix_32/lib:
-rw-r--r-- 1 1000 1000 1528394 Oct 18 06:21 libsybdb.a

nix_64/lib:
-rw-r--r-- 1 1000 1000 2169356 Oct 17 06:49 libsybdb.a

What worked for me (I have freetds 0.91-1.el5 installed):
1. rm freetds/nix_64/lib/libsybdb.a
2. ln -s /usr/lib64/libsybdb.so.5.0.0 /usr/lib64/libsybdb.so
3. python26 setup.py clean
4. python26 setup.py build
5. python26 setup.py install


测试程序
import pymssql
conn = pymssql.connect(host='SQL01', user='user', password='password', database='mydatabase')

cur = conn.cursor()
cur.execute('CREATE TABLE persons(id INT, name VARCHAR(100))')
cur.executemany("INSERT INTO persons VALUES(%d, %s)", \
[ (1, 'John Doe'), (2, 'Jane Doe') ])
https://www.360docs.net/doc/1b11616494.html,mit()

cur.execute('SELECT * FROM persons WHERE salesrep=%s', 'John Doe')

row = cur.fetchone()
while row:
print "ID=%d, Name=%s" % (row[0], row[1])
row = cur.fetchone()

conn.close()


import pymssql
conn = pymssql.connect(host='202.198.3.204:1037', user='sa', password='drcom', database='DrcomBRS')
c

ur = conn.cursor()
cur.execute('SELECT ACCOUNT,ENABLE FROM USER_CONFIG WHERE ACCOUNT=%s', '033000000022')
row = cur.fetchone()
while row:
print "ID=%s, Name=%s" % (row[0], row[1])
row = cur.fetchone()
conn.close()

相关文档
最新文档