According to documentation to specify connection to use when migrating DB is either
protected $connection = "mysql_shared";
or
Schema::connection("mysql_shared")->create(...);
I have defined a connection in database.php just like this:
"mysql_shared" =>[
'driver' => 'mysql',
'host' => "192.168.0.100",
'port' => "3306",
'database' => "db",
'username' => "user",
'password' => "password",
'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'),
]) : [],
],
So far according to docs schema upon migration should choose the connection and migrate. But there is a big BUT. it simply doesn't work unless I go to env file and add
DB_CONNECTION=mysql_shared
according to logs and common sense it tries to use the default connection due to 'default' => env('DB_CONNECTION', 'mysql'),
Connection refused (SQL: select * from information_schema.tables where table_schema = forge and table_name = migrations and table_type = 'BASE TABLE')
My Answer:
To specify the connection to use for migrations in Laravel, you can do the following:
1. In your migration file, you can specify the connection to use by adding the following line at the beginning of the file:
php
protected $connection = "mysql_shared";
2. Alternatively, you can specify the connection when creating a table using the Schema facade like this:
php
Schema::connection("mysql_shared")->create('table_name', function (Blueprint $table) {
// Define your table schema here
});
Make sure that you have defined the "mysql_shared" connection in your `config/database.php` file as you have shown in your example.
If you are still facing issues with the connection not being used for migrations, make sure that you have cleared the cache by running `php artisan config:clear` and `php artisan cache:clear`. Also, check your `.env` file to ensure that the `DB_CONNECTION` is set to `mysql_shared`.
By following these steps, you should be able to specify the connection to use for migrations in Laravel.
Rate this post
3 of 5 based on 4561 votesComments