Mysql一主多從和讀寫分離配置簡記

轉自:http://blog.csdn.net/chenjie19891104/article/details/7616665html

近期開發的系統中使用MySQL做爲數據庫,因爲數據涉及到Money,因此不得不慎重。同時,用戶對最大訪問量也提出了要求。爲了不Mysql成爲性能瓶頸並具有很好的容錯能力,特此實現主從熱備和讀寫分離。在此簡作紀要,以備往後所用!java

1、配置主從mysql

條件:兩臺PC,IP分別爲192.168.168.253,192.168.168.251。兩臺PC上的Mysql版本爲5.0。253上的Mysql爲Master,251上的Mysql爲Slave。sql

一、主數據庫服務器配置數據庫

進入主數據庫服務器安裝目錄,打開my.ini,在文件末尾增長以下配置:centos

#數據庫ID號, 爲1時表示爲Master,其中master_id必須爲1到232–1之間的一個正整數值; 
server-id = 1
#啓用二進制日誌;
log-bin=mysql-bin #須要同步的二進制數據庫名;
binlog-do-db=minishop #不一樣步的二進制數據庫名,若是不設置能夠將其註釋掉;
binlog-ignore-db=information_schema binlog-ignore-db=mysql binlog-ignore-db=personalsite binlog-ignore-db=test #設定生成的log文件名;
log-bin="D:/Database/materlog"
#把更新的記錄寫到二進制文件中;
log-slave-updates

保存文件。重啓Mysql服務。服務器

進入從數據庫服務器安裝目錄,打開my.ini,在文件末尾增長以下配置:併發

#若是須要增長Slave庫則,此id日後順延;
server-id = 2  
log-bin=mysql-bin #主庫host
master-host = 192.168.168.253
#在主數據庫服務器中創建的用於該從服務器備份使用的用戶
master-user = forslave master-password = ****** master-port = 3306
#若是發現主服務器斷線,從新鏈接的時間差;
master-connect-retry=60
#不須要備份的數據庫; 
replicate-ignore-db=mysql #須要備份的數據庫
replicate-do-db=minishop log-slave-update

 

保存文件。重啓Mysql服務。app

進入主數據庫服務器,建立上面備份使用的用戶名和密碼,同時受權replication slave,super和reload負載均衡

mysql>grant replication slave,super,reload on minishop.* to forslave@192.168.168.251 identified by '******';

進入從數據庫服務器,啓動Slave。

mysql>slave start; mysql>show slave status\G;

測試:進入主數據庫服務器,在Minishop中某張表中插入一條數據,而後到從數據庫服務器中查看是否含有剛剛插入的數據。完畢!

備註:1)運行配置後的主數據庫服務器先於從數據庫服務器,這樣運行從數據庫服務器時,主庫的 File 和 Position 與 從庫的上設置Master_Log_File、Read_Master_Log_Pos 就會一致。不然,可能出現不一致的狀況。這也能夠經過命令調整。

2)若是發現主從複製失敗時,能夠先關閉從數據庫服務器,而後刪除從數據庫服務器中data目錄下relay-log.info,hosname-relay-bin*,master.info等文件,重啓從服務器。


2、讀寫分離配置

本想採用Mysql Proxy來實現讀寫分離,奈何其使用的lua腳本着實讓人頭痛,最後決定採用國人開發的開源數據庫代理中間件Amoeba。使用Amoeba,只須要簡單的xml配置,就能夠很容易地實現讀寫分離。

Amoeba處於應用程序和數據庫服務器之間,充當一箇中間代理層。其支持負載均衡、高可用性、Query過濾、讀寫分離、可路由相關的query到目標數據庫、可併發請求多臺數據庫合併結果。功能很強大。

Amoeba默認的端口爲8066,實現了Mysql協議。應用程序中只須要修改一個數據庫鏈接就能夠實現採用Amoeba來代理數據庫訪問。比 如:java應用中,假如你原來的jdbc鏈接字符串爲:jdbc:mysql://192.168.168.42:3306/minishop,那麼現 在,你想使用Amoeba做爲數據庫訪問代理,則只須要將上面鏈接字符串改成以下(假如Amoeba所在機子IP爲 192.168.168.88):jdbc:mysql://192.168.168.88:8066/minishop。Amoeba透明性作的很贊。

主要仍是配置Amoeda,可是配置也是至關的簡單。基本只須要配置兩個文件:conf\dbServers.xml和conf\amoeba.xml。配置中各項的含義,能夠參考amoeda中文指南,這裏不作過多解釋。僅記錄下配置。

dbServers.xml主要配置

<amoeba:dbServers xmlns:amoeba="http://amoeba.meidusa.com/">

        <!-- Each dbServer needs to be configured into a Pool, If you need to configure multiple dbServer with load balancing that can be simplified by the following configuration: add attribute with name virtual = "true" in dbServer, but the configuration does not allow the element with name factoryConfig such as 'multiPool' dbServer -->
        
    <dbServer name="abstractServer" abstractive="true">
        <factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
            <property name="manager">${defaultManager}</property>
            <property name="sendBufferSize">64</property>
            <property name="receiveBufferSize">128</property>
                
            <!-- mysql port -->
            <property name="port">3306</property>
            
            <!-- mysql schema -->
            <property name="schema">minishop</property>
            
            <!-- mysql user -->
            <property name="user">chenjie</property>
            
            <!-- mysql password -->
            <property name="password">chenjie</property>

        </factoryConfig>

        <poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">
            <property name="maxActive">500</property>
            <property name="maxIdle">500</property>
            <property name="minIdle">10</property>
            <property name="minEvictableIdleTimeMillis">600000</property>
            <property name="timeBetweenEvictionRunsMillis">600000</property>
            <property name="testOnBorrow">true</property>
            <property name="testWhileIdle">true</property>
        </poolConfig>
    </dbServer>

    <dbServer name="master" parent="abstractServer">
        <factoryConfig>
            <!-- mysql ip -->
            <property name="ipAddress">192.168.168.253</property>
        </factoryConfig>
    </dbServer>

    <dbServer name="slave1" parent="abstractServer">
        <factoryConfig>
            <!-- mysql ip -->
            <property name="ipAddress">192.168.168.119</property>
        </factoryConfig>
    </dbServer>

    <dbServer name="slave2" parent="abstractServer">
        <factoryConfig>
            <!-- mysql ip -->
            <property name="ipAddress">192.168.168.251</property>

        </factoryConfig>
    </dbServer>
    
    <dbServer name="multiPool" virtual="true">
        <poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
            <!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
            <property name="loadbalance">1</property>
            
            <!-- Separated by commas,such as: server1,server2,server1 -->
            <property name="poolNames">slave1,slave2</property>
        </poolConfig>
    </dbServer>
        
