I want to seed some data to my database. So far, it all works well with the "normal" database.
Now i want to seed the same data (the same seeder) to another database running. So i try to use the --database= command
php artisan db:seed --database=cb4test --class=TestDataTableSeeder -v
But this will seed the data into the "normal" database, not in the one specified in cb4test. What am i doing wrong?
More info:
This a part of my config/database.php:
'connections' => [
'couchbase' => [
'driver' => 'couchbase',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', 8091),
'bucket' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'n1ql_hosts' => [
'http://'.env('DB_HOST', 'localhost').':8093'
]
],
'cb4test' => [
'driver' => 'couchbase',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', 8091),
'bucket' => env('DB_TEST', 'msg4test'),
'username' => env('DB_USERNAME'),
'n1ql_hosts' => [
'http://'.env('DB_HOST', 'localhost').':8093'
]
],
In my .env i do have an entry for DB_TEST pointing to the correct couchbase-bucket (in mysql that would be a mysql-table).
Still, this is not working.
I made a debug line in my TestDataTableSeeder.php that gives my the current used DB-Table and this is the one used in the default-connection (named couchbase), not the one used in connection 'cb4test'.
Using Laravel 5.4
Its common misconception, --database specifies the database connection not the name.
So in your config\database.php file you need to add up a connection which uses this different database like:
'mysql2' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE2', 'forge'),
'username' => env('DB_USERNAME2', 'forge'),
'password' => env('DB_PASSWORD2', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => 'InnoDB',
],
Then in your .env, define three variables for your other db:
DB_DATABASE2 = cb4test
DB_USERNAME2 = //username for cb4test
DB_PASSWORD2 = //password for cb4test
Then use:
php artisan db:seed --database=mysql2 --class=TestDataTableSeeder -v
OR
Better way of changing database is to change it in seeder itself.
In your: TestDataTableSeeder.php
use Illuminate\Support\Facades\DB;//at top
//then inside function:
DB::disconnect('mysql');
config(['database.connections.mysql.database' => env('DB_DATABASE2','')]);
config(['database.connections.mysql.username' => env('DB_USERNAME','')]);
config(['database.connections.mysql.password' => env('DB_PASSWORD2','')]);
Of course you will need those three variables of .env file intact.
I hope it helps
So, we figured out that the source of this problem was related to a bug in the couchbase driver.
Bug fixed, all works well.
Thanks for all the answers trying to help me out :D
Related
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.
I've installed pgsql and set up my Laravel config/database.php as follows:
'connection_name' => [
'driver' => 'pgsql',
'host' => env('DB_HOST_2', '127.0.0.1'),
'port' => env('DB_PORT_2', '3306'),
'database' => env('DB_DATABASE_2'),
'username' => env('DB_USERNAME_2'),
'password' => env('DB_PASSWORD_2', ''),
'sslmode' => env('DB_SSLMODE_2'),
'charset' => 'utf8',
'prefix' => '',
'options' => [
'sslcert' => '/some/special/path.crt',
],
'schema' => 'public',
],
'connection_name' is located in the 'connections' section of database.php.
In artisan tinker I get the following error because it's still looking in the default location for the SSL certificate. I cannot get it to look in the right location for the cert.
>>> use Illuminate\Support\Facades\DB;
>>> DB::connection('connection_name')->getPdo()
Doctrine\DBAL\Driver\PDOException with message 'SQLSTATE[08006] [7] root certificate file "/home/wes/.postgresql/root.crt" does not exist
Either provide the file or change sslmode to disable server certificate verification.'
The root cert is set by "sslrootcert". This is the certificate that signed the server's cert.
"sslcert" sets the client cert. That is something different.
This is a pretty old question, but I ran into this issue today. My solution was a combination of jjanes answer and comment. I had to set the variable at a higher level and also change to "sslrootcert"
'pgsql' => [
'driver' => 'pgsql',
'url' => env('DATABASE_URL'),
'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,
'search_path' => 'public',
'sslmode' => env('SSL_MODE'), <-- this says "verify-ca"
'sslrootcert' => env('PGSQL_SSL_CA') <-- this is fullpath
],
This worked when I used php artisan migrate on my PGSQL managed database on Digital Ocean.
I am planning to use MongoDb along with my Laravel project. I am using the jensseger/mongodb extension for it.
I have configured everything. I am getting an error saying mongodb not configured.
This is how my database.php file looks like:
'connections' => [
# Our primary database connection
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Double check your configuration in config/database.php
Also make sure that you fire php artisan config:cache after doing the changes.
Be sure to have DB_CONNECTION=mongodb set in your .env file or just replace 'default' => env('DB_CONNECTION', 'mysql') with 'default' => 'mongodb'. I would go with the former, as it's safer to rely only on the .env, rather than hardcoding your default database connection.
'mongodb' => [
'driver' => 'mongodb',
'host' => env('MONGO_DB_HOST', 'localhost'),
'port' => env('MONGO_DB_PORT', 27017),
'database' => env('MONGO_DB_DATABASE'),
'username' => env('MONGO_DB_USERNAME'),
'password' => env('MONGO_DB_PASSWORD'),
'options' => ['database' => 'Database_Name']
],
After clear cache "php artisan config:cache"
go to .env
Add these lines:
MONGO_DB_HOST=127.0.0.1
MONGO_DB_PORT=27017
MONGO_DB_DATABASE=your database name
MONGO_DB_USERNAME=your database username if you have
MONGO_DB_PASSWORD=your database password if you have
I need to make schema of postgres dynamic in laravel. The database config is below
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'stoddart'),
'username' => env('DB_USERNAME', 'postgres'),
'password' => env('DB_PASSWORD', '123456'),
'charset' => 'utf8',
'port' => env('DB_PORT', '7373'),
'prefix' => '',
'schema' => 'cp_bn',
],
Here you can see i have specified schema, but i want it to be Dynamic. The schema name will come from client side.
I am currently doing like following
$connection =DB::connection()->getPdo();
$connection->prepare("Set search_path to {$schema}")->execute();
But in this case i have to execute this whenever the connection to the database is made. I need to save it globally for all connection once it is set.
I am working on building a system using a main database and multiple tenant databases in laravel 5. I have a database migration and seed for the main database no problem, using php artisan migrate:refresh --seed.
Tenants then get their own database upon registration. I need to run a migration and seed on the tenants database.
The tenant migration files are stored in a separate folder. The migration runs (unfortunately on the main database) with the following command
\Artisan::call('migrate', [
'--path' => "database/migrations_system"
);
However I need the migration to occur on the tenant database, say DB_1.
I read the following should work
\Artisan::call('migrate', [
'--path' => "database/migrations_system",
'--database' => 'db_1'
]);
However I end up with an error
InvalidArgumentException in DatabaseManager.php line 238:
Database [db_1] not configured.
Stuck.... How can I specify the migration to run on a specific database?
UPDATE:
I have found that changing config/database.php and adding db_1 gets me past this error....
'db_1' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => 'db_1',
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'pass'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'port' => '3306'
],
However this connection is unknown and needs to be done on the fly, which I am also stuck on how to accomplish....
UPDATE Again and working solution.... Modify the config on the fly...
$connections = \Config::get('database.connections');
$tenant_database = 'db_1'; //assign from your main database
$tenant_connection = [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => $tenant_database ,
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'port' => '3306'
];
$connections['tenant'] = $tenant_connection;
\Config::set('database.connections', $connections);
\Artisan::call('migrate', [
'--path' => "database/migrations_system",
'--database' => 'tenant'
]);
So it looks like I have answered my own question, however maybe some can comment on this procedure.
I'll put my own solution down....
1) Create the database name
For example I name the new database db_1 where 1 represents the ID of the system in the main database. I have considered storing a unique scrambled name in the main database. In the end that seemed like overkill.
2) Create the connection - for this I use the exact same name as the new database.
public function createConnection()
{
$connections = \Config::get('database.connections');
if(!isset($connections[$this->getSystemName()]))
{
$tenant_connection = [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => $this->getSystemName(),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'port' => '3306'
];
$connections[$this->getSystemName()] = $tenant_connection;
\Config::set('database.connections', $connections);
//dd(\Config::get('database.connections'));
}
}
3) Create the database, which you do using your main connection
$sql = "CREATE DATABASE " . $this->tenant_name;
DB::connection('main_db')->statement($sql);
4)run the migration
$r = \Artisan::call('migrate', [
'--path' => "database/migrations_tenant",
'--database' => $this->tenant_name
]);
5) finally you can seed test data or load some default data. Access the connection like
DB::connection($this->system_name)->table($table)->insert($csv);