×

宝塔php8.0 手动安装freetds的 dblib pdo,php跑各种报错,全网唯一解决方法。

hqy hqy 发表于2025-06-26 21:47:03 浏览7 评论0

抢沙发发表评论

php8.0 安装pdo_sqlsrv,建议优先用它(微软官方驱动)成功连上MSSQL后,尝试用freetds的 dblib pdo连接,192.168.0.1的mssql是可以正常访问到的nmap -p 1433 192.168.0.1 是正常的



[root@localhost ~]# nmap -p 1433 192.168.0.1


Starting Nmap 6.40 ( http://nmap.org ) at 2025-06-26 21:34 CST

Nmap scan report for 192.168.0.1

Host is up (0.00031s latency).

PORT     STATE SERVICE

1433/tcp open  ms-sql-s

MAC Address: AA:04:CC:F1:DD:24 (Unknown)


Nmap done: 1 IP address (1 host up) scanned in 0.47 seconds






[root@localhost ~]# tsql -C

Compile-time settings (established with the "configure" script)

                            Version: freetds v1.4.16

             freetds.conf directory: /etc

     MS db-lib source compatibility: yes

        Sybase binary compatibility: yes

                      Thread safety: yes

                      iconv library: yes

                        TDS version: auto

                              iODBC: no

                           unixodbc: yes

              SSPI "trusted" logins: no

                           Kerberos: yes

                            OpenSSL: no

                             GnuTLS: yes

                               MARS: yes


/etc/freetds.conf 



        # TDS protocol version

        tds version = auto


        # Whether to write a TDSDUMP file for diagnostic purposes

        # (setting this to /tmp is insecure on a multi-user system)

;       dump file = /tmp/freetds.log

;       debug flags = 0xffff


        # Command and connection timeouts

;       timeout = 10

;       connect timeout = 10


        # To reduce data sent from server for BLOBs (like TEXT or

        # IMAGE) try setting 'text size' to a reasonable limit

;       text size = 64512


        # If you experience TLS handshake errors and are using openssl,

        # try adjusting the cipher list (don't surround in double or single quotes)

        # openssl ciphers = HIGH:!SSLv2:!aNULL:-DH


# A typical Sybase server

[egServer50]

        host = symachine.domain.com

        port = 5000

        tds version = 5.0


# A typical Microsoft server

[MSSQL2008]

 host = 192.168.0.1

 port = 1433

 tds version = 7.0

 client charset = UTF-8


运行一下代码后报错




<?php

$dsn = "dblib:host=192.168.0.1;dbname=test";

$username = "test";

$password = "test.123456";


try {

    $pdo = new PDO($dsn, $username, $password);

    echo "Connected successfully"; 

} catch (PDOException $e) {

    die("Could not connect to database: " . $e->getMessage());

}

?>


  提示Could not connect to database: SQLSTATE[01002] Adaptive Server connection failed (192.168.0.221) (severity 9)

#  Could not connect to database: SQLSTATE[HYT00]: [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired    这个是MSSQL连不上,能连上不会报这个错误。


代码修该为:

<?php

$dsn = "dblib:host=192.168.0.1:1433;dbname=test;charset=UTF-8;tds_version=7.3";

$username = "test";

$password = "test123456";


try {

    $pdo = new PDO($dsn, $username, $password);

    echo "Connected successfully"; 

} catch (PDOException $e) {

    die("Could not connect to database: " . $e->getMessage());

}

?>



/etc/freetds.conf

[MSSQL]

    host = 192.168.0.1

    port = 1433

    tds version = 7.3

    client charset = UTF-8


# DSN 可以写成    $dsn = "dblib:host=mssql;dbname=test";



确保 FreeTDS 是 较新版本(>= 1.1)

老版本 FreeTDS(如 0.91、0.95)会有各种兼容性问题,推荐版本:1.3+ 或更高

? 查看当前版本:

bash复制编辑tsql -C

输出样例:

pgsql复制编辑Compile-time settings (established with the "configure" script)                            Version: freetds v1.3.6

⚠️ 如果低于 1.1,建议升级:

bash复制编辑sudo apt remove freetds-bin libsybdb5
sudo apt install freetds-dev

或者从源码安装最新版:

bash复制编辑wget https://www.freetds.org/files/stable/freetds-1.3.6.tar.gz
tar -xvzf freetds-1.3.6.tar.gzcd freetds-1.3.6
./configure --prefix=/usr/local --with-tdsver=7.4
make
sudo make install





设置变量

export TDSVER=7.3

# 用DN确保能连接

tsql -S MSSQL -U test -P test.123456


# 或不加映射直接测试

tsql -H 192.168.0.1 -p 1433 -U test -P test.123456


结果还是报错。Adaptive Server is unavailable or does not exist  


根据这肯爹的官方文档 

http://www.freetds.org/userguide/ChoosingTdsProtocol.html


QQ_1750945210773.pngQQ_1750945221979.png


SELECT @@VERSION;


Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64)   Apr  2 2010 15:48:46   Copyright (c) Microsoft Corporation  Standard Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) 


