码迷,mamicode.com
首页 > 数据库 > 详细

Amoeba实现Mysql读写分离部署文档

时间:2014-07-08 08:46:45      阅读:289      评论:0      收藏:0      [点我收藏+]

标签:高可用   mysql复制   mysql读写分离   amoeba   

以下所有理解纯属个人理解,如若有误欢迎指出,不胜感激……o(∩_∩)o

两台服务器配置MYSQL主从复制实现数据高可用,这时读与写操作都有由master服务器来完成的,而从服务器只是复制了mster服务器的数据,这时可以利用一台服务器配置Amoeba实现mysql读写分离, master负责写,slave负责读取,当然 也可以有多个salve…… 从而减轻master服务器的压力,实现负载分摊;

 

拓扑图:

   

bubuko.com,布布扣

Mysql主从复制原理:

    两台mysql服务器一个作为master一个为slave;master上开启二进制日志,将自己所有使数据库发生改变的操作语句记录到而二进制日志中,slave端会开启一个IO线程向master请求masert二进制日志中的事件,master通过binlogdump线程将二进制日志事件发送给slave,slave将接收到的二进制日志时间保存在中继日志(relaylog)中,然后使用sql 线程之行中继日志;从而保证数据与master相同;

bubuko.com,布布扣

 

aomeba原理:

aomeba是使用java开发的,所以要首先配置jdk;mysql端配置主从复制,master上的数据复制到slave上,slave主要负责读;aomeba作为一个sql路由调度器,client发来的sql如果是write则分发到write dbserver上,如果是read ,则分发到read dbserver;

 

 

ip分配:

amoeba:192.168.1.49

master:192.168.1.39

slave:192.168.1.40

client:192.168.1.51

 

一:配置mysql主从复制

1.1:在主上创建测试数据库

1.2:在主上创建复制用户

1.3:配置主my.cnf

1.4:配置从my.cnf

1.5:锁表拷贝主服务器数据到从服务器

1.6:配置从mysql,开启复制

1.7:测试主从复制

二:配置Amoeba

2.1:在master、slave上创建用于amoeba连接数据库的用户

2.2:配置jdk

2.3:配置amoeba

2.4:启动amoeba

 

三:测试读写分离

 

1.1:在主上创建测试数据库

mysql> CREATE DATABASE `zrer90` CHARACTER SET utf8;

Query OK, 1 row affected (0.00 sec)

1.2:在主上创建复制用户

mysql> GRANT REPLICATION SLAVE ON *.* TO ‘slave‘@‘192.168.1.40‘ IDENTIFIED by ‘zrer90‘;

Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;   #刷新权限

Query OK, 0 rows affected (0.00 sec)

1.3:配置主my.cnf

server-id = 1

log-bin = /var/log/mysql/mysql-bin.log #二进制日志存放的位置,注意:mysql用户应对这个目录有写入权限;

expire_logs_days = 10 #二进制日志存放多少天之后删除;

max_binlog_size = 100M #二进制日志达到多大之后进行切割,生成新的日志;

 

重启mysql……

Mysql下查看master status:

mysql> show master status ;

+------------------+----------+--------------+------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000001 |      443 | zrer90       | mysql,test       |

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

1.4:配置从my.cnf

server-id = 2 #从服务器要与主服务器不同

#不需要开启binarylog

master-host = 192.168.1.39  #主服务器ip

master-user = slave         #用于同步复制的用户名(master上创建)

master-password = zrer90    #密码

replicate-do-db=zrer90     # 同步的数据库的名称

replicate-ignore-db=mysql   # 不复制mysql库

master-connect-retry=60   # 断点重新连接时间,60s

 

1.5:锁表拷贝主服务器数据到从服务器

Master mysql> flush tables with read lock ;  #锁表

Query OK, 0 rows affected (0.00 sec)

[root@master ~]# mysqldump -B -u root -p zrer90 > zrer90_db.sql  #备份zrer90数据库

Enter password:

[root@master ~]#

[root@master ~]# scp zrer90_db.sql  root@192.168.1.40:/root/  #将导出的sql文件上传到从服务器

root@192.168.1.40‘s password:

Master mysql> show master status ;   #记住binlog文件名与Position

+------------------+----------+--------------+------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000002 |      106 |              |                  |

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

Master mysql> unlock tables ;  #解锁

Query OK, 0 rows affected (0.00 sec)

 

#到从服务导入数据

[root@slave ~]# mysql -uroot -p <zrer90_db.sql

Slave mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| test               |

| zrer90             |

+--------------------+

4 rows in set (0.00 sec)

1.6:配置从mysql,开启复制

Slave mysql> change master to  master_log_file=‘mysql-bin.000002‘, master_log_pos=106;

Slave mysql> slave start ;

Query OK, 0 rows affected (0.00 sec)

Slave mysql> show slave status \G   #查看slave状态

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.1.39

                  Master_User: slave

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000002

          Read_Master_Log_Pos: 106

               Relay_Log_File: mysqld-relay-bin.000002

                Relay_Log_Pos: 251

        Relay_Master_Log_File: mysql-bin.000002

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

              Replicate_Do_DB: zrer90

          Replicate_Ignore_DB: mysql,test

           Replicate_Do_Table:

       Replicate_Ignore_Table:

      Replicate_Wild_Do_Table:

  Replicate_Wild_Ignore_Table:

                   Last_Errno: 0

                   Last_Error:

                 Skip_Counter: 0

          Exec_Master_Log_Pos: 106

              Relay_Log_Space: 407

              Until_Condition: None

               Until_Log_File:

                Until_Log_Pos: 0

           Master_SSL_Allowed: No

           Master_SSL_CA_File:

           Master_SSL_CA_Path:

              Master_SSL_Cert:

            Master_SSL_Cipher:

               Master_SSL_Key:

        Seconds_Behind_Master: 0

