The following is part of my config for using a single slave.
<default_read>
<connection>
<use/>
<host><![CDATA[slavedb1.amazonaws.com]]></host>
<username><![CDATA[username]]></username>
<password><![CDATA[Password]]></password>
<dbname><![CDATA[shop]]></dbname>
<initStatements><![CDATA[SET NAMES utf8]]></initStatements>
<model><![CDATA[mysql4]]></model>
<type><![CDATA[pdo_mysql]]></type>
<pdoType><![CDATA[]]></pdoType>
<active>1</active>
</connection>
</default_read>
However, I want to use multiple slaves. Is this possible with Magento?
To clarify, I have already got a single master/slave setup working with Magento already. I want to add another slave so that I have two slaves. I am wondering how the config will change to make use of this second slave.
Magento uses MySQL database triggers to improve database access during reindexing. These get created when the indexer mode is set to schedule. Magento does not support any custom triggers in the Magento database because custom triggers can introduce incompatibilities with future Magento versions.
How does Connection to Database Works in Magento 2? In Magento 2, database connection settings are contained in the app/etc/env. php file: The path to this file is stored in \Magento\Framework\Config\File\ConfigFilePool class in a private array $applicationConfigFiles.
Magento fully supports the Nginx and Apache web servers.
Access my Magento database: So there are three ways to access database: Login in cPanel and clieck on phpmyadmin to access DB. Ask Host for Database direct URL and use your DB username, password to access DB. download third party software like mysql workbench to access DB.
Given your comments above and assuming you have correct replication setup of your Databases.
In File
Find "core_read" Closing tag
<resources>
....
<core_read>
<connection>
<use>default_read</use>
</connection>
</core_read>
....
</resources>
<resources>
....
<core_read>
<connection>
<use>default_read</use>
</connection>
</core_read>
<slave_db_1>
<connection>
<use>slave_one_db</use>
</connection>
</slave_db_1>
<slave_db_2>
<connection>
<use>slave_two_db</use>
</connection>
</slave_db_2>
....
</resources>
<resources>
....
<slave_one_db>
<connection>
<host><![CDATA[localhost]]></host>
<username><![CDATA[slave_one_db_user]]></username>
<password><![CDATA[slave_one_db_password]]></password>
<dbname><![CDATA[slave_db_one_name]]></dbname>
<initStatements><![CDATA[SET NAMES utf8]]></initStatements>
<model><![CDATA[mysql4]]></model>
<type><![CDATA[pdo_mysql]]></type>
<pdoType><![CDATA[]]></pdoType>
<active>1</active>
</connection>
</slave_one_db>
<slave_two_db>
<connection>
<host><![CDATA[localhost]]></host>
<username><![CDATA[slave_tow_db_user]]></username>
<password><![CDATA[slave_tow_db_password]]></password>
<dbname><![CDATA[slave_db_one_tow]]></dbname>
<initStatements><![CDATA[SET NAMES utf8]]></initStatements>
<model><![CDATA[mysql4]]></model>
<type><![CDATA[pdo_mysql]]></type>
<pdoType><![CDATA[]]></pdoType>
<active>1</active>
</connection>
</slave_two_db>
....
</resources>
1- Find protected $_mappedTableNames;
2- Add this method below :
public function getSlaveDb()
{
$prefix = 'slave_db_'; // prefix for the slaves databased in the xml file
$cookieExpireTime = 1209600; // 2 weeks Cookie ( database selection ) expire time
$dbArray = array(1,2); // All slaves Db in-case the cookie has invalid value
$slaveDb = array_rand( array_flip($dbArray),1 ); // How to alternate between databases ( in this demo i just use 2 database ) adjust the selection of the database to fit hoe many database you want to use !
if(!isset($_COOKIE['read_db']) || !in_array($_COOKIE['read_db'],$dbArray)) // Check for the cookie values
{
setcookie("read_db", $slaveDb, time()+$cookieExpireTime); // set the current database to the user in cookie so next time user use same connection to database ! to avoid jumping or hopping on different databases in short time
}else{
$slaveDb = $_COOKIE['read_db']; // return the database selected if the user has it in the cookies
}
return $prefix.$slaveDb;
}
3- Modify the method " public function getConnection($name) " to Look like below :
public function getConnection($name)
{
if($name =='core_read') // Only applied for READ Connections !!!
{
$name = $this->getSlaveDb(); // change the name of the connection to the one we get from our main method
}
//....... Leave the rest of the function as it is !!
}
This will allow you to use as many as databases you specify in the XML and PHP CODE for core_read connection and the default_setup connection for all other connections in magento ( core_write, core_setup )
Hope this solve your problem.
As far as I am aware it is not possible to use multiple slaves with Magento.
If you are using AWS you could maybe use larger instances for your master/slave setup?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With