運維筆記--Debian/Ubuntu系統離線安裝pymssql,鏈接SqlServer

場景描述:html

開始以前,先對Debian和Ubuntu操做系統作個瞭解,二者都是Linux陣營中頗有影響力的發行版本,能夠簡單理解成「Ubuntu源自Debian,二者系統操做命令基本相同,相比Ubuntu,Debian更加穩定。python

關於Debian和Ubuntu的詳細異同能夠點擊此處linux

ubuntu和debian的版本對應關係:參考git

From 10.04 up to 19.04:
Ubuntu-版本&代號 Debian-代號&版本 19.04  disco      buster  / sid   - 10
18.10  cosmic     buster  / sid
18.04  bionic     buster  / sid
17.10  artful     stretch / sid   - 9
17.04  zesty      stretch / sid
16.10  yakkety    stretch / sid
16.04  xenial     stretch / sid
15.10  wily       jessie  / sid   - 8
15.04  vivid      jessie  / sid
14.10  utopic     jessie  / sid
14.04  trusty     jessie  / sid
13.10  saucy      wheezy  / sid   - 7
13.04  raring     wheezy  / sid
12.10  quantal    wheezy  / sid
12.04  precise    wheezy  / sid
11.10  oneiric    wheezy  / sid
11.04  natty      squeeze / sid   - 6
10.10  maverick   squeeze / sid
10.04  lucid      squeeze / sid

回到咱們主題,安裝python環境下操做SqlServer的包pymssql;「pymssql官方文檔」sql

聯網環境的話,若是已安裝pip,直接執行pip install pymssql就能夠!一條命令搞定。數據庫

若是離線環境安裝的話,就有點複雜了:ubuntu

首先確認咱們的系統版本:服務器

root@9deba54adab7:/# cat /etc/debian_version
8.11
root@9deba54adab7:/# uname -a
Linux 9deba54adab7 4.4.0-105-generic #128-Ubuntu SMP Thu Dec 14 12:42:11 UTC 2017 x86_64 GNU/Linux
root@9deba54adab7:/# more /etc/issue
Debian GNU/Linux 8 \n \l

大概內心有個印象,當前咱們系統版本爲Debian 8,大致對應ubuntu的14.04。網絡

接下來,咱們先按照正常的安裝步驟走一遍:python2.7

(1) 首先下載pymssql離線安裝源碼包--注意包版本號,以及支持的python版本:https://pypi.org/project/pymssql/2.1.4/

 幫人幫到底:點擊 Dowload files

 

下載並上傳到服務器本地以後,解壓pymssql-2.1.4.tar.gz,而後進入解壓後的目錄,執行:

python setup.py build

python setup.py install