Master_SSL_Verify_Server_Cert: No

                Last_IO_Errno: 0

                Last_IO_Error:

               Last_SQL_Errno: 0

               Last_SQL_Error:

1 row in set (0.00 sec)

1.7:测试主从复制

在主zrer90 库中创建一张表table1,并录入数据;

Master mysql> use zrer90;

Database changed

Master mysql> CREATE TABLE `table1` (`id`  tinyint NOT NULL ,`name`  varchar(255) NULL ,PRIMARY KEY (`id`));

Query OK, 0 rows affected (0.07 sec)

Master mysql> INSERT INTO `table1` (`id`, `name`) VALUES (‘1‘, ‘zhangsan‘);

Query OK, 1 row affected (0.00 sec)

查看从库,数据已同步:

Slave mysql> use zrer90;

Database changed

Slave mysql> show tables;

+------------------+

| Tables_in_zrer90 |

+------------------+

| table1           |

+------------------+

1 row in set (0.00 sec)

Slave > select * from table1;

+----+----------+

| id | name     |

+----+----------+

|  1 | zhangsan |

+----+----------+

1 row in set (0.00 sec)

查看主master logs已更新位置;

Master mysql> show master status;

+------------------+----------+--------------+------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000002 |      389 |              |                  |

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

#也可以使用mysqlbinlog 命令去查看主上面的binlog和从上的relaylog进行对比;

配置Amoeba服务器

2.1:在master、slave上创建用于amoeba连接数据库的用户

mysql> grant all privileges on *.* to ‘amoeba_connect‘@‘192.168.1.49‘ identified by ‘zrer90‘; 

Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.01 sec)

2.2:配置jdk

[root@amoeba tmp]# tar -zxvf jdk-8u5-linux-x64.gz

[root@amoeba tmp]# mv jdk1.8.0_05/ /usr/local/jdk/

[root@amoeba tmp]# vim /etc/profile

export JAVA_HOME=/usr/local/jdk

AMOEBA_HOME=/usr/local/amoeba

export PATH=$AMOEBA_HOME/bin:$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH

[root@amoeba tmp]# source  /etc/profile

[root@amoeba tmp]# java -version  #测试java配置显示正常则成功

java version "1.8.0_05"

Java(TM) SE Runtime Environment (build 1.8.0_05-b13)

Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)

2.3:配置amoeba

[root@amoeba tmp]# mkdir /usr/local/amoeba

[root@amoeba tmp]# tar -zxvf amoeba-mysql-binary-2.2.0.tar.gz  -C /usr/local/amoeba

[root@amoeba ~]# vim /usr/local/amoeba/conf/dbServers.xml

调整dbServer.xml部分参数:

#默认连接到的库

<property name="schema">zrer90</property>

#amoeba连接数据库的帐号及密码

<property name="user">amoeba_connect</property>

<property name="password">zrer90</property>

#配置两个db服务器,分别是masert和slave

        <dbServer name="master"  parent="abstractServer">

           <factoryConfig>

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

           </factoryConfig>

        </dbServer>

        <dbServer name="slave"  parent="abstractServer">

           <factoryConfig>

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

           </factoryConfig>

        </dbServer>

       #配置dbserver组:ReadPool,指定组成员为slave

        <dbServer name="ReadPool" virtual="true">

<poolConfig

class="com.meidusa.amoeba.server.MultipleServerPool">

# loadbalance:负载方式1:轮询2:加权重3:HA

<property name="loadbalance">1</property>

#组成员为slave

<property name="poolNames">slave</property>

          </poolConfig>

        </dbServer>

[root@amoeba ~]# vim /usr/local/amoeba/conf/amoeba.xml

调整amoeba.xml部分参数:

        #amoeba监听端口3306,客户端请求时使用

        <property name="port">3306</property>

        <property name="authenticator">

<beanclass="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator">

        #指定客户端连接amoeba的账户与密码

<property name="user">amoeba</property>

<property name="password">zrer90</property>

<property name="filter">

#指定默认池

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

#指定写dbserver

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

#指定读dbserver

        <property name="readPool">ReadPool</property>

[root@amoeba ~]# vim /usr/local/amoeba/bin/amoeba

调整DEFAULT_OPTS变量参数:第58行

DEFAULT_OPTS="-server -Xms256m -Xmx256m -Xss128k"

更改后:

DEFAULT_OPTS="-server -Xms256m -Xmx256m –Xss256k"

2.4:启动amoeba

[root@amoeba amoeba]# amoeba start & #不想看提示信息的话将上&>/dev/null

[root@amoeba ~]# netstat -anpt  |grep java

tcp        0      0 ::ffff:127.0.0.1:21740      :::*                        LISTEN      2476/java          

tcp        0      0 :::3306                     :::*                        LISTEN      2476/java          

tcp        0      0 ::ffff:192.168.1.49:57069   ::ffff:192.168.1.39:3306    ESTABLISHED 2476/java          

tcp        0      0 ::ffff:192.168.1.49:37374   ::ffff:192.168.1.40:3306    ESTABLISHED 2476/java          

本地监听3306端口接受客户端请求,amoeba服务器本地与master192.168.1.39:3306,slave192.168.1.40:3306建立连接;

停止amoeba:

[root@amoeba amoeba]# amoeba stop

 

本文出自 “zrer(技术群:1991706)” 博客,请务必保留此出处http://zrer90.blog.51cto.com/8679896/1435177

Amoeba实现Mysql读写分离部署文档,布布扣,bubuko.com

Amoeba实现Mysql读写分离部署文档

标签:高可用   mysql复制   mysql读写分离   amoeba   

原文地址:http://zrer90.blog.51cto.com/8679896/1435177

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!