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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 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)

Hot Topics

Java Tutorial
1673
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

How to run sublime after writing the code How to run sublime after writing the code Apr 16, 2025 am 08:51 AM

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

laravel installation code laravel installation code Apr 18, 2025 pm 12:30 PM

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

git software installation git software installation Apr 17, 2025 am 11:57 AM

Installing Git software includes the following steps: Download the installation package and run the installation package to verify the installation configuration Git installation Git Bash (Windows only)

How to set important Git configuration global properties How to set important Git configuration global properties Apr 17, 2025 pm 12:21 PM

There are many ways to customize a development environment, but the global Git configuration file is one that is most likely to be used for custom settings such as usernames, emails, preferred text editors, and remote branches. Here are the key things you need to know about global Git configuration files.

How to use sublime shortcut keys How to use sublime shortcut keys Apr 16, 2025 am 08:57 AM

Sublime Text provides shortcuts to improve development efficiency, including commonly used (save, copy, cut, etc.), editing (indentation, formatting, etc.), navigation (project panel, file browsing, etc.), and finding and replacing shortcuts. Proficiency in using these shortcut keys can significantly improve Sublime's efficiency.

See all articles