Error in Connecting to MSSQL Windows Azure Laravel 5 - php

I'm working on a project that needs to be connected to a MS SQL Database, I have a cloud database which is hosted in Microsoft Azure, I can access it using SQL Server Management Studio, however in my Laravel project when I tried to query I get . I already allowed my IP Address in the Azure Firewall.
PDOException in Connector.php line 47: SQLSTATE[08001]: [Microsoft][SQL Server Native Client 11.0]Named Pipes Provider: Could not open a connection to SQL Server [2].
Here's my config
'azure' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'ybefh1h7kh.database.windows.net,1433'),
'database' => env('DB_DATABASE', 'test'),
'username' => env('DB_USERNAME', 'myuser'),
'password' => env('DB_PASSWORD', 'password'),
'prefix' => '',
],
My query
$users = DB::connection('azure')->select("SELECT * FROM Users");
var_dump($users);exit;

use this
'azure' => [
'driver' => 'sqlsrv',
'host' => 'ybefh1h7kh.database.windows.net,1433',
'database' => 'test',
'username' => 'myuser',
'password' => 'password',
'prefix' => '',
],

Related

GAE Laravel connect to external SQL Server get error

I use Laravel + SQL Server deploy to GAE, when connect to external SQL Server, I get error
could not find driver
but in my local environment it works, so hope someone can help.
.env
DB_CONNECTION=sqlsrv
database.php
'sqlsrv' =>
['driver' => 'sqlsrv',
'host' => 'host',
'port' => '1401',
'database' => 'database',
'username' => 'username',
'password' => '',
'prefix' => '',]
Make sure of the PHP version you use (for me homestead currently using php 7.1, so I installed php7.1-sybase)
sudo apt-get install freetds-common freetds-bin unixodbc php7.1-sybase
Driver:
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'db'),
'port' => env('DB_PORT', 'port'),
'username' => env('DB_USERNAME', 'user'),
'password' => env('DB_PASSWORD', 'pass'),
'charset' => 'utf8',
'prefix' => '',
]

[ODBC Driver 17 for SQL Server]Login timeout expired - Laravel 5.7

I have 2 projects (A and B), the project A using Laravel 5.4 and connecting in a database instance from mssql (this database is to my development environment). The project B use Laravel 5.7 and cannot connect in the same instance, I get this return:
"SQLSTATE[HYT00]: [unixODBC][Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired
But both projects can connect in production without problems. I use the same database confs for project A and B.
I running a docker container and the Dockerfile is the same for both projects. I can't undestand why in one works, but in another no.
I'm using php 7.1
Production Conf:
'database_test' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'sqlsrv.xxxxx.com.br'),
'database' => env('DB_DATABASE', 'database'),
'username' => env('DB_USERNAME', 'user_database'),
'password' => env('DB_PASSWORD', 'user_password'),
'charset' => 'utf8',
'prefix' => '',
'options' => [
],
Development Conf:
'database_test' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', '99.000.000.99\MBC03'),
'database' => env('DB_DATABASE', 'database'),
'username' => env('DB_USERNAME', 'user_database'),
'password' => env('DB_PASSWORD', 'user_password'),
'charset' => 'utf8',
'prefix' => '',
'options' => [
],
],

Laravel 5.6 Unable to connect MS SQL Server 2008 R2 from PHP/Laravel

I am using Laravel 5.6, and trying to connect with MS SQL Server 2008 R2, all running on local machine. I have a test database named "ItemMaster", which is working fine with my C#.Net application.
When I try to connect same database from Laravel, I am getting this error:
SQLSTATE[08001]: [Microsoft][ODBC Driver 11 for SQL Server]TCP Provider: No connection could be made because the target machine actively refused it. (SQL: select * from ITEM_CATEGORY)
Can anybody suggest how to fix this.
In config/database.php, I have setup sqlsrv driver as follows:
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'MY-PC\MYSQL2008R2'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'ITEMMASTER'),
'username' => env('DB_USERNAME', 'myuser'),
'password' => env('DB_PASSWORD', 'mypassword'),
'charset' => 'utf8',
'prefix' => '',
],
Getting error on this line inside my controller:
$items = DB::connection('sqlsrv')->select('select * from ITEM_CATEGORY');
I installed Microsoft Drivers 4.3 for PHP for SQL Server and copied file php_pdo_sqlsrv_71_ts_x86.dll to php/ext folder.
I find solution for this problem, sharing might help someone else making same mistake.
Actually the mistake is how I defined SQL Connection parameters in config/database.php
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'MY-PC\MYSQL2008R2'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'ITEMMASTER'),
'username' => env('DB_USERNAME', 'myuser'),
'password' => env('DB_PASSWORD', 'mypassword'),
'charset' => 'utf8',
'prefix' => '',
],
In this code segment, I actually defined my parameters as second parameter to env() which (be-definition) is not the real value but will be used as default value if that specific key is not found in .env file.
You can read more about Environment configuration here
So the final code segment should be like this:
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => 'MY-PC\MYSQL2008R2',
'port' => '1433',
'database' => 'ITEMMASTER',
'username' => 'myuser',
'password' => 'mypassword',
'charset' => 'utf8',
'prefix' => '',
],
Or another alternative may be to define these parameters in .env file, in the root directory of your application .

