mysql读写分离

近来公司做了一个计费系统,每天产生的CDR数据较多,数据读写要求比较高,在此基础上,采用了数据读写分离策略,来缓解数据库的压力。

应用环境

1、mysql-proxy,采用了开发框架amoeba,mysql-proxy负责管理client实际访问database的路由策略,实现数据的读写分离。

2、数据库集群环境,database集群采用了mysql的master-slave的replication方案。

3、客户端的应用程序,基于c3p0连接mysql-proxy。


结构示意图


mysql读写分离
 

实战

一、搭建mysql的master-slave环境

1、分别在192.168.1.132(master)和192.168.1.128(slave)安装mysql数据库,本实例在CentOS5.3的环境下,mysql数据库为mysql-5.1.51.tar.gz版本。

2、配置master

    a、修改master的/etc/my.cnf文件,添加

          log-bin=mysql-bin                                              #slave会基于此log-bin来做replication
          server-id=1                                                        #master的标示
          binlog-do-db = billing                                         #用于master-slave的具体数据库

 

    b、开启mysql的日志文件,方便排错(可选)

          log-error=/usr/local/mysql/log/error.log                       #错误日志 
          log=/usr/local/mysql/log/mysql.log                               #查询日志
          long_query_time=2                                                      #慢查询日志,指执行超过多久的sql会被log下来,这里是2秒
          log-slow-queries=/usr/local/mysql/log/slowquery.log  #将查询返回较慢的语句进行记录

    c、为slave添加一个replication用户,授权

         mysql>grant replication slave on *.* to [email protected] identified by '123456'

    d、重启mysql服务,查看master的状态

mysql> show master status;
+-----------------------+------------+-------------------+-----------------------+
| File                        | Position   | Binlog_Do_DB | Binlog_Ignore_DB |
+-----------------------+------------+-------------------+-----------------------+
| mysql-bin.000005 |      106     | billing              |                              |
+-----------------------+------------+-------------------+-----------------------+
1 row in set (0.00 sec)

  

3、配置salve

   a、修改slave的/etc/my.cnf文件,添加
          server-id=2                                                        #slave的标示

 

   b、开启mysql的日志文件,方便排错(可选)

          log-error=/usr/local/mysql/log/error.log                       #错误日志 
          log=/usr/local/mysql/log/mysql.log                               #查询日志
          long_query_time=2                                                      #慢查询日志,指执行超过多久的sql会被log下来,这里是2秒
          log-slow-queries=/usr/local/mysql/log/slowquery.log  #将查询返回较慢的语句进行记录

   c、重启mysql服务,配置与master的连接

mysql> CHANGE MASTER TO
    -> MASTER_HOST='192.168.1.132',
    -> MASTER_USER='repl',
    -> MASTER_PASSWORD='123456',
    -> MASTER_LOG_FILE='mysql-bin.000005',
    -> MASTER_LOG_POS=106;

注:MASTER_HOST是master机的ip,MASTER_USER和MASTER_PASSWORD就是我们刚才在master上添加的用户,MASTER_LOG_FILE和MASTER_LOG_POS对应与master status里的信息

   d、启动slave

mysql>start slave;

 

4、验证master-salve环境是否搭配成功

通过查看slave日志(/usr/local/mysql/log/error.log)

有如下信息则表示成功

100703 10:51:42 [Note] Slave I/O thread: connected to master '[email protected]:3306',  replication started in log 'mysql-bin.000005' at position 106

没有成功则通过日志文件查看并解决问题

 

二、搭建mysql-proxy环境

本实例采用了amoeba作为mysql的代理服务器,版本为amoeba-mysql-1.2.0-GA,下装与安装详细参见官方本档,proxy服务器的IP为192.168.1.120

配置amoeba,/usr/local/amoeba-mysql-1.2.0-GA/conf/amoeba.xm

a、提供客户端应用程序的数据库配置

	<server>
		<!-- proxy server绑定的端口 -->
		<property name="port">8066</property>
		
		<!-- proxy server绑定的IP -->
		<!-- 
		<property name="ipAddress">127.0.0.1</property>
		 -->
		<!-- 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>
		
		<!-- socket Send and receive BufferSize(unit:K)  -->
		<property name="netBufferSize">128</property>
		
		<!-- Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm). -->
		<property name="tcpNoDelay">true</property>
		
		<!-- 对外验证的用户名 -->
		<property name="user">root</property>
		
		<!-- 对外验证的密码 -->
		
		<property name="password">123abc</property>
		
	</server>

 

 b、配置proxy与数据库服务器之间的连接

