读写分离的中间件:
SQL解析-->规则处理-->SQL改写-->数据源选择-->执行SQL语句-->结果集返回合并处理
拆分中间件:
SQL解析-->规则处理-->数据源选择-->执行SQL语句-->结果集返回合并处理
192.168.5.110作为mysql的master
192.168.5.111作为mysql的slave
192.168.5.101安装amoeba实现读写分离
mysql主从复制:
安装mysql-5.5.33
1、准备数据存放的文件系统
新建一个逻辑卷,并将其挂载至特定目录即可。
挂载目录为/mydata,而后需要创建/mydata/data目录做为mysql数据的存放目录。
# chown -R mysql:mysql /mydata/data
3、安装并初始化mysql-5.5.33
# tar xf mysql-5.5.33-linux2.6-i686.tar.gz -C /usr/local# chown -R root .
4、为mysql提供主配置文件:
# cd /usr/local/mysql # cp support-files/my-large.cnf /etc/my.cnf 另外还需要在mysqld段添加如下行指定mysql数据文件的存放位置:datadir = /mydata/data
5、为mysql提供sysv服务脚本:
# cd /usr/local/mysql # cp support-files/mysql.server /etc/rc.d/init.d/mysqld# chmod +x /etc/rc.d/init.d/mysqld
添加至服务列表:
# chkconfig --add mysqld# chkconfig mysqld on
6、输出mysql的man手册至man命令的查找路径:
编辑/etc/man.config,添加如下行即可:MANPATH /usr/local/mysql/man
7、输出mysql的头文件至系统头文件路径/usr/include:
# ln -sv /usr/local/mysql/include /usr/include/mysql
8、输出mysql的库文件给系统库查找路径:
# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
而后让系统重新载入系统库:
# ldconfig
9、修改PATH环境变量,让系统可以直接使用mysql的相关命令。
master
mysql> grant replication slave,replication client on *.* to 'repuser'@'192.168.%.%' identified by 'reppass';Query OK, 0 rows affected (0.02 sec) mysql> flush privileges;Query OK, 0 rows affected (0.00 sec) mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000002 | 107 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.01 sec)
slave:
修slave的配置文件my.cnf
#log-bin=mysql-binrelay-log=rely-bin# binary logging format - mixed recommendedbinlog_format=mixed # required unique id between 1 and 2^32 - 1# defaults to 1 if master-host is not set# but will not function as a master if omitted#server-id = 1server-id = 11
mysql> change master to master_host='192.168.5.110',master_user='repuser',master_password='reppass',master_log_file='mysql-bin.000002',master_log_pos=107;Query OK, 0 rows affected (0.10 sec) mysql> start slave ;Query OK, 0 rows affected (0.01 sec)mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.5.110 Master_User: repuser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000002 Read_Master_Log_Pos: 107 Relay_Log_File: rely-bin.000002 Relay_Log_Pos: 253 Relay_Master_Log_File: mysql-bin.000002 Slave_IO_Running: Yes
安装jdk-1.8.0_20
# tar xf jdk-8u20-linux-i586.gz -C /usr/local/# ln -sv /usr/local/jdk1.8.0_20 /usr/local/java 导出java环境变量# vim /etc/profile.d/java.sh export JAVA_HOME=/usr/local/javaexport JRE_HOME=/usr/local/java/jreexport PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATHexport CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME:$JRE_HOME/lib:$CLASSPATH # java -versionjava version "1.8.0_20"Java(TM) SE Runtime Environment (build 1.8.0_20-b26)Java HotSpot(TM) Client VM (build 25.20-b23, mixed mode)# javac -versionjavac 1.8.0_20
安装amoeba实现数据读写分离:
http://www.sf.net/projects/amoeba
amoeba使用javacc研发,需要安装jdk环境
# mkdir /usr/local/amoeba# tar xf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba/# cd /usr/local/amoeba/在master授权mysql> grant all on *.* to 'amoeba'@'192.168.%.%' identified by 'amoeba';Query OK, 0 rows affected (0.12 sec) mysql> flush privileges;Query OK, 0 rows affected (0.20 sec)
配置amoeba.xml
# vim amoeba.xml <?xml version="1.0" encoding="gbk"?> <!DOCTYPE amoeba:configuration SYSTEM "amoeba.dtd"><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">127.0.0.1</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">root</property> <property name="password"></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><?xml version="1.0" encoding="gbk"?> <!DOCTYPE amoeba:configuration SYSTEM "amoeba.dtd"><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">3306</property> <!-- bind ipAddress --> <!-- <property name="ipAddress">127.0.0.1</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">amoeba</property> <property name="password">amoeba</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">readservers</property> <!-- <property name="writePool">server1</property> <property name="readPool">server1</property> --> <property name="needParse">true</property> </queryRouter></amoeba:configuration>
修改dbServer.xml
# vim dbServers.xml <?xml version="1.0" encoding="gbk"?> <!DOCTYPE amoeba:dbServers SYSTEM "dbserver.dtd"><amoeba:dbServers xmlns:amoeba="http://amoeba.meidusa.com/"> <!-- Each dbServer needs to be configured into a Pool, 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">test</property> <!-- mysql user --> <property name="user">amoeba</property> <property name="password">amoeba</property> <!-- mysql password <property name="password">password</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="testOnReturn">true</property> <property name="testWhileIdle">true</property> </poolConfig> </dbServer> <dbServer name="master" parent="abstractServer"> <factoryConfig> <!-- mysql ip --> <property name="ipAddress">192.168.5.110</property> </factoryConfig> </dbServer> <dbServer name="slave" parent="abstractServer"> <factoryConfig> <!-- mysql ip --> <property name="ipAddress">192.168.5.111</property> </factoryConfig> </dbServer> <dbServer name="readservers" 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">master,slave</property> </poolConfig> </dbServer> </amoeba:dbServers>
启动amoeba:
# ../bin/amoeba startlog4j:WARN log4j config load completed from file:/usr/local/amoeba/conf/log4j.xml2014-10-12 22:30:54,422 INFO context.MysqlRuntimeContext - Amoeba for Mysql current versoin=5.1.45-mysql-amoeba-proxy-2.2.0log4j:WARN ip access config load completed from file:/usr/local/amoeba/conf/access_list.conf2014-10-12 22:31:06,090 INFO net.ServerableConnectionManager - Amoeba for Mysql listening on 0.0.0.0/0.0.0.0:3306.2014-10-12 22:31:06,090 INFO net.ServerableConnectionManager - Amoeba Monitor Server listening on /127.0.0.1:30486.
在安装amoeba的服务器上打开一个新的虚拟终端登录安装mysql客户端之后,基于amoeba前端登录mysql
# mysql -h192.168.5.101 -uamoeba -pamoebaWelcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 22831918Server version: 5.1.45-mysql-amoeba-proxy-2.2.0 Source distributionCopyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || test |+--------------------+4 rows in set (0.13 sec)mysql> create database testdb; Query OK, 1 row affected (0.12 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | testdb | +--------------------+ 5 rows in set (0.01 sec)
登录master服务器验证:
mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || test || testdb |+--------------------+5 rows in set (0.00 sec)
登录slave服务器验证:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| testdb |
+--------------------+
5 rows in set (0.00 sec)