简体   繁体   English

SQL Server 连接到 Laravel

[英]SQL Server connection to Laravel

I'm trying to establish a connection to SQL Server with my Laravel app locally .我正在尝试使用我的 Laravel 应用程序在本地建立与 SQL Server 的连接。

What I've done is checking pdo files are installed correctly and phpinfo shows sqlsrv.我所做的是检查 pdo 文件是否安装正确并且 phpinfo 显示 sqlsrv。

ENV Connection String ENV 连接字符串

DB_CONNECTION=sqlsrv
DB_HOST=DESKTOP-Q6F14ID
DB_PORT=1433
DB_DATABASE=MYDB
DB_USERNAME=
DB_PASSWORD=

I've set the default to sqlsrv我已将默认设置为 sqlsrv

'default' => env('DB_CONNECTION', 'sqlsrv'),

And still get the error仍然得到错误

SQLSTATE[08001]: [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: No connection could be made because the target machine actively refused it. SQLSTATE[08001]:[Microsoft][ODBC Driver 17 for SQL Server]TCP 提供程序:无法建立连接,因为目标计算机主动拒绝了它。 (SQL: select * from sys.sysobjects where id = object_id(migrations) and xtype in ('U', 'V')) (SQL: select * from sys.sysobjects where id = object_id(migrations) and xtype in ('U', 'V'))

Illuminate\Database\QueryException .. PDO::__construct("sqlsrv:Server=DESKTOP-Q6F14ID,1433;Database=MYDB", "", "", []) Illuminate\Database\QueryException .. PDO::__construct("sqlsrv:Server=DESKTOP-Q6F14ID,1433;Database=MYDB", "", "", [])

I've tried windows auth to connect but it didn't work.我试过windows auth来连接,但没有用。 I've tried this string too.我也试过这个字符串。

DB_DATABASE=MYDB
DB_USERNAME=DESKTOP-Q6F14ID\MyUsername
DB_PASSWORD=

I've tried these host names too我也试过这些主机名

DB_HOST=localhost
DB_HOST=127.0.0.1
DB_HOST=MyLocalIP
DB_HOST=DESKTOP-Q6F14ID

And tried these usernames too without passwords并在没有密码的情况下也尝试了这些用户名

DB_USERNAME=DESKTOP-Q6F14ID
DB_USERNAME=DESKTOP-Q6F14ID/MyUsername
DB_USERNAME=MyUsername

and tried SQL Server authentication instead of windows and I created a user in SQL Server and ii can't even login.并尝试了 SQL Server 身份验证而不是 windows,我在 SQL Server 中创建了一个用户,我什至无法登录。

DB_USERNAME=Laravel
DB_PASSWORD=admin123

I've added a rule in the firewall to enable 1433 port still nothing.我在防火墙中添加了一条规则来启用 1433 端口仍然没有。

And I've checked the port in SQL config that is using 1433 correctly.我已经检查了 SQL 配置中正确使用 1433 的端口。

Check the config files name (database.php) and scroll down check to find sqlsrv and .env files add DATABASE_URL=检查配置文件名(database.php)并向下滚动检查以找到 sqlsrv 和 .env 文件添加 DATABASE_URL=

database this one miss it数据库这个想念它

 'default' => env('DB_CONNECTION', 'sqlsrv'),

this miss its on evn DATABASE_URL=这错过了 evn DATABASE_URL=

DB_CONNECTION=sqlsrv
DATABASE_URL=
DB_HOST=127.0.0.1
DB_PORT=1433
DB_DATABASE=DBNAME
DB_USERNAME=
DB_PASSWORD=

'sqlsrv' => [
            'driver' => 'sqlsrv',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '1433'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,
        ],

try restart php service尝试重启php服务

systemctl restart php7.0-fpm.service systemctl 重启 php7.0-fpm.service

启用共享内存和远程连接后,发现唯一缺少的是在 sql server config 中启用 TCP/IP 并立即解决了问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM