linux php 连接sql server2000
说明:
Linux服务器系统:CentOS 5.5
Web环境:LAMP或者LNMP
Windows服务器系统:Windows Server 2003
Windows服务器IP地址:192.168.21.134
数据库环境:SQL Server 2000数据库
数据库账号:sa
数据库密码:123456
实现目的:Linux服务器中的php程序能够连接到Windows服务器中的SQL Server 2000数据库
具体步骤:
一、在Linux服务器中安装freetds
1、安装编译工具及驱动程序
yum install make apr* autoconf automake gcc gcc-c++ wget
2、安装unixODBC驱动程序
yum -y install unixODBC unixODBC-devel
3、安装freetds
cd /usr/local/src
wget ftp://ftp.freetds.org/pub/freetds/stable/freetds-stable.tgz #下载
gunzip freetds-stable.tgz #解压
tar -xvf freetds-stable.tar
cd freetds-0.91
./configure --prefix=/usr/local/freetds --with-tdsver=8.0 --with-unixodbc=/usr --enable-msdblib #配置
make #编译
make install #安装
4、配置freetds
cp /usr/local/freetds/etc/freetds.conf /usr/local/freetds/etc/freetds.confbak #备份原有文件
vi /usr/local/freetds/etc/freetds.conf #在最后位置修改编辑以下内容
系统运维 www.osyunwei.com 温馨提醒:qihang01原创内容版权所有,转载请注明出处及原文链
[192.168.21.134]
host = 192.168.21.134 #SQL Server 2000服务器地址
port = 1433 #SQL Server 2000数据库端口
tds version = 8.0 #8.0代表数据库版本为SQL Server 2000
client charset = UTF-8 #设置字符集
5、测试freetds
/usr/local/freetds/bin/tsql -S 192.168.21.134 -U sa #测试,提示输入数据库密码,出现下面界面,配置成功
cd /usr/local/freetds/bin
./tsql -S 192.168.21.134 -p 1433 -U sa -P 123456 -D master #测试,出现下面界面,配置成功
二、配置Linux php连接SQL Server 2000数据库
1、php是用yum命令在线安装
yum install php-mssql #安装php-mssql扩展,如果提示没有此安装包,请先配置第三方yum源
wget http://www.atomicorp.com/installers/atomic #下载
sh ./atomic #安装
[root@huangwei bin]# sh ./atomic
ln: "/etc/system-release" 与"/etc/fedora-release" 为同一文件
Atomic Free Unsupported Archive installer, version 2.0.12
BY INSTALLING THIS SOFTWARE AND BY USING ANY AND ALL SOFTWARE
PROVIDED BY ATOMICORP LIMITED YOU ACKNOWLEDGE AND AGREE:
THIS SOFTWARE AND ALL SOFTWARE PROVIDED IN THIS REPOSITORY IS
PROVIDED BY ATOMICORP LIMITED AS IS, IS UNSUPPORTED AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ATOMICORP LIMITED, THE
COPYRIGHT OWNER OR ANY CONTRIBUTOR TO ANY AND ALL SOFTWARE PROVIDED
BY OR PUBLISHED IN THIS REPOSITORY BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
====================================================================
THIS SOFTWARE IS UNSUPPORTED. IF YOU REQUIRE SUPPORTED SOFWARE
PLEASE SEE THE URL BELOW TO PURCHASE A NUCLEUS LICENSE AND DO NOT
PROCEED WITH INSTALLING THIS PACKAGE.
====================================================================
For supported software packages please purchase a Nucleus license:
https://www.atomicorp.com/products/nucleus.html
All atomic repository rpms are UNSUPPORTED.
Do you agree to these terms? (yes/no) [Default: yes] yes
Configuring the [atomic] yum archive for this system
Installing the Atomic GPG key: OK
Downloading atomic-release-1.0-19.fc20.art.noarch.rpm:
OK
The Atomic Rocket Turtle archive has now been installed and configured for your system
The following channels are available:
atomic - [ACTIVATED] - contains the stable tree of ART packages
atomic-testing - [DISABLED] - contains the testing tree of ART packages
atomic-bleeding - [DISABLED] - contains the development tree of ART packages
yum install php-mssql #再次执行
[root@iZ25vqhgbx2Z freetds-0.91]# yum install php-mssql
Loaded plugins: security
atomic | 3.4 kB 00:00
atomic/primary_db | 844 kB 00:10
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package php-mssql.x86_64 0:5.3.3-3.el6 will be updated
---> Package php-mssql.x86_64 0:5.4.31-37.el6.art will be an update
--> Processing Dependency: php-pdo(x86-64) = 5.4.31-37.el6.art for package: php-mssql-5.4.31-37.el6.art.x86_64
--> Running transaction check
---> Package php-pdo.x86_64 0:5.3.3-27.el6_5.1 will be updated
---> Package php-pdo.x86_64 0:5.4.31-37.el6.art will be an update
--> Processing Dependency: php-common(x86-64) = 5.4.31-37.el6.art for package: php-pdo-5.4.31-37.el6.art.x86_64
--> Running transaction check
---> Package php-common.x86_64 0:5.3.3-27.el6_5.1 will be updated
--> Processing Dependency: php(api) = 20090626 for package: php-mcrypt-5.3.3-3.el6.x86_64
--> Processing Dependency: php(zend-abi) = 20090626 for package: php-mcrypt-5.3.3-3.el6.x86_64
--> Processing Dependency: php-common(x86-64) = 5.3.3-27.el6_5.1 for package: php-gd-5.3.3-27.el6_5.1.x86_64
--> Processing Dependency: php-common(x86-64) = 5.3.3-27.el6_5.1 for package: php-cli-5.3.3-27.el6_5.1.x86_64
--> Processing Dependency: php-common(x86-64) = 5.3.3-27.el6_5.1 for package: php-mysql-5.3.3-27.el6_5.1.x86_64
--> Processing Dependency: php-common(x86-64) = 5.3.3-27.el6_5.1 for package: php-bcmath-5.3.3-27.el6_5.1.x86_64
--> Processing Dependency: php-common(x86-64) = 5.3.3-27.el6_5.1 for package: php-xml-5.3.3-27.el6_5.1.x86_64
--> Processing Dependency: php-common(x86-64) = 5.3.3-27.el6_5.1 for package: php-ldap-5.3.3-27.el6_5.1.x86_64
--> Processing Dependency: php-common(x86-64) = 5.3.3-27.el6_5.1 for package: php-odbc-5.3.3-27.el6_5.1.x86_64
--> Processing Dependency: php-common(x86-64) = 5.3.3-27.el6_5.1 for package: php-xmlrpc-5.3.3-27.el6_5.1.x86_64
--> Processing Dependency: php-common(x86-64) = 5.3.3-27.el6_5.1 for package: php-imap-5.3.3-27.el6_5.1.x86_64
--> Processing Dependency: php-common(x86-64) = 5.3.3-27.el6_5.1 for package: php-5.3.3-27.el6_5.1.x86_64
--> Processing Dependency: php-common(x86-64) = 5.3.3-27.el6_5.1 for package: php-mbstring-5.3.3-27.el6_5.1.x86_64
---> Package php-common.x86_64 0:5.4.31-37.el6.art will be an update
--> Running transaction check
---> Package php.x86_64 0:5.3.3-27.el6_5.1 will be updated
---> Package php.x86_64 0:5.4.31-37.el6.art will be an update
---> Package php-bcmath.x86_64 0:5.3.3-27.el6_5.1 will be updated
---> Package php-bcmath.x86_64 0:5.4.31-37.el6.art will be an update
---> Package php-cli.x86_64 0:5.3.3-27.el6_5.1 will be updated
---> Package php-cli.x86_64 0:5.4.31-37.el6.art will be an update
---> Package php-gd.x86_64 0:5.3.3-27.el6_5.1 will be updated
---> Package php-gd.x86_64 0:5.4.31-37.el6.art will be an update
--> Processing Dependency: libt1.so.5()(64bit) for package: php-gd-5.4.31-37.el6.art.x86_64
---> Package php-imap.x86_64 0:5.3.3-27.el6_5.1 will be updated
---> Package php-imap.x86_64 0:5.4.31-37.el6.art will be an update
---> Package php-ldap.x86_64 0:5.3.3-27.el6_5.1 will be updated
---> Package php-ldap.x86_64 0:5.4.31-37.el6.art will be an update
---> Package php-mbstring.x86_64 0:5.3.3-27.el6_5.1 will be updated
---> Package php-mbstring.x86_64 0:5.4.31-37.el6.art will be an update
---> Package php-mcrypt.x86_64 0:5.3.3-3.el6 will be updated
---> Package php-mcrypt.x86_64 0:5.4.31-37.el6.art will be an update
---> Package php-mysql.x86_64 0:5.3.3-27.el6_5.1 will be updated
---> Package php-mysql.x86_64 0:5.4.31-37.el6.art will be an update
---> Package php-odbc.x86_64 0:5.3.3-27.el6_5.1 will be updated
---> Package php-odbc.x86_64 0:5.4.31-37.el6.art will be an update
---> Package php-xml.x86_64 0:5.3.3-27.el6_5.1 will be updated
---> Package php-xml.x86_64 0:5.4.31-37.el6.art will be an update
---> Package php-xmlrpc.x86_64 0:5.3.3-27.el6_5.1 will be updated
---> Package php-xmlrpc.x86_64 0:5.4.31-37.el6.art will be an update
--> Running transaction check
---> Package t1lib.x86_64 0:5.1.2-6.el6_2.1 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Updating:
php-mssql x86_64 5.4.31-37.el6.art atomic 66 k
Installing for dependencies:
t1lib x86_64 5.1.2-6.el6_2.1 base 160 k
Updating for dependencies:
php x86_64 5.4.31-37.el6.art atomic 2.7 M
php-bcmath x86_64 5.4.31-37.el6.art atomic 60 k
php-cli x86_64 5.4.31-37.el6.art atomic 2.6 M
php-common x86_64 5.4.31-37.el6.art atomic 932 k
php-gd x86_64 5.4.31-37.el6.art atomic 143 k
php-imap x86_64 5.4.31-37.el6.art atomic 79 k
php-ldap x86_64 5.4.31-37.el6.art atomic 62 k
php-mbstring x86_64 5.4.31-37.el6.art atomic 944 k
php-mcrypt x86_64 5.4.31-37.el6.art atomic 51 k
php-mysql x86_64 5.4.31-37.el6.art atomic 136 k
php-odbc x86_64 5.4.31-37.el6.art atomic 78 k
php-pdo x86_64 5.4.31-37.el6.art atomic 120 k
php-xml x86_64 5.4.31-37.el6.art atomic 171 k
php-xmlrpc x86_64 5.4.31-37.el6.art atomic 70 k
Transaction Summary
================================================================================
Install 1 Package(s)
Upgrade 15 Package(s)
Total download size: 8.3 M
Is this ok [y/N]: y
Downloading Packages:
(1/16): php-5.4.31-37.el6.art.x86_64.rpm | 2.7 MB 00:09
(2/16): php-bcmath-5.4.31-37.el6.art.x86_64.rpm | 60 kB 00:00
(3/16): php-cli-5.4.31-37.el6.art.x86_64.rpm | 2.6 MB 00:22
(4/16): php-common-5.4.31-37.el6.art.x86_64.rpm | 932 kB 00:02
(5/16): php-gd-5.4.31-37.el6.art.x86_64.rpm | 143 kB 00:00
(6/16): php-imap-5.4.31-37.el6.art.x86_64.rpm | 79 kB 00:00
(7/16): php-ldap-5.4.31-37.el6.art.x86_64.rpm | 62 kB 00:00
(8/16): php-mbstring-5.4.31-37.el6.art.x86_64.rpm | 944 kB 00:02
(9/16): php-mcrypt-5.4.31-37.el6.art.x86_64.rpm | 51 kB 00:00
(10/16): php-mssql-5.4.31-37.el6.art.x86_64.rpm | 66 kB 00:00
(11/16): php-mysql-5.4.31-37.el6.art.x86_64.rpm | 136 kB 00:00
(12/16): php-odbc-5.4.31-37.el6.art.x86_64.rpm | 78 kB 00:00
(13/16): php-pdo-5.4.31-37.el6.art.x86_64.rpm | 120 kB 00:00
(14/16): php-xml-5.4.31-37.el6.art.x86_64.rpm | 171 kB 00:01
(15/16): php-xmlrpc-5.4.31-37.el6.art.x86_64.rpm | 70 kB 00:00
(16/16): t1lib-5.1.2-6.el6_2.1.x86_64.rpm | 160 kB 00:00
--------------------------------------------------------------------------------
Total 166 kB/s | 8.3 MB 00:51
warning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID 4520afa9: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY.art.txt
Importing GPG key 0x5EBD2744:
Userid : Atomic Rocket Turtle <admin@atomicrocketturtle.com>
Package: atomic-release-1.0-19.el6.art.noarch (installed)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY.art.txt
Is this ok [y/N]: y
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY.atomicorp.txt
Importing GPG key 0x4520AFA9:
Userid : Atomicorp (Atomicorp Official Signing Key) <support@atomicorp.com>
Package: atomic-release-1.0-19.el6.art.noarch (installed)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY.atomicorp.txt
Is this ok [y/N]: y
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Warning: RPMDB altered outside of yum.
Updating : php-common-5.4.31-37.el6.art.x86_64 1/31
warning: /etc/php.ini created as /etc/php.ini.rpmnew
Updating : php-pdo-5.4.31-37.el6.art.x86_64 2/31
Updating : php-cli-5.4.31-37.el6.art.x86_64 3/31
Installing : t1lib-5.1.2-6.el6_2.1.x86_64 4/31
Updating : php-gd-5.4.31-37.el6.art.x86_64 5/31
Updating : php-5.4.31-37.el6.art.x86_64 6/31
Updating : php-mssql-5.4.31-37.el6.art.x86_64 7/31
Updating : php-odbc-5.4.31-37.el6.art.x86_64 8/31
Updating : php-mysql-5.4.31-37.el6.art.x86_64 9/31
Updating : php-bcmath-5.4.31-37.el6.art.x86_64 10/31
Updating : php-xml-5.4.31-37.el6.art.x86_64 11/31
Updating : php-imap-5.4.31-37.el6.art.x86_64 12/31
Updating : php-mcrypt-5.4.31-37.el6.art.x86_64 13/31
Updating : php-mbstring-5.4.31-37.el6.art.x86_64 14/31
Updating : php-ldap-5.4.31-37.el6.art.x86_64 15/31
Updating : php-xmlrpc-5.4.31-37.el6.art.x86_64 16/31
Cleanup : php-5.3.3-27.el6_5.1.x86_64 17/31
Cleanup : php-mysql-5.3.3-27.el6_5.1.x86_64 18/31
Cleanup : php-odbc-5.3.3-27.el6_5.1.x86_64 19/31
Cleanup : php-mssql-5.3.3-3.el6.x86_64 20/31
Cleanup : php-pdo-5.3.3-27.el6_5.1.x86_64 21/31
Cleanup : php-cli-5.3.3-27.el6_5.1.x86_64 22/31
Cleanup : php-xmlrpc-5.3.3-27.el6_5.1.x86_64 23/31
Cleanup : php-ldap-5.3.3-27.el6_5.1.x86_64 24/31
Cleanup : php-gd-5.3.3-27.el6_5.1.x86_64 25/31
Cleanup : php-mbstring-5.3.3-27.el6_5.1.x86_64 26/31
Cleanup : php-mcrypt-5.3.3-3.el6.x86_64 27/31
Cleanup : php-imap-5.3.3-27.el6_5.1.x86_64 28/31
Cleanup : php-xml-5.3.3-27.el6_5.1.x86_64 29/31
Cleanup : php-bcmath-5.3.3-27.el6_5.1.x86_64 30/31
Cleanup : php-common-5.3.3-27.el6_5.1.x86_64 31/31
Verifying : php-bcmath-5.4.31-37.el6.art.x86_64 1/31
Verifying : php-mssql-5.4.31-37.el6.art.x86_64 2/31
Verifying : php-xml-5.4.31-37.el6.art.x86_64 3/31
Verifying : php-imap-5.4.31-37.el6.art.x86_64 4/31
Verifying : php-cli-5.4.31-37.el6.art.x86_64 5/31
Verifying : php-mcrypt-5.4.31-37.el6.art.x86_64 6/31
Verifying : php-odbc-5.4.31-37.el6.art.x86_64 7/31
Verifying : php-mbstring-5.4.31-37.el6.art.x86_64 8/31
Verifying : php-pdo-5.4.31-37.el6.art.x86_64 9/31
Verifying : php-mysql-5.4.31-37.el6.art.x86_64 10/31
Verifying : php-gd-5.4.31-37.el6.art.x86_64 11/31
Verifying : php-ldap-5.4.31-37.el6.art.x86_64 12/31
Verifying : t1lib-5.1.2-6.el6_2.1.x86_64 13/31
Verifying : php-common-5.4.31-37.el6.art.x86_64 14/31
Verifying : php-xmlrpc-5.4.31-37.el6.art.x86_64 15/31
Verifying : php-5.4.31-37.el6.art.x86_64 16/31
Verifying : php-pdo-5.3.3-27.el6_5.1.x86_64 17/31
Verifying : php-5.3.3-27.el6_5.1.x86_64 18/31
Verifying : php-mbstring-5.3.3-27.el6_5.1.x86_64 19/31
Verifying : php-odbc-5.3.3-27.el6_5.1.x86_64 20/31
Verifying : php-bcmath-5.3.3-27.el6_5.1.x86_64 21/31
Verifying : php-imap-5.3.3-27.el6_5.1.x86_64 22/31
Verifying : php-mssql-5.3.3-3.el6.x86_64 23/31
Verifying : php-xmlrpc-5.3.3-27.el6_5.1.x86_64 24/31
Verifying : php-common-5.3.3-27.el6_5.1.x86_64 25/31
Verifying : php-cli-5.3.3-27.el6_5.1.x86_64 26/31
Verifying : php-gd-5.3.3-27.el6_5.1.x86_64 27/31
Verifying : php-mysql-5.3.3-27.el6_5.1.x86_64 28/31
Verifying : php-mcrypt-5.3.3-3.el6.x86_64 29/31
Verifying : php-xml-5.3.3-27.el6_5.1.x86_64 30/31
Verifying : php-ldap-5.3.3-27.el6_5.1.x86_64 31/31
Dependency Installed:
t1lib.x86_64 0:5.1.2-6.el6_2.1
Updated:
php-mssql.x86_64 0:5.4.31-37.el6.art
Dependency Updated:
php.x86_64 0:5.4.31-37.el6.art php-bcmath.x86_64 0:5.4.31-37.el6.art
php-cli.x86_64 0:5.4.31-37.el6.art php-common.x86_64 0:5.4.31-37.el6.art
php-gd.x86_64 0:5.4.31-37.el6.art php-imap.x86_64 0:5.4.31-37.el6.art
php-ldap.x86_64 0:5.4.31-37.el6.art php-mbstring.x86_64 0:5.4.31-37.el6.art
php-mcrypt.x86_64 0:5.4.31-37.el6.art php-mysql.x86_64 0:5.4.31-37.el6.art
php-odbc.x86_64 0:5.4.31-37.el6.art php-pdo.x86_64 0:5.4.31-37.el6.art
php-xml.x86_64 0:5.4.31-37.el6.art php-xmlrpc.x86_64 0:5.4.31-37.el6.art
Complete!
[root@iZ25vqhgbx2Z freetds-0.91]#
2、php是用源码编译安装
cd /usr/local/src/php-5.3.15/ext/mssql #进入php源码包的ext下mssql目录
/usr/local/php5/bin/phpize
./configure --with-php-config=/usr/local/php5/bin/php-config --with-mssql=/usr/local/freetds #配置
注意:--with-mssql=/usr/local/freetds #是指freetds的安装路径
make #编译
make install #安装
vi /usr/local/php5/etc/php.ini #编辑,在最后位置添加下面一行
extension = "mssql.so"
:wq! #保存退出
3、重启LAMP或者LNMP
service httpd restart #重启apache
service php-fpm restart #重启php-fpm
三、测试php连接SQL Server 2000数据库
进入php程序目录,新建一个测试文件
cd /usr/local/nginx/html #请改为自己的网站目录
vi sql.php #添加以下内容
<?php
$mssql=mssql_connect("192.168.21.134","sa","123456");
if ($mssql) {
echo "Connent Mssql Succeed";
}
else {
echo "connent Mssql Error!";
}
?>
:wq! #保存退出
注意:这里的192.168.21.134必须要和/usr/local/freetds/etc/freetds.conf中的[192.168.21.134]完全一样
在浏览器中打开,出现下面的界面,连接成功
可以在新建一个文件
vi index.php
<?php
phpinfo();
?>
在浏览器中打开,能找到mssql,说明配置成功,如下图所示
至此,Linux下php连接SQL Server 2000数据库配置完成
下一篇: mysql事务隔离级别详细讲解