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)
Related
I have updated a Laravel 5.5-installation to 5.8 and am now attempting to update the MySQL-installation from 5.7 to 8.0.19. The Laravel-installation is on an EC2-instance and works great with the previous RDS MySQL/Aurora installation, but the version did not cut it.
We are running this on an nginx-server.
First I got an error-message stating
(2/2) PDOException
SQLSTATE[HY000] [2002] Connection refused
Tried to change the AWS uri and use the ip instead, and got a 504 instead.
I tried to access the database through MySQL Workbench and through new PDO('mysql:host=xx-rds-staging-mysql-8.xxxxxxxxxxxx.eu-west-1.rds.amazonaws.com;port=3306;dbname=database', 'username', 'password', [0, 2, 0, false, false, false]) and... they both work perfectly from the EC2-instance.
Env-file:
DB_CONNECTION="mysql"
DB_HOST_WRITE="xx-rds-staging-mysql-8.xxxxxxxxxxxx.eu-west-1.rds.amazonaws.com"
DB_HOST_READ="xx-rds-staging-mysql-8.xxxxxxxxxxxx.eu-west-1.rds.amazonaws.com"
DB_PORT_READ_AND_WRITE="3306"
DB_DATABASE="database"
DB_USERNAME="username"
DB_PASSWORD="password"
config/database.php
'mysql' => [
'read' => [
'host' => env('DB_HOST_READ', '127.0.0.1'),
],
'write' => [
'host' => env('DB_HOST_WRITE', '127.0.0.1'),
],
'port' => env('DB_PORT_READ_AND_WRITE', '3306'),
'driver' => 'mysql',
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
'sticky' => true,
'version' => 8,
'modes' => [
#'ONLY_FULL_GROUP_BY',
'STRICT_TRANS_TABLES',
'NO_ZERO_IN_DATE',
'NO_ZERO_DATE',
'ERROR_FOR_DIVISION_BY_ZERO',
'NO_ENGINE_SUBSTITUTION',
],
'options' => [
PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false,
],
],
I have cleared the cache by all the artisan-commands and by deleting the cache-folder.
I am rubbish at asking for help here, not ever certain what information I need to share to make my case.
My first thought is that there ought to be some setting in Laravel which I could swap to make this work, but I have no idea what it could be.
The mysql-server does not run on the same machine as the laravel-application and AWS has not offered a public ip, just the uri.
What makes it even stranger is that I can enter php artisan tinker and run, for example, User::first(); and it will fetch the appropriate row from the database, without any issue.
No details informaton but you can review below points:
Verify your Laravel/php config or connection file didn't change and have same settings as before.
Did you set any zone specific resstriction in RDS? Does your testing environment complies it?
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.
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 have a project developed in Laravel and I want to upload the project to a Linux shared hosting.
In my project I have 2 connection to different database. First is mysql database that is in shared hosting and works well. Second is an sql server database that access to another server. So for that second database I use sqlsrv driver but I having problems to put this working at linux shared hosting, returns me this error:
SQLSTATE[HY000] Unable to connect: Adaptive Server is unavailable or does not exist (severity 9)
at PDO->__construct('dblib:host=IP:1433;dbname=DBNAME;charset=utf8', 'USER', 'PASSWORD', array(0, 2, 0, false))
I set the connection like this:
'DB2' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'IP'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'DBNAME'),
'username' => env('DB_USERNAME', 'USER'),
'password' => env('DB_PASSWORD', 'PASSWORD'),
'charset' => 'utf8',
'prefix' => '',
],
Before this error I have a lot of another errors because this connection, so I have to add some extensions to php and update to 7.2 version. I also add some PHP pear packages .
The next image show you the extensions that I add:
In my local machine I can run the project without any problem and both connection working fine.
How can I solve that?
You should define DB_HOST and DB_PORT environment variables inside .env file for sqlsrv driver. Currently, it looks like the default values are used for the connection. (Default value - is the second parameter of env() function).
P.S. Please note, if you use two different connection - variable names should be different, for example:
.env content:
...
MYSQL_DB_HOST=0.0.0.0
MYSQL_DB_PORT=3939
SQLSRV_DB_HOST=0.0.0.0
SQLSRV_DB_PORT=1433
...
config/database.php content:
...
'DB' => [
'driver' => 'mysql',
'host' => env('MYSQL_DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3939'),
'database' => env('DB_DATABASE', 'DBNAME'),
'username' => env('DB_USERNAME', 'USER'),
'password' => env('DB_PASSWORD', 'PASSWORD'),
'charset' => 'utf8',
'prefix' => '',
],
'DB2' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'DBNAME'),
'username' => env('DB_USERNAME', 'USER'),
'password' => env('DB_PASSWORD', 'PASSWORD'),
'charset' => 'utf8',
'prefix' => '',
],
...
I think first of all check your hosting server php version and try to put correct PDO driver for PHP version.
if you use a PHP Version 7.1.7 for *86 Architecture then put that .dll file.
extension=php_pdo_sqlsrv_71_ts_*86.dll
and if you use another Version of PHP then try to put a correct .dll file and also declare into php.ini file.
and restart your service and run it...
I hope it would help you...
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 .