I am trying to establish connection with SQL Server local instance in Laravel. The SQL Server has Windows Authentication.
'driver' => 'sqlsrv',
'host' => 'SKILL....\SQLEXPRESS',
'port' => '1433',
'database' => 'dbname',
'username' => 'SKILL....\101...',
'password' => 'W.....',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'prefix_indexes' => true,
Unable to establish connection.
Related
Currently i use the package cooperl/laravel-db2 for the database connection. For my project, i should to toggle schema database.
I try this in db2.php :
'connections' => [
'ibmi_1' => [
...
'host' => '***********',
'username' => '***********',
'password' => '***********',
'database' => 'database',
'prefix' => '',
'schema' => 'schema_1',
'port' => *****,
'date_format' => 'Y-m-d H:i:s',
...
],
'ibmi_2' => [
...
'host' => '***********',
'username' => '***********',
'password' => '***********',
'database' => 'database',
'prefix' => '',
'schema' => 'schema_2',
'port' => *****,
...
],
And this in .env
DB_CONNECTION=ibmi_1
DB_HOST=
DB_PORT=
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=
DB_CONNECTION=ibmi_2
DB_HOST=
DB_PORT=
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=
But when i try in tinker
DB::connection('ibmi_2')
I have this error
InvalidArgumentException with message 'Database connection [ibmi_2] not configured.'
What is wrong ?
The problem was that the database configuration wasn't in bootstrap/cache/config.php.
when trying to run php artisan migrate on laravel i'm getting the error
In database.php line 79:
Undefined class constant 'SQLSRV_ENCODING_SYSTEM'
i have the sql drivers etc installed but unsure how to solve this problem
database.php file
'sqlsrv' => [
'driver' => 'sqlsrv',
'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' => '',
'options' => [PDO::SQLSRV_ENCODING_SYSTEM => false], // Used for MSSQL encoding
],
Explanations:
PDO::SQLSRV_ENCODING_SYSTEM is PDO_SQLSRV Driver Constant, not an option name. You should use PDO::SQLSRV_ATTR_ENCODING => PDO::SQLSRV_ENCODING_SYSTEM in options. Have in mind, that this option is Microsoft Drivers for PHP for SQL Server specific driver attribute (one of PDO::SQLSRV_ATTR_ENCODING and PDO::SQLSRV_ATTR_DIRECT_QUERY).
<?php
...
'sqlsrv' => [
'driver' => 'sqlsrv',
'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' => '',
'options' => [PDO::SQLSRV_ATTR_ENCODING => PDO::SQLSRV_ENCODING_SYSTEM]
],
...
?>
Notes:
Documentation: Microsoft SQL Server Functions (PDO_SQLSRV) and Constants (Microsoft Drivers for PHP for SQL Server).
In my package, I've own config file and database connection file. Merging config with the app config is quit simple, publishing data migration files too. But how can I merge the database connection file in my Service Provider with the app /config/database.php?
/packages/el/adm4y/src/config/database.php
return [
'connections' => [
'adm4y' => [
'driver' => 'mysql',
'host' => env('DB_ADM4Y_HOST', 'localhost'),
'database' => env('DB_ADM4Y_DATABASE', ''),
'username' => env('DB_ADM4Y_USERNAME', ''),
'password' => env('DB_ADM4Y_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_general_ci',
'prefix' => '',
'strict' => false,
]
]
];
I wanted to ask, if is possible to work with 2 databases in cakephp3, using raw sql ?
I have query like this:
select * from shop.brochures b, upload.documents up where b.doc_id = up.id;
The problem is, this are 2 databases.
I don't know, how setup connection, i think that will not work ( doc example )
$conn = ConnectionManager::get('default');
Thank You for any advice.
in /config/app.php you can set as many db as you want; i have 2.
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'sisarticulos',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
],
'gente' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'sggeneral',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,ยด
then in the controller you can set
$conn = ConnectionManager::get('default');
//some code
$conn = ConnectionManager::get('get');
use default first and then use gente
My MySQL connection details are different for both my local connection and my deployed live hosted. I am using CakePHP 3
At the moment I have to keep changing the default datasource which is not really the best way to do it.
I have not added two datasources but I am not sure how to switch between them?
'Datasources' => [
'development' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => '127.0.0.1',
'port' => '8889',
'username' => 'root',
'password' => 'root',
'database' => 'local',
],
'deployment' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
'username' => 'username',
'password' => 'password',
'database' => 'live_database',
],
In boostrap or in App Controller, paste this
if(Configure::read('debug')){
ConnectionManager::config('deployment');
}
this change the default config of database when the debug is true.