This question already has answers here:
How to use multiple databases in Laravel
(7 answers)
Closed 2 years ago.
So I have hundreds of database and I want to change what database to use according to what value I pass in
For example I have many company branch that has branch_code (000,001,002,003, etc. up to 200). They all have the same tables but was just created in different databases. Also assuming that only the DB Name is different. My question is how can I access a different database when there's hundreds of them? Should I change the .env file dynamically? Should I make a function that changes the connection whenever the user chooses another branch, and if so how?
And yes, I have tried to connect two databases by configuring and adding a new database connection in config/database.php but I don't know how to do that with hundreds of them. Is there like a quicker way to do it, maybe add a for loop in the database.php? Or maybe somehow changing the .env DB_DATABASE variable.
Actually no there is not other way than setting it in .env becuase even if you want to make it dynamic you have to set like 100 ifs for that if you just want to see 100 settings without writing 100 lines of code you can set a loop in this file :
app/config/database.php
like below :
return array(
'default' => 'mysql',
'connections' => array(
# Primary/Default database connection
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'database1',
'username' => 'root',
'password' => 'secret'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
# You loop here
'mysql2' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'database2',
'username' => 'root',
'password' => 'secret'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
);
thats the easiest way to do that . so here as instructed below and then in your eloquent you should specify what database connection you want to use for that query !! :
You can set in Env file like below :
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database1
DB_USERNAME=root
DB_PASSWORD=secret
DB_CONNECTION_SECOND=mysql
DB_HOST_SECOND=127.0.0.1
DB_PORT_SECOND=3306
DB_DATABASE_SECOND=database2
DB_USERNAME_SECOND=root
DB_PASSWORD_SECOND=secret
and then you can go into your condif/database.php and do the setting as below :
'mysql2' => [
'driver' => env('DB_CONNECTION_SECOND'),
'host' => env('DB_HOST_SECOND'),
'port' => env('DB_PORT_SECOND'),
'database' => env('DB_DATABASE_SECOND'),
'username' => env('DB_USERNAME_SECOND'),
'password' => env('DB_PASSWORD_SECOND'),
],
and then in your eloquent you can use as below :
$users = DB::connection('mysql2')->select(...);
Hope this can help you
note :
you can refer to this link for further info
https://fideloper.com/laravel-multiple-database-connections
Related
So. There is 2 different sites.
site_1 and site_2
Site_1:
PHP 7.3
Laravel 6.0
MYSQL
Contains corporate portal with helpdesk,news and so.
Site_2:
PHP 7.2
Laravel 5.6
MYSQL
Contains Videoportal(yeah like youtube:)) with users and webcams from construction sites.
On site_1 i have a class User with some properties (i.e. user id,department,and so) contains in db.
On site_2 i have different DB with user_id and cams .I want to select only webcams that belongs to user from site_1.
How can i pass value of User_id from site_1 to site_2 to select cams only for exact user?
In database.phpm define second mysql connection as
<?php
return array(
'default' => 'mysql',
'connections' => array(
# Our primary database connection
'mysql' => array(
'driver' => 'mysql',
'host' => 'host1',
'database' => 'database1',
'username' => 'user1',
'password' => 'pass1'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
# Our secondary database connection
'mysql2' => array(
'driver' => 'mysql',
'host' => 'host2',
'database' => 'database2',
'username' => 'user2',
'password' => 'pass2'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
);
Then use
$users = DB::connection('mysql2')->select('select * from users');
Hint
https://laravel.com/docs/5.7/database#using-multiple-database-connections
Are they using the same database or are the databases on those apps synced? If not, then this is a bad practice. Well as for passing values from one Laravel app to another, just create endpoints wherein it will send/accept values.
Read the docs especially on the Controllers and Routing and then study on how you will implement it using your own logic.
Ok, my system connects to remote databases on which I consistently change depending on what data I want to access.
The setup is multiple identical structure databases resides on different IP addresses, so I always update the .env DB_HOST line every time I want to access different database.
Now I'm planning to create a view on my system that will accepts an IP address and set it as the current database to use.
Referencing this answer on Laracasts:
Set two database connections in your config (one for your unchanging main connection, the other one for your custom dynamic connection):
'main' => array(
'driver' => 'mysql',
'host' => 'hostname',
'database' => 'database',
'username' => 'username',
'password' => 'password',
'prefix' => '',
),
'dynamic' => array(
'driver' => 'mysql',
'host' => '',
'database' => '',
'username' => '',
'password' => '',
'prefix' => '',
),
Then change your database connection parameters for the second connection as needed:
Config::set('database.connections.dynamic.host', $newHost);
Config::set('database.connections.dynamic.username', $newUsername);
Config::set('database.connections.dynamic.password', $newPassword);
Config::set('database.connections.dynamic.database', $newDatabase);
Config::set('database.default', 'dynamic');
And reconnect the database:
DB::reconnect('mysql');
I know that in Laravel you can use multiple database connections by specifying them in the config/database.php file, then using DB::connection('my_conn_name'), but is there anyway to use a connection that is not specified in the config/database.php file?
I am writing an archiving application, so the user can specify what connection they would like to use for the process (host, user and password), and I am hoping that I can return the results from show databases for the supplied connection.
After the user has specified the db parameters you could store it in a session to populate a custom connection at config/database.php:
'connections' => [
'mysql' => [
'...'
],
'testing' => [
'...'
],
'custom' => [
'driver' => 'mysql',
'host' => session()->get()->db_host,
'database' => session()->get()->db_database,
'username' => session()->get()->db_username,
'password' => session()->get()->db_password,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
]
]
I have recently started using Laravel and absolutely love it, however I keep coming across the error:
SQLSTATE[42000] [1203] User 'root' already has more than 'max_user_connections' active connections
I know there is a way to change the MySQL variable to allow more connections, however this isn't an option with my host and along with this, there is no way I should of hit this limit.
Some example queries are:
return User::where('users.username', '=', Auth::user()->username)->join('settings', 'settings.username', '=', 'users.username')->first();
return Char::orderBy('calculate', 'ASC')->groupBy('charid')->get();
So my question is, what should I be looking for in order to combat the error? Should I be disconnecting myself from the database at the end of each function? Or something completely different?
Any help would be appreciated in this matter.
No, Once Change the username root to following username like *system a*nd try it
What version of Laravel do you have?
Go to "application/config.database.php" or "app/config/database.php". From here you can add custom options to your laravel database settings.
The "database.php" contains at the end something like this:
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'db_name',
'username' => 'db_username',
'password' => 'username_password',
'charset' => 'utf8',
'prefix' => '',
),
),
You can add "options" to that array, just like this:
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'db_name',
'username' => 'db_username',
'password' => 'username_password',
'charset' => 'utf8',
'prefix' => '',
'options' => array(
//here you will add your custom options
),
),
),
php artisan migrate:install
{"error":{"type":"ErrorException","message":"PDO::__construct(): [2002] Connection refused (trying to connect via tcp:\/\/127.0.0.1:3306)","file":"\/Applications\/MAMP\/htdocs\/DRCSports\/vendor\/laravel\/framework\/src\/Illuminate\/Database\/Connectors\/Connector.php","line":47}}
In my database.php I have updated the information to mysql
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'Laravel_DRCSports',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
I am not sure if I am understanding the error right, but to me it looks like my laravel isn't connecting to mysql right. If that is the case I have no clue how to fix it.
The problem was that mysql is running on port 8888, while Laravel's default port value is 3306 (as it's the default port of mysql servers).
The solution is to add 'port' key to the array (For example: 'port' => 8888) and it'll
do the work.
This is what i did... in /app/config/app.php
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost:8889',
'database' => 'pic',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
and at the bottom of the php code
'redis' => array(
'cluster' => false,
'default' => array(
'host' => '127.0.0.1',
'port' => 8888,
'database' => 0,
),
),
it has to work it...
I experienced problems (Laravel 4) when I used MySQL on a port other than 3306.
A browser-run app expects the following app/config/database syntax:
'mysql' => array(
...
'host' => 'localhost',
'port' => '8889',
...
)
While the command-line run artisan expects the following syntax:
'mysql' => array(
...
'host' => 'localhost:8889',
...
)
The issue is described here:
https://github.com/laravel/laravel/issues/1182
Most articles suggest a workaround using Laravel environments, but it results in duplicate config files and violates the DRY principle (Don't Repeat Yourself), so here's another alternative:
At the top of app/config/database.php:
$my_hostname = 'localhost';
$my_port = '8889';
$my_database = 'database';
$my_username = 'username';
$my_password = 'password';
if (App::runningInConsole()) { // artisan runs from the command line
// change 'localhost' to 'localhost:8889'
$my_hostname = $my_hostname.':'.$my_port;
}
And further down:
'mysql' => array(
'driver' => 'mysql',
'host' => $my_hostname,
'port' => $my_port,
'database' => $my_database,
'username' => $my_username,
'password' => $my_password,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
Cheers
Change your database information in your config/database.php and .env file.
I did these changes and worked like a champ :
in database.php :
host : localhost:8889
Port: 8889
and my mamp has a password so I there was two way to putting that password either in database.php file or in .env file I changed the password '' value to 'forge' and then use my MAMP password in the .env file
by the way, you can see your specific information about MAMP in MAMP application in the port tab (MySQL one)
Make sure to edit this part of ".env" file.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=8889
DB_DATABASE=blog
DB_USERNAME=root
DB_PASSWORD=root
This worked for me.
i guess you can solve this problem by add the following code inside app/config/database syntax:
'mysql' => array(
...
'pconnect' => 'TRUE',
...
)