I have three databases for my project. When I try Laravel migration with PostgreSQL. I am getting some error. But Laravel migration with other mysql databases is okay. I am using XAMPP. Could you please check and correct me?
Error
[PDOException]
could not find driver
Database
<?php
return [
'fetch' => PDO::FETCH_CLASS,
'default' => env('DB_CONNECTION', 'mysql_shop'),
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => database_path('database.sqlite'),
'prefix' => '',
],
'mysql_cms' => [
'driver' => 'mysql',
'host' => env('DB_CONTAO_HOST', 'localhost'),
'database' => env('DB_CONTAO_DATABASE', 'forge'),
'username' => env('DB_CONTAO_USERNAME', 'forge'),
'password' => env('DB_CONTAO_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'mysql_shop' => [
'driver' => 'mysql',
'host' => env('DB_SHOP_HOST', 'localhost'),
'database' => env('DB_SHOP_DATABASE', 'forge'),
'username' => env('DB_SHOP_USERNAME', 'forge'),
'password' => env('DB_SHOP_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_PGSQL_HOST', 'localhost'),
'database' => env('DB_PGSQL_DATABASE', 'maindb'),
'username' => env('DB_PGSQL_USERNAME', 'postgres'),
'password' => env('DB_PGSQL_PASSWORD', 'postgres'),
'charset' => 'utf8',
'prefix' => '',
'schema' => env('DB_PGSQL_SCHEMA', 'fes'),
],
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
],
],
Migration Code
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePasswordResetsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::connection('pgsql')->create('fes.password_resets', function (Blueprint $table) {
$table->string('email')->index();
$table->string('token')->index();
$table->timestamp('created_at');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::connection('pgsql')->drop('fes.password_resets');
}
}
PHP INFO
Related
I have this migration file:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::connection('DB1')->create('feed_mappings', function (Blueprint $table) {
$table->bigIncrements('id_feed')->autoIncrement();
$table->string('id_project', 255);
$table->string('import_field_slug', 155);
$table->string('internal_field_slug', 155);
$table->tinyInteger('import');
$table->tinyInteger('custom_field')->default(0);
$table->dateTime('date_add');
$table->dateTime('date_upd');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('feed_mappings');
}
};
And I have this config in the database.php file:
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'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' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => 'InnoDB',
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'DB1' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '8889'),
'database' => env('DB_DATABASE', 'DB1'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'root'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => 'InnoDB',
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
Now, I have run this command :
php artisan migrate:refresh --seed
Now, All the migration file is creating a tables on default database but I want this feed_mappings should go to DB1 database.
BUT this feed_mapping is not going to DB1 database. Can you tell me why?
So the issue is in your database.php file where you are using the same variables for connections 1 and 2
'mysql' => [
'driver' => 'mysql',
***'url' => env('DATABASE_URL'),
'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' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => 'InnoDB',
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'DB1' => [
'driver' => 'mysql',
**'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '8889'),
'database' => env('DB_DATABASE', 'DB1'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'root'),**
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => 'InnoDB',
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
what you need to do is create separate variables for db connections e.g:
DB_HOST // for connection 1
DB_HOST_2 // for connection 2
and use these in your config files for more info visit: https://codelapan.com/post/how-to-use-multiple-database-connections-in-laravel-8
config/database.php
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'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' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'mysql2' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST_2', '127.0.0.1'),
'port' => env('DB_PORT_2', '3306'),
'database' => env('DB_DATABASE_2', 'forge'),
'username' => env('DB_USERNAME_2', 'forge'),
'password' => env('DB_PASSWORD_2', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
Setup .env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_multiple_database1
DB_USERNAME=root
DB_PASSWORD=
DB_CONNECTION_2=mysql
DB_HOST_2=127.0.0.1
DB_PORT_2=3306
DB_DATABASE_2=laravel_multiple_database2
DB_USERNAME_2=root
DB_PASSWORD_2=
in Model example Post
protected $connection = 'mysql2';
in migration file
public function up()
{
Schema::connection('mysql2')->create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->timestamps();
});
}
public function down()
{
Schema::connection('mysql2')->dropIfExists('posts');
}
This question already has answers here:
Laravel: connect to databases dynamically
(8 answers)
Closed 3 years ago.
I would like to get databases informations stored in my database and add it in the database.php.
I tried to get all infos in the database but I always have an error like this :
[2019-04-18 20:22:20] laravel.ERROR: Call to a member function
connection() on null {"exception":"[object]
(Symfony\Component\Debug\Exception\FatalThrowableError(code: 0):
Call to a member function connection() on null at
/var/www/vhosts/xxxx.net/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1249)
I added this on the top of database.php :
<?php
use App\Campaign;
$database_infos = Campaign::all();
$campains_db = array();
foreach ($database_infos as $info) {
if (isset($info->db_name)) {
$campains_db[$info->keyword] = array(
'driver' => 'mysql',
'host' => $info->db_host,
'database' => $info->db_name,
'username' => $info->db_user,
'password' => decrypt($info->db_password),
'prefix' => $info->db_prefix,
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'strict' => false,
'engine' => null,
);
}
}
return [
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
$campains_db,
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
],
'wordpress' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'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' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
'schema' => 'public',
'sslmode' => 'prefer',
],
'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' => '',
'prefix_indexes' => true,
],
],
'migrations' => 'migrations',
'redis' => [
'client' => env('REDIS_CLIENT', 'predis'),
'options' => [
'cluster' => env('REDIS_CLUSTER', 'predis'),
],
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DB', 0),
],
'cache' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_CACHE_DB', 1),
],
],
];
I found this on an other post :
The simplest solution is to set your database config at runtime.
Laravel might expect these settings to be loaded from the
config/database.php file, but that doesn't mean you can't set or
change them later on.
The config loaded from config/database.php is stored as database in
Laravel config. Meaning, the connections array inside
config/database.php is stored at database.connections.
Config::set("database.connections.mysql", [
"host" => "...",
"database" => "...",
"username" => "...",
"password" => "..."
]);
Driver [app.database] not supported. Laravel 5.1
I have cloned a repository of a project of the company, and when trying to lift the project I throw myself the following error, already reviewed the connection parameters and all this well,
The error is with W7 and Laravel 5.1.
config.databases.php
return [
'fetch' => PDO::FETCH_CLASS,
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => storage_path('database.sqlite'),
'prefix' => '',
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => '4242',
'database' => env('DB_DATABASE', '***'),
'username' => env('DB_USERNAME', '***'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'options' => [
\PDO::ATTR_EMULATE_PREPARES => true
]
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
],
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
],
],
'migrations' => 'migrations',
'redis' => [
'cluster' => false,
'default' => [
'host' => '127.0.0.1',
'port' => 6379,
'database' => 0,
],
],
];
I have created multi-tenant application, still now that working as expected.
Here is my sample code:
middleware :
public function handle($request, Closure $next)
{
$user = Auth::user();
if (! is_null($user)) {
Config::set('database.connections.tenant.host', $user->hostname);
Config::set('database.connections.tenant.database', $user->database);
Config::set('database.connections.tenant.username', $user->username);
Config::set('database.connections.tenant.password', $user->password);
}
return $next($request);
}
database.php :
'connections' => [
'main' => [
'driver' => env('DB_DRIVER', 'mysql'),
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', '123'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'tenant' => [
'driver' => env('DB_DRIVER', 'mysql'),
'host' => '',
'port' => env('DB_PORT', '3306'),
'database' => '',
'username' => '',
'password' => '',
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
],
My doubt is : Above code is is possible to collapse DB connection, When multiple user access same time ? (each user has different DB)
for example :user A has school database and user B has college database
user A is try to logging [loading the page] that situation user B is login.
user B has no problem but user A -- get student details from college database or student database?
We've using Lumen for building API's , Now we need to access multiple databases.
Currently using .env for database config but unable to found the way to multiple databases in .env
where we need to read 2nd connection ...
First, you'll need to configure your connections. If you don't already have one you'll need to create a config directory in your project and add the file config/database.php. It might look like this:
<?php
return [
'default' => 'accounts',
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST'),
'port' => env('DB_PORT'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'mysql2' => [
'driver' => 'mysql',
'host' => env('DB2_HOST'),
'port' => env('DB_PORT'),
'database' => env('DB2_DATABASE'),
'username' => env('DB2_USERNAME'),
'password' => env('DB2_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
],
];
Once you've added your connection configurations, you can access them by getting the database manager object out of the container and calling ->connection('connection_name').
// Use default connection
app('db')->connection()->select('xx');
DB::connection()->select('yy');
// Use mysql2 connection
app('db')->connection('mysql2')->select('xx');
DB::connection('mysql2')->select('yy');
Hope this helps you!!
This also worked. In the current version of Lumen 5.7
config/database.php
<?php
return [
'default' => env('DB_CONNECTION', 'sqlsrv'),
'migrations' => 'migrations',
'connections' => [
'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' => '',
'prefix_indexes' => true,
],
'sqlsrv2' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE2', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
],
],
];
.env
DB_CONNECTION=sqlsrv
DB_HOST=localhost
DB_PORT=1433
DB_DATABASE=database1
DB_USERNAME=username
DB_PASSWORD=password
DB_DATABASE2=database2
Usage:
Model: protected $connection = 'sqlsrv2';
Other: ->connection('sqlsrv2')
I hope i help you!
Reference:https://fideloper.com/laravel-multiple-database-connections