Laravel SQLSRV在修补模式下工作,但在浏览器中不工作 [英] Laravel sqlsrv works in tinker mode but not in browser
问题描述
我有一个Laravel 5项目(在CentOS 7机器上),该项目连接到外部MSSQL数据库.
I have a Laravel 5 project (on a CentOS 7 machine) that connects to an external MSSQL database.
我按照概述的设置进行操作这里使用FreeTDS.
I followed the setup outlined here using FreeTDS.
当我使用浏览器访问页面时,出现错误:SQLSTATE[HY000] Unable to connect: Adaptive Server is unavailable or does not exist (severity 9)
.
When I hit the page using a browser I get the error: SQLSTATE[HY000] Unable to connect: Adaptive Server is unavailable or does not exist (severity 9)
.
但是,当我使用php artisan tinker
并执行类似App\MyModel::get();
的操作时,它会返回带有记录/模型的Illuminate\Database\Eloquent\Collection
实例.
But when I use php artisan tinker
and do something like App\MyModel::get();
, it returns an Illuminate\Database\Eloquent\Collection
instance with the records/models just fine.
有人知道这是怎么回事吗?
Anyone knows what's wrong with this?
更新
我的.env
如下所示:
SQLSRV_DB_DRIVER=sqlsrv
SQLSRV_DB_HOST=host.ip.address
SQLSRV_DB_DATABASE=my_db
SQLSRV_DB_USERNAME=my_username
SQLSRV_DB_PASSWORD=my_password
我的config/database.php
看起来像:
'sqlsrv' => [
'driver' => env('SQLSRV_DB_DRIVER'),
'host' => env('SQLSRV_DB_HOST'),
'database' => env('SQLSRV_DB_DATABASE'),
'username' => env('SQLSRV_DB_USERNAME'),
'password' => env('SQLSRV_DB_PASSWORD'),
'prefix' => '',
];
我的模型指定了一个连接:protected $connection = 'sqlsrv';
My model has a connection specified: protected $connection = 'sqlsrv';
我正在将FreeTDS与unixODBC一起使用.
I'm using FreeTDS with unixODBC.
推荐答案
SELinux阻止了您与MSSQL的传出连接.
SELinux is blocking your outgoing connection to MSSQL.
正确的解决方案是允许apache建立连接:
The proper solution is to allow apache to make the connection:
setsebool -P httpd_can_network_connect_db 1
-P
标志可使规则持久化,否则重新启动后该规则将丢失.
The -P
flag makes the rule persistent, otherwise the rule is lost after a reboot.
这篇关于Laravel SQLSRV在修补模式下工作,但在浏览器中不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!