How PHP uses sqlsrv to connect to Microsoft database under Linux

Source: Internet
Author: User
PHP uses sqlsrv to connect to Microsoft's SQL Server database to use the extended pdo_srv.
Pdo_srv extended installation can be installed directly using PECL

When writing this article, the environment is
Ubuntu 16.04 LTS
PHP 7.0.27
UnixODBC 2.3.5
FreeTDS 1.00.82
2018-03-02 Day

Pecl
The program is in the bin directory under the PHP installation directory, if your PHP directory is not in the Linux environment variable. Or your PECL program is not linked to the system bin path and will prompt you to pecl the program is not installed.

Installing SQLSRV requires UNIXODBC support, so if you haven't installed UNIXODBC before installing, you'll need to install UNIXODBC first, and UNIXODBC need freetds to connect to SQL Server. Because UNIXODBC only defines a common set of interfaces, and there is no specific implementation method,

First, install the FreeTDS
a), download

http://www.freetds.org/

b), compile and install

./configure--with-tdsver=auto--prefix=/usr/local/freetds--enable-msdblib--with-gnu-ld--enable-shared-- Enable-static--with-unixodbc=/usr/localmakemake Install

Some of the online tutorials are written in –with-tdsver=8.0, I compile the error, the specific support values can be seen. Description of With-tdsver in/configure–help

After the installation is completed, a etc directory is generated in/usr/local/freetds and we need to use the freetds.conf file.
c), configuration
Configure the freetds.conf file. Can not be configured

The method of not configuring freetds.conf can be seen in the configuration of UNIXODBC

[Global] #全局配置tds Version = auto# More configuration can refer to FreeTDS official documents, I have this configuration is sufficient for [Servertag] #定义一个叫 servertag node, the name of this node can be customized, not fixed #所有 the contents of the # symbol can not be written in the configuration file, here is just comment explanation with # More details configuration Look official description # Database connection IP Address host = 192.168.9.12 #数据库的端口port = 1433 #连接时默认打开的库名, "Can not configure this item" Database = Master #tds version has a global configuration, it can also not write TDS Version = Auto

Second, installation Unixodbc

a), download

  https://sourceforge.net/projects/unixodbc/files/unixODBC/  http://www.unixodbc.org/  More than two sites can be downloaded to, It is recommended to download it on the second website.

b), compile and install

./configure--PREFIX=/USR/LOCAL/UNIXODBC--includedir=/usr/include  --libdir=/usr/lib-bindir=/usr/bin-- Sysconfdir=/etcmake make Install

c), configuration
adding configurations in/etc/odbc.ini

There are tutorials on the web that are compiled and installed in/usr/local/etc/odbc.ini
, although this file is generated after compilation, the configuration does not take effect in this file. Or do you want to configure it in/etc/odbc.ini

[ODBC Data Sources] #描述而已, no test not write line, theoretically feasible SQLSERVER2018912 = 9.12 db[sqlserver2018912] #Driver point to the directory is FreeTDS compile the generated file, Similar to a driver file, the installation method is explained later driver =/usr/local/freetds/lib/libtdsodbc.sodescription  = 9.12 DB, text description trace = No#server can be written directly The Servertag node name # configured in the FreeTDS freetds.conf is not configured with the freetds.conf configuration. There are few options that can be configured #因为在此直接写server IP, and freetds.conf can configure more of the details # Here I have configured the ip,freetds.conf in fact can not be configured. As long as the FreeTDS is installed, server = 192.168.9.12Port = 1433#7.4 or auto should be OK. No detailed studies have been done on tds_version = 7.4[default] #全局默认配置. Theoretically can not write # ibid, default use Driver =/usr/local/freetds/lib/libtdsodbc.so

Once the above configuration is complete, you can test it.
Test with the ISQL program generated by UNIXODBC compile time. Path in/usr/bin/isql

/usr/bin/isql SQLSERVER2018912 Database account password

Use of isql can be viewed using isql–help
Configuration if there is no error, you can see the connection to the database. You can use SQL statements to query for things.
After the UNIXODBC configuration is complete, you can install the PHP extension

Third, install PHP sqlsrv extension

PECL Install sqlsrv

After installation, you need to go to php.ini to load the extension, and then restart Apache to take effect.
Watch what the screen prints after the compilation.

