Laravel连接到SQL Server 2008命名实例

7

我正在尝试从Ubuntu机器连接SQL服务器,除了命名实例之外,一切都很顺利:

这个可以正常工作

'data' => array(
            'driver'   => 'sqlsrv',
            'host'     => 'xxxx',
            'port'     => 1433,
            'database' => 'db',
            'username' => 'user',
            'password' => 'pwd',
            'prefix'   => '',
        ),

这不是

  'data' => array(
                'driver'   => 'sqlsrv',
                'host'     => 'yyyy\NAMEDINSTANCE',
                'port'     => 1433,
                'database' => 'db',
                'username' => 'user',
                'password' => 'pwd',
                'prefix'   => '',
            ),

我经常遇到这个错误:
exception 'PDOException' with message 'SQLSTATE[HY000] Unknown host machine name (severity 2)' in /var/www/public/my.api/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:47

我已经尝试了各种可能的组合方式:
  • host \ INSTANCE
  • host / INSTANCE
  • host \\ INSTANCE
有人能帮帮我吗? 编辑: 因为我也尝试过没有实例名称(如此陈述),脚本会一直尝试连接,直到我收到这个错误:
exception 'PDOException' with message 'SQLSTATE[HY000] Unable to connect: Adaptive Server is unavailable or does not exist (severity 9)' in /var/www/public/my.api/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:47

任何像Management Studio、DBeaver或Database.NET这样的编辑器只能通过指定INSTANCENAME连接到该实例,因此似乎更多是PDO和DSN的问题。我也尝试直接使用tsql连接到该命名实例,但没有更多的运气。
tsql -S SERVER -U usr -P pwd -L dbname

这里是/etc/freetds.conf文件:

[global]
        tds version = 8.0
        client charset = UTF-8
        port = 1433
        dump file = /tmp/freetds.log
        dump file append = yes
        text size = 64512

[SERVER]
        host = ip
        port = 1433
        instance = instance_name

[SERVER2]
        host = ip
        port = 1433

还有tds日志文件:

log.c:196:Starting log file for FreeTDS 0.91
        on 2015-03-19 15:35:46 with debug flags 0x4fff.
iconv.c:330:tds_iconv_open(0xc163a0, UTF-8)
iconv.c:187:local name for ISO-8859-1 is ISO-8859-1
iconv.c:187:local name for UTF-8 is UTF-8
iconv.c:187:local name for UCS-2LE is UCS-2LE
iconv.c:187:local name for UCS-2BE is UCS-2BE
iconv.c:349:setting up conversions for client charset "UTF-8"
iconv.c:351:preparing iconv for "UTF-8" <-> "UCS-2LE" conversion
iconv.c:391:preparing iconv for "ISO-8859-1" <-> "UCS-2LE" conversion
iconv.c:394:tds_iconv_open: done
net.c:205:Connecting to 195.70.16.92 port 1433 (TDS version 7.1)
net.c:270:tds_open_socket: connect(2) returned "Operation now in progress"
net.c:306:getsockopt(2) reported: Connection timed out
net.c:316:tds_open_socket() failed
util.c:331:tdserror(0xc16140, 0xc163a0, 20009, 110)
util.c:361:tdserror: client library returned TDS_INT_CANCEL(2)
util.c:384:tdserror: returning TDS_INT_CANCEL(2)
mem.c:615:tds_free_all_results()

当然,如果我尝试连接到SERVER2(一个非命名实例),一切都会顺利进行...


1
尝试将端口号设置为 NULL,并使用 'host' => 'yyyy\NAMEDINSTANCE' - Guillermo Gutiérrez
同样的错误,我已经更新了我的问题并进行了更多的测试,tsql在命名实例上给了我一个超时错误,所以我怀疑这更多是一个freeTDS的问题而不是PDO的问题。 - kitensei
1
@GuillermoGutiérrez 工作得非常好...多年后仍然如此。非常感谢。 - Murwa
3个回答

8
我终于找到了解决方案,有两个问题:
  • SQL服务器没有监听正确的默认端口(我的错)
  • Laravel(PDO?)不知道如何处理(或者至少我没有找到如何处理)命名实例,我尝试了任何可能的组合(请参见问题)
因此,我最终使用FreeTDS DSN与Laravel结合使用,以连接SQL命名实例服务器。 /etc/freetds.conf DSN配置:
[NAMED_INSTANCE]
   host = 127.0.0.1
   port = 55021

在 Laravel 数据库适配器中:

'webcmd' => array(
    'driver'   => 'sqlsrv',
    'host'     => 'NAMED_INSTANCE',
    'database' => 'db',
    'username' => 'usr',
    'password' => 'pwd',
    'prefix'   => '',
),

这解决了我的问题,希望也能帮助到其他人


这拯救了我的一天。 - Viet Nguyen
1
谢谢!在Ubuntu 16.04上使用ppa:ondrej / php,我必须首先运行以下命令:sudo apt-get install php7.0-sybase freetds-common libsybdb5 - Palantir

1

你好,我曾遇到这个问题,但现在已经解决了。 我正在使用Docker,通过curl安装了最新版本的Laravel(curl -s https://laravel.build/example-app | bash),此安装包含php8.0、Laravel 8.0和Ubuntu 21.04。 修改Dockerfile并添加以下库:

nano unixodbc unixodbc-dev freetds-common freetds-dev tdsodbc freetds-bin php8.0-sybase

最后一个(php8.0-sybase)很重要,因为它允许您进行迁移。
另外,请将以下内容添加到文件中:
RUN echo "[sqlserver] \ n \
host = IPSERVER \ n \
port = 1433 \ n \
tds version = 7.3 ">> /etc/freetds/freetds.conf

我想连接到 MS SQL Server 2008,根据 FREETDS 文档 (https://www.freetds.org/userguide/ChoosingTdsProtocol.html),我将 TDS 版本设置为 7.3。

然后在 Laravel 的 .env 文件中修改与数据库的连接:

DB_CONNECTION = sqlsrv
DB_HOST = sqlserver // the same one that was configured in the freetds.conf file
DB_PORT = 1433
DB_DATABASE = databasename
DB_USERNAME = username
DB_PASSWORD = password

在 Laravel 的 database.php 文件中,更改默认连接:
'default' => env ('DB_CONNECTION', 'sqlsrv'),

And that's it.
微软的驱动程序从未对我起作用。

0

感谢参与解决这个连接问题。我也遇到了这个问题,以下是我是如何解决的。

在我的情况下,与 tsql 的连接可以工作,但不适用于 Laravel(5.4)。

我采取的一个技巧是理解它不是使用默认端口(1433)。

要查找端口,您必须启动 shell 连接:

tsql -D DB -S "IP\INSTANCE" -U login -P pass

请查看日志,好的端口在此处为1168

Net.c: 1059:实例端口是1168

freetds.conf文件内容:

[global]
    text size = 64512
    dump file = /var/log/freetds.log
    dump file append = yes

[mssql]
    host = MSSQLSRV
    port = 1168
    tds version = auto
    instance = IP\INSTANCE
    dump file = /var/log/freetds.log
    dump file append = yes

文件内容:config/database.php

 'sqlsrv' => [
            'driver'   => 'sqlsrv',
            'host'     => 'mssql',
            'port'     => '1168',
            'database' => 'DB',
            'username' => 'login',
            'password' => 'pass',
            'prefix'   => '',
        ],

现在一切都正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接