centos mssql pdo驱动及配置

    |     2017年4月5日   |   学习偶记   |     评论已关闭   |    6178

centos下安装freeTDS 安装unixODBC 编译php_pdo_odbc模块 编译php_mssql模块 编译php_pdo_dblib模块

首先了解一下PDO的一些基本知识:

PDO是PHPDateObject的简称,它是PHP 5.1版本一起发行的,目前支持的数据库包括Firebird,FressTDS,MySQL,Ms SQL Server,ODBC,Oracle等。

有了PDO,您不必再使用mysql_*函数,oci_*函数或者mssql_*函数,也不必再为它们封闭数据库操作类,只需要使用PDO接口中的方法就可以对数据库进行操作,在选择不同的数据库时,只修改PDO的DSN即可。

PDO是一个数据库访问抽象层,作用是统一各种数据库的访问接口,与MYSQL和MSSQL函数库相比,PDO让跨数据库的使用更具亲和力,与ADODB和MDB2相比,PDO更高效。

PDO将通过一种轻型,清晰,方便的函数,统一各种不同的RDBMS库的共有我,实现PHP脚本在最大程序上的抽象性和兼容性。
PDO吸取了现有数据库扩展成功和失败的经验教训,利用PHP5的最新特性,可以轻松地与各种数据库进行交互。
PDO扩展是模块化,能够在运行时为用户数据库后端加载驱动快,而不必重新编译或重新安装整个PHP程序。

原先写的3篇安装文档因为回档丢失了,现在重新写一份,把3篇内容合并的一篇中。【Centos版】

安装必要的扩展unixODBC 和 freeTDS

1、cd /root 【进入自己的目录,我的是root】

2、yum -y install unixODBC unixODBC-devel  【说明:安装ODBC驱动,然并卵,mssql的驱动并不在这里,默认包含了mysql的odbc驱动,安装mssql的liunx驱动则需要借助第三方扩展包freeTDS】

3、wget -c ftp://ftp.freetds.org/pub/freetds/stable/freetds-patched.tar.gz 【下载freeTDS】

4、tar -zxf freetds-patched.tar.gz 【解压下载的文件】

5、cd freetds-1.00.21  【进入freetds目录,我下载的时候,该扩展是1.00.21版】

6、./configure –prefix=/usr/local/freetds –with-tdsver=7.3 –with-unixodbc=/usr –enable-msdblib    【/usr/local/freetds:我计划要安装的位置,7.3是支持的sqlserver版本为sql server 2008 ,点击这里查看TDS对应的版本。 –enable-msdblib是是否允许Microsoft数据库函数库,这里为允许】

7、make && make install 【编译安装freeTDS】

编译freeTDS

编译freeTDS

8、cd /usr/local/freetds/bin 【进入freetds目录,测试一下是否安装好了】

9、./tsql -C 【查看freetds是否安装好了,这个命令显示了你的初始freetds配置】

freeTDS_配置

freeTDS_配置


常见错误:configure: error: Cannot find FreeTDS in known installation directories

解决方法:新建两个空文件即可

$ touch /usr/local/freetds/include/tds.h
$ touch /usr/local/freetds/lib/libtds.a

odbc中freeTDS的配置

1、cd /usr/local/freetds/etc

2、vi freetds.conf

3、添加一下mssql的配置

# A typical Microsoft server
[Server2008]  //自己起的名字,我这里起名Server2008,在odbc.ini中需要用到。
        host = 127.0.0.1     # 要连接的sql server服务器IP
        port = 1433             # 要连接的sql server服务器端口
        tds version = 7.3   # 要连接的sql server 版本
        client charset  = utf8 # 如果乱码可以加上这一句

4、然后配置一下odbc.ini 和 odbcinst.ini ,这两个文件在/etc 目录下。

5、先查找find / -name libtdsodbc.so 和  find / -name libtdsS.so,查找出路径备用。

odbcinst.ini配置如下【在文件后面添加,前面默认是mysql的odbc连接】:
# Driver from the mssql-connector-odbc package
# Setup from the freeDTS package
[MSSQL]自己起名,odbc.ini中要用
Description     = v7.3 for 2008server
Driver              = /usr/local/freetds/lib/libtdsodbc.so
Driver64         = /usr/local/freetds/lib/libtdsodbc.so
Setup              = /usr/lib64/libtdsS.so
Setup64         = /usr/lib64/libtdsS.so
UsageCount      = 1
CPTimeout       =
CPReuse         =

红色部分是你自己的so模块文件的真实路径。

odbc.ini配置如下:

[my_mssql]
Description     = My SQL_server link
Driver          = MSSQL  #和odbcinst.ini中对应
Servername   = Server2008  #和freeDTS中的配置相对应
Database        =  默认的数据库名
UID                = 用户名
PWD             =  密码
Port            = 端口

6、这时,你可以测试一下是否可以通过odbc的dns方式连接数据库了。

测试freedts:/usr/local/freetds/bin/tsql -S Server2008 -U root   【你自己的配置内容和安装路径】

odbc连接成功

odbc连接成功  使用quit退出。

或者用odbc的isql方式连接测试: isql -v my_ssql 用户名 密码 参数

