简体   繁体   English

从 Ubuntu 到 SQL 服务器的 PHP ODBC 连接

[英]PHP ODBC Connection to a SQL server from Ubuntu

My host is a ubuntu machine 20.04 I'm using PHP 7.4 with PDO_ODBC installed我的主机是 ubuntu 机器 20.04 我使用的是 PHP 7.4 并安装了PDO_ODBC

The goal is to be able to access to a SQL database who is using Windows credential on a ubuntu machine.目标是能够访问在 ubuntu 机器上使用 Windows 凭据的 SQL 数据库。

This is my /etc/odbc.ini file :这是我的/etc/odbc.ini文件:

[msodbcsql]
Description=SQL Server driver
Driver=/usr/lib/libmsodbcsql-17.so

[pds_database]
Driver=msodbcsql
Description=PDS database access
Server=<server ip>\PDS
Port=1433

When I run tsql -S pds_database -U <domain>\\\\<user> -P<passqord> I get this promt : 1>当我运行tsql -S pds_database -U <domain>\\\\<user> -P<passqord>我得到这个提示: 1>

I presume my pds_database connection is setup correctly ...我认为我的pds_database连接设置正确......

now, This is my PHP code :现在,这是我的 PHP 代码:

$db = new PDO ("odbc:pds_database", '<domain>\\<user>', '<password>');

I get the following error exception :我收到以下错误异常:

"SQLSTATE[IM002] SQLConnect: 0 [unixODBC][Driver Manager]Data source name not found and no default driver specified"

Do you have any idea what I missed during this connection ?你知道我在这个连接过程中错过了什么吗?

PS : /usr/lib/libmsodbcsql-17.so is a symbolic link to /opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.7.so.2.1 What is mean I'm running a libmsodbcsql 64 bits like my PHP PS: /usr/lib/libmsodbcsql-17.so/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.7.so.2.1的符号链接是什么意思我正在运行像我的 PHP 一样的libmsodbcsql 64 位

I've the exactly same problem with you我和你有完全一样的问题

and I'm gonna share my full code about connection .php我将分享我关于连接 .php 的完整代码

<?php

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

// define variables;
/* $servername = "";
$username = "";
$password = "";
$dbname = ""; */

// Create connection
try {

    // THIS IS OLD WRONG CODE, PLEASE CHECK BELOW
    $DSN = 'odbc:Driver={ODBC Driver 17 for SQL Server};
        Server=192.168.X.X,1234\\MSSQLSERVER;Database=db_um_1699;
        Trusted_Connection=yes';

    $dbconn = new PDO($DSN, 'username', 'userPassword');
    
    $dbconn -> setattribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
    $dbconn -> setattribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    "BRIDGE BUILD! \n <hr />";// Check connection

}catch (PDOException $e) {
    echo "Broke: ". $e->getMessage();
}

And yet I'm still using old way to connecting which is a mess and so difficult to operated.但是我仍然使用旧的连接方式,这是一个混乱且难以操作的方式。


12-16-2021 edit: 12-16-2021 编辑:

I was changed odbc:driver to odbc:FreeTDS that worked!我已将odbc:driver更改为odbc:FreeTDS有效!

first assume we all had odbc drivers;首先假设我们都有 odbc 驱动程序; we need to edit odbc.ini file in /etc/odbcinst.ini我们需要编辑/etc/odbcinst.ini odbc.ini文件

[FreeTDS]
Description = ODBC for FreeTDS
Driver64    = /usr/lib64/libtdsodbc.so
Setup64     = /usr/lib64/libtdsodbc.so
FileUsage   = 1

next try to build the connection:接下来尝试建立连接:

// Create connection
try {

    // CHANGE ODBC to FREETDS
    $DSN = 'odbc:Driver={FreeTDS};
        Server=192.168.X.X,1234\\MSSQLSERVER;Database=db_um_1699;
        Trusted_Connection=yes';

    $dbconn = new PDO($DSN, 'username', 'userPassword');
    
    $dbconn -> setattribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
    $dbconn -> setattribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    "BRIDGE BUILD! \n <hr />";// Check connection

}catch (PDOException $e) {
    echo "Broke: ". $e->getMessage();
}

hope this information would be anyone who was same problem I had希望这些信息是我遇到同样问题的任何人

but this still maybe not the good enough for all kind similar problem since our database really aged which was MS-sql-2008但这对于所有类似的问题可能仍然不够好,因为我们的数据库确实老化了MS-sql-2008

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

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