Magento Certified Developer GuideCategory: 4. DatabaseWhy and how does Magento differentiate between setup, read, and write database resources
Manish Prakash Staff asked 5 years ago

Magento provides different adapters for setup, read and write, the main reason being this is for allowing mysql master/slave configuration. With the capability of having different read/write adapter, we can provide different database for reading and writing. This is ideal for master/slave configuration in magento.

To provide different database options for read/write it can be done in app/etc/local.xml via

 <resources>
        <db>
            <table_prefix><![CDATA[]]></table_prefix>
        </db>
        <core_read>
            <connection>
                <use /> 
                <host><![CDATA[localhost]]></host>
                <username><![CDATA[root]]></username>
                <password><![CDATA[]]></password>
                <dbname><![CDATA[db_read_name]]></dbname>
                <initStatements><![CDATA[SET NAMES utf8]]></initStatements>
                <model><![CDATA[mysql4]]></model>
                <type><![CDATA[pdo_mysql]]></type>
                <pdoType><![CDATA[]]></pdoType>
                <active>1</active>
            </connection>
        </core_read>
        <core_write>
            <connection>
                <use />
                <host><![CDATA[localhost]]></host>
                <username><![CDATA[root]]></username>
                <password><![CDATA[]]></password>
                <dbname><![CDATA[db_write_name]]></dbname>
                <initStatements><![CDATA[SET NAMES utf8]]></initStatements>
                <model><![CDATA[mysql4]]></model>
                <type><![CDATA[pdo_mysql]]></type>
                <pdoType><![CDATA[]]></pdoType>
                <active>1</active>
            </connection>
        </core_write>
    </resources>

usually we only define default_setup in local.xml

All the 3 different connections are initialized in ‘core/resource’ getConnection function