Magento 2 separate read/write database connections - mysql

What is the proper way to define separate read and write connections to separate MySQL databases in app/etc/env.php for Magento 2?

So the key here is to think of the Magento 1.X read write connections as master/slave connections in 2.X. I believe this is an enterprise only feature so community editions users might be out of luck. Below is an excerpt of my app/etc/env.php. We are using haproxy to balance read/write connections to a Percona cluster so unless you have the same setup you will need to configure your hosts to the appropriate IP's.
...
'db' =>
array (
'connection' =>
array (
'default' =>
array (
// HaProxy Write (master) connection
'host' => '127.0.0.1:3308',
'port' => '3308',
'dbname' => 'magento_db',
'username' => 'username',
'password' => 'password',
'active' => '1',
),
),
'slave_connection' =>
array (
'default' =>
array (
// HaProxy Read (slave) connection
'host' => '127.0.0.1:3307',
'port' => '3307',
'dbname' => 'magento_db',
'username' => 'username',
'password' => 'password',
'active' => '1',
),
),
'table_prefix' => '',
),
...

Related

Virtualmin Database: connection timed out

I installed virtualmin on a VM instance using google compute engine. I have set up 1 domain and 1 subdomain. On the subdomain i created a database (flexijobs) and added the Drupal tables. Everything works fine except the connection to the database. On loading the drupal index page i get this error:
PDOException: SQLSTATE[HY000] [2002] Connection timed out
My guess is that the settings are not correct, so in a virtualmin setup, what should i use here, where can i set the password/user, what port should i use, what is the host?
$databases['default']['default'] = array (
'database' => 'flexijobs',
'username' => 'someUsername',
'password' => 'somePassword',
'prefix' => '',
'host' => 'db.somedomain.com',
'port' => '3306',
'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
'driver' => 'mysql',
);
Create a new database through Webmin > Servers > MariaDB Database Server
Note the name, username and password for the database you just made and modify the configuration file to include the information for your database.
The host field should be set to localhost
$databases['default']['default'] = array (
'database' => 'database_name',
'username' => 'db_username',
'password' => 'db_password',
'prefix' => '',
'host' => 'localhost',
'port' => '3306',
'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
'driver' => 'mysql',
);

Master Slave Configuration in Laravel 5.5

How to configure Laravel 5.5 with master slave MySQL replication ?
I want to make write operations and read operations in master and slave respectively .
Optional: Is there any way to do connection pooling and max / min no of open connections in ideal conditions. ?
Just change your config/database.php file to include read (slave) and write (master) hosts like so like the Laravel docs suggest:
'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' => '',
],

Where to configure MYSQL settings (such as sort_buffer_size) with Laravel's Homestead Vagrant Box?

During the process of trying to install a package to my Laravel project with Composer, I have encountered the following error:
SQLSTATE[HY001]: Memory allocation error: 1038 Out of sort memory, consider increasing server sort buffer size
Searching this error tells me I need to increase the sort_buffer_size setting, something which is usually done in the /etc/mysql/my.cnf file. However, I don't understand how to tune settings like this using Vagrant- or in particular, the Homestead box. Is this something which can be set in the homestead.yaml file?
As given in this link, you can pass the MySQL options as an array with key 'options' in [yourappname]/config/database.php file.
Example:
<?php
return array(
/* other settings removed for brevity */
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'options' => array(
'sort_buffer_size' => '100M', //set the value as desired
//other options you want to set
),
),
/* other connections removed for brevity */
)
)

Does yii2 have persistent connection options?

I can't find documentation about Yii2 persistent connection.
I have problems with Yii2 behavior. It always opens the connection and closes it after executing the query. I think creating a persistent connection is the answer to my problems.
How to do that?
See the following github issue:
'db' => array(
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=dbname',
'username' => 'root',
'password' => '',
'tablePrefix' => '',
'charset' => 'utf8',
'attributes'=>[
PDO::ATTR_PERSISTENT => true
]
),

How to replace environment-specific config, not cascade

I want to have a local development environment configuration for my database.php. I've created a new config director in my app and copied the database.php file into it. I'm using MongoDB and on production, I'm using a replica set with multiple servers, but locally, I'm just running a single server, so my connection info for production has more options:
Production:
...
'connections' => array(
'mongodb' => array(
'driver' => 'mongodb',
'host' => array('mongoDBA', 'mongoDBB'),
'port' => 27017,
'username' => 'myUserName',
'password' => 'myPassword',
'database' => 'theDatabase',
'options' => array('replicaSet' => 'myReplicaSet')
)
),
Local:
...
'connections' => array(
'mongodb' => array(
'driver' => 'mongodb',
'host' => 'localhost',
'port' => 27017,
'database' => 'theDatabase'
)
),
The problem is, when my local environment config loads, it merges the "connections" array. I want a way to completely replace the "mongodb" connection, so it's either one or the other, not both.
How can I accomplish this?
Create different set of configuration files:
app/config/local/database.php
app/config/production/database.php
And delete the file (or what you don't want of it):
app/config/database.php
And the you have to set your environment as
local
development
EDIT
This is how I do set my environment flawlessly, so I don't have to deal with hostnames and still don't get my local environment conflict with staging and production.
Create a .environment file in the root of your application and define your environment and add your sensitive information to it:
<?php
return array(
'APPLICATION_ENV' => 'development', /// this is where you will set your environment
'DB_HOST' => 'localhost',
'DB_DATABASE_NAME' => 'laraveldatabase',
'DB_DATABASE_USER' => 'laraveluser',
'DB_DATABASE_PASSWORD' => '!Bassw0rT',
);
Add it to your .gitignore file, so you don't risk having your passwords sent to Github or any other of your servers.
Right before $app->detectEnvironment, in the file bootstrap/start.php, load your .environment file to PHP environment:
foreach(require __DIR__.'/../.environment' as $key => $value)
{
putenv(sprintf('%s=%s', $key, $value));
}
And then you just have to use it:
$env = $app->detectEnvironment(function () {
return getenv('APPLICATION_ENV'); // your environment name is in that file!
});
And it will work everywhere, so you don't need to have separate dirs for development and production anymore:
<?php
return array(
'connections' => array(
'postgresql' => array(
'driver' => 'pgsql',
'host' => getenv('DB_HOST'),
'database' => getenv('DB_DATABASE_NAME'),
'username' => getenv('DB_DATABASE_USER'),
'password' => getenv('DB_DATABASE_PASSWORD'),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
),
),
);
Note that I don't set a fallback:
return getenv('APPLICATION_ENV') ?: 'local';
Because, if I don't set the file, I want it to fail on every server I deploy my app to.