I configured the way I separate the database to read and write.
'mysql' => [
'read' => [
'host' => '192.168.1.1',
],
'write' => [
'host' => '196.168.1.2'
],
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
But now, I want to change the database driver in the session and I want to be able to read and write operations to a single host of the session value.
How can I only run on a single host in the session process without disturbing the above structure?
I think I found a solution.
We are adding a new connection into the first database.php connections.
'session' => array(
'host' => "HOST_NAME",
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
then we give the name of the connection a little bit before we install the connection value in session.php file.
'connection' => "session",
Thats it.
Related
Connecting to Different Database in October CMS
Hello,
I have two databases first one was created by OctoberCMS during installation process and the other one which i had already. how do i connect to that other database, i tried using builder plug-in but i am confused how to do it.
I want to connect to the other database and fetch information from one table and display on my home page.
Please help me with this. I am newbie to this CMS.
Thank you.
In your config/database.php you will have something like this (assuming MySQL):
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'port' => 3306,
'database' => 'database_name',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
Just add another one after it, changing the name to something else (mysql-legacy in my example below) and the relevant connection details:
'mysql-legacy' => [
'driver' => 'mysql',
'host' => 'localhost',
'port' => 3306,
'database' => 'database_name',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
After that, you can use DB; in your model or controller and then do things like this:
$query = DB::connection('mysql-legacy')->select('SELECT * FROM tablename');
I'm using Laravel's illuminate database library outside laravel with jessengers mongodb.
My requirement is multiple database connectivity through illuminate database.
Currently, I've added two connection one mysql and one mongodb.
To split the database load, I need to connect to mysql router instead of mysql db server directly. Also, in that I want one only for Read operation and one for Read/Write operation.
Kindly help me out on this.
Thanks in advance.
Current connections
$db = new Capsule;
$db->addConnection([
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'test',
'username' => 'test',
'password' => 'test#123#',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
], "default");
$db->addConnection([
'driver' => 'mongodb',
'host' => '127.0.0.1',
'port' => 27017,
'database' => 'test',
'username' => null,
'password' => null,
'options' => []
], "mongodb");
$db->getDatabaseManager()->extend('mongodb', function ($config) {
return new Connection($config);
});
$db->setEventDispatcher(new Dispatcher(new Container));
$db->setAsGlobal();
$db->bootEloquent();
I need to replace one mysql connection with two mysql connection for Read and Read/Write operation through mysql router.
You can define read/write options separately with either mysql host or mysql router host and port
$db->addConnection([
'driver' => 'mysql',
'read' => [
'host' => '<mysql_router_host_ip>',
'port' => '<mysql_router_host_port>'
],
'write' => [
'host' => '<mysql_router_host_ip>',
'port' => '<mysql_router_host_port>'
],
'database' => '<mysql_database>',
'username' => '<mysql_database_user>',
'password' => '<mysql_database_password>',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
], "mysql");
So I have 3 mysql DB's defined in my database.php file. I had 2 for the longest time and everything worked fine. I have one titled mysql, one mysql1 and one mysql2. The first 2 work fine, I copied the second one, changed the name to mysql2 and added the proper parameters but it still always says "Database mysql1 not configured. Is there some sort of cache? or somewhere else I have to define it? I am calling the DB through a model like this.
protected $connection = 'mysql1';
This is my database.php for clarification
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
'mysql1' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
'mysql2' => [
'driver' => 'mysql',
'host' => 'localhost',
'port' => '3306',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
This was fixed by using the command
php artisan config:cache
I submitted a Laravel issue request to fix it since that absolutely should not be required to change the DB connections variable.
If you can not delete cache with php artisan config:cache because it throws error after you run it:
[InvalidArgumentException]
Database [] not configured
And if you are sure that your connections parameters are good then try to manually delete bootstrap cache config files which are placed in app/bootstrap/cache folder.
I was wondering if anyone knew whether the above was likely to be achievable or if I'm doing something nonsensical. These connection details work to an RDS (i.e. blah.blah.eu-west-1.rds.amazonaws.com) database:
'db1' => [
'driver' => 'pgsql',
'host' => env('DB_HOST_BRAIN'),
'database' => env('DB_DATABASE_BRAIN'),
'username' => env('DB_USERNAME_BRAIN'),
'password' => env('DB_PASSWORD_BRAIN'),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'xyz_admin',
],
but these do not work to a Redshift (i.e. blah.blah.eu-west-1.redshift.amazonaws.com) database:
'db2' => [
'driver' => 'pgsql',
'host' => env('DB_HOST_PINKY'),
'database' => env('DB_DATABASE_PINKY'),
'username' => env('DB_USERNAME_PINKY'),
'password' => env('DB_PASSWORD_PINKY'),
'port' => env('DB_PORT_PINKY'),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'xyz',
],
Assuming all the details are correct, is there a compelling reason why this is never going to work? Is there any way I can make it work?
As long as the environment variables are correct, your config works for connecting to Redshift:
$take_over_the_world = DB::connection('db2')->select('SELECT tonight FROM going_to_do');
I am developing a project using laravel 4 framework. In my database.php file I get the following error:
Undefined index: driver
And my connection is as following:
$connections = array(
'mysql' => array(
'read' => array(
'host' => 'localhost',
'driver' => 'mysql',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'write' => array(
'host' => 'localhost',
'driver' => 'mysql',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
'mysql2' => array(
'read' => array(
'host' => 'localhost',
'driver' => 'mysql',
'database' => 'app_userdata',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'write' => array(
'host' => 'localhost',
'driver' => 'mysql',
'database' => 'app_userdata',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
)
);
I am also using environments in order to set different mysql connections. What is wrong with the code?
In my case it was because I deleted
'default' => 'mysql',
by mistake from app/config/database.php.
Moving the 'driver' key up a level should fix the issue.
$connections = array(
'mysql' => array(
'read' => array(
'host' => 'localhost',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'write' => array(
'host' => 'localhost',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'driver' => 'mysql'
),
Most of the other params that are shared can me moved as well
$connections = array(
'mysql' => array(
'read' => array(
'host' => 'localhost',
),
'write' => array(
'host' => 'localhost',
),
'driver' => 'mysql',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
This happens to me because I deleted
'default' =>env('DB_CONNECTION', 'mysql'),
From app/config/database.php. It's necesary have a default connection
Go to root directory .env
This values are taken first.
If you have multiple different connections (for example to multiple databases on the same host) and it doesn't make sense to set a default connection, you can specify a connection in the Model class.
<?php
namespace App\Http\Models;
use Illuminate\Database\Eloquent\Model;
class Character extends Model {
protected $connection = 'my_db_connection_name_here';
}
This would be on the of the connections defined in config/database.php.
I solved my problem by adding some permissions. My .env file was exists but wasn't readable. I just added 755 permission.