Table of Contents
本文介绍Linux平台如何使用Freetds连接SQL Server服务器,兼容PHP和Laravel,希望对php中文网php初学者有所帮助!" >本文介绍Linux平台如何使用Freetds连接SQL Server服务器,兼容PHP和Laravel,希望对php中文网php初学者有所帮助!
本文在CentOS 7 64bit和Laravel 4.2环境测试通过。" >本文在CentOS 7 64bit和Laravel 4.2环境测试通过。
1.下载源码并解压缩
2.配置并生成makefile
3.编译安装
4.测试
5.编译PHP扩展
6.编译pdo_dblib.so扩展适配Laravel
Home php教程 php手册 Linux平台使用Freetds连接SQL Server服务器,兼容PHP和Laravel

Linux平台使用Freetds连接SQL Server服务器,兼容PHP和Laravel

May 01, 2017 am 10:40 AM
freetds linux sql connect

本文介绍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
Copy after login

2.配置并生成makefile

./configure --with-tdsver=8.0 --enable-msdblib
Copy after login

3.编译安装

make
sudo make install
Copy after login

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
Copy after login

在文件的最后位置添加如下配置,即可连接SQL Server 2000

[sql-server-2000]
        host = 192.168.182.9
        port = 1433
        tds version = 7.0
Copy after login

如果要连接SQL Server 2005或2008,需要添加以下配置

[sql-server-2005]
        host = 192.168.70.1
        port = 1433
        tds version = 8.0
Copy after login

4.测试

/usr/local/bin/tsql -S sql-server-2000 -U sa -P test
Copy after login


如果成功连接,将会出现以下提示

locale is "zh_CN.UTF-8"
locale charset is "UTF-8"
using default charset "UTF-8"
1>
Copy after login

至此,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
Copy after login

php官网上最新的5.4版本是 5.4.39,下载源码到本地

 wget http://cn2.php.net/distributions/php-5.4.39.tar.gz
Copy after login


解压并进入扩展目录

tar zxvf php-5.4.39.tar.gz
cd php-5.4.39/ext/mssql
Copy after login

使用phpize生成configure脚本文件

phpize
Copy after login


生成makefile

./configure
Copy after login


编译

make
Copy after login


编译之后将会在modules子目录生成mssql.so扩展文件。复制扩展文件到php的扩展文件目录

sudo cp modules/mssql.so /usr/lib64/php/modules/
Copy after login

在/etc/php.d目录下新建mssql.ini 文件,输入以下内容

; Enable mssql extension module
extension=mssql.so
Copy after login


这样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);
?>
Copy after login

代码中的数据库配置信息可以替换成别的。测试命令如下

php -f test-mssql.php
Copy after login


成功执行后将会打印出数据库表中记录数据。

目前原生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/
Copy after login

再到/etc/php.d下新建pdo_dblib.ini,输入以下内容

; Enable pdo_dblib extension module
extension=pdo_dblib.so
Copy after login

再编辑Laravel的app/config/database.php文件,将sqlsrv区域改为一下形式

&#39;sqlsrv&#39; => array(
                        &#39;driver&#39;   => &#39;sqlsrv&#39;,
                        &#39;host&#39;     => &#39;sql-server-2000&#39;,
                        &#39;database&#39; => &#39;msdb&#39;,
                        &#39;username&#39; => &#39;sa&#39;,
                        &#39;password&#39; => &#39;test&#39;,
                        &#39;prefix&#39;   => &#39;&#39;,
                ),
Copy after login


这样Laravel也可以连接SQL Server了。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

deepseek web version entrance deepseek official website entrance deepseek web version entrance deepseek official website entrance Feb 19, 2025 pm 04:54 PM

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.

How to install deepseek How to install deepseek Feb 19, 2025 pm 05:48 PM

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 official website installation (2025 beginner's guide) BITGet official website installation (2025 beginner's guide) Feb 21, 2025 pm 08:42 PM

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 installation package is directly included Ouyi okx installation package is directly included Feb 21, 2025 pm 08:00 PM

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.

Get the gate.io installation package for free Get the gate.io installation package for free Feb 21, 2025 pm 08:21 PM

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 Exchange Download Official Portal Ouyi Exchange Download Official Portal Feb 21, 2025 pm 07:51 PM

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 official website registration installation package link gate.io official website registration installation package link Feb 21, 2025 pm 08:15 PM

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.

How to Install phpMyAdmin with Nginx on Ubuntu? How to Install phpMyAdmin with Nginx on Ubuntu? Feb 07, 2025 am 11:12 AM

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 (

See all articles