我正在尝试从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
我尝试了所有可能的组合:
有人可以帮助我吗?
修改: 因为我也尝试过没有实例名称(as stated here),脚本会一直尝试连接,直到我收到此错误:
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 连接到该实例,因此它似乎更像是DSN的PDO问题
我还尝试使用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 (这是一个非命名的实例),一切顺利......
答案 0 :(得分:7)
我终于找到了解决方案,有两个问题:
所以我最终使用了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' => '',
),
这解决了我的问题,希望它也会帮助别人
答案 1 :(得分:1)
嗨,我遇到了这个问题,但我解决了。 我正在使用 docker,我使用 curl (curl -s https://laravel.build/example-app | bash) 安装了最新版本的 laravel,此安装包含 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 文档,我将 tds 版本设置为 7.3。 (https://www.freetds.org/userguide/ChoosingTdsProtocol.html)
然后在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'),
就是这样。
微软驱动程序从来不适合我
答案 2 :(得分: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' => '',
],
现在一切正常。