I wish to hide critical server access information as Laravel recommended, so I have setup config/database.php and .env as below, but these couldn't connect each one. which line was wrong?
Laravel version: 5.4
config/database.php
'connections' => [
'master_db' => [
'driver' => 'mysql',
'host' => env('DB_HOST_MASTER', '127.0.0.1'),
'port' => env('DB_PORT_MASTER', '3306'),
'database' => env('DB_DATABASE_MASTER'),
'username' => env('DB_USERNAME_MASTER'),
'password' => env('DB_PASSWORD_MASTER'),
'unix_socket' => env('DB_SOCKET_MASTER'),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'slave_db' => [
'driver' => 'mysql',
'host' => env('DB_HOST_SLAVE', '192.1.0.2'),
'port' => env('DB_PORT_SLAVE', '3306'),
'database' => env('DB_DATABASE_SLAVE'),
'username' => env('DB_USERNAME_SLAVE'),
'password' => env('DB_PASSWORD_SLAVE'),
'unix_socket' => env('DB_SOCKET_SLAVE'),
'charset' => 'latin1',
'collation' => 'latin1_general_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
]
.env
DB_CONNECTION=master_db
DB_HOST_MASTER=127.0.0.1
DB_PORT_MASTER=3306
DB_DATABASE_MASTER=db_master
DB_USERNAME_MASTER=user_master
DB_PASSWORD_MASTER=passwordmaster
DB_CONNECTION=slave_db
DB_HOST_SLAVE=192.1.0.2
DB_PORT_SLAVE=3307
DB_DATABASE_SLAVE=db_slave
DB_USERNAME_SLAVE=user_slave
DB_PASSWORD_SLAVE=passwordslave
If you want use like upper config/database.php file style, then .env file recommend as below as simple. :)
DB_CONNECTION=master_db
DB_HOST_MASTER=127.0.0.1
DB_PORT_MASTER=3306
DB_DATABASE_MASTER=db_master
DB_USERNAME_MASTER=user_master
DB_PASSWORD_MASTER=passwordmaster
// DB_CONNECTION=slave_db <------ no needed this line
DB_HOST_SLAVE=192.1.0.2
DB_PORT_SLAVE=3307
DB_DATABASE_SLAVE=db_slave
DB_USERNAME_SLAVE=user_slave
DB_PASSWORD_SLAVE=passwordslave
Related
I am creating Laravel 6.6 project,
my query is how to create new multiple databases in laravel and then how to handle it,
if I fetch data from the new database that I was recently created then how to register a new database in .env file dynamically?
I am doing this the following way.
I do have a dummy entry for my client connections in my config/database.php like this:
'clientDb' => [
'driver' => 'mysql',
'host' => env('DB_CLIENT_HOST', '127.0.0.1'),
'port' => env('DB_CLIENT_PORT', '3306'),
'database' => env('DB_CLIENT_DATABASE', 'some_default_client_name'),
'username' => env('DB_CLIENT_USERNAME', 'root'),
'password' => env('DB_CLIENT_PASSWORD', ''),
'unix_socket' => env('DB_CLIENT_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
'dump' => [],
],
So, all my client-related models are used with $connection = 'clientDb';, the others using the default connection.
When changing a client (so i need to switch the connection), i just set the new connection by calling:
Config::set('database.connections.clientDb', [
'database' => NEW_DATABASE_NAME,
// all the other params from config
]);
After that, i figured out to call DB::reconnect('clientDb'); to get the connection really running.
I hope, this will lead you to the right direction 😉
You should usage from database.php in config directory. for example I have another mysql database which is different from main db:
so I add this code to database.php and connections section:
'mysql_second_db' => [
'driver' => 'mysql',
'host' => env('DB_HOST_SECOND_DB', '127.0.0.1'),
'port' => env('DB_PORT_SECOND_DB', '3306'),
'database' => env('DB_DATABASE_SECOND_DB', 'forge'),
'username' => env('DB_USERNAME_SECOND_DB', 'forge'),
'password' => env('DB_PASSWORD_SECOND_DB', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8',
'collation' => 'utf8_general_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
],
You can use that:
$names=['name1','name2'];
Schema::create('t1', function($table)
{
$table->increments('id');
});
To set env look at that: ENV
I tried to connect to a new bbdd from laravel giving me the error that appears in the title
I leave below the configuration file of bbdd (database.php) and as we call it from the controller
'reporting' => [
'driver' => 'mysql',
'host' => env('DB_REPORTING_HOST', 'localhost'),
'port' => env('DB_REPORTING_PORT', '3306'),
'database' => env('DB_REPORTING_DATABASE', 'reporting'),
'username' => env('DB_REPORTING_USERNAME', 'paco'),
'password' => env('DB_REPORTING_PASSWORD', 'qwerty'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
],
and the call
$DB = DB::connection('reporting');
$DB->table('clients')->get();
fix it
'reporting' => [
'driver' => 'mysql',
'host' => '127.0.0.1',
'port' => '3306',
'database' =>'reporting',
'username' => 'paco',
'password' => 'qwerty',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
And in the controller
$dataL = DB::connection('reporting');//->select('select * from leyenda');
$dataL2 = $dataL->table('familias')->get()
This is recommended regulation for database divide setting in Laravel.
'mysql' => [
'read' => [
'host' => '192.168.1.1',
],
'write' => [
'host' => '196.168.1.2'
],
'sticky' => true,
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
],
But if I want to use different access information as DB_NAME, USER_ID, PASS etc to each read/write databases, then how could I make it? Thank you.
You can create two connections then specify the connection with Eloquent using the on method:
'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,
],
'mysql2' => [
'driver' => 'mysql',
'host' => env('DB_HOST2', '127.0.0.1'),
'port' => env('DB_PORT2', '3306'),
'database' => env('DB_DATABASE2', 'forge'),
'username' => env('DB_USERNAME2', 'forge'),
'password' => env('DB_PASSWORD2', ''),
'unix_socket' => env('DB_SOCKET2', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
User::on('mysql2')->where('id', $id)->update($data);
Or using Query Builder:
DB::connection('mysql2')->table('users')->where('id', $id)->update($data);
You can declare another db conecction like this:
'mysql' => [
'read' => [
'host' => '192.168.1.1',
],
'write' => [
'host' => '192.168.1.1'
],
'sticky' => true,
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
],
'writecon' => [
'read' => [
'host' => '196.168.1.2',
],
'write' => [
'host' => '196.168.1.2'
],
'driver' => 'mysql',
'port' => env('DB2_PORT', '3306'),
'database' => env('DB2_DATABASE', 'db2'),
'username' => env('DB2_USERNAME', 'somename'),
'password' => env('DB2_PASSWORD', 'somepass'),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
],
Then in your .env add:
DB2_PORT=3306
DB2_DATABASE=db2
DB2_USERNAME=somename
DB2_PASSWORD=somepass
And you can use it like this:
$someModel->setConnection('writecon');
$someModel->save();
not tested but you can try it.
I get following error in my laravel application. ( I am using Laravel 5.1)
PDOException in Connector.php line 55:
SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: NO)
at the details it says that I do not parse any password:
array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'gen', 'username' => 'root', 'password' => '', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name' => 'mysql'), array('0', '2', '0', false, false)) in MySqlConnector.php line 22
My .env file looks like this in Laravel:
APP_ENV=local
APP_DEBUG=true
APP_KEY=************************************
DB_HOST=localhost
DB_DATABASE=gen
DB_USERNAME=miad2
DB_PASSWORD=***************
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
I use phpmyadmin and created that user there.
database.php looks like this:
<?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'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'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 stuck here for some hours and do not know how to fix this, need some help.
I figured it out, so what I did now was changing the server name which was set to root and the password which was empty in the config.inc.php to that user i created. And Voila it worked.
I am trying to use laravel for the first time. I opned the database.php file located in the config directory and then update the mysql config.
but every time I try to do this command
php artisan migrate:install
I get this
[PDOException]
SQLSTATE[HY000] [2002] No connection could be made because the target machi
ne actively refused it.
I have to let laravel to connect to a different port somehow.
I have tried the following and none worked.
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '10.15.1.5'),
'port' => '3308',
'database' => env('DB_DATABASE', 'mydb_dev'),
'username' => env('DB_USERNAME', 'user'),
'password' => env('DB_PASSWORD', 'pass'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
and this
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '10.15.1.5:3308'),
'database' => env('DB_DATABASE', 'mydb_dev'),
'username' => env('DB_USERNAME', 'user'),
'password' => env('DB_PASSWORD', 'pass'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
and this
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '10.15.1.5'),
'port' => env('DB_PORT', '3308'),
'database' => env('DB_DATABASE', 'mydb_dev'),
'username' => env('DB_USERNAME', 'user'),
'password' => env('DB_PASSWORD', 'pass'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
finally, I tried this
'mysql' => [
'driver' => 'mysql',
'host' => '10.15.1.5:3308',
'database' => env('DB_DATABASE', 'mydb_dev'),
'username' => env('DB_USERNAME', 'user'),
'password' => env('DB_PASSWORD', 'pass'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
this gives me a different error
Access denied for user 'homestead'#'10.xxxxxx' (using password: YES)
I am not sure where is the user homestead is coming from.
How can I tell laravel to connect to mysql on port 3308?
I know you figured it out, but of all the attempts you provided, the answer you gave wasn't clear. For those looking in the future, here is what you need:
(This is assuming Laravel 5.1 using a Postgres DB, but should work with alternate versions of Laravel, and different DBs... also, don't mind the alternate/different config settings that my database.php has as opposed to yours, these were for advanced configurations.)
Add a 'port' section to your config/database.php, that looks like follows:
'pgsql' => [
'read' => [
'host' => env('DB_READ', 'localhost')
],
'write' => [
'host' => env('DB_WRITE', 'localhost')
],
'port' => env('DB_PORT', '5432'),
'driver' => 'pgsql',
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => env('DB_SCHEMA', 'public'),
'options' => array(
PDO::ATTR_PERSISTENT => env('DB_PERSISTENT', false),
),
],
Then in your .env you can override the port setting as follows:
DB_PORT=32769
I figured out the issue.
the file .env needs to be updated with the correct information