Mysql
 sql >> Database >  >> RDS >> Mysql

Magento può utilizzare più slave MySQL?

Dati i tuoi commenti sopra e supponendo che tu abbia la corretta configurazione della replica dei tuoi database.

Soluzione :-

- Passaggio 1:

In File

app/etc/config.xml

Trova il tag di chiusura "core_read"

<resources>
    ....
    <core_read>
            <connection>
                <use>default_read</use>
            </connection>
        </core_read>
    ....
</resources>

Aggiungi Dopo il tag di chiusura (tanti quanti database vuoi utilizzare) dovrebbe apparire come di seguito:

<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>

- Passaggio 2:

E aggiungi la nuova connessione nel tuo apt/etc/local.xml dopo ( tag di chiusura "/default_setup> " )

<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>

- Passaggio 3:

Copia il file da " app/code/core/Mage/Core/Model/Resource.php " ==TO ==> " app/code/local/Mage/Core/Model/Resource.php "

1- Trova $_mappedTableNames protetti;

2- Aggiungi questo metodo di seguito:

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- Modifica il metodo " public function getConnection($name) " in modo che appaia come di seguito:

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 !!
}

Ciò ti consentirà di utilizzare tutti i database specificati nel CODICE XML e PHP per la connessione core_read e la connessione default_setup per tutte le altre connessioni in magento ( core_write, core_setup )

Spero che questo risolva il tuo problema.