简体   繁体   English

如何从 Ubuntu 18.04 使用 PHP 连接到 MSSQL Server?

[英]How to connect to MSSQL Server with PHP from Ubuntu 18.04?

I want to establish a MSSQL Connection from Ubunutu 18.04.我想从 Ubunutu 18.04 建立一个 MSSQL 连接。 It was quite hard to get this set up, but know it works so far that I can use sql_srv class or pdo class.进行此设置非常困难,但我知道到目前为止我可以使用 sql_srv 类或 pdo 类。 But when I want to connect, the connection fails with error但是当我想连接时,连接失败并出现错误

Array ( [0] => Array ( [0] => 01000 [SQLSTATE] => 01000 [1] => 0 [code] => 0 [2] => [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 13 for SQL Server' : file not found [message] => [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 13 for SQL Server' : file not found ) ).

How can I resolve this issue and what does this error mean?我该如何解决这个问题,这个错误是什么意思? I've installed ODBC 17.我已经安装了 ODBC 17。

 odbcinst -j
unixODBC 2.3.7
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
FILE DATA SOURCES..: /etc/ODBCDataSources
USER DATA SOURCES..: /root/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8

And the nano /etc/odbcinst.ini shows: nano /etc/odbcinst.ini 显示:

[ODBC Driver 17 for SQL Server] Description=Microsoft ODBC Driver 17 for SQL Server Driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.5.so.1.1 UsageCount=1 [ODBC Driver 17 for SQL Server] Description=Microsoft ODBC Driver 17 for SQL Server Driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.5.so.1.1 UsageCount=1

What should I do to connect with MSSQL Server 2014 from ubuntu 18.04?我应该怎么做才能从 ubuntu 18.04 连接到 MSSQL Server 2014?

SOLVED: via this three pages I could install the correct Driver Version:已解决:通过这三页我可以安装正确的驱动程序版本:

https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15#ubuntu17 https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15#ubuntu17

Pecl install sqlsrv returns No releases available for package Pecl install sqlsrv 返回没有可用于包的版本

Download drivers from http://pecl.php.net/package/sqlsrv/5.8.0http://pecl.php.net/package/sqlsrv/5.8.0下载驱动程序

sudo bash -c "echo extension=sqlsrv.so > /etc/php/7.2/mods-available/sqlsrv.ini"
sudo ln -s /etc/php/7.2/mods-available/sqlsrv.ini /etc/php/7.2/apache2/conf.d/sqlsrv.ini
sudo ln -s /etc/php/7.2/mods-available/sqlsrv.ini /etc/php/7.2/cli/conf.d/sqlsrv.ini
sudo bash -c "echo extension=pdo_sqlsrv.so > /etc/php/7.2/mods-available/pdo_sqlsrv.ini"
sudo ln -s /etc/php/7.2/mods-available/pdo_sqlsrv.ini /etc/php/7.2/apache2/conf.d/pdo_sqlsrv.ini
sudo ln -s /etc/php/7.2/mods-available/pdo_sqlsrv.ini /etc/php/7.2/cli/conf.d/pdo_sqlsrv.ini


sudo systemctl restart apache2
  1. Install PECL安装 PECL
  2. Follow steps mentioned on https://docs.microsoft.com/en-us/sql/connect/php/installation-tutorial-linux-mac?view=sql-server-ver15按照https://docs.microsoft.com/en-us/sql/connect/php/installation-tutorial-linux-mac?view=sql-server-ver15 上提到的步骤操作
  3. Restart Apache重启阿帕奇
  4. Use below PHP script to test the connection使用下面的 PHP 脚本来测试连接
//format: serverName\\instanceName, portNumber (default is 1433) $serverName = "localhost"; $connectionInfo = array("Database" => "dbName", "UID" => "myUserName", "PWD" => "myPassword"); $conn = sqlsrv_connect($serverName, $connectionInfo); if ($conn) { echo "Got a connection!<br />"; } else { echo "Connection could not be established.<br />"; die(print_r(sqlsrv_errors(), true)); }

Use Microsoft Drivers for PHP for Microsoft SQL Server .使用Microsoft Drivers for PHP for Microsoft SQL Server You will find detailed installation instructions there.您将在那里找到详细的安装说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM