APT 未找到 Microsoft ODBC v18

APT 未找到 Microsoft ODBC v18

我正在跟进这些在 Ubuntu 22.04 上安装 ODBC 驱动程序 v18 的说明。我知道该脚本不想为 Ubuntu 22.04 安装任何东西,但我敢打赌这是一个拼写错误,因为22.04确实存在。

发出后:

sudo su
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
curl https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/prod.list > /etc/apt/sources.list.d/mssql-release.list
exit
sudo apt-get update

它没有找到版本 18:

$ apt-cache search msodbc
msodbcsql17 - ODBC Driver for Microsoft(R) SQL Server(R)

我到底犯了什么错?

编辑

回答后测试代码mizera

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

$SERVER = "192.168.0.50";
$DATABASE = "DB";
$DRIVER = "/usr/lib/libmsodbcsql-18.so";
const USERNAME = "user";
const PASSWORD = "pswd";

try {
    $db = new PDO("odbc:driver=$DRIVER;server=$SERVER;database=$DATABASE", USERNAME, PASSWORD);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo '<div class="alert alert-danger" role="alert">';
    echo "Connection to database failed:" . PHP_EOL . $e->getMessage();
    echo '</div>';

    var_dump($e);
}

输出为:

object(PDOException)#3 (8) { ["message":protected]=> string(21) "could not find driver" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(27) "/home/user/www/database.php" ["line":protected]=> int(25) ["trace":"Exception":private]=> array(2) { [0]=> array(5) { ["file"]=> string(27) "/home/user/www/database.php" ["line"]=> int(25) ["function"]=> string(11) "__construct" ["class"]=> string(3) "PDO" ["type"]=> string(2) "->" } [1]=> array(4) { ["file"]=> string(25) "/home/user/www/tables.php" ["line"]=> int(2) ["args"]=> array(1) { [0]=> string(27) "/home/user/www/database.php" } ["function"]=> string(7) "require" } } ["previous":"Exception":private]=> NULL ["errorInfo"]=> NULL }

答案1

我尝试从 Ubuntu 21.10 包安装,并且它可以正常工作(第一步很重要,因为如果没有它安装会导致错误):

apt-get install odbcinst

sudo curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -

echo "deb [arch=amd64] https://packages.microsoft.com/ubuntu/21.10/prod impish main" | sudo tee /etc/apt/sources.list.d/mssql-release.list

apt update

apt install msodbcsql18

编辑: 我的回答是关于安装驱动程序,如果您想与 PHP 一起使用,请尝试针对 PHP 8.1(php-dev、pecl、php-pear 等)的以下步骤:

    add-apt-repository ppa:ondrej/php -y
    apt-get update -y
    apt-get install php8.1-dev -y --allow-unauthenticated
    apt-get install unixodbc-dev -y
    apt install php-pear -y 
    pecl install pdo_sqlsrv
    pecl install sqlsrv
    printf "; priority=20\nextension=sqlsrv.so\n" > /etc/php/8.1/mods-available/sqlsrv.ini
    printf "; priority=30\nextension=pdo_sqlsrv.so\n" > /etc/php/8.1/mods-available/pdo_sqlsrv.ini
    phpenmod -v 8.1 sqlsrv pdo_sqlsrv
    systemctl restart apache2

编辑2 测试代码(针对MSSql Server 2016):

    $conn = \sqlsrv_connect(
        $server, 
            [
                'Database' => $db, 
                'UID' => $user, 
                'PWD' => $pass, 
                'CharacterSet' => 'UTF-8', 
                'Encrypt' => 1, 
                'TrustServerCertificate' => 1
            ]
        );
    if ($conn === false) {
        echo "Unable to connect.</br>";
        die(print_r(\sqlsrv_errors(), true));
    }

答案2

我正在尝试在 Jammy 22.04 docker 容器上安装 msodbcsql18 包,Google 带来了这里。截至 2023 年 6 月,该包似乎已经存在。lsb-base 和 apt-key 命令失败,您可以按如下方式安装它:

curl -sS https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor | tee /etc/apt/trusted.gpg.d/mssql.gpg
curl https://packages.microsoft.com/config/ubuntu/22.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
apt-get update
ACCEPT_EULA=Y apt-get install -y msodbcsql18

您很可能需要使用 sudo 或 su。这是从已经以 root 身份运行的 CI 开始的

相关内容