</amoeba:dbServers>

amoeba.xml配置:

<amoeba:configuration xmlns:amoeba="http://amoeba.meidusa.com/">

    <proxy>
    
        <!-- service class must implements com.meidusa.amoeba.service.Service -->
        <service name="Amoeba for Mysql" class="com.meidusa.amoeba.net.ServerableConnectionManager">
            <!-- port -->
            <property name="port">8066</property>
            
            <!-- bind ipAddress -->

            <property name="ipAddress">192.168.168.253</property>

            
            <property name="manager">${clientConnectioneManager}</property>
            
            <property name="connectionFactory">
                <bean class="com.meidusa.amoeba.mysql.net.MysqlClientConnectionFactory">
                    <property name="sendBufferSize">128</property>
                    <property name="receiveBufferSize">64</property>
                </bean>
            </property>
            
            <property name="authenticator">
                <bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator">
                    
                    <property name="user">chenjie</property>
                    
                    <property name="password">chenjie</property>
                    
                    <property name="filter">
                        <bean class="com.meidusa.amoeba.server.IPAccessController">
                            <property name="ipFile">${amoeba.home}/conf/access_list.conf</property>
                        </bean>
                    </property>
                </bean>
            </property>
            
        </service>
        
        <!-- server class must implements com.meidusa.amoeba.service.Service -->
        <service name="Amoeba Monitor Server" class="com.meidusa.amoeba.monitor.MonitorServer">
            <!-- port -->
            <!-- default value: random number <property name="port">9066</property> -->
            <!-- bind ipAddress -->
            <property name="ipAddress">127.0.0.1</property>
            <property name="daemon">true</property>
            <property name="manager">${clientConnectioneManager}</property>
            <property name="connectionFactory">
                <bean class="com.meidusa.amoeba.monitor.net.MonitorClientConnectionFactory"></bean>
            </property>
            
        </service>
        
        <runtime class="com.meidusa.amoeba.mysql.context.MysqlRuntimeContext">
            <!-- proxy server net IO Read thread size -->
            <property name="readThreadPoolSize">20</property>
            
            <!-- proxy server client process thread size -->
            <property name="clientSideThreadPoolSize">30</property>
            
            <!-- mysql server data packet process thread size -->
            <property name="serverSideThreadPoolSize">30</property>
            
            <!-- per connection cache prepared statement size -->
            <property name="statementCacheSize">500</property>
            
            <!-- query timeout( default: 60 second , TimeUnit:second) -->
            <property name="queryTimeout">60</property>
        </runtime>
        
    </proxy>
    
    <!-- Each ConnectionManager will start as thread manager responsible for the Connection IO read , Death Detection -->
    <connectionManagerList>
        <connectionManager name="clientConnectioneManager" class="com.meidusa.amoeba.net.MultiConnectionManagerWrapper">
            <property name="subManagerClassName">com.meidusa.amoeba.net.ConnectionManager</property>
            <!-- default value is avaliable Processors <property name="processors">5</property> -->
        </connectionManager>
        <connectionManager name="defaultManager" class="com.meidusa.amoeba.net.MultiConnectionManagerWrapper">
            <property name="subManagerClassName">com.meidusa.amoeba.net.AuthingableConnectionManager</property>
            
            <!-- default value is avaliable Processors <property name="processors">5</property> -->
        </connectionManager>
    </connectionManagerList>
    
        <!-- default using file loader -->
    <dbServerLoader class="com.meidusa.amoeba.context.DBServerConfigFileLoader">
        <property name="configFile">${amoeba.home}/conf/dbServers.xml</property>
    </dbServerLoader>
    
    <queryRouter class="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter">
        <property name="ruleLoader">
            <bean class="com.meidusa.amoeba.route.TableRuleFileLoader">
                <property name="ruleFile">${amoeba.home}/conf/rule.xml</property>
                <property name="functionFile">${amoeba.home}/conf/ruleFunctionMap.xml</property>
            </bean>
        </property>
        <property name="sqlFunctionFile">${amoeba.home}/conf/functionMap.xml</property>
        <property name="LRUMapSize">1500</property>

        <property name="defaultPool">master</property>
        

        <property name="writePool">master</property>
        <property name="readPool">multiPool</property>

        <property name="needParse">true</property>
    </queryRouter>
</amoeba:configuration>

至此,Mysql主從熱備和讀寫分離配置完畢。可是,具體應用到生產環境究竟如何,還有待測試和考察。後來測試一主多從,又加入了一臺Mysql從數據庫服務器,這就是爲何上面amoeba配置中多了一個IP爲119的緣由。

另外,可據參考:Amoeba搞定mysql主從讀寫分離

CentOS下使用mysql proxy實現讀寫分離

相關文章
相關標籤/搜索