How to connect Laravel and Microsoft sql server

Here is my configuration in laravel:
'default' => env('DB_CONNECTION', 'sqlsrv'),
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', '192.168.1.15'),
'database' => env('DB_DATABASE', 'gaspos'),
'username' => env('DB_USERNAME', 'vlado'),
'password' => env('DB_PASSWORD', 'test1'),
'charset' => 'utf8',
'prefix' => '',
],
And when I try:
php artisan migrate, I get:
[PDOException]
SQLSTATE[08001]: [Microsoft][ODBC Driver 11 for SQL Server]Named Pipes Prov
ider: Could not open a connection to SQL Server [2].
Anyone know what is porblem?

Connecting to Sql Server with Laravel

I have just installed Laravel with a fresh install via Composer, and changed Config/database.php to
'default' => 'sqlsrv',
And changed the settings for the connection
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'Host IP'),
'database' => env('DB_DATABASE', 'WebServices'),
'username' => env('DB_USERNAME', 'User'),
'password' => env('DB_PASSWORD', 'Password'),
'prefix' => '',
],
but i am getting this error when trying to connect.
1/1
PDOException in Connector.php line 47:
SQLSTATE[08001]: [Microsoft][ODBC Driver 11 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [2].
in Connector.php line 47
at PDO->__construct('sqlsrv:Server=localhost;Database=homestead', 'homestead', 'secret', array('0', '2', '0', false)) in Connector.php
line 47 at Connector >createConnection('sqlsrv:Server=localhost;Database=homestead', array('driver' => 'sqlsrv', 'host' => 'localhost', 'database' => 'homestead', 'username' => 'homestead', 'password' => 'secret', 'prefix' => '', 'name' => 'sqlsrv'), array('0', '2', '0', false)) in SqlServerConnector.php
line 29 at SqlServerConnector->connect(array('driver' => 'sqlsrv', 'host' => 'localhost', 'database' => 'homestead', 'username' => 'homestead', 'password' => 'secret', 'prefix' => '', 'name' => 'sqlsrv')) in compiled.php
line 10673 at ConnectionFactory->createSingleConnection(array('driver' => 'sqlsrv', 'host' => 'localhost', 'database' => 'homestead', 'username' => 'homestead', 'password' => 'secret', 'prefix' => '', 'name' => 'sqlsrv')) in compiled.php
line 10669 at ConnectionFactory->make(array('driver' => 'sqlsrv', 'host' => 'localhost', 'database' => 'homestead', 'username' => 'homestead', 'password' => 'secret', 'prefix' => ''), 'sqlsrv') in compiled.php line 10587
I have no idea why it is doing this Any help would be much appropriated, it's currntly hosted in WAMP under www/Laravel/
Can you check app/config/local/database.php file if isset. Probably your configuration infos getting from there.
My environment :
Windows Server 2008
SQL Server Express 2014
Xampp win32 with PHP 5.6.23
Laravel 5.2
Cause : PDO connection failed because of the TCP/IP Protocols issue.
SQL Server 2014 Configuration Manager
Go to >> Configuration Tools
SQL Server 2014 Configuration Manager
SQL Server Network Configuration
Protocols for "your_sqlserver_service"
Right click on TCP/IP, select Properties
Choose IP Addresses tab
Scroll down to IPALL
At TCP Port, key in 1433
Ok and restart SQL Server service.
Let's try it. Feel free to correct me if I am wrong.
Also I was caught out with this error when using Xamp with a virtual host. I changed the port from 80 to 8000 but forgot to change the vhosts file to reflect the port change. Just thought I'd add this for any other poor soul.

Categories