Linux平臺(tái)使用Freetds連接SQL Server服務(wù)器,兼容PHP和Laravel
May 01, 2017 am 10:40 AM本文介紹Linux平臺(tái)如何使用Freetds連接SQL Server服務(wù)器,兼容PHP和Laravel,希望對(duì)php中文網(wǎng)php初學(xué)者有所幫助!
本文在CentOS 7 64bit和Laravel 4.2環(huán)境測(cè)試通過。
1.下載源碼并解壓縮
wget ftp://ftp.freetds.org/pub/freetds/stable/freetds-stable.tgz tar zxvf freetds-stable.tgz cd freetds-0.91
2.配置并生成makefile
./configure --with-tdsver=8.0 --enable-msdblib
3.編譯安裝
make sudo make install
4.配置
默認(rèn)安裝的配置文件位于/usr/local/etc,在/usr/local/etc編輯freetds.conf 文件,默認(rèn)為
# $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
在文件的最后位置添加如下配置,即可連接SQL Server 2000
[sql-server-2000] host = 192.168.182.9 port = 1433 tds version = 7.0
如果要連接SQL Server 2005或2008,需要添加以下配置
[sql-server-2005] host = 192.168.70.1 port = 1433 tds version = 8.0
4.測(cè)試
/usr/local/bin/tsql -S sql-server-2000 -U sa -P test
如果成功連接,將會(huì)出現(xiàn)以下提示
locale is "zh_CN.UTF-8" locale charset is "UTF-8" using default charset "UTF-8" 1>
至此,F(xiàn)reeTDS已經(jīng)是Linux具備連接SQL Server的功能了。
5.編譯PHP擴(kuò)展
PHP 5.4之后已經(jīng)沒有原生支持的SQL Server的驅(qū)動(dòng)了,因此需要手動(dòng)編譯PHP源碼的擴(kuò)展添加對(duì)SQL Server的驅(qū)動(dòng)支持。CentOS 7自帶的是5.4版本的PHP,因此我們通過編譯5.4版的PHP源碼獲得擴(kuò)展。
目前CentOS yum源里最新的php是5.4.16,php可以通過yum安裝到系統(tǒng)
sudo yum install php php-devel php-fpm php-common php-mysql php-pdo libzip
php官網(wǎng)上最新的5.4版本是 5.4.39,下載源碼到本地
wget http://cn2.php.net/distributions/php-5.4.39.tar.gz
解壓并進(jìn)入擴(kuò)展目錄
tar zxvf php-5.4.39.tar.gz cd php-5.4.39/ext/mssql
使用phpize生成configure腳本文件
phpize
生成makefile
./configure
編譯
make
編譯之后將會(huì)在modules子目錄生成mssql.so擴(kuò)展文件。復(fù)制擴(kuò)展文件到php的擴(kuò)展文件目錄
sudo cp modules/mssql.so /usr/lib64/php/modules/
在/etc/php.d目錄下新建mssql.ini 文件,輸入以下內(nèi)容
; Enable mssql extension module extension=mssql.so
這樣PHP就能加載SQL Server驅(qū)動(dòng)了。使用如下代碼測(cè)試PHP連接SQL Server。
<?php header("Content-type: text/html; charset=utf-8"); $msdb=mssql_connect("sql-server-2000","sa","test"); if (!$msdb) { echo "connect sqlserver error"; exit; } mssql_select_db("msdb",$msdb); $result = mssql_query("SELECT top 5 * FROM employee", $msdb); while($row = mssql_fetch_array($result)) { var_dump($row); } mssql_free_result($result); ?>
代碼中的數(shù)據(jù)庫(kù)配置信息可以替換成別的。測(cè)試命令如下
php -f test-mssql.php
成功執(zhí)行后將會(huì)打印出數(shù)據(jù)庫(kù)表中記錄數(shù)據(jù)。
目前原生PHP代碼已經(jīng)可以連接SQL Server了,但是Laravel還是不行,還需要再編譯生成一個(gè)pdo_dblib.so擴(kuò)展驅(qū)動(dòng)。
6.編譯pdo_dblib.so擴(kuò)展適配Laravel
cd php-5.4.39/ext/pdo_dblib ./configure make sudo cp modules/pdo_dblib.so /usr/lib64/php/modules/
再到/etc/php.d下新建pdo_dblib.ini,輸入以下內(nèi)容
; Enable pdo_dblib extension module extension=pdo_dblib.so
再編輯Laravel的app/config/database.php文件,將sqlsrv區(qū)域改為一下形式
'sqlsrv' => array( 'driver' => 'sqlsrv', 'host' => 'sql-server-2000', 'database' => 'msdb', 'username' => 'sa', 'password' => 'test', 'prefix' => '', ),
這樣Laravel也可以連接SQL Server了。

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

