Linux平台使用Freetds连接SQL Server服务器,兼容PHP和Laravel

时间:2023-03-08 23:20:23
Linux平台使用Freetds连接SQL Server服务器,兼容PHP和Laravel

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

1.下载源码并解压缩

  1. wget ftp://ftp.freetds.org/pub/freetds/stable/freetds-stable.tgz
  1. tar zxvf freetds-stable.tgz
  1. cd freetds-0.91

2.配置并生成makefile

  1. ./configure --with-tdsver=8.0 --enable-msdblib

3.编译安装

  1. make
  2. sudo make install

4.配置

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

  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. # Global settings are overridden by those in a database
  9. # server specific section
  10. [global]
  11. # TDS protocol version
  12. ;       tds version = 4.2
  13. # Whether to write a TDSDUMP file for diagnostic purposes
  14. # (setting this to /tmp is insecure on a multi-user system)
  15. ;       dump file = /tmp/freetds.log
  16. ;       debug flags = 0xffff
  17. # Command and connection timeouts
  18. ;       timeout = 10
  19. ;       connect timeout = 10
  20. # If you get out-of-memory errors, it may mean that your client
  21. # is trying to allocate a huge buffer for a TEXT field.
  22. # Try setting 'text size' to a more reasonable limit
  23. text size = 64512
  24. # A typical Sybase server
  25. [egServer50]
  26. host = symachine.domain.com
  27. port = 5000
  28. tds version = 5.0
  29. # A typical Microsoft server
  30. [egServer70]
  31. host = ntmachine.domain.com
  32. port = 1433
  33. tds version = 7.0

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

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

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

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

4.测试

  1. /usr/local/bin/tsql -S sql-server-2000 -U sa -P test

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

  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安装到系统

  1. sudo yum install php php-devel php-fpm php-common php-mysql php-pdo libzip

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

  1. wget http://cn2.php.net/distributions/php-5.4.39.tar.gz

解压并进入扩展目录

  1. tar zxvf php-5.4.39.tar.gz
  2. cd php-5.4.39/ext/mssql

使用phpize生成configure脚本文件

  1. phpize

生成makefile

  1. ./configure

编译

  1. make

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

  1. sudo cp modules/mssql.so /usr/lib64/php/modules/

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

  1. ; Enable mssql extension module
  2. extension=mssql.so

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

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

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

  1. php -f test-mssql.php

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

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

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

  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,输入以下内容

  1. ; Enable pdo_dblib extension module
  2. extension=pdo_dblib.so

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

  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了。