zoukankan      html  css  js  c++  java
  • Linux平台使用Freetds连接SQL Server服务器,兼容PHP和Laravel

    本文在CentOS 7 64bit和Laravel 4.2环境测试通过。

     

    1.下载源码并解压缩

    [html] view plain copy
     
    1. wget ftp://ftp.freetds.org/pub/freetds/stable/freetds-stable.tgz  
    [html] view plain copy
     
    1. tar zxvf freetds-stable.tgz   
    [html] view plain copy
     
    1. cd freetds-0.91  

    2.配置并生成makefile

    [html] view plain copy
     
    1. ./configure --with-tdsver=8.0 --enable-msdblib   

    3.编译安装

    [html] view plain copy
     
    1. make  
    2. sudo make install  

    4.配置

    默认安装的配置文件位于/usr/local/etc,在/usr/local/etc编辑freetds.conf 文件,默认为

    [html] view plain copy
     
    1. #   $Id: freetds.conf,v 1.12 2007/12/25 06:02:36 jklowden Exp $  
    2. #  
    3. # This file is installed by FreeTDS if no file by the same   
    4. # name is found in the installation directory.    
    5. #  
    6. # For information about the layout of this file and its settings,   
    7. # see the freetds.conf manpage "man freetds.conf".    
    8.   
    9. # Global settings are overridden by those in a database  
    10. # server specific section  
    11. [global]  
    12.         # TDS protocol version  
    13. ;       tds version = 4.2  
    14.   
    15.         # Whether to write a TDSDUMP file for diagnostic purposes  
    16.         # (setting this to /tmp is insecure on a multi-user system)  
    17. ;       dump file = /tmp/freetds.log  
    18. ;       debug flags = 0xffff  
    19.   
    20.         # Command and connection timeouts  
    21. ;       timeout = 10  
    22. ;       connect timeout = 10  
    23.   
    24.         # If you get out-of-memory errors, it may mean that your client  
    25.         # is trying to allocate a huge buffer for a TEXT field.    
    26.         # Try setting 'text size' to a more reasonable limit   
    27.         text size = 64512  
    28.   
    29. # A typical Sybase server  
    30. [egServer50]  
    31.         host = symachine.domain.com  
    32.         port = 5000  
    33.         tds version = 5.0  
    34.   
    35. # A typical Microsoft server  
    36. [egServer70]  
    37.         host = ntmachine.domain.com  
    38.         port = 1433  
    39.         tds version = 7.0  

    在文件的最后位置添加如下配置,即可连接SQL Server 2000

    [html] view plain copy
     
    1. [sql-server-2000]  
    2.         host = 192.168.182.9  
    3.         port = 1433  
    4.         tds version = 7.0  

    如果要连接SQL Server 2005或2008,需要添加以下配置

    [html] view plain copy
     
    1. [sql-server-2005]  
    2.         host = 192.168.70.1  
    3.         port = 1433  
    4.         tds version = 8.0  



    4.测试

    [html] view plain copy
     
    1. /usr/local/bin/tsql -S sql-server-2000 -U sa -P test   


    如果成功连接,将会出现以下提示

    [html] view plain copy
     
    1. locale is "zh_CN.UTF-8"  
    2. locale charset is "UTF-8"  
    3. using default charset "UTF-8"  
    4. 1>  

    至此,FreeTDS已经是Linux具备连接SQL Server的功能了。

    5.编译PHP扩展

    PHP 5.4之后已经没有原生支持的SQL Server的驱动了,因此需要手动编译PHP源码的扩展添加对SQL Server的驱动支持。CentOS 7自带的是5.4版本的PHP,因此我们通过编译5.4版的PHP源码获得扩展。

    目前CentOS yum源里最新的php是5.4.16,php可以通过yum安装到系统

    [html] view plain copy
     
    1. sudo yum install php php-devel php-fpm php-common php-mysql php-pdo libzip  

    php官网上最新的5.4版本是 5.4.39,下载源码到本地

    [html] view plain copy
     
    1. wget http://cn2.php.net/distributions/php-5.4.39.tar.gz  


    解压并进入扩展目录

    [html] view plain copy
     
    1. tar zxvf php-5.4.39.tar.gz  
    2. cd php-5.4.39/ext/mssql  

    使用phpize生成configure脚本文件

    [html] view plain copy
     
    1. phpize  



    生成makefile

    [html] view plain copy
     
    1. ./configure  


    编译

    [html] view plain copy
     
    1. make  


    编译之后将会在modules子目录生成mssql.so扩展文件。复制扩展文件到php的扩展文件目录

    [html] view plain copy
     
    1. sudo cp modules/mssql.so /usr/lib64/php/modules/  

    在/etc/php.d目录下新建mssql.ini 文件,输入以下内容

    [html] view plain copy
     
    1. ; Enable mssql extension module  
    2. extension=mssql.so  


    这样PHP就能加载SQL Server驱动了。使用如下代码测试PHP连接SQL Server。

    [html] view plain copy
     
    1. <?php  
    2. header("Content-type: text/html; charset=utf-8");  
    3.   
    4. $msdb=mssql_connect("sql-server-2000","sa","test");  
    5. if (!$msdb) {  
    6.         echo "connect sqlserver error";  
    7.         exit;       
    8. }  
    9.   
    10. mssql_select_db("msdb",$msdb);  
    11. $result = mssql_query("SELECT top 5 * FROM employee", $msdb);  
    12. while($row = mssql_fetch_array($result)) {  
    13.         var_dump($row);  
    14. }  
    15.   
    16. mssql_free_result($result);  
    17. ?>  

    代码中的数据库配置信息可以替换成别的。测试命令如下

    [html] view plain copy
     
    1. php -f test-mssql.php   


    成功执行后将会打印出数据库表中记录数据。

    目前原生PHP代码已经可以连接SQL Server了,但是Laravel还是不行,还需要再编译生成一个pdo_dblib.so扩展驱动。

    6.编译pdo_dblib.so扩展适配Laravel

    [html] view plain copy
     
    1. cd php-5.4.39/ext/pdo_dblib  
    2. ./configure  
    3. make  
    4. sudo cp modules/pdo_dblib.so /usr/lib64/php/modules/  

    再到/etc/php.d下新建pdo_dblib.ini,输入以下内容

    [html] view plain copy
     
    1. ; Enable pdo_dblib extension module  
    2. extension=pdo_dblib.so  

    再编辑Laravel的app/config/database.php文件,将sqlsrv区域改为一下形式

    [html] view plain copy
     
    1. 'sqlsrv' => array(  
    2.                         'driver'   => 'sqlsrv',  
    3.                         'host'     => 'sql-server-2000',  
    4.                         'database' => 'msdb',  
    5.                         'username' => 'sa',  
    6.                         'password' => 'test',  
    7.                         'prefix'   => '',  
    8.                 ),  


    这样Laravel也可以连接SQL Server了。

  • 相关阅读:
    python 字典
    python 列表
    被闭包啪啪啪的打脸之 闭包的错误使用
    TCP的三次握手和四次挥手
    传输层的TCP和UDP协议
    个人小程序应用开发指南
    ES2019 / ES10有什么新功能?
    CSS开启硬件加速来提高网站性能
    js中this的指向问题
    Js面向对象构造函数继承
  • 原文地址:https://www.cnblogs.com/xiaoleiel/p/8301022.html
Copyright © 2011-2022 走看看