<dbServerList>
		<!-- 
			一台mysqlServer 需要配置一个pool,
			如果多台 平等的mysql需要进行loadBalance, 
			平台已经提供一个具有负载均衡能力的objectPool:com.meidusa.amoeba.mysql.server.MultipleServerPool
			简单的配置是属性加上 virtual="true",该Pool 不允许配置factoryConfig
			或者自己写一个ObjectPool。
		-->
		<dbServer name="server1">
			
			<!-- PoolableObjectFactory实现类 -->
			<factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
				<property name="manager">defaultManager</property>
				
				<!-- 真实mysql数据库端口 -->
				<property name="port">3306</property>
				
				<!-- 真实mysql数据库IP -->
				<property name="ipAddress">192.168.1.132</property>
				<property name="schema">billing</property>
				
				<!-- 用于登陆mysql的用户名 -->
				<property name="user">root</property>
				
				<!-- 用于登陆mysql的密码 -->
				
				
				<property name="password">123456</property>
				
				
			</factoryConfig>
			
			<!-- ObjectPool实现类 -->
			<poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">
				<property name="maxActive">200</property>
				<property name="maxIdle">200</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="multiPool1" virtual="true">
			<poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
				<!-- 负载均衡参数 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
				<property name="loadbalance">1</property>
				
				<!-- 参与该pool负载均衡的poolName列表以逗号分割 -->
				<property name="poolNames">server1</property>
			</poolConfig>
		</dbServer>
		
		<dbServer name="server2">
			
			<!-- PoolableObjectFactory实现类 -->
			<factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
				<property name="manager">defaultManager</property>
				
				<!-- 真实mysql数据库端口 -->
				<property name="port">3306</property>
				
				<!-- 真实mysql数据库IP -->
				<property name="ipAddress">192.168.1.128</property>
				<property name="schema">billing</property>
				
				<!-- 用于登陆mysql的用户名 -->
				<property name="user">root</property>
				
				<!-- 用于登陆mysql的密码 -->
				
				
				<property name="password">123456</property>
				
				
			</factoryConfig>
			
			<!-- ObjectPool实现类 -->
			<poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">
				<property name="maxActive">200</property>
				<property name="maxIdle">200</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="multiPool2" virtual="true">
			<poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
				<!-- 负载均衡参数 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
				<property name="loadbalance">1</property>
				
				<!-- 参与该pool负载均衡的poolName列表以逗号分割 -->
				<property name="poolNames">server2</property>
			</poolConfig>
		</dbServer>
		
	</dbServerList>

 

c、配置proxy读写分离

写道
<queryRouter class="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter">
<property name="ruleConfig">${amoeba.home}/conf/rule.xml</property>
<property name="functionConfig">${amoeba.home}/conf/functionMap.xml</property>
<property name="ruleFunctionConfig">${amoeba.home}/conf/ruleFunctionMap.xml</property>
<property name="LRUMapSize">1500</property>
<property name="defaultPool">server1</property>
<property name="writePool">server1</property>
<property name="readPool">server2</property>
<property name="needParse">true</property>
</queryRouter>

  

d、运行mysql-proxy

[root@localhost etc]#./usr/local/amoeba-mysql-1.2.0-GA/bin/amoeba

有如下提示表示启动成功

[app@SIMBANK /]$ ./home/app/java/amoeba-mysql-1.2.0-GA/bin/amoeba
log4j:WARN log4j config load completed from file:/home/app/java/amoeba-mysql-1.2.0-GA/conf/log4j.xml
log4j:WARN ip access config load completed from file:/home/app/java/amoeba-mysql-1.2.0-GA/conf/access_list.conf
2011-05-13 11:37:54,888 INFO  net.ServerableConnectionManager - Server listening on 0.0.0.0/0.0.0.0:8066.

  

 

三、测试

a、编辑测试程序,本实例采用基于SSH框架的应用程序进行测试,具体代码略,数据库连接配置如下:

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://192.168.1.120:8066/billing" />
<property name="user" value="root" />
<property name="password" value="123abc" />
<property name="minPoolSize" value="1" />
<property name="maxPoolSize" value="1" />
<property name="maxIdleTime" value="1800" />
<property name="acquireIncrement" value="1" />
<property name="maxStatements" value="0" />
<property name="initialPoolSize" value="1" />
<property name="idleConnectionTestPeriod" value="1800" />
<property name="acquireRetryAttempts" value="6" />
<property name="acquireRetryDelay" value="1000" />
<property name="breakAfterAcquireFailure" value="false" />
<property name="testConnectionOnCheckout" value="true" />
<property name="testConnectionOnCheckin" value="false" />
</bean>

 

b、测试插入数据

Hibernate: 
    insert 
    into
        tf_user
        (name, password) 
    values
        (?, ?)

 

查看master日志文件

写道
5 Query SHOW COLLATION
5 Query set names latin1
5 Query SET NAMES latin1
5 Query SET character_set_results = NULL
5 Query SET autocommit=1
5 Query SHOW FULL TABLES FROM `amoeba_study` LIKE 'PROBABLYNOT'
5 Query SHOW FULL TABLES FROM `amoeba_study` LIKE 'PROBABLYNOT'
5 Query SET autocommit=0
5 Query insert into tf_user (name, password) values ('china', 'china')
5 Query commit
5 Query SET autocommit=1

 

查看slave日志文件

110513 11:55:32     5 Query     SELECT @@session.tx_isolation
110513 11:55:33     2 Query     insert into tf_user (name, password) values ('china', 'china')

 说明实现了master上的writer操作,同时master向slave进行了replication操作

 

c、查询数据

Hibernate: 
    select
        this_.id as id6_0_,
        this_.name as name6_0_,
        this_.password as password6_0_ 
    from
        tf_user this_

 

查看slave日志文件

写道
110513 12:00:11 5 Query SELECT @@session.tx_isolation
5 Query select this_.id as id6_0_, this_.name as name6_0_, this_.password as password6_0_ from tf_user this_

 

综上所述,一个简单的mysql读写分离项目已经完成。

你可能感兴趣的:(sql,mysql,log4j,应用服务器,server,配置管理)