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

ubuntu php 连接sql server的更多相关文章

  1. Ubuntu下erlang连接SQL SERVER 2008

    erlang连接SQL Server使用ODBC方法,但在网络上还是缺少资料,自己折腾了2天才成功.现在特记录下来,以供大家借鉴. 基本思路是 erlang odbcserver + unixodbc ...

  2. SQL Server on Ubuntu——Ubuntu上的SQL Server(全截图)

    本文从零开始一步一步介绍如何在Ubuntu上搭建SQL Server 2017,包括安装系统.安装SQL等相关步骤和方法(仅供测试学习之用,基础篇). 一.   创建Ubuntu系统(Create U ...

  3. .net core 连接sql server 时提示Connection Timeout Expired

    .net core开发环境是ubuntu LINUX, 在ubuntu 上 telnet 数据库IP 端口是通的. SQL SERVER 是2008 ,未打SP补丁. 打完 SQL SERVER  2 ...

  4. Unity3d连接SQL Server数据库出现SocketException: 使用了与请求的协议不兼容的地址错误

    这两天,同学问我Unity3d连接SQL Server的问题,当时我只是简单的说:“应该一样吧,就是那简单的几句啊”.之后他让我试了下,我才发现有问题了.故此写下一篇博客,要牢记这件事的教训,操作数据 ...

  5. vbs连接sql server及写文件操作

    此段代码是连接SQL SERVER的 代码内connMMSQL的参数要根据实际情况传入 Function connMMSQL(ip,user,pwd,database,strsql) Dim conn ...

  6. python 使用pymssql连接sql server数据库

    python 使用pymssql连接sql server数据库   #coding=utf-8 #!/usr/bin/env python#------------------------------ ...

  7. NetBeans连接SQL server数据库教程

    不废话,直接开始 1.下载sqljdbc.jar 可以从微软中国官方网站下载 SQLJDBC微软中国 笔者提供一个网盘链接Sqljdbc.jar 4个压缩包视版本选择,SQL 2012 用sqljdb ...

  8. JDBC连接SQL Server代码模板

    *                  JDBC连接SQL Server数据库 代码模板* Connection: 连接数据库并担任传送数据的任务:* Statement :  执行SQL语句:* Re ...

  9. FireDAC 连接SQL Server一些要注意的地方

    TFDConnection: FetchOptions.Mode 设置为fmAll, 返回全部结果, 否则默认只返回前50条, 效果与open以后再执行FetchAll一样 Specifies how ...

随机推荐

  1. go语言的基本命令

    go run命令: 用于运行命令源码文件 只能接受一个命令源码文件以及若干个库源码文件作为文件参数其内部操作是:先编译源码文件在执行 -v:列出被编译的代码包的名称 -work: 显示编译时创建的临时 ...

  2. Qt测试计算时间

    博客转载自:https://blog.csdn.net/lg1259156776/article/details/52325508 一.标准C和C++都可用 1. 获取时间用time_t time( ...

  3. JavaPersistenceWithMyBatis3笔记-第3章SQL Mappers Using XMLs-001

    一. 1.Mapper 2.Service 3.Domain package com.mybatis3.domain; import java.io.Serializable; import java ...

  4. 999F Cards and Joy

    传送门 题目大意 有n个人n*m张牌,每个人分m张牌.每个人有一个自己喜欢的数值,如果他的牌中有x张数值等于这个值则他的高兴度为L[x],求怎样分配牌可以使得所有人的总高兴度最大. 分析 我们发现每一 ...

  5. 在windows中安装OpenSSH,无密码登录,永远不断线

    到这里下载最新安装包:https://github.com/PowerShell/Win32-OpenSSH/releases下载下来解压,然后用管理员打开服务器自带的PowerShell,运行下列命 ...

  6. Entity Framework Tutorial Basics(36):Eager Loading

    Eager Loading: Eager loading is the process whereby a query for one type of entity also loads relate ...

  7. Entity Framework Tutorial Basics(27):Update Entity Graph

    Update Entity Graph using DbContext: Updating an entity graph in disconnected scenario is a complex ...

  8. Entity Framework Tutorial Basics(26):Add Entity Graph

    Add Entity Graph using DbContext: Adding entity graph with all new entities is a simple task. We can ...

  9. 数据结构_成绩查询_cjcx

    问题描述 录入 n 个学生的成绩,并查询.★数据输入第一行输入包括 n. m(1<=n<=50,000,1<=m<=100,000)两个数字.接下来 n 行,每行包含名字和成绩 ...

  10. git命令(二)

    2.查看.添加.提交.删除.找回,重置修改文件 3.查看文件diff git diff <file> # 比较当前文件和暂存区文件差异 git diff git diff <$id1 ...