How to setup Laravel using a shared host with sqlsrv? - php

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...

Related

Connection of Laravel project ( hosted: 127.15.255.64) To pgsql (hosted: 127.15.255.13)

I am working on a laravel project and we use 2 different servers to host the code( hosted: 127.15.255.64) and the database(hosted: 127.15.255.13).
I modified the .env file as:
DB_CONNECTION=pgsql
DB_HOST=127.15.255.13
DB_PORT=5432
DB_DATABASE=db
DB_USERNAME=postgres
DB_PASSWORD=postgres
Also Modified database.php file as:
'pgsql' => [
'driver' => 'pgsql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.15.255.13'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'db'),
'username' => env('DB_USERNAME', 'postgres'),
'password' => env('DB_PASSWORD', 'postgres'),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
'search_path' => 'public',
'sslmode' => 'prefer',
],
and my servers php.ini file does not have any pgsql extension to uncomment so i tried running the php artisan migrate to check if the db is connected but it just didn't do anything.
I am stuck and dont know what to do next i tried calling "DB::table('public.db')->get()" where 'public' is schema and 'db' is databse name, But it gave me an error which showed could not find driver, can anybody please help me with this situation. i dont know what top do next.

Enable MARS in Laravel

For my Laravel application, I want to use MARS to connect to our SQL server. However, the documentation makes no note of this, nor is there any info on how to pass extra config parameters to the connection.
I'm on Laravel v7.30.
The database works fine apart for the few cases where I need to run several queries on one connection. Right now, those throw a "Protocol error in TDS stream" error. I'm fairly certain this is due to MARS not being activated. If, however, this could have a different reason I'm of course open to suggestions!
My config/database.php entry for SQL Server looks like this:
'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,
'appname' => env('DB_APPNAME', 'MyAppName-' . env('APP_ENV')),
],
Apart from that, the database connection is working fine and queries perform as expected.

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' => '',
]

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 .

Laravel different config/database.php for production and development

I'm developing Laravel app on my local server where I have database and also I'm putting in online in production environment.
Each environment has different db connection information. So far I dealt with this just commenting information when I'm committing like this:
'mysql' => [
'driver' => 'mysql',
'host' => $host,
'database' => $database,
'username' => $username,
'password' => $password,
// 'host' => env('DB_HOST', 'localhost'),
// 'database' => env('DB_DATABASE', 'forge'),
// 'username' => env('DB_USERNAME', 'forge'),
// 'password' => env('DB_PASSWORD', ''),
// 'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
but this is sure wrong approach. How can I set different database connection information for each environment is some better way?
Use the .env file instead.. Have a .env in your production and one in your development..
Take a look at your .env.example
Laravel Environment Configuration Documentation

Categories