LNMP Configure PHP support to connect MSSQL examples

Source: Internet
Author: User
Tags comments ini mssql mssql server php website sybase versions port number

Install FreeTDS

FreeTDS is the Linux/unix next set of software development kits for implementing Microsoft SQL Server and Sybase's tabular DataStream Protocol protocol. With FreeTDS, you can connect SQL Server and Sybase databases under Linux/unix.

can go to FreeTDS website to see detailed introduction.

Download and install FreeTDS

Go to the Linux server and download it directly to the server through the following commands to freetds-0.95.92.tar.gz for example.

# download FreeTDS to download the latest ftp://ftp.freetds.org/pub/freetds/stable/freetds-patched.tar.gz
$ wget ftp://ftp.freetds.org/pub/freetds/stable/freetds-0.95.92.tar.gz

# decompression
$ TAR-ZXVF freetds-0.95.92.tar.gz

# into the unpacked directory, note the version number
$ CD freetds-0.95.92

# View the currently supported version of Tdsver, select the corresponding supported MSSQL version
$./configure--help

# recompile installation, note tdsver version
$./configure--prefix=/usr/local/freetds--enable-msdblib--with-tdsver=7.3
$ make && make install
Note: Ms Official website speed is too slow, can directly into the FTP download package upload to the server to install.

Organize notes

For some simple comments on the above command line:

Default installation directory for--prefix=/usr/local/freetds FreeTDS/usr/local/freetds
--enable-msdblib allows Microsoft's library of functions
--with-tdsver=7.3 specifies the protocol version of TDS, the default 5.0 version is not filled in, and 7.3 supports MSSQL2008.
TDS Protocol version

Read the Official document first http://www.freetds.org/userguide/choosingtdsprotocol.htm

Microsoft SQL Server 7.1 Include support for bigint (the bit integers), variant and collation on all fields. Collation is not widely used.
Microsoft SQL Server 7.2 Includes support for varchar (max), varbinary (max), XML datatypes and Mars[a].
Microsoft SQL Server 2008 7.3 Includes Support for time, date, datetime2, DateTimeOffset.
N/A 8.0 FreeTDS'll alias this version to 7.1 for backwards compatibility reasons, but this Shou LD is avoided due to future compatibility concerns. Below on obsolete versions.
The following command is used in the above installation command to view the list of protocol versions supported by the currently installed FreeTDS:

$ CD freetds-0.95.92
$./configure--help
The results of the operation are as follows:

--with-tdsver=version TDS Protocol VERSION (4.2/4.6/5.0/7.0/7.1/7.2/7.3) [5.0]
Configuring freetds.conf Files

Run the following command:

$ vi/usr/local/freetds/etc/freetds.conf

# Modify configuration for MSSQL
# A Typical Microsoft server
[Mssql_test]
Host = 1.2.3.4
Port = 1433
TDS Version = 7.3
Client Charset=utf-8 #确保中文无乱码
Detect if the installation was successful

Run the following command:

$ cd/usr/local/freetds/bin
$./tsql-c
The installation succeeds if the following appears:

Compile-time settings (established with the "Configure" script)
Version:freetds v0.95.92
freetds.conf Directory:/usr/local/freetds/etc
MS Db-lib Source Compatibility:yes
Sybase binary Compatibility:no
Thread Safety:yes
Iconv Library:yes
TDS version:7.3
Iodbc:no
Unixodbc:no
SSPI "Trusted" logins:no
Kerberos:no
Openssl:no
Gnutls:no
More detailed configuration can be official documents: http://www.freetds.org/userguide/config.htm

Trying to connect to MSSQL

Enter the FreeTDS installation directory and execute the following command:

$ cd/usr/local/freetds/bin
$./tsql-h 192.168.0.254-p 1433-u sa-p 123456
The following appears to indicate a successful test connection

Locale is "ZH_CN. UTF-8 "
Locale CharSet is "UTF-8"
Using default CharSet "UTF-8"
1>
# Press CTRL + C to exit
Comments:

-H is a MSSQL server address that can test remote addresses
-P is the port number
-U is the database user name
-P is the database password
Compiling PHP extensions MSSQL

After the MSSQL expansion library is added, PHP can use the following code to connect the MSSQL

$con = Mssql_connect (' host:1433 ', ' user ', ' password ') or Die ("SQL SERVER database connection failed!") ");
can go to PHP website to download the corresponding version of the PHP full package and decompression, here to php-5.4.41 as an example

# Enter the MSSQL module source directory
$ CD Php-5.4.41/ext/mssql

# Generate compilation configuration file
$/usr/local/php/bin/phpize

# Compile Installation
$./configure--with-php-config=/usr/local/php/bin/php-config--with-mssql=/usr/local/freetds
$ make && make install
Run result after make install:

Build complete.
Don ' t forget to run ' make test '.
Installing Shared extensions:/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/
mssql.so files are generated in the above directory, then edit php.ini to increase the MSSQL Extension library support

$ vi/usr/local/php/etc/php.ini

# enabled as follows, if none is added
Extension = "mssql.so"

# Save and reboot
$/root/lnmp Restart

# to see if the configuration was successful
$/usr/local/php/bin/php-m
Extended reading: PHP Extended MSSQL connection remote MSSQL

Compiling PHP extensions Pdo_dblib libraries

After the Pdo_dblib extension library is added, PHP can use the following code to connect the MSSQL

$con = new PDO ("Dblib:host=myhost;dbname=mydb", "MyUserName", "MyPassword");
can go to PHP website to download the corresponding version of the PHP full package and decompression, here to php-5.4.41 as an example

# Enter the MSSQL module source directory
$ CD Php-5.4.41/ext/pdo_dblib

# Generate compilation configuration file
$/usr/local/php/bin/phpize

# Compile Installation
$./configure--with-php-config=/usr/local/php/bin/php-config--with-pdo_dblib=/usr/local/freetds
$ make && make install
When the installation is complete, pdo_dblib.so files are generated in the following directory

Installing Shared extensions:/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/
Next edit php.ini, add pdo_dblib Extension Library support

$ vi/usr/local/php/etc/php.ini

# enabled as follows, if none is added
Extension = "pdo_dblib.so"

# Save and reboot
$/root/lnmp Restart

# to see if the configuration was successful
$/usr/local/php/bin/php-m

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.