I would like to know: How can I maintain different credentials for my database. Like when I am working locally, laravel should rely on Database A while after pushing it on my server it should rely on Database B?
Do like this
config/database.php (production setting)
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'db'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', '********'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
.env file (local setting)
APP_ENV=local
APP_DEBUG=true
APP_URL=project.dev
DB_HOST=localhost
DB_DATABASE=mydb
DB_USERNAME=root
DB_PASSWORD=
I think the better approach is to use different variable names for local, staging and production something like below (not tested).
database.php
use Illuminate\Support\Facades\App;
//... Other code
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => (App::environment('local') ?
env('DB_HOST', '127.0.0.1') : App::environment('production')) ?
env('PROD_DB_HOST') : env('STAGING_DB_HOST'),
'port' => (App::environment('local') ?
env('DB_PORT', '3306') : App::environment('production')) ?
env('PROD_DB_PORT') : env('STAGING_DB_PORT'),
'database' => (App::environment('local') ?
env('DB_DATABASE', 'forge') : App::environment('production')) ?
env('PROD_DB_DATABASE') : env('STAGING_DB_DATABASE'),
'username' => (App::environment('local') ?
env('DB_USERNAME', 'root') : App::environment('production')) ?
env('PROD_DB_USERNAME') : env('STAGING_DB_USERNAME'),
'password' => (App::environment('local') ?
env('DB_PASSWORD', 'localhost') : App::environment('production')) ?
env('PROD_DB_PASSWORD') : env('STAGING_DB_PASSWORD'),
//... other variables
],
//... other code
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=localdb
DB_USERNAME=root
DB_PASSWORD=admin123
STAGING_DB_HOST=127.0.0.1
STAGING_DB_PORT=3306
STAGING_DB_DATABASE=stagdb
STAGING_DB_USERNAME=staginguser
STAGING_DB_PASSWORD=stag123
PROD_DB_HOST=127.0.0.1
PROD_DB_PORT=3306
PROD_DB_DATABASE=proddb
PROD_DB_USERNAME=produser
PROD_DB_PASSWORD=prod123
use env and change accordingly.
Config::set('database.default', 'production-DB');
Laravel Configs
Related
I'm really surprised the Laravel documentation is limited for explaining how to connect to MS SQL Server. There are two areas that I think need configuration -- the .env file and the database.php. Most examples on the web don't show the same format compared to Laravel 7. I would like to know where I can find the correct info for the following.
How to tell what port my DB is on
What to enter for URL
What to enter for HOST
When testing the connection it says 'driver not found' - is it saying the details are incorrect or is it looking for a missing file?
before changing the database.php file it has the word forge as the second argument for the env() function - what is forge???
If anyone can point me to a good resource id be in your debt!
DB_CONNECTION=sqlsrv
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=test_db_1
DB_USERNAME=sa
DB_PASSWORD=mypassword
```
and the database.php...
'default' => env('DB_CONNECTION', 'sqlsrv'), ...
...
'sqlsrv' => [
'driver' => 'sqlsrv',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'test_db_1'),
'username' => env('DB_USERNAME', 'sa'),
'password' => env('DB_PASSWORD', 'mypassword'),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
],
Your DB_HOST will always be localhost, so if the database is on the same machine, 127.0.0.1 will be fine. TCP port 1433 is typically the port used by a default instance of SQL Server, set it to DB_PORT=1433.
Laravel Forge is a tool for deploying and configuring web applications and can be used to automate the deployment of any web application that uses a PHP server. Since we are using the .env to overwrite the values in config/database.php there is no need to worry about the "forge" values. Finally, you may need to install drivers on the machine to support PHP and SQL Server.
.env
DB_CONNECTION=sqlsrv
DB_HOST=127.0.0.1
DB_PORT=1433
DB_DATABASE=test_db_1
DB_USERNAME=sa
DB_PASSWORD=mypassword
config/database.php
'sqlsrv' => [
'driver' => 'sqlsrv',
'url' => env('DATABASE_URL'),
'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,
]
i want to use multiple databases, i have 1db/user and 1 main db,already set main db in config file but how to manage user database(db name = {user_id}_db). thanks in advance
this is my config code
'main' => [
'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' => '',
'strict' => true,
'engine' => null,
],
You can use following configuration for multiple dbs.
<?php
return array(
'default' => 'mysql',
'connections' => array(
# primary
'mysql' => array(
'driver' => 'mysql',
'host' => 'ip1',
'database' => 'db1',
'username' => 'user',
'password' => 'pwd'
'charset' => 'utf8',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
),
# secondary db connection
'mysql2' => array(
'driver' => 'mysql',
'host' => 'dbip2',
'database' => 'db2',
'username' => 'user',
'password' => 'pwd'
'charset' => 'utf8',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null
),
'sqlite' => array(
'driver' => 'sqlite',
'database' => __DIR__.'/../database/db.sqlite',
'prefix' => '',
),
),
);
Then you can use the the connection() method:
$connection = ‘mysql2’;
Schema::connection($connection)->create(function(Blueprint $table) {
//
});
Creating DB for each user is useless, even if you are doing it for security or any other purpose whatsoever. Useful SO link MySQL performance: multiple tables vs. index on single table and partitions
Your question (connecting to multiple databases in Laravel) is already answered here. How to use multiple database in Laravel
UPDATE
I guess, you want to create new databases on the fly and connect to them (without using .env configuration). Create dynamic connections instead of specifying 'connection-name' when trying to connect to a new database. Then, using Config::set you can create new temporary connections and then use DB::connection with the temporary name you just created. read more https://lukevers.com/2015/03/25/on-the-fly-database-connections-with-laravel-5
Can anyone help me to fix this problem please
the error is database [postgres] not configured.
here is my .env file :
DB_CONNECTION=postgres
DB_HOST=localhost
DB_PORT=5432
DB_DATABASE=referentiel
DB_USERNAME=postgres
DB_PASSWORD=postgres
and here is my database.php:
'default' => 'postgres',
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => 'referentiel',
'username' => env('postgres', ''),
'password' => env('postgres', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
],
Change the default parameter in your config to match the name of your postgres connection - pgsql in your case:
'default' => 'pgsql',
And put correct database parameters in your .env file.
Try to edit your .env
From: DB_CONNECTION=postgres
To: DB_CONNECTION=pgsql
i think the problem in php.ini if you use xampp open app xampp > config > php.ini
find ;extension=php_pdo_pgsql.dll
;extension=php_pdo_sqlite.dll
delete ;
change extension=php_pdo_pgsql.dll
extension=php_pdo_sqlite.dll
I am working on a multiple database system. System will integrate clients local mssql server.
I will store credential info in my master database. And i want to migrate some tables to my customers local database.
As you know we can choose connection as below.
Schema::connection('sqlsrv')->create('products', function (Blueprint $table) {
});
But I have to use it dynamically.
This connection reads from config/database.php
'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', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('SQLSRV_HOST', 'localhost'),
'database' => env('SQLSRV_DATABASE', 'forge'),
'username' => env('SQLSRV_USERNAME', 'forge'),
'password' => env('SQLSRV_PASSWORD', ''),
'collation' => 'Turkish_CI_AS',
'charset' => 'utf8',
'prefix' => env('SQLSRV_PREFIX', ''),
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
],
],
I did it in using DB Facade.
public function getConnection($connection_id)
{
/** Bağlantı Bilgilerini Çeker */
$connection = Connection::find($connection_id);
try
{
/** Bağlantı Ayarları */
config()->set('database.connections.sqlsrv.host', $connection->host);
config()->set('database.connections.sqlsrv.database', $connection->db);
config()->set('database.connections.sqlsrv.username', $connection->username);
config()->set('database.connections.sqlsrv.password', $connection->password);
config()->set('database.connections.sqlsrv.charset', $connection->charset);
config()->set('database.connections.sqlsrv.collation', $connection->collation);
config()->set('database.connections.sqlsrv.prefix', $connection->prefix);
/** Ayarlar ile yeniden bağlanma */
return DB::reconnect($connection->driver);
}catch (\Exception $e)
{
throw new \Exception($e);
}
}
Is there any method or solution like DB::reconnect() in Schema Facade ?
Here's what I ended up doing. Hope it helps your case.
// Erase the tenant connection, thus making Laravel
// get the default values all over again.
DB::purge('tenant');
// Make sure to use the database name we want to establish a connection.
Config::set('database.connections.tenant.host', $company->database->main_server);
Config::set('database.connections.tenant.database', $company->mysql_database);
Config::set('database.connections.tenant.username', $company->mysql_username);
Config::set('database.connections.tenant.password', $company->mysql_password);
// Rearrange the connection data
DB::reconnect('tenant');
// Ping the database. This will thrown an exception in case
// the database does not exists.
Schema::connection('tenant')->getConnection()->reconnect();
I noticed that if you do not DB::purge, Schema will not be able to pick up any connection change.
On the other hand, if you do purge, but the database doesn't exist, you won't get an exception until you actually try to do something with the database connection. The reconnect() in the last line will trigger that.
When I trying to install drupal-7 in my server, at the setup database step it shows the error
Failed to connect to your database server. The server reports the following message: SQLSTATE[42000]: Syntax error or access violation: 1231 Variable 'sql_mode' can't be set to the value of 'NO_AUTO_CREATE_USER'.
The database name, username, and password are all correct.
Is there any solution to this problem?
How have I fixed (Syntax error or access violation: 1231 Variable 'sql_mode' can't be set to the value of 'NO_AUTO_CREATE_USER') problem in laravel
SO go to config/database.php
in mysql connection add the following
code
'modes' => [
'ONLY_FULL_GROUP_BY',
'STRICT_TRANS_TABLES',
'NO_ZERO_IN_DATE',
'NO_ZERO_DATE',
'ERROR_FOR_DIVISION_BY_ZERO',
'NO_ENGINE_SUBSTITUTION',
],
The whole connection will be something like the following
'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' => '',
'strict' => true,
'engine' => null,
'modes' => [
'ONLY_FULL_GROUP_BY',
'STRICT_TRANS_TABLES',
'NO_ZERO_IN_DATE',
'NO_ZERO_DATE',
'ERROR_FOR_DIVISION_BY_ZERO',
'NO_ENGINE_SUBSTITUTION',
],
],
Drupal isn't allowed to create a new user in your MySQL database. You need at least MySQL 5.0.15