When writing this article, the environment is
Ubuntu 16.04 LTS
PHP 7.0.27
UnixODBC 2.3.5
FreeTDS 1.00.82
2018-03-02 Day

PHP uses sqlsrv to connect to Microsoft's SQL Server database to use the extended pdo_srv.
Pdo_srv extended installation can be installed directly using PECL

Pecl
The program is in the bin directory under the PHP installation directory, if your PHP directory is not in the Linux environment variable. Or your PECL program is not linked to the system bin path and will prompt you to pecl the program is not installed.

Installing SQLSRV requires UNIXODBC support, so if you haven't installed UNIXODBC before installing, you'll need to install UNIXODBC first, and UNIXODBC need freetds to connect to SQL Server. Because UNIXODBC only defines a common set of interfaces, and there is no specific implementation method,

First, install the FreeTDS
a), download

http://www.freetds.org/

b), compile and install

./configure--with-tdsver=auto--prefix=/usr/local/freetds--enable-msdblib--with-gnu-ld--enable-shared-- Enable-static--with-unixodbc=/usr/localmakemake Install

Some of the online tutorials are written in –with-tdsver=8.0, I compile the error, the specific support values can be seen. Description of With-tdsver in/configure–help

After the installation is completed, a etc directory is generated in/usr/local/freetds and we need to use the freetds.conf file.
c), configuration
Configure the freetds.conf file. Can not be configured

The method of not configuring freetds.conf can be seen in the configuration of UNIXODBC

[Global] #全局配置tds Version = auto# More configuration can refer to FreeTDS official documents, I have this configuration is sufficient for [Servertag] #定义一个叫 servertag node, the name of this node can be customized, not fixed #所有 the contents of the # symbol can not be written in the configuration file, here is just comment explanation with # More details configuration Look official description # Database connection IP Address host = 192.168.9.12 #数据库的端口port = 1433 #连接时默认打开的库名, "Can not configure this item" Database = Master #tds version has a global configuration, it can also not write TDS Version = Auto

Second, installation Unixodbc

a), download

  https://sourceforge.net/projects/unixodbc/files/unixODBC/  http://www.unixodbc.org/  More than two sites can be downloaded to, It is recommended to download it on the second website.

b), compile and install

./configure--PREFIX=/USR/LOCAL/UNIXODBC--includedir=/usr/include  --libdir=/usr/lib-bindir=/usr/bin-- Sysconfdir=/etcmake make Install

c), configuration
adding configurations in/etc/odbc.ini

There are tutorials on the web that are compiled and installed in/usr/local/etc/odbc.ini
, although this file is generated after compilation, the configuration does not take effect in this file. Or do you want to configure it in/etc/odbc.ini

[ODBC Data Sources] #描述而已, no test not write line, theoretically feasible SQLSERVER2018912 = 9.12 db[sqlserver2018912] #Driver point to the directory is FreeTDS compile the generated file, Similar to a driver file, the installation method is explained later driver =/usr/local/freetds/lib/libtdsodbc.sodescription  = 9.12 DB, text description trace = No#server can be written directly The Servertag node name # configured in the FreeTDS freetds.conf is not configured with the freetds.conf configuration. There are few options that can be configured #因为在此直接写server IP, and freetds.conf can configure more of the details # Here I have configured the ip,freetds.conf in fact can not be configured. As long as the FreeTDS is installed, server = 192.168.9.12Port = 1433#7.4 or auto should be OK. No detailed studies have been done on tds_version = 7.4[default] #全局默认配置. Theoretically can not write # ibid, default use Driver =/usr/local/freetds/lib/libtdsodbc.so

Once the above configuration is complete, you can test it.
Test with the ISQL program generated by UNIXODBC compile time. Path in/usr/bin/isql

/usr/bin/isql SQLSERVER2018912 Database account password

Use of isql can be viewed using isql–help
Configuration if there is no error, you can see the connection to the database. You can use SQL statements to query for things.
After the UNIXODBC configuration is complete, you can install the PHP extension

Third, install PHP sqlsrv extension

PECL Install sqlsrv
After the

is installed, you need to go to php.ini to load the extension, and then restart Apache to take effect.
Notice what the screen prints after the compilation

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.