Linux環境PHP5.5以上連接SqlServer2008
linux版本:64位CentOS 6.4
Nginx版本:nginx1.8.0
php版本:php5.5.28
Sqlserver版本:2008
FreeTDS版本:0.95
關於Linux環境安裝Nginx+PHP參考Linux環境Nginx安裝與調試以及PHP安裝 即可。
一般來說,PHP+mysql是最經典的組合,跑在Linux環境是非常好的,如果是PHP+Sqlserver是跑在windows環境下的。
今 天需要Linux環境下PHP調用Sqlserver,用了一天的時間,終於把這個問題徹底研究清楚,網上其他類似文章我大都看了,其實有的是因為太久遠 不適用,有的有錯誤,還有的有幾個關鍵問題沒有說清楚,看此文其他可以忽略了,說真的踩坑真的很累,也沒必要,照著這篇來做就是,所以本文才號稱是全網最經典無錯版,其實這麽說主要是希望大家節省時間。
1.首先需要編譯安裝FreeTDS
說明:一定要從官網下載最新的版本FreeTDS-0.95 ftp://ftp.freetds.org/pub/freetds/stable/freetds-patched.tar.gz
如果官網實在太慢建議從本人上傳的這裏一樣很快下載:http://download.csdn.net/detail/21aspnet/9000357
# wget ftp://ftp.freetds.org/pub/freetds/stable/freetds-patched.tar.gz
# tar -zxvf freetds-patched.tar.gz
# cd freetds-0.95
需要註意的就是這裏的--with-tdsver=7.3,這個非常重要,你需要根據你的數據庫版本選擇正確的配置項,由於現在大多是SQLserve2008所以需要選擇7.3.
關於這個問題網上有的說是7.1,也有的說是7.2,甚至有的說是8.0,可以看文末參考帖子,不過那些說的都有問題。
造成這個配置項混亂的根源是很多人用的是FreeTDS-0.91,經過我的測試FreeTDS-0.91只支持7.1,如果是7.2以上配置那麽通通會變為5.0。
其實參考官網的文檔就知道這個問題了,不過由於很多人下載了舊版FreeTDS-0.91,即使設置為--with-tdsver=7.2以上也沒有用。
總結:FreeTDS-0.91只支持7.1,其余都會默認為5.0。只有最新的FreeTDS-0.95,也就是對Sqlserver2008的最佳配置。
# ./configure --prefix=/usr/local/freetds --with-tdsver=7.3 --enable-msdblib
# make && make install
安裝好會看到這樣的信息:
配置FreeTDS
# cd ../
# echo "/usr/local/freetds/lib/" > /etc/ld.so.conf.d/freetds.conf
# ldconfig
驗證FreeTDS版本
這一步非常重要,通過才可以繼續,不然後面的步驟都是無意義的。
首先看看版本信息
# /usr/local/freetds/bin/tsql -C
測試數據庫是否聯通
# /usr/local/freetds/bin/tsql -H 數據庫服務器IP -p 端口號 -U 用戶名 -P 密碼
關於freetds/etc/freetds.conf配置項
很多其他帖子寫了需要配置/usr/local/freetds/etc/freetds.conf,其實這個不需要配置。如果配置也可以,配置了PHP就可以調用這個配置項,否則需要PHP代碼裏指定數據庫服務器信息即可。
另外需要註意的是/usr/local/freetds/etc/下的freetds.conf不同於前面/usr/local/freetds/lib/那個freetds.conf。
如果配置了這裏,那麽PHP頁面就可以使用這裏的配置,不然PHP頁面指定一樣可以。
默認是這樣的:
[cpp] view plain copy- # $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 = 4.2
- # 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 = ntmachine.domain.com
- port = 1433
- tds version = 7.0
如果你想使用配置項,只要修改[egServer70]即可:
[cpp] view plain copy- [egServer70]
- host = 192.168.1.235 這個是數據庫服務器IP
- port = 1433
- tds version = 7.1
其他都不用動,關於[egServer70]的名字也是隨意的,這個就是給PHP調用的,和PHP代碼裏一致即可。
3.添加PHP擴展mssql和pdo的pdo_dblib
說明:這2種擴展都可以達到相同的目的,選其一即可。
(1).增加PHP擴展mssql
# cd /usr/php-5.5.28/ext/mssql/
linux下用phpize給PHP動態添加擴展
# /usr/local/php/bin/phpize
# ./configure --with-php-config=/usr/local/php/bin/php-config --with-mssql=/usr/local/freetds/
# make && make install
(2).增加PHP擴展pdo的pdo_dblib
# cd /usr/php-5.5.28/ext/pdo_dblib/
linux下用phpize給PHP動態添加擴展
# /usr/local/php/bin/phpize
# ./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-dblib=/usr/local/freetds/
# make && make install
(3).在php.ini配置文件中增加.so
# cd /usr/local/php/lib下的php.ini
增加:
[cpp] view plain copy- extension = "mssql.so"
- extension ="pdo_dblib.so"
如果你只需要上述2種擴展之一,自然只要新增其中一個的.so擴展到php.ini即可。
(4).重啟PHP FastCGI
# killall php-fpm
# /etc/init.d/php-fpm
如果沒有正確生成擴展是不能重啟php-fpm的。
這時候在phpinfo裏就可以看到擴展添加成功的信息了。
4.使用PHP調用SQLserver
(1).mssql_connect配置版
[php] view plain copy- <?php
- header("Content-type: text/html; charset=utf-8");
- $msdb=mssql_connect("egServer70","blog.csdn.net.unix21","password");
- if (!$msdb) {
- echo "connect sqlserver error";
- exit;
- }
- mssql_select_db("數據庫名",$msdb);
- $result = mssql_query("SELECT top 5 * FROM tablename", $msdb);
- while($row = mssql_fetch_array($result)) {
- print_r($row);
- }
- mssql_free_result($result);
- ?>
註意:上面的egServer70就是前面freetds/etc/freetds.conf配置的。
(2).mssql_connect非配置版
[php] view plain copy- <?php
- header("Content-type: text/html; charset=utf-8");
- //$msdb=mssql_connect("數據庫IP","blog.csdn.net.unix21","password");
- //$msdb=mssql_connect("數據庫IP:1433","blog.csdn.net.unix21","password");
- $msdb=mssql_connect("數據庫IP:49151","blog.csdn.net.unix21","password");
- if (!$msdb) {
- echo "connect sqlserver error";
- exit;
- }
- mssql_select_db("數據庫名",$msdb);
- $result = mssql_query("SELECT top 5 * FROM tablename", $msdb);
- while($row = mssql_fetch_array($result)) {
- print_r($row);
- }
- mssql_free_result($result);
- ?>
(3).PDO版本
- <?php
- header("Content-type: text/html; charset=utf-8");
- try {
- $hostname = "數據庫IP";
- $port = 1433;
- $dbname = "數據庫名";
- $username = "blog.csdn.net.unix21";
- $pw = "password";
- $dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");
- } catch (PDOException $e) {
- echo "Failed to get DB handle: " . $e->getMessage() . "\n";
- exit;
- }
- $stmt = $dbh->prepare("SELECT top 5 * FROM tablename");
- $stmt->execute();
- while ($row = $stmt->fetch()) {
- print_r($row);
- }
- unset($dbh); unset($stmt);
- ?>
顯示數據:
以上本人都是驗證過的。
參考信息:
1.這個帖子解釋了第一個重要問題為什麽是--with-tdsver=7.1
http://blog.csdn.net/dlutxie/article/details/6851429
2.官方信息
http://php.net/manual/zh/ref.pdo-dblib.php
http://php.net/manual/zh/function.mssql-connect.php
3.本人之前也是參考這些的,但是有些地方有錯誤,網上主要也就這些帖子,其他都是互相引用的。
http://zyan.cc/php_sqlserver_freetds/
http://my.oschina.net/robanlee/blog/168626?p={{currentPage+1}}
http://blog.csdn.net/rgb_rgb/article/details/8762769
http://blog.sina.com.cn/s/blog_87b9bbc70101avmh.html
http://stackoverflow.com/questions/13180746/mssql-connect-unable-to-connect-to-server-without-freetds-conf
http://blog.csdn.net/chenjiebin/article/details/7278304
http://blog.163.com/koumm@126/blog/static/9540383720112157055119/
http://www.linuxidc.com/Linux/2012-09/70192.htm
http://blog.csdn.net/helonsy/article/details/7207497
Linux環境PHP5.5以上連接SqlServer2008