Laravel SQLSRV在修补模式下工作,但在浏览器中不工作 [英] Laravel sqlsrv works in tinker mode but not in browser

查看:96
本文介绍了Laravel SQLSRV在修补模式下工作,但在浏览器中不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