PHP ????? ?? ??? ??? ? ??? ??? CI (Continuous Integration) ????? ???? ? ????. 1. DockerFile? ???? ?? ???, ?? ??, ??? ?? ? ?? ??? ???? PHP ??? ?????. 2. Gitlabci? ?? CI/CD ??? ???? .gitlab-ci.yml ??? ?? ??, ??? ? ?? ??? ???? ?? ??, ??? ? ??? ?????. 3. PHPUNIT? ?? ??? ??? ??? ???? ?? ?? ? ???? ???? ????????. 4. Kubernetes? ?? ?? ?? ??? ???? ?? .yaml ??? ?? ?? ??? ?????. 5. Dockerfile ??? ? ??? ??? ??????

??? ? PHP ?? ???? ?? ??? Docker? ?? ??? ? ????. ?? ??? ??? ????. 1. Docker ? DockerCompose? ??? ??????. 2. DockerFile ? Crontab ??? ?????? ?? ????? ????. 3. PHPCLI ??? ???? CRON ? ??? ??? ????? dockerfile? ??????. 4. ??? ??? ???? ?? Crontab ??? ??????. 5. docker-compose.yml ??? ???? ????? ???? ?? ??? ??????. 6. ????? ???? ??? ??????. ? ?????? ??? ??? ???? ?? ??? ? ?? ????? ?? ??, ??? ??, ??? ??? ? ?? ??? ??? ????. ?? ? ?? ??? ?????

??? ??? ???? ???? ??? ?? ?? ??? ?? ?? (? : /dev /sda)? ??????. 2. sudoddif =/dev/Zeroof =/dev/sdxbs = 1mstatus = ?? ????? 0 ?? ?? ???? ?? ?? ???? ????? ?????. 3. 3 ?? ?? ??? ?? ??? sudoshred-v-n3/dev/sdx? ???? ?? ? ? ??? ??????. 4. ????? ???? ?? ???? ?? SudobadBlocks-WSV/Dev/SDX? ?????. ????? sudohexdump-c/dev/sdx | head? ???? ?? ???? ???? ??? ?? ???? ??????.

thefinstallinglinux, theFirstStepSincludeUpdatingYSTEM, ?? ? ?? ? ?? ? ??? BackUpandSecurityMeasures ? Customizing theinterFacetOsuityOUrpReferences.1) UpdateYSTEMUSINGSHEAPPRIATECOMMANDFORYOURDISTO (? : SUDOAPTUPDATE & AM

cronisusedforprecisesCeedulingonalways-onsystems, whileanacronensuresperiodictasksrunonsystems that thatorenuouslypowered, suchaslaptops; 1. usecronforexacttiming (? : 3amdaily) viacrontab-ewithsyntaxminhourdomondowcommand;

?? ?? SQL??? ?????? ??? ?? ?? ??? ???????. MySQL?%y,%m ? selectDate_format (now (), '%y-%m-%d')? ?? ?? ??? ?????. sqlserver? convert () ?? format ()? ????, ??? selectConvert (varchar, getDate (), 112)?? ??? selectFormat (getDate (), 'yyyy-mm-dd'); PostgreSQL? selectto_char (now (), 'y? ?? to_char ()? ?????

GIT ?? : ??? ??? ???? ?? GIT? ???? ??? ??????. 2. ?? GIT ??? ??? : AddUser? ???? GIT ???? ??? ????? ? ???? ??????. 3. ??? SSH ??? ?? : GIT ???? ?? .ssh ???? ? ?? ?_keys ??? ???? ???? ?? ?? ??????. 4. ?? ????? ?? : ???? ?? ?????? ????? ??? ???? ??????. 5. ????? ??? ? ?? : ???? SSH? ?? ???? ???? ?? ??? ?? ? ? ??? ????? ???? ?? GIT ??? ??? ?????.