MSSQL是2008,应该选版本是7.3啊?


写一段测试脚本快速验证多个 TDS 版本是否可连?

vi test_tds_versions.sh 

#!/bin/bash

# SQL Server 连接信息
HOST="192.168.0.221"
PORT=1433
USER="kaoqin"
PASS="Aa.123456"

# 要测试的 TDS 版本列表
TDS_VERSIONS=("7.4" "7.3" "7.2" "7.1" "7.0" "5.0")

echo "开始测试连接 SQL Server($HOST:$PORT)"
echo "----------------------------------------"

for ver in "${TDS_VERSIONS[@]}"; do
    echo "正在测试 TDSVER=$ver ..."
    TDSVER=$ver tsql -H $HOST -p $PORT -U $USER -P $PASS <<EOF > /tmp/tsql_test_output 2>&1
quit
EOF

    if grep -q "locale" /tmp/tsql_test_output && ! grep -q "Error" /tmp/tsql_test_output; then
        echo "✅ 成功连接!TDSVER=$ver"
    else
        echo "❌ 连接失败,TDSVER=$ver"
        grep -E "Error|Adaptive|Server|failed" /tmp/tsql_test_output | sed 's/^/   /'
    fi

    echo "----------------------------------------"
done

# 清理临时文件
rm -f /tmp/tsql_test_output


chmod +x test_tds_versions.sh  


./test_tds_versions.sh



开始测试连接 SQL Server(192.168.0.1:1433)

----------------------------------------

[root@localhost ~]# ./test.sh 

开始测试连接 SQL Server(192.168.0.1:1433)

----------------------------------------

正在测试 TDSVER=7.4 ...

❌ 连接失败,TDSVER=7.4

   Error 20002 (severity 9):

        Adaptive Server connection failed

----------------------------------------

正在测试 TDSVER=7.3 ...

❌ 连接失败,TDSVER=7.3

   Error 20002 (severity 9):

        Adaptive Server connection failed

----------------------------------------

正在测试 TDSVER=7.2 ...

❌ 连接失败,TDSVER=7.2

   Error 20002 (severity 9):

        Adaptive Server connection failed

----------------------------------------

正在测试 TDSVER=7.1 ...

❌ 连接失败,TDSVER=7.1

   Error 20002 (severity 9):

        Adaptive Server connection failed

----------------------------------------

正在测试 TDSVER=7.0 ...

✅ 成功连接!TDSVER=7.0

----------------------------------------

正在测试 TDSVER=5.0 ...

❌ 连接失败,TDSVER=5.0

   Error 20017 (severity 9):

   Error 20002 (severity 9):

        Adaptive Server connection failed

----------------------------------------



泥马。。。。MSSQL2008不是说7.3吗? 怎么还是7.0 ???


设置变量

export TDSVER=7.0

# 用DN确保能连接

tsql -S MSSQL -U test -P test.123456


# 或不加映射直接测试

tsql -H 192.168.0.1 -p 1433 -U test -P test.123456


返回 

locale is "zh_CN.UTF-8"

locale charset is "UTF-8"

using default charset "UTF-8"

1> 

都是OK了



<?php

$dsn = "dblib:host=192.168.0.1:1433;dbname=test;charset=UTF-8;tds_version=7.0";

$username = "test";

$password = "test.123456";


try {

    $pdo = new PDO($dsn, $username, $password);

    echo "Connected successfully"; 

} catch (PDOException $e) {

    die("Could not connect to database: " . $e->getMessage());

}

?>

运行后,泥马报 Connected successfully  了!!!!!!


打赏

本文链接:https://kinber.cn/post/5185.html 转载需授权!

分享到:


推荐本站淘宝优惠价购买喜欢的宝贝:

image.png

 您阅读本篇文章共花了: 

群贤毕至

访客