[my_mssql是odbc.ini中配置的名字,参数可选]

isql连接成功

isql连接成功


让php可以使用odbc连接数据库

1、需要php源码,我的php是5.6版本的,用的是军哥的lnmp套件,可以直接 cd lnmp1.3-full/scr,你也许需要下载相应的php版本安装包。

2、解压php源码。tar -zxf php-5.6.22.tar.gz

3、进入php源码中的ext/mssql目录。cd php-5.6.22/ext/mssql

4、执行phpize命令(本机正在运行的php目录下的phpize)/usr/local/php/bin/phpize

5、开始设置配置文件,红色部分是你真实的路径:./configure –with-php-config=/usr/local/php/bin/php-config –with-mssql=/usr/local/freetds

6、编译:make

mssql扩展make成功

mssql扩展make成功

7、编译安装:make install

mssql扩展make后安装成功

mssql扩展make后安装成功

8、打开你的php.ini,加入extension=mssql.so这一行。(位置随意,我一般都紧挨着;extension=XXX.dll后天面添加)

9、重启php ,军哥lnmp直接就执行lnmp restart即可。phpinfo可以看到扩展

mssql扩展安装成功

mssql扩展安装成功

PS:如果你想直接用unixODBC进行php的mssql odbc连接,可以接着编译pdo_odbc模块。否则直接跳过进入第17步开始看,编译安装pdo_dblib模块。建议不用odbc连接,据说速度慢。

10、安装pdo_odbc扩展:进入php源码中的ext/pdo_odbc目录。cd php-5.6.22/ext/pdo_odbc

11、执行phpize命令:/usr/local/php/bin/phpize

12、执行编译配置,注意最后两个参数用,号分割:./configure –with-php-config=/usr/local/php/bin/php-config –with-pdo-odbc=unixODBC,/usr

13、make && make install

ps:可能出现的错误:

ODBC header file ‘/usr/local/incl/sqlext.h’ not found!  

这时你先看是否有这样的提示:

    A、wget -c https://sourceforge.net/projects/re2c/files/0.16/re2c-0.16.tar.gz
    B、tar zxf re2c-0.16.tar.gz && cd re2c-0.16
    C、./configure
    D、make && make install

安装完re2c后,再make就不会报错了。

14、修改php.ini,增加:extension=pdo_odbc.so

15、重启php.

16、pdo_odbc连接的示例代码。

17、安装dblib扩展,以便程序可以自由移植。 (windows方式下,推荐使用dblib连接,当然liunx也可以用dblib连接,据说odbc连接方式比较慢。)

18、进入php源码中的ext/pdo_dblib目录。cd php-5.6.22/ext/pdo_dblib

19、执行phpize命令:/usr/local/php/bin/phpize

20、执行配置:./configure –with-php-config=/usr/local/php/bin/php-config –with-pdo-dblib=/usr/local/freetds

31、执行:make && make install

32、修改php.ini

添加:extension=pdo_dblib.so

33、重启php。至此,安装完毕。

php的pdo示例语句:

PS:环境:centos 6.X + nginx 1.10.0 + php 5.6.22 + sql server 2008


Table 3-1. Versions of the TDS Protocol, by Product

Product TDS Version Comment
Sybase before System 10, Microsoft SQL Server 6.x 4.2 Still works with all products, subject to its limitations.
Sybase System 10 and above 5.0 Still the most current protocol used by Sybase.
Sybase System SQL Anywhere 5.0 only Originally Watcom SQL Server, a completely separate codebase. Our best information is that SQL Anywhere first supported TDS in version 5.5.03 using the OpenServer Gateway (OSG), and native TDS 5.0 support arrived with version 6.0.
Microsoft SQL Server 7.0 7.0 Includes support for the extended datatypes in SQL Server 7.0 (such as char/varchar fields of more than 255 characters), and support for Unicode.
Microsoft SQL Server 2000 7.1 Include support for bigint (64 bit integers), variant and collation on all fields. Collation is not widely used.
Microsoft SQL Server 2005 7.2 Includes support for varchar(max), varbinary(max), xml datatypes and MARS[a].
Microsoft SQL Server 2008 7.3 Includes support for time, date, datetime2, datetimeoffset.
Microsoft SQL Server 2012 or 2014 7.4 Includes support for session recovery.
N/A 8.0 FreeTDS will alias this version to 7.1 for backwards compatibility reasons, but this should be avoided due to future compatibility concerns. See note below on obsolete versions.
Notes:
a. Multiple Active Result Sets.

For best results, use the highest version of the protocol supported by your server. If you encounter problems, try a lower version. If that works, though, please report it to the mailing list! [1]

TDS 4.2 has limitations

  • ASCII only, of course.

  • RPC is not supported.

  • BCP is not supported.

  • varchar fields are limited to 255 characters. If your table defines longer fields, they’ll be truncated.

  • dynamic queries (also called prepared statements) are not supported.

The protocol version may also affect how database servers interpret commands. For example, Microsoft SQL Server 2000 is known to behave differently with versions 4.2 and 7.0. Version 7.0 is recommended for compatibility with SQL Server tools.

噢!评论已关闭。