Linux平台使用Freetds连接SQL Server服务器,兼容PHP和Laravel
本文介绍Linux平台如何使用Freetds连接SQL Server服务器,兼容PHP和Laravel,希望对php中文网php初学者有所帮助!
本文在CentOS 7 64bit和Laravel 4.2环境测试通过。
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.配置
默认安装的配置文件位于/usr/local/etc,在/usr/local/etc编辑freetds.conf 文件,默认为
# $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.测试
/usr/local/bin/tsql -S sql-server-2000 -U sa -P test
如果成功连接,将会出现以下提示
locale is "zh_CN.UTF-8" locale charset is "UTF-8" using default charset "UTF-8" 1>
至此,FreeTDS已经是Linux具备连接SQL Server的功能了。
5.编译PHP扩展
PHP 5.4之后已经没有原生支持的SQL Server的驱动了,因此需要手动编译PHP源码的扩展添加对SQL Server的驱动支持。CentOS 7自带的是5.4版本的PHP,因此我们通过编译5.4版的PHP源码获得扩展。
目前CentOS yum源里最新的php是5.4.16,php可以通过yum安装到系统
sudo yum install php php-devel php-fpm php-common php-mysql php-pdo libzip
php官网上最新的5.4版本是 5.4.39,下载源码到本地
wget http://cn2.php.net/distributions/php-5.4.39.tar.gz
解压并进入扩展目录
tar zxvf php-5.4.39.tar.gz cd php-5.4.39/ext/mssql
使用phpize生成configure脚本文件
phpize
生成makefile
./configure
编译
make
编译之后将会在modules子目录生成mssql.so扩展文件。复制扩展文件到php的扩展文件目录
sudo cp modules/mssql.so /usr/lib64/php/modules/
在/etc/php.d目录下新建mssql.ini 文件,输入以下内容
; Enable mssql extension module extension=mssql.so
这样PHP就能加载SQL Server驱动了。使用如下代码测试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); ?>
代码中的数据库配置信息可以替换成别的。测试命令如下
php -f test-mssql.php
成功执行后将会打印出数据库表中记录数据。
目前原生PHP代码已经可以连接SQL Server了,但是Laravel还是不行,还需要再编译生成一个pdo_dblib.so扩展驱动。
6.编译pdo_dblib.so扩展适配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,输入以下内容
; Enable pdo_dblib extension module extension=pdo_dblib.so
再编辑Laravel的app/config/database.php文件,将sqlsrv区域改为一下形式
'sqlsrv' => array( 'driver' => 'sqlsrv', 'host' => 'sql-server-2000', 'database' => 'msdb', 'username' => 'sa', 'password' => 'test', 'prefix' => '', ),
这样Laravel也可以连接SQL Server了。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

DeepSeek is a powerful intelligent search and analysis tool that provides two access methods: web version and official website. The web version is convenient and efficient, and can be used without installation; the official website provides comprehensive product information, download resources and support services. Whether individuals or corporate users, they can easily obtain and analyze massive data through DeepSeek to improve work efficiency, assist decision-making and promote innovation.

There are many ways to install DeepSeek, including: compile from source (for experienced developers) using precompiled packages (for Windows users) using Docker containers (for most convenient, no need to worry about compatibility) No matter which method you choose, Please read the official documents carefully and prepare them fully to avoid unnecessary trouble.

BITGet is a cryptocurrency exchange that provides a variety of trading services including spot trading, contract trading and derivatives. Founded in 2018, the exchange is headquartered in Singapore and is committed to providing users with a safe and reliable trading platform. BITGet offers a variety of trading pairs, including BTC/USDT, ETH/USDT and XRP/USDT. Additionally, the exchange has a reputation for security and liquidity and offers a variety of features such as premium order types, leveraged trading and 24/7 customer support.

Ouyi OKX, the world's leading digital asset exchange, has now launched an official installation package to provide a safe and convenient trading experience. The OKX installation package of Ouyi does not need to be accessed through a browser. It can directly install independent applications on the device, creating a stable and efficient trading platform for users. The installation process is simple and easy to understand. Users only need to download the latest version of the installation package and follow the prompts to complete the installation step by step.

Gate.io is a popular cryptocurrency exchange that users can use by downloading its installation package and installing it on their devices. The steps to obtain the installation package are as follows: Visit the official website of Gate.io, click "Download", select the corresponding operating system (Windows, Mac or Linux), and download the installation package to your computer. It is recommended to temporarily disable antivirus software or firewall during installation to ensure smooth installation. After completion, the user needs to create a Gate.io account to start using it.

Ouyi, also known as OKX, is a world-leading cryptocurrency trading platform. The article provides a download portal for Ouyi's official installation package, which facilitates users to install Ouyi client on different devices. This installation package supports Windows, Mac, Android and iOS systems. Users can choose the corresponding version to download according to their device type. After the installation is completed, users can register or log in to the Ouyi account, start trading cryptocurrencies and enjoy other services provided by the platform.

Gate.io is a highly acclaimed cryptocurrency trading platform known for its extensive token selection, low transaction fees and a user-friendly interface. With its advanced security features and excellent customer service, Gate.io provides traders with a reliable and convenient cryptocurrency trading environment. If you want to join Gate.io, please click the link provided to download the official registration installation package to start your cryptocurrency trading journey.

This tutorial guides you through installing and configuring Nginx and phpMyAdmin on an Ubuntu system, potentially alongside an existing Apache server. We'll cover setting up Nginx, resolving potential port conflicts with Apache, installing MariaDB (
