apache+php+SQL server
时间:2009-07-29 来源:freyson
公司要在linux的环境下连接SQL Server数据库,比较无奈,没有办法有需求就要做,我从网上找了些资料,写如下文章.这篇文章写的时候比较早了,一直也没有写博客所以就一直放着,有可能插件过时.
下载:主要软件包,
httpd-2.2.6.tar.gz
php-5.1.6.tar.gz 安装php所需的软件包
(其中libxml2是安装php5必须的,为什么还需要perl呆会说明原因。)
libxml2-2.6.19.tar.gz
libxslt-1.1.15.tar.gz
ActivePerl-5.8.8.817-i686-linux-2.2.17-gcc-257965.rpm 如果是2。6的内核,就不用安装了。
curl-7.15.0.tar.gz GD库所需要软件包
(有人说PHP源码包里自带了GD2.0.28和zlib,
只要安装GD的三个支持包:jpg,png和freetype,但是我们还是
下载)
gd-2.0.33.tar.gz
freetype-2.1.10.tar.gz
jpegsrc.v6b.tar.gz
libpng-1.2.8.tar.gz
zlib-1.2.3.tar.gz
Apache-2.2.3.tar.gz
Php-5.1.6.tar.gz
对于这些文件的下载直接可以去网站上搜
安装前必须说明要删除这些文件在你的机器上默认安装的。
二:安装apache2。
# cd /usr/local/src/
进入目录/usr/local/src/
# tar -zvxf httpd-2.2.3.tar.gz
解压httpd-2.2.3.tar.gz
# cd httpd-2.2.3
进入httpd-2.2.3目录
# mkdir -p /usr/server/apache2
生成/usr/server/apache2目录
# ./configure --prefix=/usr/server/apache2
或者
# ./configure --prefix=/usr/local/apache2 --enable-modules=so --enable-rewrite
# make; make install
安装apache
# vi /usr/server/apache2/conf/httpd.conf
#ServerName <http://www.fzone.com/> 在其下增加
ServerName <http://www.fzone.com/>
保存退出,现在就这么简单配置apache。 # /usr/server/apache2/bin/apachectl -k start
启动apahce,
用浏览器查看http://www.fzone.com,得到it works,说明apache已经配置成功了。
# /usr/local/apache/bin/apachectl -k stop
停止apache。
如果要更改apache的文件目录,DocumentRoot "/usr/local/apache2/htdocs" 比如更改到DocumentRoot "/home/ftpsite",如果单纯的更改这个是不能生效了,还要把#<Directory "/usr/local/apache2/htdocs">更改成<Directory "home/ftpsite">就可以访问了。 三:安装GD库(让PHP支持GIF,PNG,JPEG)
a.安装 jpeg6
建立目录:
# mkdir -p /usr/server/jpeg6
# mkdir -p /usr/server/jpeg6/bin
# mkdir -p /usr/ server/jpeg6/lib
# mkdir -p /usr/ server/jpeg6/include
# mkdir -p /usr/server/jpeg6/man
# mkdir -p /usr/lserver/jpeg6/man1
# mkdir -p /usr/server/jpeg6/man/man1 # cd /usr/local/src/
# tar -zvxf jpegsrc.v6b.tar.gz
# cd jpeg-6b
# ./configure --prefix=/usr/server/jpeg6/ --enable-shared --enable-static
# make; make install
如果是centos5会出现如下问题:checking host system type… Invalid configuration `x86_64-unknown-linux-gnu ‘: machine `x86_64-unknown ‘ not recognized
checking build system type… Invalid configuration `x86_64-unknown-linux-gnu ‘: machine `x86_64-unknown ‘ not recognized
configure: error: libtool configure failed
解决方法如下:上面的错误是说libtool的版本太低或者没有安装上
可以先检查一下rpm -qa | grep libtool 如果没有就直接yum安装。
cp /usr/share/libtool/config.guess .
cp /usr/share/libtool/config.sub .
如果是64位还要做如下操作:
# vi Makefile
找到 CFLAGS = -g -O2 后边加上 -fPIC 然后再安装
# make && make install
b.安装libpng
# cd /usr/local/src/
# tar -zvxf libpng-1.2.8.tar.gz
# cd libpng-1.2.8
# cp scripts/makefile.std makefile
# make; make install 64位的安装
#tar zxvf libpng-1.2.34.tar.gz
#cd libpng-1.2.34
#cp scripts/makefile.gcmmx makefile
#./configure --prefix=/usr/local/libpng2/
# vi Makefile
找到 CFLAGS = -g -O2 后边加上 -fPIC
# make && make install c.安装 freetype
# cd /usr/local/src/
# tar -zvxf freetype-2.1.10.tar.gz
# cd freetype-2.1.10
# mkdir -p /usr/server/freetype
# ./configure --prefix=/usr/server/freetype
# make;make install d.:安装zlib
# cd /usr/local/src/
#tar -zxvf zlib-1.2.3.tar.gz
#cd zlib.1.2.3
# ./configure
# make;make install
如果是64位如下操作:
#./configure
#vi Makefile
找到 CFLAGS=-O3 -DUSE_MMAP
在后面加入-fPIC,即变成CFLAGS=-O3 -DUSE_MMAP -fPIC
# make
# make install e.安装GD库
# cd /usr/local/src/
# tar -zvxf gd-2.0.33.tar.gz
# mkdir -p /usr/server/gd2
# cd gd-2.0.33
# ./configure --prefix=/usr/server/gd2 --with-jpeg=/usr/server/jpeg6/
--with-png=/usr/local/lib/
--with-zlib=/usr/local/lib/
--with-freetype=/usr/server/freetype/
# make; make install e.安装Curl库
# cd /usr/local/src/
# tar -zxf curl-7.15.0.tar.gz
# mkdir -p /usr/server/curl
# ./configure --prefix=/usr/server/curl
# make; make install 四:安装php5,php5必须有libxml2支持!
a.安装libxml2
# cd /usr/local/src/
# tar -zxf libxml2-2.6.19.tar.gz
# cd libxml2-2.6.19
# mkdir -p /usr/server/libxml2
# ./configure --prefix=/usr/server/libxml2
# make; make install b.安装 libxslt(可选安装,你可以不安装)
# cd /usr/local/src/
# tar -zxf libxslt-1.1.15.tar.gz
# mkdir -p /usr/server/libxslt
# cd libxslt-1.1.15
# ./configure --prefix=/usr/server/libxslt --with-libxml-prefix=/usr/server/libxml2
# make; make install c.安装php5
# cd /usr/local/src/
# tar -zvxf php-5.1.2.tar.gz
# mkdir -p /usr/server/php5
# cd php-5.1.2
# ./configure --prefix=/usr/server/php5 --with-apxs2=/usr/server/apache2/bin/apxs
--with-curl=/usr/server/curl --enable-ftp
--with-libxml-dir=/usr/server/libxml2
--with-expat-dir=/usr/lib --enable-soap
--with-xsl=/usr/server/libxslt --enable-xslt
--with-gd=/usr/server/gd2/
--with-jpeg-dir=/usr/server/jpeg6/
--with-zlib-dir=/usr/local/lib/
--with-png=/usr/local/lib/
--with-freetype-dir=/usr/server/freetype/
# make
# make install
# cp php.ini-dist /usr/server/php/lib/php.ini (别忘记了呵呵) 其中./configure 后的
--prefix=/usr/server/php5
--with-apxs2=/usr/server/apache2/bin/apxs
--with-libxml-dir=/usr/server/libxml2
是必要的选项 --with-gd=/usr/server/gd2/
--with-jpeg-dir=/usr/server/jpeg6/
--with-png=/usr/local/lib
--with-zlib-dir=/usr/local/lib
--with-freetype-dir=/usr/lserverfreetype
这是让PHP支持GD库的配置选项 --with-curl=/usr/server/curl 支持CURL库
--enable-ftp 打开FTP库函数支持 --enable-soap --with-xsl=/usr/server/libxslt --enable-xslt
让PHP支持SOAP, 上面这些一般用得少, 可以去掉 五:重新配置apache2让他支持php。 配置 httpd.conf 让apache支持PHP
# vi /usr/server/apache2/conf/httpd.conf
找到 AddType application/x-gzip .gz .tgz 在其下添加如下内容
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps 重启apache
# /usr/server/apache2/bin/apachectl restart
在htdocs目录里建一内容为 <? phpinfo(); ?> PHP文件, 输入URL地址查看PHP配置是否正确 ok,web环境apache2+php5就已经介绍完了,接下来我们要让他支持MSSQL数据库。
首先我们不能在linux下安装MSSQL,但是可以通过ODBC连接Windows系统的SQLServer数据库。
六:关于测试环境及Linux连接SQL Server 的说明;
关于MSSQL Server说明:
MS从来没有提供过SQLServer for Linux,所以大家也不要去尝试在Linux系统安装SQLServer,但是可以通过ODBC连接Windows系统的SQLServer数据库;
Windows Server 2003用户注意:
在Windows Server 2003系统上安装的SQLServer是不会打开1433端口的,所以请Windows2003用户在安装完SQLServer之后一定装上SQLServer SP3布丁
七、下载相关软件 unixODBC、freetds和DBD-ODBC
首先需要下载3个文件:
Linux系统的ODBC
unixODBC-2.2.8.tar.gz ( http://www.unixodbc.org)
连接SQLServer或Sybase的驱动
freetds-0.62.4.tar.gz ( http://www.freetds.org)
perl的ODBC模块
DBD-ODBC-1.12.tar.gz ( http://cpan.perl.org)
把下载到的三个文件放到同一个目录
八、安装和配置
1、安装unixODBC
# tar vxzf unixODBC-2.2.8.tar.gz
# cd unixODBC-2.2.8
# ./configure --prefix=/usr/server/unixODBC
# make
# make install
2、安装freetds
# tar vxzf freetds-0.62.4.tar.gz
# cd freetds-0.62.4
# ./configure --prefix=/usr/server/freetds --with-unixodbc=/usr/server/unixODBC --with-tdsver=8.0
# make
# make install 3、安装DBD-ODBC
在安装之前要先设置一下环境变量
# export ODBCHOME=/usr/server/unixODBC 之后开始编译安装
# tar vxzf DBD-ODBC-1.12.tar.gz
# cd DBD-ODBC-1.12
# perl Makefile.PL
# make
# make install
所有的软件到现在都安装完成了,接下来就是配置了 4、配置freetds
# cd /usr/server/freetds
# vi etc/freetds.conf 修改以下的一段,并把;去掉
改好之后的内容:
[MyServer2k]
host=192.168.1.200
port=1433
tds version=8.0
client charset = UTF-8
其中的[MyServer2k]代表在客户端使用的服务器名称,host代表SQLServer服务器的IP地址,port代表端口,测试连接;
# bin/tsql -S MyServer2k -H 192.168.1.200 -p 1433 -U sa -P password
1> use hompy
2> go
1> select count(*) from test
9
1> quit
一切都很顺利
现在来说明以下这些参数的作用,-S MyServer2k是在freetds.conf中定义好的,-H 192.168.1.200服务器IP地址,和freetds.conf中的一致,如果使用了-S参数,这个参数可以不用,-p 1433用来指定端口是1433 ,-U sa用来指定连接数据库的用户名,-P password用来指定连接数据库的密码。
5、配置unixODBC
# cd /usr/server/unixODBC 向ODBC添加SQLServer驱动
# vi etc/odbcinst.ini 写入如下内容:
[FreeTDS] 驱动名称
Description = FreeTDS ODBC driver for MSSQL 描述
Driver = /usr/server/freetds/lib/libtdsodbc.so 驱动程序
Setup = /usr/server/freetds/lib/libtds.so 不要问我,我也不知道这是什么
FileUsage = 1 保存退出
添加DSN
# vi etc/odbc.ini 写入如下内容
[mail] DSN名称
description = Asterisk ODBC for MSSQL 描述
Driver = FreeTDS ODBC驱动
Server = 192.168.1.200 服务器IP
Database = hompy 要使用的数据库
Port = 1433 端口
Socket =
Option =
Stmt = 保存并退出,测试ODBC的连接
# bin/isql -v mail sa password
SQL> select count(*) from test
+----------+
| |
+----------+
| 9 |
+----------+
SQL> quit
OK,测试通过,开始测试perl能否正确使用ODBC
在使用perl连接ODBC之前要做些准备工作
把ODBC的共享库都复制到/usr/lib目录
# cp /usr/server/unixODBC/lib/libodbc.* /usr/lib
6、perl脚本测试能否成功;
# vi test.pl 内容如下
#!/usr/bin/perl
use DBI;
$dbh=DBI->connect('dbi:ODBC:mail','sa','password');
my $sth=$dbh->prepare("select * from test";
$sth->execute();
while (@data=$sth->fetchrow_array()){
print "$data[0] $data[1] $data[2]\n";
} 保存退出
# perl test.pl 如果可以看到数据库中的记录,就说明可以正常使用ODBC perl了。
既然已经可以实现perl通过ODBC来连接SQLServer了,那么同样可以使用PHP+ODBC+SQLServer,具体方法请查询PHP官方网站。
注:1、isql不支持中文的问题
可以设置环境变量export LANG=zh_CN.UTF-8
如果不行export LANG=zh_CN.GB18030
如下测试:[root@liuyan-fc5 etc]# export LANG=zh_CN.UTF-8
[root@liuyan-fc5 etc]# isql -v test
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL> select top 1 staffName from test
+---------------------------------------------------+
| staffName |
+---------------------------------------------------+
| |
+---------------------------------------------------+
SQLRowCount returns 1
1 rows fetched
SQL>
更改后
[root@liuyan-fc5 etc]# export LANG=zh_CN.GB18030
[root@liuyan-fc5 etc]# isql -v test
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL> select top 1 staffName from test
+---------------------------------------------------+
| staffName |
+---------------------------------------------------+
| 刘研 |
+---------------------------------------------------+
SQLRowCount returns 1
1 rows fetched
SQL> 这样可以显示中文了
2.在编译compile DBD::ODBC 時出現 error, 可試一下 upgrade 你的 DBI
# perl -MCPAN -e shell
> install DBI
然後再裝, 應該就可以了!!
3、安装unixODBC出现如下错误:
/usr/bin/ld: cannot find /lib/libpthread.so.0
collect2: ld returned 1 exit status
make[1]: *** [isql] Error 1
make[1]: Leaving directory `/usr/local/src/unixODBC-2.2.9/exe'
make: *** [all-recursive] Error 1
不能找到libpthread.so.0可以去网上下载相关系统的这个文件。
属于glibc出现的问题,可以在安装光盘中找到重新安装或卸载后安装。
httpd-2.2.6.tar.gz
php-5.1.6.tar.gz 安装php所需的软件包
(其中libxml2是安装php5必须的,为什么还需要perl呆会说明原因。)
libxml2-2.6.19.tar.gz
libxslt-1.1.15.tar.gz
ActivePerl-5.8.8.817-i686-linux-2.2.17-gcc-257965.rpm 如果是2。6的内核,就不用安装了。
curl-7.15.0.tar.gz GD库所需要软件包
(有人说PHP源码包里自带了GD2.0.28和zlib,
只要安装GD的三个支持包:jpg,png和freetype,但是我们还是
下载)
gd-2.0.33.tar.gz
freetype-2.1.10.tar.gz
jpegsrc.v6b.tar.gz
libpng-1.2.8.tar.gz
zlib-1.2.3.tar.gz
Apache-2.2.3.tar.gz
Php-5.1.6.tar.gz
对于这些文件的下载直接可以去网站上搜
安装前必须说明要删除这些文件在你的机器上默认安装的。
二:安装apache2。
# cd /usr/local/src/
进入目录/usr/local/src/
# tar -zvxf httpd-2.2.3.tar.gz
解压httpd-2.2.3.tar.gz
# cd httpd-2.2.3
进入httpd-2.2.3目录
# mkdir -p /usr/server/apache2
生成/usr/server/apache2目录
# ./configure --prefix=/usr/server/apache2
或者
# ./configure --prefix=/usr/local/apache2 --enable-modules=so --enable-rewrite
# make; make install
安装apache
# vi /usr/server/apache2/conf/httpd.conf
#ServerName <http://www.fzone.com/> 在其下增加
ServerName <http://www.fzone.com/>
保存退出,现在就这么简单配置apache。 # /usr/server/apache2/bin/apachectl -k start
启动apahce,
用浏览器查看http://www.fzone.com,得到it works,说明apache已经配置成功了。
# /usr/local/apache/bin/apachectl -k stop
停止apache。
如果要更改apache的文件目录,DocumentRoot "/usr/local/apache2/htdocs" 比如更改到DocumentRoot "/home/ftpsite",如果单纯的更改这个是不能生效了,还要把#<Directory "/usr/local/apache2/htdocs">更改成<Directory "home/ftpsite">就可以访问了。 三:安装GD库(让PHP支持GIF,PNG,JPEG)
a.安装 jpeg6
建立目录:
# mkdir -p /usr/server/jpeg6
# mkdir -p /usr/server/jpeg6/bin
# mkdir -p /usr/ server/jpeg6/lib
# mkdir -p /usr/ server/jpeg6/include
# mkdir -p /usr/server/jpeg6/man
# mkdir -p /usr/lserver/jpeg6/man1
# mkdir -p /usr/server/jpeg6/man/man1 # cd /usr/local/src/
# tar -zvxf jpegsrc.v6b.tar.gz
# cd jpeg-6b
# ./configure --prefix=/usr/server/jpeg6/ --enable-shared --enable-static
# make; make install
如果是centos5会出现如下问题:checking host system type… Invalid configuration `x86_64-unknown-linux-gnu ‘: machine `x86_64-unknown ‘ not recognized
checking build system type… Invalid configuration `x86_64-unknown-linux-gnu ‘: machine `x86_64-unknown ‘ not recognized
configure: error: libtool configure failed
解决方法如下:上面的错误是说libtool的版本太低或者没有安装上
可以先检查一下rpm -qa | grep libtool 如果没有就直接yum安装。
cp /usr/share/libtool/config.guess .
cp /usr/share/libtool/config.sub .
如果是64位还要做如下操作:
# vi Makefile
找到 CFLAGS = -g -O2 后边加上 -fPIC 然后再安装
# make && make install
b.安装libpng
# cd /usr/local/src/
# tar -zvxf libpng-1.2.8.tar.gz
# cd libpng-1.2.8
# cp scripts/makefile.std makefile
# make; make install 64位的安装
#tar zxvf libpng-1.2.34.tar.gz
#cd libpng-1.2.34
#cp scripts/makefile.gcmmx makefile
#./configure --prefix=/usr/local/libpng2/
# vi Makefile
找到 CFLAGS = -g -O2 后边加上 -fPIC
# make && make install c.安装 freetype
# cd /usr/local/src/
# tar -zvxf freetype-2.1.10.tar.gz
# cd freetype-2.1.10
# mkdir -p /usr/server/freetype
# ./configure --prefix=/usr/server/freetype
# make;make install d.:安装zlib
# cd /usr/local/src/
#tar -zxvf zlib-1.2.3.tar.gz
#cd zlib.1.2.3
# ./configure
# make;make install
如果是64位如下操作:
#./configure
#vi Makefile
找到 CFLAGS=-O3 -DUSE_MMAP
在后面加入-fPIC,即变成CFLAGS=-O3 -DUSE_MMAP -fPIC
# make
# make install e.安装GD库
# cd /usr/local/src/
# tar -zvxf gd-2.0.33.tar.gz
# mkdir -p /usr/server/gd2
# cd gd-2.0.33
# ./configure --prefix=/usr/server/gd2 --with-jpeg=/usr/server/jpeg6/
--with-png=/usr/local/lib/
--with-zlib=/usr/local/lib/
--with-freetype=/usr/server/freetype/
# make; make install e.安装Curl库
# cd /usr/local/src/
# tar -zxf curl-7.15.0.tar.gz
# mkdir -p /usr/server/curl
# ./configure --prefix=/usr/server/curl
# make; make install 四:安装php5,php5必须有libxml2支持!
a.安装libxml2
# cd /usr/local/src/
# tar -zxf libxml2-2.6.19.tar.gz
# cd libxml2-2.6.19
# mkdir -p /usr/server/libxml2
# ./configure --prefix=/usr/server/libxml2
# make; make install b.安装 libxslt(可选安装,你可以不安装)
# cd /usr/local/src/
# tar -zxf libxslt-1.1.15.tar.gz
# mkdir -p /usr/server/libxslt
# cd libxslt-1.1.15
# ./configure --prefix=/usr/server/libxslt --with-libxml-prefix=/usr/server/libxml2
# make; make install c.安装php5
# cd /usr/local/src/
# tar -zvxf php-5.1.2.tar.gz
# mkdir -p /usr/server/php5
# cd php-5.1.2
# ./configure --prefix=/usr/server/php5 --with-apxs2=/usr/server/apache2/bin/apxs
--with-curl=/usr/server/curl --enable-ftp
--with-libxml-dir=/usr/server/libxml2
--with-expat-dir=/usr/lib --enable-soap
--with-xsl=/usr/server/libxslt --enable-xslt
--with-gd=/usr/server/gd2/
--with-jpeg-dir=/usr/server/jpeg6/
--with-zlib-dir=/usr/local/lib/
--with-png=/usr/local/lib/
--with-freetype-dir=/usr/server/freetype/
# make
# make install
# cp php.ini-dist /usr/server/php/lib/php.ini (别忘记了呵呵) 其中./configure 后的
--prefix=/usr/server/php5
--with-apxs2=/usr/server/apache2/bin/apxs
--with-libxml-dir=/usr/server/libxml2
是必要的选项 --with-gd=/usr/server/gd2/
--with-jpeg-dir=/usr/server/jpeg6/
--with-png=/usr/local/lib
--with-zlib-dir=/usr/local/lib
--with-freetype-dir=/usr/lserverfreetype
这是让PHP支持GD库的配置选项 --with-curl=/usr/server/curl 支持CURL库
--enable-ftp 打开FTP库函数支持 --enable-soap --with-xsl=/usr/server/libxslt --enable-xslt
让PHP支持SOAP, 上面这些一般用得少, 可以去掉 五:重新配置apache2让他支持php。 配置 httpd.conf 让apache支持PHP
# vi /usr/server/apache2/conf/httpd.conf
找到 AddType application/x-gzip .gz .tgz 在其下添加如下内容
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps 重启apache
# /usr/server/apache2/bin/apachectl restart
在htdocs目录里建一内容为 <? phpinfo(); ?> PHP文件, 输入URL地址查看PHP配置是否正确 ok,web环境apache2+php5就已经介绍完了,接下来我们要让他支持MSSQL数据库。
首先我们不能在linux下安装MSSQL,但是可以通过ODBC连接Windows系统的SQLServer数据库。
六:关于测试环境及Linux连接SQL Server 的说明;
关于MSSQL Server说明:
MS从来没有提供过SQLServer for Linux,所以大家也不要去尝试在Linux系统安装SQLServer,但是可以通过ODBC连接Windows系统的SQLServer数据库;
Windows Server 2003用户注意:
在Windows Server 2003系统上安装的SQLServer是不会打开1433端口的,所以请Windows2003用户在安装完SQLServer之后一定装上SQLServer SP3布丁
七、下载相关软件 unixODBC、freetds和DBD-ODBC
首先需要下载3个文件:
Linux系统的ODBC
unixODBC-2.2.8.tar.gz ( http://www.unixodbc.org)
连接SQLServer或Sybase的驱动
freetds-0.62.4.tar.gz ( http://www.freetds.org)
perl的ODBC模块
DBD-ODBC-1.12.tar.gz ( http://cpan.perl.org)
把下载到的三个文件放到同一个目录
八、安装和配置
1、安装unixODBC
# tar vxzf unixODBC-2.2.8.tar.gz
# cd unixODBC-2.2.8
# ./configure --prefix=/usr/server/unixODBC
# make
# make install
2、安装freetds
# tar vxzf freetds-0.62.4.tar.gz
# cd freetds-0.62.4
# ./configure --prefix=/usr/server/freetds --with-unixodbc=/usr/server/unixODBC --with-tdsver=8.0
# make
# make install 3、安装DBD-ODBC
在安装之前要先设置一下环境变量
# export ODBCHOME=/usr/server/unixODBC 之后开始编译安装
# tar vxzf DBD-ODBC-1.12.tar.gz
# cd DBD-ODBC-1.12
# perl Makefile.PL
# make
# make install
所有的软件到现在都安装完成了,接下来就是配置了 4、配置freetds
# cd /usr/server/freetds
# vi etc/freetds.conf 修改以下的一段,并把;去掉
改好之后的内容:
[MyServer2k]
host=192.168.1.200
port=1433
tds version=8.0
client charset = UTF-8
其中的[MyServer2k]代表在客户端使用的服务器名称,host代表SQLServer服务器的IP地址,port代表端口,测试连接;
# bin/tsql -S MyServer2k -H 192.168.1.200 -p 1433 -U sa -P password
1> use hompy
2> go
1> select count(*) from test
9
1> quit
一切都很顺利
现在来说明以下这些参数的作用,-S MyServer2k是在freetds.conf中定义好的,-H 192.168.1.200服务器IP地址,和freetds.conf中的一致,如果使用了-S参数,这个参数可以不用,-p 1433用来指定端口是1433 ,-U sa用来指定连接数据库的用户名,-P password用来指定连接数据库的密码。
5、配置unixODBC
# cd /usr/server/unixODBC 向ODBC添加SQLServer驱动
# vi etc/odbcinst.ini 写入如下内容:
[FreeTDS] 驱动名称
Description = FreeTDS ODBC driver for MSSQL 描述
Driver = /usr/server/freetds/lib/libtdsodbc.so 驱动程序
Setup = /usr/server/freetds/lib/libtds.so 不要问我,我也不知道这是什么
FileUsage = 1 保存退出
添加DSN
# vi etc/odbc.ini 写入如下内容
[mail] DSN名称
description = Asterisk ODBC for MSSQL 描述
Driver = FreeTDS ODBC驱动
Server = 192.168.1.200 服务器IP
Database = hompy 要使用的数据库
Port = 1433 端口
Socket =
Option =
Stmt = 保存并退出,测试ODBC的连接
# bin/isql -v mail sa password
SQL> select count(*) from test
+----------+
| |
+----------+
| 9 |
+----------+
SQL> quit
OK,测试通过,开始测试perl能否正确使用ODBC
在使用perl连接ODBC之前要做些准备工作
把ODBC的共享库都复制到/usr/lib目录
# cp /usr/server/unixODBC/lib/libodbc.* /usr/lib
6、perl脚本测试能否成功;
# vi test.pl 内容如下
#!/usr/bin/perl
use DBI;
$dbh=DBI->connect('dbi:ODBC:mail','sa','password');
my $sth=$dbh->prepare("select * from test";
$sth->execute();
while (@data=$sth->fetchrow_array()){
print "$data[0] $data[1] $data[2]\n";
} 保存退出
# perl test.pl 如果可以看到数据库中的记录,就说明可以正常使用ODBC perl了。
既然已经可以实现perl通过ODBC来连接SQLServer了,那么同样可以使用PHP+ODBC+SQLServer,具体方法请查询PHP官方网站。
注:1、isql不支持中文的问题
可以设置环境变量export LANG=zh_CN.UTF-8
如果不行export LANG=zh_CN.GB18030
如下测试:[root@liuyan-fc5 etc]# export LANG=zh_CN.UTF-8
[root@liuyan-fc5 etc]# isql -v test
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL> select top 1 staffName from test
+---------------------------------------------------+
| staffName |
+---------------------------------------------------+
| |
+---------------------------------------------------+
SQLRowCount returns 1
1 rows fetched
SQL>
更改后
[root@liuyan-fc5 etc]# export LANG=zh_CN.GB18030
[root@liuyan-fc5 etc]# isql -v test
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL> select top 1 staffName from test
+---------------------------------------------------+
| staffName |
+---------------------------------------------------+
| 刘研 |
+---------------------------------------------------+
SQLRowCount returns 1
1 rows fetched
SQL> 这样可以显示中文了
2.在编译compile DBD::ODBC 時出現 error, 可試一下 upgrade 你的 DBI
# perl -MCPAN -e shell
> install DBI
然後再裝, 應該就可以了!!
3、安装unixODBC出现如下错误:
/usr/bin/ld: cannot find /lib/libpthread.so.0
collect2: ld returned 1 exit status
make[1]: *** [isql] Error 1
make[1]: Leaving directory `/usr/local/src/unixODBC-2.2.9/exe'
make: *** [all-recursive] Error 1
不能找到libpthread.so.0可以去网上下载相关系统的这个文件。
属于glibc出现的问题,可以在安装光盘中找到重新安装或卸载后安装。
相关阅读 更多 +