I'm creating an application that reads information from a number of different databases, but doesn't actually have its own database, as there is no information being written anywhere.
Basically, a user selects a record and a type, and the application will generate a .pdf file based on their choices. I have multiple connections defined in app/config/database.php but I don't want to connect to any of them by default. Is there a way to tell Laravel not to connect to a database? I've tried a few things (all in app/config/database.php), first being:
'default' => NULL,
// and
//'default' => '',
Which both return:
Undefined index: driver
I've also tried:
'default' => 'none',
'connections' => array(
'none' => array(
'driver' => '',
'host' => '',
...
),
),
which in turn returns:
Unsupported driver []
Unsupported host[]
...
And lastly setting 'default' => '', which returns:
Database [] not configured.
I've found ways to use Laravel's models without a database connection, but not actually Laravel itself.
Edit:
Setting the connection to an existing mysql connection and not selecting a database "works":
'default' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => '',
'username' => '****',
'password' => '****',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
but I'm not sure if that's the right way to do it. It feels like a work-around and not an actual solution.
I would do the following:
'driver' => 'sqlite',
and for the sqlite connection settings
'sqlite' => [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]
That way, it'll use an in-memory database and the database will cease to exist when the database connection is closed, and since you won't be opening a connection, you'll never have a local database open.
You could also remove the database service provider from your app.php config file:
'Illuminate\Cookie\CookieServiceProvider',
//'Illuminate\Database\DatabaseServiceProvider',
'Illuminate\Encryption\EncryptionServiceProvider',
and the facades for Fluent and Eloquent in the same file:
'Crypt' => 'Illuminate\Support\Facades\Crypt',
//'DB' => 'Illuminate\Support\Facades\DB',
//'Eloquent' => 'Illuminate\Database\Eloquent\Model',
'Event' => 'Illuminate\Support\Facades\Event',
which will prevent you from being able to connect to local databases and not even boot the sqlite database connection you've just set up.
What you need to do define the connection in the app/config/database.php file and specify the connection when running a query with laravel's DB::connection('connection_name').
More information here --> http://laravel.com/docs/4.2/database#accessing-connections
Simply define your db driver as 'sqlite' in the database config.
'default' => 'sqlite',
As long as you haven't changed anything in the default sqlite config section, all should work. You don't necessarily have to use the database, but it will alleviate those errors you received.
In case you have changed the default sqlite config for whatever reason, you can follow the below steps to configure the sqlite db.
'sqlite' => array(
'driver' => 'sqlite',
'database' => __DIR__.'/../database/production.sqlite',
'prefix' => '',
),
Also, make sure this 'production.sqlite' file exists if it doesn't already. You can do this easily from the command line:
touch app/database/production.sqlite
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.
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
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
),
),
),