Mysql一主多从和读写分离配置简记
Posted 专注it
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql一主多从和读写分离配置简记相关的知识,希望对你有一定的参考价值。
近期开发的系统中使用MySQL作为数据库,由于数据涉及到Money,所以不得不慎重。同时,用户对最大访问量也提出了要求。为了避免mysql成为性能瓶颈并具备很好的容错能力,特此实现主从热备和读写分离。在此简做纪要,以备日后所用!
一、配置主从
条件:两台PC,IP分别为192.168.168.253,192.168.168.251。两台PC上的Mysql版本为5.0。253上的Mysql为Master,251上的Mysql为Slave。
1、主数据库服务器配置
进入主数据库服务器安装目录,打开my.ini,在文件末尾增加如下配置:
- #数据库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服务。
进入主数据库服务器,创建上面备份使用的用户名和密码,同时授权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等文件,重启从服务器。
二、读写分离配置
本想采用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的原因。
以上是关于Mysql一主多从和读写分离配置简记的主要内容,如果未能解决你的问题,请参考以下文章
『青云志』青云QingCloud RDS服务全面升级支持一主多从和读写分离
MySQL主从(MySQL proxy Lua读写分离设置,一主多从同步配置,分库分表方案)