1.下载最新的freetds ,访问 http://www.freetds.org/, 或者在 ubuntu上用 wget ftp://ftp.freetds.org/pub/freetds/stable/freetds-stable.tgz 下载稳定的版本。

2.安装freetds 和 配置 freetds

cd 进入freetds所在目录,

$ tar zxvf freetds-stable.tgz(解压)
$ ./configure --prefix=/usr/local/freetds --with-tdsver=8.0 --enable-msdblib

$ make 
$ make install

configure 步骤和 make 步骤可以用 普通用户的权限来完成,install 步骤最好用root 用户,或者是对/usr/local/ 目录有读写的权限的用户.

配置:

编辑/etc/ld.so.conf,在其中插入一行:
/usr/local/freetds/lib
然后运行以下指令使更改生效:
ldconfig

编辑freetds 的配置文件,配置数据库连接信息

vim /usr/local/freetds/etc/freetds.conf 内容如下:

"/usr/local/freetds/etc/freetds.conf" 40L, 1126C written
nvr@ubuntu:~$ sudo vim  /usr/local/freetds/etc/freetds.conf

#   $Id: freetds.conf,v 1.12 2007/12/25 06:02:36 jklowden Exp $
#
# This file is installed by FreeTDS if no file by the same 
# name is found in the installation directory.  
#
# For information about the layout of this file and its settings, 
# see the freetds.conf manpage "man freetds.conf".

# Global settings are overridden by those in a database
# server specific section
[global]
        # TDS protocol version
      tds version = 7.1

# Whether to write a TDSDUMP file for diagnostic purposes
        # (setting this to /tmp is insecure on a multi-user system)
;       dump file = /tmp/freetds.log
;       debug flags = 0xffff

# Command and connection timeouts
;       timeout = 10
;       connect timeout = 10

# If you get out-of-memory errors, it may mean that your client
        # is trying to allocate a huge buffer for a TEXT field.  
        # Try setting 'text size' to a more reasonable limit 
        text size = 64512

# A typical Sybase server
#[egServer50]
        #host = symachine.domain.com
        #port = 5000
        #tds version = 5.0

# A typical Microsoft server
[egServer70]
        host = 115.28.239.229
        port = 1433
        tds version =7.1
        client charset=utf8

请注意绿色修改的部分

在这里找到egServer70节点,配置你数据所在的服务器地址,一般端口不用改,除非数据库访问用的是其它端口。注意一般这个配置文件中是没有 client charset 的选项的,

需要手动添加,防止数据库乱码。这里设置成 utf8 , 这个编码要和数据库编码一致。修改完成之后就保存退出。然后验证这个配置是否是ok 的。

cd /usr/local/freetds/bin/

./tsql -H 115.28.239.229 -p 1433 -U 用户名 -P 密码 -D 数据库

注意你的密码有特殊字符例如 !和 # 之类的,那么需要加上转义符 \!\# ,这样就会被 freetds 识别。

如果执行该命令返回的是 如下信息

Unexpected EOF from the server

那说明是freetds 的版本信息不正确,关于freetds 版本的信息 可以参考 http://www.freetds.org/userguide/choosingtdsprotocol.htm

那么对应的版本信息修改只需要在 /usr/local/freetds/etc/freetds.conf 中修改 特定数据库连接的tds version 修改内容的绿色部分

.php 连接 SQLserver

上面的测试直接用freetds 测试数据库是否可以链接数据库,那么怎么才能让php也支持SqlServer 呢? 这里需要用到php5的开发者模式,之前我默认只安装了php5,

所以这里你需要安装一下php5-dev ,直接用命令 apt-get install php5-dev,安装完成后在/usr/bin 目录下就会看到 phpize5 等相关php 开发这模式的插件

同样也可以用 wget 下载。下载后用 tar xf php-5.5.9.tar.gz ,

wget http://us2.php.net/get/php-5.5.9.tar.xz/from/this/mirror

这个地方下载下来可能是mirror 那么用 tar xf mirror

进入到 ext/mssql 目录执行以下命令:

/usr/bin/phpize5

./configure --with-php-config=/usr/local/php/bin/php-config5 --with-mssql=/usr/local/freetds/
make

sudo make install

记得make install 要用sudo 命令

(注意:php-config5 可能各个系统存放的位置不一样,请使用sudo find -name php-config5 来查找目录 freetds 类似 )

安装成功后,你可以在 /usr/lib/php5/20121206 这目录下有 mssql.so ,当然这个文件的大体目录是位于 /usr/lib/php5 ,至于后面有年月日组成的这个文件夹,在不同不服务器上是不一样的。

接下来就编辑php.ini 配置文件 /etc/php5/apache2/php.ini

在文件中加上 extension=mssql.so

然后重启 apache  service apache2 restart

测试:

在/var/www/html 目录下新建 index.php 文件

echo phpinfo();

进一步测试代码:

header("Content-type: text/html; charset=utf-8");  
$msdb=mssql_connect("115.28.239.22:1433","sa","");  
if (!$msdb) {  
        echo "connect sqlserver error";  
        exit;  
}  
mssql_select_db("drupaldb",$msdb);  
$result = mssql_query("SELECT  * FROM test", $msdb);  
while($row = mssql_fetch_array($result)) {  
        var_dump($row);  
}  
mssql_free_result($result);

乱码 在/usr/local/freetds/etc/freetds.conf 下,添加

client charset = UTF-8

最新文章

  1. 他山之石——vs2013 安装与部署及程序打包
  2. C#调用exe文件,IIS发布后无法掉用本地exe程序的解决方法
  3. Linux 命令 - netstat: 检查网络设置及相关统计数据
  4. 转载 ASP.NET常用的正则表达式
  5. cad2007 钢筋符号显示为问号
  6. 老旧Webkit浏览器行内元素0间距问题
  7. Flipping Game(枚举)
  8. 使用HttpClient工具类测试Http接口
  9. python之二维码生成
  10. IntelliJ IDEA下Maven SpringMVC+Mybatis入门搭建例子
  11. if判断 -z -n 参数
  12. spring框架学习笔记4:SpringAOP实现原理
  13. Got error -1 from storage engine
  14. MyBatis源码解析(十二)——binding绑定模块之MapperRegisty
  15. 3、zookeeper 集群模式搭建
  16. (注意格式,代替C++的getchar())汉字统计hdu2030
  17. Django:查询后,分页功能为全部对象分页,丢失查询查询参数
  18. Java - 异常解析基础
  19. js基础知识入门总结
  20. python数据分析所需要了解的操作。

热门文章

  1. css知多少(9)——float下篇(转)
  2. PLSQL语法
  3. POJ 2176 Folding(区间DP)
  4. 浅谈android代码保护技术_ 加固
  5. wrodcount
  6. 《Maven实战》笔记-4-生命周期和插件
  7. 解决.jsp及静态资源文件访问404的问题
  8. [译]在Javascript中制造二维数列
  9. css笔记-1
  10. C#.Net使用正则表达式抓取百度百家文章列表