How can I connect my Laravel to my SQL SERVER 2014?
TRIED METHODS
first
I tried to use sqlsvr in my .env and change my config > database.php to 'default' => env('DB_CONNECTION', 'sqlsvr').
"my..." replace my actual credentials.
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => 'myServerName',
'port' => '1433',
'database' => 'myDatabaseName',
'username' => 'myUserName',
'password' => 'myPassword',
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
],
But it throws an error of:
Database [sqlsrv] not configured
second
I tried some method using odbc connection find some package on GitHub but no luck, some have errors and some not supported by Laravel 5+
OTHER INFO
I'm using Larave 6.0.1 and SQL Server 2014.
Any help will do.
Related
On a Linux/Ubuntu box, I'm trying to connect to an external SQL Server database, but I only get errors. I know that the hostname, db name and credentials are correct, and I can't change them.
$odbc="odbc:Driver={SQL Server};Server=$server;Database=$database;";
$db = new PDO($odbc, $user, $password);
This just gives me could not find driver. I have tried every tutorial I could find, and installed tons of packages, and restarted nginx over and over again. I don't know what to do next.
You need to install drivers for php: https://learn.microsoft.com/en-us/sql/connect/php/download-drivers-php-sql-server?view=sql-server-2017
Then update config/database.php to use the sqlserv connection and driver:
'default' => env('DB_CONNECTION', 'sqlsrv'),
'connections' => [
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'default_database'),
'username' => env('DB_USERNAME', 'default_username'),
'password' => env('DB_PASSWORD', ''),
'prefix' => '',
],
//[...]
],
After you've done that, you can use php artisan tinker to confirm the driver is available:
>>> DB::availableDrivers()
=> [
0 => "mysql",
2 => "sqlite",
]
and test your connection:
>>> DB::connection()
=> Illuminate\Database\MySqlConnection {#161}
(Mine doesn't show sqlserv because I don't use that driver)
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' => [
],
],
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 .
Hi I am using Laravel 5 with homestead. I am experiencing homestead first time so facing problem. I have created a database 'myDb' and imported data into this. I have installed vagrant and homestead, cloned my code and configured homestead.yaml file accordingly. I have set up my .env. When I try to run my project is is giving me unknown database myDb error. I can see my database in my phpmyadmin but I do not know why it is giving me this error. My .env file looks like
DB_HOST=localhost
DB_DATABASE=myDb
DB_USERNAME=homestead
DB_PASSWORD=secret
And I have same settings in my config/database.php too.
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'myDb'),
'username' => env('DB_USERNAME', 'homestead'),
'password' => env('DB_PASSWORD', 'secret'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
]
Its really making me mad. What Am I doing wrong? I am getting following
in Connector.php line 55
at PDO->__construct('mysql:host=localhost;dbname=myDb', 'homestead', 'secret', array('0', '2', '0', false, '0')) in Connector.php line 55
at Connector->createConnection('mysql:host=localhost;dbname=myDb', array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'myDb', 'username' => 'homestead', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name' => 'mysql'), array('0', '2', '0', false, '0')) in MySqlConnector.php line 22
at MySqlConnector->connect(array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'myDb', 'username' => 'homestead', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name' => 'mysql')) in ConnectionFactory.php line 60
at ConnectionFactory->createSingleConnection(array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'myDb', 'username' => 'homestead', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name' => 'mysql')) in ConnectionFactory.php line 49
One common mistake that you might be making is that you're probably accessing MySQL server on your localhost while you should remember that Homestead is a complete virtual dev. environment. It has it's own Web Server, MySQL server and etc.
To see if you have "myDB" on the Homestead MySQL server as well, try accessing homestead via the following command.
homestead ssh
Once done that, try opening the mysql console via the mysql command.
mysql -u username -p
And after that list all your databases using
show databases
If you can't see "myDb" database there, than you should try connecting to Homestead MySQL server using Navicat or MySQL Workbench and move your database from your local MySQL server to the Homestead MySQL server.
That would probably fix your problem.
If you are migrating your database, check if you are migrating the same database name inside your Homestead.yaml file and the database name in your .env file.
Inside Homestead.yaml file:
databases:
- laravel
Inside your .env file:
DB_DATABASE=laravel
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' => '',
],