(上邊兩條命令,執行的前提是系統已安裝了setuptools包:https://pypi.org/project/setuptools/#downloads

執行python setup.py build的時候出現以下異常:
Traceback (most recent call last):
  File "setup.py", line 88, in <module>
    from Cython.Distutils import build_ext as _build_ext
  File "/mnt/tt_package/pymssql-2.1.4/Cython-0.29.13-py2.7-linux-x86_64.egg/Cython/Distutils/__init__.py", line 1, in <module>
    from Cython.Distutils.build_ext import build_ext
  File "/mnt/tt_package/pymssql-2.1.4/Cython-0.29.13-py2.7-linux-x86_64.egg/Cython/Distutils/build_ext.py", line 5, in <module>
    from setuptools.command.build_ext import build_ext as _build_ext
  File "/usr/lib/python2.7/dist-packages/setuptools/command/build_ext.py", line 50, in <module>
    class build_ext(_build_ext):
TypeError: Error when calling the metaclass bases str() takes at most 1 argument (3 given)

基於上述異常,網絡查找資料,WTF,資料少的可憐,難道跟Linux下用某軟sqlserver的用戶少有關麼?!找了半天,無果!

中途嘗試了其餘安裝python包的方式,如:dpkg的方式安裝.deb包,https://pkgs.org/download/pymssql

將上述包,下載下來後執行安裝: 
dpkg -i python-pymssql_1.0.2+dfsg-1+b3_amd64.deb

結果~ 結果~!報了一堆依賴問題,擦汗!來來來,順便提供個下載debian依賴包的地址:https://packages.debian.org/buster/libsybdb5

學了一遍,ubuntu下如何蒐集依賴及解決依賴問題:

https://www.youcl.com/info/5946

https://blog.csdn.net/junbujianwpl/article/details/52811153

最後,最後仍是無果!

***********************************************************************************

分割線以後,休息一下,中午吃個飯,一直尋思到底啥問題,甚至有了放棄的念頭,實在不行,寫個Django接口服務,也是一種思路提供參考!

即:在能夠聯網的機器上用Docker裝好pymssql環境,用Django起個服務,跟當前須要訪問sqlserver的程序部署在同一臺服務器上,經過調用接口來實現間接訪問sqlserver數據庫。

PS:這塊功能已實現,回頭梳理下整理出來。

***********************************************************************************

仍是不甘心,怎麼辦?怎麼辦?難道咱們安裝的版本出了問題麼?

更換版本試下;pymssql-2.1.1.tar.gz

按照第一次的安裝步驟,上傳解壓,執行python setup.py install;出現異常:

root@2bb33e0d50af:/mnt/tt_package/pymssql-2.1.1# python setup.py install
setup.py: platform.system() => 'Linux'
setup.py: platform.architecture() => ('64bit', '')
setup.py: platform.linux_distribution() => ('debian', '8.11', '')
setup.py: platform.libc_ver() => ('glibc', '2.4')
setup.py: Not using bundled FreeTDS
setup.py: include_dirs = ['/usr/local/include']
setup.py: library_dirs = ['/usr/local/lib']

Installed /mnt/tt_package/pymssql-2.1.1/setuptools_git-1.2-py2.7.egg
running install
running bdist_egg
running egg_info
writing pymssql.egg-info/PKG-INFO
writing top-level names to pymssql.egg-info/top_level.txt
writing dependency_links to pymssql.egg-info/dependency_links.txt
reading manifest file 'pymssql.egg-info/SOURCES.txt'
writing manifest file 'pymssql.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_ext
building '_mssql' extension
creating build
creating build/temp.linux-x86_64-2.7
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC -I/usr/local/include -I/usr/include/python2.7 -c _mssql.c -o build/temp.linux-x86_64-2.7/_mssql.o -DMSDBLIB
_mssql.c:314:22: fatal error: sqlfront.h: No such file or directory
 #include "sqlfront.h"
                      ^
compilation terminated.
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

出現了新的錯誤,網上根據錯誤關鍵字查找,

找到以下靠譜參考:

https://blog.csdn.net/u012965373/article/details/52759391

https://blog.csdn.net/Mr0Yang/article/details/52351595

https://blog.51cto.com/linuxshow/1407255

http://www.javashuo.com/article/p-vxmwsvuq-dh.html

裏邊基本上都提到了,系統缺乏包:freetds,會致使安裝pymssql的時候,出現異常:error: command 'gcc' failed with exit status 1

 OK!

咱們接着繼續安裝缺乏的包:freetds,上邊幾篇參考博客中基本附了下載地址:http://mirrors.ibiblio.org/freetds/stable/

也能夠點擊這裏下載 提取碼: fpmw 

下載到本地後,上傳到目標服務器,而後解壓tgz包安裝:

# 解壓
tar zxvf freetds-stable.tgz 
cd freetds-0.91 
# 編譯安裝 ---耐心等待執行完成
./configure --prefix=/usr/local/freetds --with-tdsver=8.0 --enable-msdblib --enable-dbmfix --with-gnu-ld --enable-shared --enable-static 

make && make install 

# 配置freetds:
echo "/usr/local/freetds/lib" >> /etc/ld.so.conf.d/freetds.conf 
# 而後運行如下指令使配置生效
ldconfig -v

到此沒有出現異常的話,基本上freetds包已安裝完成!

接下來重頭戲,從新執行安裝:pymssql-2.1.1.tar.gz,到相應目錄下執行: python setup.py install  ---耐心等待

.....................省略部分輸出...............
creating 'dist/pymssql-2.1.1-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 pymssql-2.1.1-py2.7-linux-x86_64.egg
creating /usr/local/lib/python2.7/dist-packages/pymssql-2.1.1-py2.7-linux-x86_64.egg
Extracting pymssql-2.1.1-py2.7-linux-x86_64.egg to /usr/local/lib/python2.7/dist-packages
Adding pymssql 2.1.1 to easy-install.pth file

Installed /usr/local/lib/python2.7/dist-packages/pymssql-2.1.1-py2.7-linux-x86_64.egg
Processing dependencies for pymssql==2.1.1
Finished processing dependencies for pymssql==2.1.1

按耐不住心裏的小激動,OK,驗證下是否安裝成功:pip list 查看安裝狀況

# python環境下執行導入操做:
root@2bb33e0d50af:/mnt/tt_package/pymssql-2.1.1# python
Python 2.7.9 (default, Sep 25 2018, 20:42:16) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> import pymssql
>>>

不容易~ 一波三折~ 搞定啦!!!

相關文章
相關標籤/搜索