运维笔记--Debian/Ubuntu系统离线安装pymssql,连接SqlServer
<p><strong>场景描述:</strong></p><p>开始之前,先对Debian和Ubuntu操作系统做个了解,两者都是Linux阵营中很有影响力的发行版本,可以简单理解成“<span style="color: rgba(102, 102, 153, 1)"><strong>Ubuntu源自Debian,两者系统操作命令基本相同,相比Ubuntu,Debian更加稳定。</strong></span>”</p>
<p>关于Debian和Ubuntu的详细异同可以点击此处。</p>
<p>ubuntu和debian的版本对应关系:<span style="color: rgba(51, 51, 153, 1)"><strong><span style="color: rgba(51, 51, 153, 1)">参考</span></strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(102, 102, 153, 1)"><strong>From 10.04 up to 19.04</strong></span><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(102, 102, 153, 1)"><strong>:</strong></span>
<strong><span style="color: rgba(102, 102, 153, 1)">Ubuntu-版本&代号 Debian-代号&版本
</span></strong></span>19.04disco buster/ sid - 10
18.10cosmic buster/<span style="color: rgba(0, 0, 0, 1)"> sid
</span>18.04bionic buster/<span style="color: rgba(0, 0, 0, 1)"> sid
</span>17.10artful stretch / sid - 9
17.04zesty stretch /<span style="color: rgba(0, 0, 0, 1)"> sid
</span>16.10yakkety stretch /<span style="color: rgba(0, 0, 0, 1)"> sid
</span>16.04xenial stretch /<span style="color: rgba(0, 0, 0, 1)"> sid
</span>15.10wily jessie/ sid - 8
15.04vivid jessie/<span style="color: rgba(0, 0, 0, 1)"> sid
</span>14.10utopic jessie/<span style="color: rgba(0, 0, 0, 1)"> sid
</span>14.04trusty jessie/<span style="color: rgba(0, 0, 0, 1)"> sid
</span>13.10saucy wheezy/ sid - 7
13.04raring wheezy/<span style="color: rgba(0, 0, 0, 1)"> sid
</span>12.10quantal wheezy/<span style="color: rgba(0, 0, 0, 1)"> sid
</span>12.04precise wheezy/<span style="color: rgba(0, 0, 0, 1)"> sid
</span>11.10oneiric wheezy/<span style="color: rgba(0, 0, 0, 1)"> sid
</span>11.04natty squeeze / sid - 6
10.10maverick squeeze /<span style="color: rgba(0, 0, 0, 1)"> sid
</span>10.04lucid squeeze / sid</pre>
</div>
<p>回到我们主题,安装python环境下操作SqlServer的包pymssql;「pymssql官方文档」</p>
<p>联网环境的话,如果已安装pip,直接执行pip install pymssql就可以!一条命令搞定。</p>
<p>如果离线环境安装的话,就有点复杂了:</p>
<p>首先确认我们的系统版本:</p>
<div class="cnblogs_code">
<pre>root@9deba54adab7:/<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> cat /etc/debian_version</span>
8.11<span style="color: rgba(0, 0, 0, 1)">
root@9deba54adab7:</span>/<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> uname -a</span>
Linux 9deba54adab7 4.4.0-105-generic <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">128-Ubuntu SMP Thu Dec 14 12:42:11 UTC 2017 x86_64 GNU/Linux</span>
root@9deba54adab7:/<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> more /etc/issue</span>
Debian GNU/Linux 8 \n \l</pre>
</div>
<p>大概心里有个印象,当前我们系统版本为Debian 8,大体对应ubuntu的14.04。</p>
<p>接下来,我们先按照正常的安装步骤走一遍:</p>
<p>(1) 首先下载pymssql离线安装源码包--注意包版本号,以及支持的python版本:https://pypi.org/project/pymssql/2.1.4/</p>
<p> 帮人帮到底:点击 <strong>Dowload files <span style="color: rgba(255, 0, 0, 1)">(PS: 小伙伴们注意下,这个版本的包可能装不上,这里只是提供给想要测试我遇到的安装问题的版本)</span></strong></p>
<p><img src="https://img2018.cnblogs.com/blog/575577/201909/575577-20190909105800072-551180676.png" alt="" width="550"></p>
<p> </p>
<p><img src="https://img2018.cnblogs.com/blog/575577/201909/575577-20190909105857842-1508770925.png" alt="" width="550"></p>
<p>下载并上传到服务器本地之后,解压pymssql-2.1.4.tar.gz,然后进入解压后的目录,执行:</p>
<p>python setup.py build</p>
<p>python setup.py install</p>
<p>(上边两条命令,执行的前提是系统已安装了setuptools包:https://pypi.org/project/setuptools/#downloads)</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">执行python setup.py build的时候出现如下异常:
Traceback (most recent call last):
File </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">setup.py</span><span style="color: rgba(128, 0, 0, 1)">"</span>, line 88, <span style="color: rgba(0, 0, 255, 1)">in</span> <module>
<span style="color: rgba(0, 0, 255, 1)">from</span> Cython.Distutils <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> build_ext as _build_ext
File </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">/mnt/tt_package/pymssql-2.1.4/Cython-0.29.13-py2.7-linux-x86_64.egg/Cython/Distutils/__init__.py</span><span style="color: rgba(128, 0, 0, 1)">"</span>, line 1, <span style="color: rgba(0, 0, 255, 1)">in</span> <module>
<span style="color: rgba(0, 0, 255, 1)">from</span> Cython.Distutils.build_ext <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> build_ext
File </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">/mnt/tt_package/pymssql-2.1.4/Cython-0.29.13-py2.7-linux-x86_64.egg/Cython/Distutils/build_ext.py</span><span style="color: rgba(128, 0, 0, 1)">"</span>, line 5, <span style="color: rgba(0, 0, 255, 1)">in</span> <module>
<span style="color: rgba(0, 0, 255, 1)">from</span> setuptools.command.build_ext <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> build_ext as _build_ext
File </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">/usr/lib/python2.7/dist-packages/setuptools/command/build_ext.py</span><span style="color: rgba(128, 0, 0, 1)">"</span>, line 50, <span style="color: rgba(0, 0, 255, 1)">in</span> <module>
<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> build_ext(_build_ext):
<strong><span style="color: rgba(255, 0, 0, 1)">TypeError: Error when calling the metaclass bases
str() takes at most </span></strong></span><strong><span style="color: rgba(255, 0, 0, 1)">1 argument (3 given)</span></strong></pre>
</div>
<p>基于上述异常,网络查找资料,WTF,资料少的可怜,难道跟Linux下用某软sqlserver的用户少有关么?!找了半天,无果!</p>
<p>中途尝试了其他安装python包的方式,如:dpkg的方式安装.deb包,https://pkgs.org/download/pymssql</p>
<p><img src="https://img2018.cnblogs.com/blog/575577/201909/575577-20190909115206214-1660752962.png" alt="" width="550"></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">将上述包,下载下来后执行安装:
dpkg </span>-i python-pymssql_1.0.2+dfsg-1+b3_amd64.deb</pre>
</div>
<p>结果~ 结果~!报了一堆依赖问题,擦汗!来来来,顺便提供个下载debian依赖包的地址:https://packages.debian.org/buster/libsybdb5</p>
<p>学了一遍,ubuntu下如何搜集依赖及解决依赖问题:</p>
<p>https://www.youcl.com/info/5946</p>
<p>https://blog.csdn.net/junbujianwpl/article/details/52811153</p>
<p>最后,最后还是无果!</p>
<p>***********************************************************************************</p>
<p>分割线之后,休息一下,中午吃个饭,一直寻思到底啥问题,甚至有了放弃的念头,实在不行,写个Django接口服务,也是一种思路提供参考!</p>
<p><span style="color: rgba(102, 102, 153, 1)"><strong>即:在可以联网的机器上用Docker装好pymssql环境,用Django起个服务,跟当前需要访问sqlserver的程序部署在同一台服务器上,通过调用接口来实现间接访问sqlserver数据库。</strong></span></p>
<p><span style="color: rgba(102, 102, 153, 1)"><strong>PS:这块功能已实现,回头梳理下整理出来。</strong></span></p>
<p>***********************************************************************************</p>
<p>还是不甘心,怎么办?怎么办?难道我们安装的版本出了问题么?</p>
<p>更换版本试下;pymssql-2.1.1.tar.gz</p>
<p><img src="https://img2018.cnblogs.com/blog/575577/201909/575577-20190909113550410-434817261.png" alt="" width="550"></p>
<p>按照第一次的安装步骤,上传解压,执行python setup.py install;出现异常:</p>
<div class="cnblogs_code">
<pre>root@2bb33e0d50af:/mnt/tt_package/pymssql-2.1.1<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> python setup.py install</span>
setup.py: platform.system() => <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Linux</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
setup.py: platform.architecture() </span>=> (<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">64bit</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">''</span><span style="color: rgba(0, 0, 0, 1)">)
setup.py: platform.linux_distribution() </span>=> (<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">debian</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">8.11</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">''</span><span style="color: rgba(0, 0, 0, 1)">)
setup.py: platform.libc_ver() </span>=> (<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">glibc</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">2.4</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
setup.py: Not using bundled FreeTDS
setup.py: include_dirs </span>= [<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">/usr/local/include</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">]
setup.py: library_dirs </span>= [<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">/usr/local/lib</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">]
Installed </span>/mnt/tt_package/pymssql-2.1.1/setuptools_git-1.2-py2.7<span style="color: rgba(0, 0, 0, 1)">.egg
running install
running bdist_egg
running egg_info
writing pymssql.egg</span>-info/PKG-<span style="color: rgba(0, 0, 0, 1)">INFO
writing top</span>-level names to pymssql.egg-info/<span style="color: rgba(0, 0, 0, 1)">top_level.txt
writing dependency_links to pymssql.egg</span>-info/<span style="color: rgba(0, 0, 0, 1)">dependency_links.txt
reading manifest file </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">pymssql.egg-info/SOURCES.txt</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
writing manifest file </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">pymssql.egg-info/SOURCES.txt</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
installing library code to build</span>/bdist.linux-x86_64/<span style="color: rgba(0, 0, 0, 1)">egg
running install_lib
running build_ext
building </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">_mssql</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)"> extension
creating build
creating build</span>/temp.linux-x86_64-2.7<span style="color: rgba(0, 0, 0, 1)">
x86_64</span>-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 -<span style="color: rgba(0, 0, 0, 1)">DMSDBLIB
_mssql.c:</span>314:22: fatal error: sqlfront.h: No such file <span style="color: rgba(0, 0, 255, 1)">or</span><span style="color: rgba(0, 0, 0, 1)"> directory
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">include "sqlfront.h"</span>
^<span style="color: rgba(0, 0, 0, 1)">
compilation terminated.
<span style="color: rgba(255, 0, 0, 1)"><strong>error: command </strong></span></span><span style="color: rgba(255, 0, 0, 1)"><strong>'x86_64-linux-gnu-gcc' failed with exit status 1</strong></span></pre>
</div>
<p>出现了新的错误,网上根据错误关键字查找,</p>
<p>找到如下靠谱参考:</p>
<p>https://blog.csdn.net/u012965373/article/details/52759391</p>
<p>https://blog.csdn.net/Mr0Yang/article/details/52351595</p>
<p>https://blog.51cto.com/linuxshow/1407255</p>
<p>https://www.cnblogs.com/dbcloud/p/6397270.html</p>
<p>里边基本上都提到了,系统缺少包:<strong><span style="color: rgba(255, 0, 0, 1)">freetds</span></strong>,会导致安装pymssql的时候,出现异常:<strong><span style="color: rgba(255, 0, 0, 1)">error: command 'gcc' failed with exit status 1</span></strong></p>
<p> OK!</p>
<p>我们接着继续安装缺少的包:freetds,上边几篇参考博客中基本附了下载地址:http://mirrors.ibiblio.org/freetds/stable/</p>
<p><img src="https://img2018.cnblogs.com/blog/575577/201909/575577-20190909120521470-1129743179.png" alt="" width="550"></p>
<p>也可以<strong>点击这里下载</strong> 提取码: fpmw </p>
<p>下载到本地后,上传到目标服务器,然后解压tgz包安装:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 解压</span>
tar zxvf freetds-<span style="color: rgba(0, 0, 0, 1)">stable.tgz
cd freetds</span>-0.91
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 编译安装 ---耐心等待执行完成</span>
./configure --prefix=/usr/local/freetds --with-tdsver=8.0 --enable-msdblib --enable-dbmfix --with-gnu-ld --enable-shared --enable-<span style="color: rgba(0, 0, 0, 1)">static
make </span>&&<span style="color: rgba(0, 0, 0, 1)"> make install
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 配置freetds:</span>
echo <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">/usr/local/freetds/lib</span><span style="color: rgba(128, 0, 0, 1)">"</span> >> /etc/ld.so.conf.d/<span style="color: rgba(0, 0, 0, 1)">freetds.conf
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 然后运行以下指令使配置生效</span>
ldconfig -v</pre>
</div>
<p>到此没有出现异常的话,基本上freetds包已安装完成!</p>
<p>接下来重头戏,重新执行安装:pymssql-2.1.1.tar.gz,到相应目录下执行: python setup.py install ---耐心等待</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">.....................省略部分输出...............
creating </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">dist/pymssql-2.1.1-py2.7-linux-x86_64.egg</span><span style="color: rgba(128, 0, 0, 1)">'</span> <span style="color: rgba(0, 0, 255, 1)">and</span> adding <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">build/bdist.linux-x86_64/egg</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)"> to it
removing </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">build/bdist.linux-x86_64/egg</span><span style="color: rgba(128, 0, 0, 1)">'</span> (<span style="color: rgba(0, 0, 255, 1)">and</span><span style="color: rgba(0, 0, 0, 1)"> everything under it)
Processing pymssql</span>-2.1.1-py2.7-linux-<span style="color: rgba(0, 0, 0, 1)">x86_64.egg
creating </span>/usr/local/lib/python2.7/dist-packages/pymssql-2.1.1-py2.7-linux-<span style="color: rgba(0, 0, 0, 1)">x86_64.egg
Extracting pymssql</span>-2.1.1-py2.7-linux-x86_64.egg to /usr/local/lib/python2.7/dist-<span style="color: rgba(0, 0, 0, 1)">packages
Adding pymssql </span>2.1.1 to easy-<span style="color: rgba(0, 0, 0, 1)">install.pth file
Installed </span>/usr/local/lib/python2.7/dist-packages/pymssql-2.1.1-py2.7-linux-<span style="color: rgba(0, 0, 0, 1)">x86_64.egg
Processing dependencies </span><span style="color: rgba(0, 0, 255, 1)">for</span> pymssql==2.1.1<span style="color: rgba(0, 0, 0, 1)">
Finished processing dependencies </span><span style="color: rgba(0, 0, 255, 1)">for</span> pymssql==2.1.1</pre>
</div>
<p>按耐不住内心的小激动,OK,验证下是否安装成功:pip list 查看安装情况</p>
<p><img src="https://img2018.cnblogs.com/blog/575577/201909/575577-20190909123000901-72251308.png" alt="" width="300"></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> python环境下执行导入操作:</span>
root@2bb33e0d50af:/mnt/tt_package/pymssql-2.1.1<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> python</span>
Python 2.7.9 (default, Sep 25 2018, 20:42:16<span style="color: rgba(0, 0, 0, 1)">)
on linux2
Type </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">help</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">copyright</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">credits</span><span style="color: rgba(128, 0, 0, 1)">"</span> <span style="color: rgba(0, 0, 255, 1)">or</span> <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">license</span><span style="color: rgba(128, 0, 0, 1)">"</span> <span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> more information.
</span>>>>
>>> <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> pymssql
</span>>>></pre>
</div>
<p>不容易~ 一波三折~ 搞定啦!!!</p>
<p>如果提示缺少包:setuptools-git</p>
<p>到下面地址下载:</p>
<p>https://pypi.org/project/setuptools-git/#files</p>
<p> </p>
<p><span style="color: rgba(128, 0, 0, 1)"><strong>PS: 虽然我按照上边的流程,最终安上了,但是小伙伴们可能遇到新的问题,如果有新的情况,欢迎留言区补充,大家共同学习进步。</strong></span></p>
<p> </p><br><br>
来源:https://www.cnblogs.com/hellojesson/p/11481502.html
頁:
[1]