I have two separate Laravel Homestead projects called "Laravel" and "authapp". I would like each of these projects to have their own database.
I have set up my Homestead.yaml file to create one database for each project. And it does just that. However, when I am in my "authapp" project and run the migrate command, it insists on writing to the database specified for the "Laravel" project (the "homestead" database).
Here is my Homestead.yaml file:
provider: virtualbox
authorize: ~/.ssh/id_rsa.pub
keys:
- ~/.ssh/id_rsa
folders:
- map: ~/Code/Laravel
to: /home/vagrant/Code/Laravel
- map: ~/Code/authapp
to: /home/vagrant/Code/authapp
sites:
- map: homestead.app
to: /home/vagrant/Code/Laravel/public
- map: authapp.app
to: /home/vagrant/Code/authapp/public
databases:
- homestead
- authapp
And my database.php file for the "Laravel" project.
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'homestead'),
'username' => env('DB_USERNAME', 'homestead'),
'password' => env('DB_PASSWORD', 'secret'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
And my database.php file for the "authapp" project.
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'authapp'),
'username' => env('DB_USERNAME', 'homestead'),
'password' => env('DB_PASSWORD', 'secret'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
I have run vagrant provision a bunch of times and that doesn't do the trick.
Check that the database authapp exist in MySQL. Also check your .env file, in the root of the directory, to make sure that DB_DATABASE has not accidentally been set.
Whenever you use env('VARIABLE_NAME', 'default value') the VARIABLE_NAME set in the .env file will always override the string value passed in as the second parameter to the env() function.
Related
I'm trying to deploy a laravel 5.8 application to Azure, but I'm getting the following error;
"SSL connection is required. Please specify SSL options and retry."
In the config folder there wasn't a database.php file, so I created one using the database.php in the Laravel Git Repository. I downloaded the BaltimoreCyberTrustRoo.crt.pem file from MS and placed this inside the root of my application inside a folder called ssl. I've then modified the database.php file to this;
'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,
'sslmode' => env('DB_SSLMODE', 'prefer'),
'options' => (env('MYSQL_SSL') && extension_loaded('pdo_mysql')) ? [
PDO::MYSQL_ATTR_SSL_KEY => base_path('ssl/BaltimoreCyberTrustRoot.crt.pem'),
] : []
],
However, even with this change, I'm still seeing the same error.
I've attached a screenshot of the folder structure incase it is useful.
I managed to resolve this by changing the mysql options value to;
'options' => [
PDO::MYSQL_ATTR_SSL_KEY => base_path('ssl/BaltimoreCyberTrustRoot.crt.pem'),
]
instead of
'options' => (env('MYSQL_SSL') && extension_loaded('pdo_mysql')) ? [
PDO::MYSQL_ATTR_SSL_KEY => base_path('ssl/BaltimoreCyberTrustRoot.crt.pem'),
] : []
Mine is a non production(just learning) scenario, so I disabled ssl.
If it is enabled, I am getting this error.
This question already has answers here:
How to use multiple databases in Laravel
(7 answers)
Closed last month.
How can i connect second database in model?
.env file
DB_HOST=localhost
DB_DATABASE=crmgen_lara1
DB_USERNAME=crmgen_lara1
DB_PASSWORD=Y.vfdsf
DB_HOST=localhost
DB_DATABASE=crmgen_restorant
DB_USERNAME=crmgen_restorant
DB_PASSWORD=#X(dsfs}
Database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'crmgen_lara1'),
'username' => env('DB_USERNAME', 'crmgen_lara1'),
'password' => env('DB_PASSWORD', 'Y.ıquwewqe'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
],
'mysql2' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'crmgen_restorant'),
'username' => env('DB_USERNAME', 'crmgen_restorant'),
'password' => env('DB_PASSWORD', '#X(sadsa}'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
],
TestModel.php
$testt = new TestModel();
$testt= TestModel::on('mysql2')->get();
dd($testt);
die();
ERROR: Table 'crmgen_lara1.test' doesn't exist (SQL: select * from test)
Because I can not connect second database. How can I solve this problem?
You just have to differentiate the different keys of the .env, because the values replace each other. DB_HOST, DB_HOST2 etc...
I have next error :
Illuminate\Database\QueryException
include(/home/xxxx/xxxxxx/vendor/composer/../doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php): failed to open stream: No such file or directory (SQL: select * from `users`)
I tried to change .env and database.php host to localhost or 127.0.0.1 or change port to 33060, composer update / install / dump-autoload , etc (All solution i found on website)
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'xxxxx'),
'username' => env('DB_USERNAME', 'xxxx'),
'password' => env('DB_PASSWORD', 'xxxx'),
'unix_socket' => env('DB_SOCKET', '/var/lib/mysql/mysql.sock'),
'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'),
]) : [],
],
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
Laravel Framework 6.18.6
There is a much stronger way of cleaning a confused install...
composer dumpautoload
Works nearly every time for me, including in cases where this particular error was encountered.
Do i need to change database configuration in database.php file in laravel when upload to server?
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
Don't change anything in database.php config file. What you need to do is to change DB credentials in .env file on server side.
So, you'll have different .env files on a local machine and server, but the same database.php config file.
https://laravel.com/docs/5.3/configuration#environment-configuration
Update your database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'yourdatabasename'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'password'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
I am trying to set a separate database for tests. For some reason when I run the migrations I have always a message saying: "Nothing to migrate."
Here is how I run the migrations:
php artisan migrate --database=mysql_test
Nothing to migrate.
And here is my config/database.php:
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'mydb'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'dev'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'mysql_test' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'mydbtest'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'dev'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
And obviously I can login to the database mydbtest with the credentials in the config.
Updating here after some troubleshooting:
After emptying mydb database and running the same command it filled the mydb database not mydbtest. This means it does not take into account what I am giving as database connection name.
Any suggestions to troubleshoot this are welcome.
Thanks
I found the problem.
After commenting out this line in my .env file it works now:
#DB_DATABASE=mydb
Thanks