Connecting SQL Server to laravel as a second database - sql-server

I have laravel 5.8 setup and working with MySQL. I need to connect a second database (SQL Server).
I have the following in my .env file
DB_EXT_CONNECTION=sqlsrv
DB_EXT_HOST=0.0.0.0
DB_EXT_PORT=1433
DB_EXT_DATABASE=database
DB_EXT_USERNAME=user
DB_EXT_PASSWORD=password
database.php
'sqlsrv' => [
'driver' => 'sqlsrv',
'url' => env(''),
'host' => env('DB_HOST', '0.0.0.0'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'database'),
'username' => env('DB_USERNAME', 'user'),
'password' => env('DB_PASSWORD', 'password'),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
],
I am running xampp and have the following dll's installed
extension=php_pdo_sqlsrv_73_ts.dll
extension=php_sqlsrv_73_ts.dll
I keep getting
SQLSTATE[HY000]: [Microsoft][ODBC Driver 11 for SQL Server]Protocol error in TDS stream (SQL:select * from [dbo].[t_people]) (View: C:\xampp\htdocs
I have
$users = DB::connection('sqlsrv')->table('dbo.t_people')->select('*')->get();
in my blade template.

Change all the DB_DATABASE from your config file to DB_EXT_DATABASE, as you wrote in your .env file.
Try instead of
$users = DB::connection('sqlsrv')->table('dbo.t_people')->select('*')->get();
do the
$users = DB::connection('sqlsrv')->table('t_people')->select('*')->get();
And don't do it in your blade files, do it in controller.

Related

Laravel - Sqlsrv Remote Connection

I am trying to connect to the sql server database on the remote server with Laravel, but I could not do it, I made all the necessary settings as follows:
php.ini settings
extension=php_pdo_sqlsrv_81_ts_x64
extension=php_sqlsrv_81_ts_x64
database.php settings
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'IP ADRESS'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'DATABASE NAME'),
'username' => env('DB_USERNAME', 'UID'),
'password' => env('DB_PASSWORD', 'UPW'),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
// 'encrypt' => env('DB_ENCRYPT', 'yes'),
// 'trust_server_certificate' => env('DB_TRUST_SERVER_CERTIFICATE', 'false'),
The error I get is like this
PDOException::("SQLSTATE[IMSSP]: This extension requires the Microsoft ODBC Driver for SQL Server to communicate with SQL Server. Access the following URL to download the ODBC Driver for SQL Server for x64:
You need to install an ODBC from the official page:
https://learn.microsoft.com/en-us/sql/connect/odbc/download-odbc-driver-for-sql-server?view=sql-server-ver16

How do I connect Laravel 7 to Microsoft SQL Server?

I'm really surprised the Laravel documentation is limited for explaining how to connect to MS SQL Server. There are two areas that I think need configuration -- the .env file and the database.php. Most examples on the web don't show the same format compared to Laravel 7. I would like to know where I can find the correct info for the following.
How to tell what port my DB is on
What to enter for URL
What to enter for HOST
When testing the connection it says 'driver not found' - is it saying the details are incorrect or is it looking for a missing file?
before changing the database.php file it has the word forge as the second argument for the env() function - what is forge???
If anyone can point me to a good resource id be in your debt!
DB_CONNECTION=sqlsrv
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=test_db_1
DB_USERNAME=sa
DB_PASSWORD=mypassword
```
and the database.php...
'default' => env('DB_CONNECTION', 'sqlsrv'), ...
...
'sqlsrv' => [
'driver' => 'sqlsrv',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'test_db_1'),
'username' => env('DB_USERNAME', 'sa'),
'password' => env('DB_PASSWORD', 'mypassword'),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
],
Your DB_HOST will always be localhost, so if the database is on the same machine, 127.0.0.1 will be fine. TCP port 1433 is typically the port used by a default instance of SQL Server, set it to DB_PORT=1433.
Laravel Forge is a tool for deploying and configuring web applications and can be used to automate the deployment of any web application that uses a PHP server. Since we are using the .env to overwrite the values in config/database.php there is no need to worry about the "forge" values. Finally, you may need to install drivers on the machine to support PHP and SQL Server.
.env
DB_CONNECTION=sqlsrv
DB_HOST=127.0.0.1
DB_PORT=1433
DB_DATABASE=test_db_1
DB_USERNAME=sa
DB_PASSWORD=mypassword
config/database.php
'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,
]

How to add SSL to the backend mysql database and connect using Laravel

I have a requirement to add SSL to the backend mysql database and connect using Laravel. But unable to know the way to do it.
Do I need to have MySql Db configured to use encrypted connection and then provide some parameters in Database.php file in Laravel.
My site has https enabled.
Please guide what should be taken care and to be done.
What you will need to is first configure SSL/TLS on your MySQL Server and the instructions will be specific to the OS hosting the MySQL Server as well as the MySQL Version itself.
Here's an example guide for MySQL 5.x and Ubuntu:
https://www.digitalocean.com/community/tutorials/how-to-configure-ssl-tls-for-mysql-on-ubuntu-16-04
Once you've done this, you can configure your laravel app to tell it how to communicate over encrypted mysql connection by adding this line to your db config:
'options' => array(
PDO::MYSQL_ATTR_SSL_CA => $cert_base . '/ssl-ca-bundle.pem'
),
the full config will look something like this in config/database.php
'mysql' => array(
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'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' => true,
'engine' => null,
'options' => array(
PDO::MYSQL_ATTR_SSL_CA => '/path/to/ssl-ca-bundle.pem'
),
),

Laravel Database [postgres] not configured

Can anyone help me to fix this problem please
the error is database [postgres] not configured.
here is my .env file :
DB_CONNECTION=postgres
DB_HOST=localhost
DB_PORT=5432
DB_DATABASE=referentiel
DB_USERNAME=postgres
DB_PASSWORD=postgres
and here is my database.php:
'default' => 'postgres',
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => 'referentiel',
'username' => env('postgres', ''),
'password' => env('postgres', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
],
Change the default parameter in your config to match the name of your postgres connection - pgsql in your case:
'default' => 'pgsql',
And put correct database parameters in your .env file.
Try to edit your .env
From: DB_CONNECTION=postgres
To: DB_CONNECTION=pgsql
i think the problem in php.ini if you use xampp open app xampp > config > php.ini
find ;extension=php_pdo_pgsql.dll
;extension=php_pdo_sqlite.dll
delete ;
change extension=php_pdo_pgsql.dll
extension=php_pdo_sqlite.dll

NO_AUTO_CREATE_USER error while installing drupal-7

When I trying to install drupal-7 in my server, at the setup database step it shows the error
Failed to connect to your database server. The server reports the following message: SQLSTATE[42000]: Syntax error or access violation: 1231 Variable 'sql_mode' can't be set to the value of 'NO_AUTO_CREATE_USER'.
The database name, username, and password are all correct.
Is there any solution to this problem?
How have I fixed (Syntax error or access violation: 1231 Variable 'sql_mode' can't be set to the value of 'NO_AUTO_CREATE_USER') problem in laravel
SO go to config/database.php
in mysql connection add the following
code
'modes' => [
'ONLY_FULL_GROUP_BY',
'STRICT_TRANS_TABLES',
'NO_ZERO_IN_DATE',
'NO_ZERO_DATE',
'ERROR_FOR_DIVISION_BY_ZERO',
'NO_ENGINE_SUBSTITUTION',
],
The whole connection will be something like the following
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'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' => true,
'engine' => null,
'modes' => [
'ONLY_FULL_GROUP_BY',
'STRICT_TRANS_TABLES',
'NO_ZERO_IN_DATE',
'NO_ZERO_DATE',
'ERROR_FOR_DIVISION_BY_ZERO',
'NO_ENGINE_SUBSTITUTION',
],
],
Drupal isn't allowed to create a new user in your MySQL database. You need at least MySQL 5.0.15

Resources