- 論壇徽章:
- 0
|
本來(lái)想在CentOS5.2下,直接通過(guò)PHP的sybase(或者mssql)庫(kù)訪問(wèn)windows下的MSSQL2000(apt-get install php5-sybase),結(jié)果CentOS沒(méi)有相應(yīng)的包(debian咋就有捏?)。
當(dāng)然你可以自己編譯一個(gè),也可以到
http://rpmfind.net/linux/rpm2html/search.php?query=mssql.so
找一個(gè)現(xiàn)成的。
如果不想自己編譯,還有兩種方式(第一種方式還不如自己編譯算了):
一、“Installing The PHP-MSSQL Module On CentOS 5.0”(
http://www.howtoforge.com/installing_php_mssql_centos5.0
)
Author & Content by Tim Haselaars (
http://www.trinix.be
)
As you might have noticed on Centos 5.0, there is no PHP-MSSQL module/extension available in the default yum repositories. So if you want to use it you can alter the PHP binary or you can compile an mssql module/extension. In this article I will explain how to compile the mssql module/extension.
You need to activate the RPMForge custom repository (formely known as Dag Wieers):
http://dag.wieers.com/rpm/packages/rpmforge-release/
Grab your specific RPM and install it:
wget http://dag.wieers.com/rpm/packages/rpmforge-release/rpmforge-release-*.rpm rpm --install rpmforge-release-*.rpm
Next step is to install freetds, freetds-devel, php-devel, and the Development tools.
yum groupinstall 'Development Tools'
yum install freetds freetds-devel --enablerepo=rpmforge
yum install php-devel
Next we alter the php.spec file, which contains the php configuration for compiling.
vi /usr/src/redhat/SPECS/php.spec
If you don't have the php.spec file, you can download the php src rpm.
wget
centos.mirrors.skynet.be/pub/centos/5/os/SRPMS/php-5.1.6-5.el5.src.rpm
and next
rpm –install php-5.1.6-5.el5.src.rpm
And then do an updatedb and locate php.spec.
Edit the CFLAGS= line (~line 310) of the php.spec file - remove -Wno-pointer-sign. Then add the following piece of code to it:
Group: Development/Languages
Requires: php = %{version}-%{release}, php-pdo
Summary: A module for PHP applications that use the MSSQL database.
provides: php_database
BuildRequires: freetds-devel
%description mssql
The MSSQL package contains a dynamic shared object that will add
support for accessing MSSQL databases to php.
Start building your php rpm:
rpmbuild -bb ./php.spec
If you are asked for some dependencies, just yum install them.
cd /usr/src/redhat/BUILD/php-5.1.6/ext/mssql/
Now we are going to make the mssql module without having to rewrite the php binary:
phpize
./configure --with-mssql
make
make install
Next look for the mssql.so file and make sure it is in the module directory specified in your php.ini. Copy it to the specified location if needed.
Add this line to your php.ini:
vi /etc/php.ini
extension=mssql.so
Save the php.ini and restart Apache:
/etc/init.d/httpd restart
二、CentOs下php連mssql并配置unixODBC (
http://rickyfang.blog.51cto.com/1213/125837
)(
http://www.rickyfang.net/article.asp?id=235
)
目前,我實(shí)面了種情況下的安裝:
第一:系統(tǒng)安裝時(shí)已經(jīng)安裝配置好LAMP。這時(shí)重新編譯PHP環(huán)境相當(dāng)困難。
我使用RPM 包和TAR包兩種方式來(lái)安裝環(huán)境。并不改動(dòng)PHP的安裝及配置,但只能使用ODBC連接MS SQL(數(shù)據(jù)源)。
第二:系統(tǒng)安裝后,使用TAR包安裝配置LAMP環(huán)境。這時(shí)可以重新編譯PHP。
我使用TAR包來(lái)安裝,且有重新編譯PHP和不重新編譯PHP的兩種方案。重新編譯可以實(shí)現(xiàn)直接連接和ODBC連接兩種方式。不重新編譯可以實(shí)現(xiàn)ODBC連接MS SQL(數(shù)據(jù)源)
。
這篇博文,以第一種情況,且使用TAR包的方式來(lái)安裝freetds,以rpm安裝unixODBC-devel及unixODBC。使用ODBC連接MS SQL(數(shù)據(jù)源)。(要想了解更多的關(guān)于php下如何連接MS SQL,以及幾種方式就參考www.freetds.org中的HOWTO文檔,我接觸到的使用了兩種一是直接連接,一是odbc。)
一、安裝環(huán)境
1、CentOs 5.1 + FreeTds0.64.tar.gz + unixODBC-2.2.11-7.1(除了FreeTds是額外的包,其它的都可以使用YUM來(lái)安裝,或是系統(tǒng)安裝時(shí)就裝好)
2、最好也安裝上XWINDOWS,我安裝的是GNOME的,因?yàn)榕渲肈SN(數(shù)據(jù)源)時(shí)會(huì)用上。當(dāng)然,你也可以在文本界面中配置。
二、部分包的安裝及版本顯示
1、[root@rpm-mssql ~]# rpm -aq |grep httpd
system-config-httpd-1.3.3.1-1.el5
httpd-2.2.3-11.el5_1.centos.3
httpd-devel-2.2.3-11.el5_1.centos.3 (這個(gè)包包含 Apache HTTP 服務(wù)器的包含文件、頭文件和 APXS 工具程序。如果你打算載入額外的模塊(除了該產(chǎn)品所提供的),你需要它。 嘿嘿,如果裝PHP時(shí),不要使用TAR包的HTTPD,那就可以使用--with-apxs2=/usr/sbin/apxs.關(guān)于此的安裝另文說(shuō)明)
httpd-manual-2.2.3-11.el5_1.centos.3
2、[root@rpm-mssql ~]# rpm -aq |grep php
php-cli-5.1.6-15.el5
php-common-5.1.6-15.el5
php-5.1.6-15.el5
php-odbc-5.1.6-15.el5
php-pdo-5.1.6-15.el5
php-pear-1.4.9-4
php-mysql-5.1.6-15.el5
php-ldap-5.1.6-15.el5
你并不需要安裝如此之多,根據(jù)自已的需求,安裝。因?yàn)槲沂窍到y(tǒng)安裝時(shí)定制安裝成LAMP環(huán)境的。
3、[root@rpm-mssql ~]# rpm -aq |grep unixODBC
unixODBC-devel-2.2.11-7.1 (這個(gè)包的作用同httpd-devel,一定要裝上啊,安裝freetds.tar.gz時(shí)要用上的,它會(huì)自動(dòng)尋找unixODBC的安裝路徑的。否則有可能編譯出錯(cuò)。)
unixODBC-2.2.11-7.1
unixODBC-kde-2.2.11-7.1(同志們,這個(gè)要裝啊,本來(lái)是KDE桌面下的圖形配置ODBC數(shù)據(jù)源的工具。呵呵,在GNOME中也能用的。)
OK,mysql的安裝包我不多說(shuō),因?yàn)檫@里主要是使用ODBC(DSN數(shù)據(jù)源)在LAMP環(huán)境下PHP連MS SQL的。
4、安裝要求的包:
freetds-0.6.tar.gz
http://www.ibiblio.org/pub/Linux/ALPHA/freetds/stable/
unixODBC-kde-2.2.11-7.1
unixODBC-devel-2.2.11-7.1.i386.rpm yum install -y unixODBC-devel
http://mirror.centos.org/centos/5.1/os/i386/CentOS/
三、安裝、測(cè)試和配置:
1、安裝freetds(均是在root用戶下,如果你在其它用戶下,記得在運(yùn)行make install時(shí)su - root)
注:在系統(tǒng)安裝時(shí)要安裝上GCC編譯器等開(kāi)發(fā)工具及庫(kù)。不過(guò),在CentOs下少什么就用yum來(lái)安裝啦。
注:同樣的條件可以安裝在REDHAT AS5.1 及AS5下。
[root@rpm-mssql freetds-0.64]# tar zxvf freetds-0.64.tar.gz
[root@rpm-mssql ~]# cd freetds-0.64
[root@rpm-mssql freetds-0.64]# ./configure --with-tdsver=7.0 --with-unixODBC
以下的要在root用戶下執(zhí)行,尤其是make install
[root@rpm-mssql freetds-0.64]# make;make install
[root@rpm-mssql freetds-0.64]# make clean
ok,現(xiàn)在已安裝好啦。這時(shí)會(huì)在/usr/local/etc生成freetds.conf文件
[root@rpm-mssql freetds-0.64]# cd /usr/local/etc
[root@rpm-mssql etc]# ll
total 12
-rw-r--r-- 1 root root 3572 Jan 24 19:13 freetds.conf
-rw-r--r-- 1 root root 362 Jan 24 19:13 locales.conf
-rw-r--r-- 1 root root 219 Jan 24 19:13 pool.conf
可以使用ODBCConfig來(lái)配置啦
注,在安裝unixODBC unixODBC-devel后,就會(huì)在/etc生成兩個(gè)文件odbc.ini odbcinst.ini:
[root@rpm-mssql freetds-0.64]# ll /etc/od
odbc.ini odbcinst.ini.rpmnew oddjobd.conf.d/
odbc.ini.rpmnew oddjob/
odbcinst.ini oddjobd.conf
2、測(cè)試和配置
A、安裝好freetds后,先使用tsql來(lái)測(cè)試下:
[root@rpm-mssql etc]# /usr/local/bin/tsql -S 211.103.155.xxx -U sa Password:password (xxx是三個(gè)數(shù)字,為了安全,變了。而:后的password為真實(shí)的SA的密碼)
locale is "en_US.UTF-8"
locale charset is "UTF-8"
Password: (這里還要再輸入一次密碼)
1>
如果你足夠幸運(yùn),呵呵,就會(huì)出現(xiàn) “1>” 說(shuō)明你之前的編譯正確啦。
接下來(lái)就開(kāi)始真正的進(jìn)入正題啦,配置freetds.conf
B、編輯freetds.conf(經(jīng)測(cè)試,這個(gè)步驟可以省去。因?yàn)檠bfreetds的目的是為了它的驅(qū)動(dòng)。實(shí)際上這也暗示著大家采用rpm包安裝時(shí)需要做些什么啦)
[root@rpm-mssql etc]# vi /usr/local/etc/freetds.conf
加入如下代碼,
[MySql2k] (在FreeTds0.64版本中,這個(gè)參數(shù)很重要。在“System DSN”中,需要用的到。這也是和之前版本的一些差別)
host = 211.103.155.xxx (這個(gè)是裝有sql server 2k的服務(wù)器IP,連SQL 2K5也能行)
port = 1433
tds version = 8.0
C、配置/etc/odbc.ini /etc/odbcinst.ini (注,為了更直觀,我使用X視窗的方式下圖形配置,當(dāng)然,我也會(huì)把經(jīng)過(guò)圖形配置后的ini文件拷配過(guò)來(lái))
這里需要用到ODBCConfig /usr/bin/ODBCConfig
[root@rpm-mssql etc]# /usr/bin/ODBCConfig
ODBCConfig: cannot connect to X server
D、切換到X WINDOWS,前面說(shuō)過(guò),使用的是GNOME,各位亦可以使用KDE。君不見(jiàn)unixODBC-kde就是為KDE專門做的嘛。
[root@rpm-mssql etc]# /usr/bin/ODBCConfig后,出現(xiàn)如下圖,圖示中有說(shuō)明。但要聲明的是,我使用的FreeTds的版本是0.64的,和前一版本0.63有些使用上的差別。主要是"Servername"的變化,以及"Setup"的變化。在接下來(lái)的一系列圖示中,會(huì)分別說(shuō)明。
![]()
安裝FreeTds的目的,就是為了提供PHP連接MSSQL的驅(qū)動(dòng)的。上面所有的動(dòng)作,就是為此做準(zhǔn)備的。驅(qū)動(dòng)有了裝載的路徑,就要增加數(shù)據(jù)源,以使連接MSSQL成功。
接下來(lái)的E,F(xiàn)就是重點(diǎn)的以圖文并茂的方式來(lái)說(shuō)明的。
E、圖形設(shè)置Driver的過(guò)程,就是配置/etc/odbcinst.ini的過(guò)程。
freetds0.64版本增加了對(duì)64位數(shù)據(jù)庫(kù)的支持。
![]()
下面的是圖形配置后的odbcinst.ini的文件,其中MySql2k是新增加地:
[root@rpm-mssql ~]# cat /etc/odbcinst.ini
[PostgreSQL]
Description = ODBC for PostgreSQL
Driver = /usr/lib/libodbcpsqlS.so
Driver64 = /usr/lib
Setup = /usr/lib
Setup64 = /usr/lib
UsageCount = 1
CPTimeout =
CPReuse =
[MySql2k]
Description = v0.64 with Protocol v8.0
Driver = /usr/local/lib/libtdsodbc.so
Driver64 = /usr/local/lib/libtdsodbc.so
Setup = /usr/lib/libtdsS.so
Setup64 = /usr/lib/libtdsS.so
UsageCount = 1
CPTimeout =
CPReuse =
F、DSN數(shù)據(jù)源建立及注意事項(xiàng):
圖形設(shè)置"System DSN"的過(guò)程,是配置/etc/odbc.ini文件的過(guò)程:
![]()
![]()
下面的是圖形配置"System DSN"后,odbc.ini的文件,如果沒(méi)有或是不想安裝X 視窗的話,直接設(shè)置。
[root@rpm-mssql ~]# cat /etc/odbc.ini
[mssql]
Description = MySql2k
Driver = MySql2k
Servername = MySql2k (這里,不要設(shè)置IP地址什么的,要和FREETDS.CONF里的設(shè)置一樣,至少我試的就是如此,在FREETDS0.63中并沒(méi)有此現(xiàn)象。)
Database =
UID = sa
PWD =
Port = 1433
G、isql 測(cè)試:
![]()
成功了,呵呵,實(shí)際上這篇文章也可以就此打住啦。不過(guò)。還是實(shí)測(cè)下PHP連MS SQL的效果吧
H、php頁(yè)面測(cè)試
[root@rpm-mssql ~]# vi /var/www/html/phpmssql.php
![]()
我在編譯FreeTDS時(shí)使用的參數(shù)是:
./configure --with-tdsver=8.0 --with-unixODBC
結(jié)果使用PHP或者如下兩個(gè)工具查詢數(shù)據(jù)后,返回的中文數(shù)據(jù)是亂碼:
isql -v mssql myusername mypasswd
DataManager
此問(wèn)題也有兩種方法解決:
1、編輯 /usr/local/etc/freetds.conf 在相應(yīng)的段增加一行:client charset = UTF-8 (
http://aseity.blog.163.com/blog/static/16481711200702061211659/
)
例如:[MySql2k]
host = 192.224.2.8
port = 1433
tds version = 8.0
client charset = UTF-8
FreeTDS能夠自動(dòng)識(shí)別服務(wù)器端的charset. 因此 FreeTDS 需要用戶設(shè)定客戶端的
charset. 這也就是客戶端應(yīng)用程序期待從FreeTDS獲得的數(shù)據(jù)所應(yīng)該使用的charset.
一旦client charset設(shè)定, FreeTDS將實(shí)現(xiàn)從 server charset client charset
的轉(zhuǎn)換.
如果有兩個(gè)客戶應(yīng)用程序都要訪問(wèn)同一個(gè)MS SQLServer,這兩個(gè)客戶程序所
接受的字符集分別是 UTF-8 和 GB2312. 那么解決的辦法是在FreeTDS.conf中設(shè)置
兩組DataSource,它們的服務(wù)器設(shè)置相同,但client charset分別設(shè)為 UTF-8和GB2312. 在
odbc.ini中也設(shè)置兩組不同的DSN 分別指向這兩組DataSource. 而最終兩個(gè)客戶程序
各自使用與之相應(yīng)的DSN即可.
2、在編譯安裝freetds時(shí),需要在./configure參數(shù)中加上--disable-libiconv,因?yàn)閟ql server 2k默認(rèn)的編碼方式是CP936,如果沒(méi)有加上這個(gè)參數(shù),freetds在讀取數(shù)據(jù)的時(shí)候,要默認(rèn)做一次轉(zhuǎn)換,導(dǎo)致亂碼的出現(xiàn)。
本文來(lái)自ChinaUnix博客,如果查看原文請(qǐng)點(diǎn):http://blog.chinaunix.net/u/5652/showart_1806884.html |
|