读写分离的中间件:

    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数据的存放目录。

    2、新建用户以安全方式运行进程:     # groupadd -r mysql     # useradd -g mysql -r -s /sbin/nologin -M -d /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
    # cd /usr/local/     # ln -sv mysql-5.5.33-linux2.6-i686  mysql     # cd mysql      # chown -R mysql:mysql  .     # scripts/mysql_install_db --user=mysql --datadir=/mydata/data

    # 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                                                                                             8066                                                                          ${clientConnectioneManager}                                                                                                 128                                        64                                                                                                                                                          root                                                                                                                                                                                          ${amoeba.home}/conf/access_list.conf                                                                                                                                                                                                                                                                                                  127.0.0.1                        true                        ${clientConnectioneManager}                                                                                                                                                                  20                                                 30                                                 30                                                 500                                                 60                                                                                  com.meidusa.amoeba.net.ConnectionManager                                                                                com.meidusa.amoeba.net.AuthingableConnectionManager                                                                                           3306                                                                          ${clientConnectioneManager}                                                                                                 128                                        64                                                                                                                                                          amoeba                                         amoeba                                                                                                                                                 ${amoeba.home}/conf/access_list.conf                                                                                                                                                                                                                                                                                                  127.0.0.1                        true                        ${clientConnectioneManager}                                                                                                                                                                  20                                                 30                                                 30                                                 500                                                 60                                                                                  com.meidusa.amoeba.net.ConnectionManager                                                                                com.meidusa.amoeba.net.AuthingableConnectionManager                                                                                          ${amoeba.home}/conf/dbServers.xml                                                                                         ${amoeba.home}/conf/rule.xml                                ${amoeba.home}/conf/ruleFunctionMap.xml                                                        ${amoeba.home}/conf/functionMap.xml                1500                master                master                readservers                                 true        

修改dbServer.xml

    

# vim dbServers.xml                                                                     ${defaultManager}                        64                        128                                                 3306                                                 test                                                 amoeba                        amoeba                                                                                  500                        500                        10                        600000                        600000                        true                        true                        true                                                                                                 192.168.5.110                                                                                                 192.168.5.111                                                                                                 1                                                 master,slave                         

启动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)