Chinaunix首页 | 论坛 | 博客
  • 博客访问: 122945
  • 博文数量: 22
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 187
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-03 21:11
文章分类
文章存档

2015年(5)

2014年(17)

分类: Mysql/postgreSQL

2014-12-29 10:17:13

一、基于amoeba做mysql的负载均衡
此处我一共用五台服务器做的负载均衡
master1     IP:192.168.10.161       test1
master2     ip:192.168.10.162        test2
slave1      ip:192.168.10.163        test3
slave2      ip:192.168.10.164        test4
还有一台amoeba服务器192.168.10.160
四台机器全部安装mysql,此处略去安装步骤,小伙伴门根据自己的习惯安装自己用的版本就好。我用的是percona数据库。
二、开始配置双主
1、[root@test1 ~]# vim /etc/my.cnf
   [mysqld]
   symbolic-links=0
   log-bin=mysql-bin
   server-id = 1
   log-slave-updates
   slave-skip-errors=all
   skip-name-resolve
   sync_binlog=1
   auto_increment_increment=2
   auto_increment_offset=1
   default-character-set=utf8
   其他配置略
启动数据库
[root@test1 ~]# mysql -uroot -p   
   查看主状态
   mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000005 |     3922 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
配置为162的从状态
mysql>change master to master_host='192.168.10.162', master_user='root', master_password='123456',  master_log_file='mysql-bin.000004', master_log_pos=3922;
启动从状态
mysql> start slave;
查看同步状态
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.10.162
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 3922
               Relay_Log_File: mysqld-relay-bin.000013
                Relay_Log_Pos: 251
        Relay_Master_Log_File: mysql-bin.000004
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
显示yes说明状态已经同步
授权其他数据库远程登录
mysql> grant all privileges on *.* to 'root'@'192.168.10.162,3,4' identified by '161';           
2、[root@test2 ~]# vim /etc/my.cnf
   [mysqld]
   symbolic-links=0
   default-character-set=utf8
   log-bin=mysql-bin
   server-id = 2
   log-slave-updates
   slave-skip-errors=all
   skip-name-resolve
   sync_binlog=1
   auto_increment_increment=2
   auto_increment_offset=2
启动并进入数据库
[root@test2 ~]# mysql -uroot -p
查看主状态
mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 |     3922 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)  
 配置为161的从库
 mysql> change master to master_host='192.168.10.161', master_user='root', master_password='161', master_log_file='mysql-bin.000005', master_log_pos=3922;
启动从状态
mysql> start slave;
查看同步状态
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.10.161
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000005
          Read_Master_Log_Pos: 3922
               Relay_Log_File: mysqld-relay-bin.000012
                Relay_Log_Pos: 251
        Relay_Master_Log_File: mysql-bin.000005
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
授权其他数据库远程登录
mysql> grant all privileges on *.* to 'root'@'192.168.10.161,3,4' identified by '162';             
3、[root@test3 ~]# vim /etc/my.cnf
   server-id       = 3
   relay-log-index = slave-relay-bin.index
   relay-log = slave-relay-bin
   [root@test3 ~]# service mysqld restart
进入mysql配置和主同步
mysql>change master to master_host='192.168.10.161', master_user='root', master_password='161', master_log_file='mysql-bin.000005', master_log_pos=3663;
启动从状态
mysql> start slave;
查看同步状态  
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.10.161
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000005
          Read_Master_Log_Pos: 3922
               Relay_Log_File: slave-relay-bin.000004
                Relay_Log_Pos: 251
        Relay_Master_Log_File: mysql-bin.000005
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
授权其他数据库远程登录
mysql> grant all privileges on *.* to 'root'@'192.168.10.161' identified by '163';            
4、[root@test4 ~]# vim /etc/my.cnf
   server-id       = 4
   relay-log-index = slave-relay-bin.index
   relay-log = slave-relay-bin
   [root@test4 ~]# service mysqld restart
进入mysql配置和主同步
change master to master_host='192.168.10.162', master_user='root', master_password='162', master_log_file='mysql-bin.000006', master_log_pos=3663;
启动从状态
mysql> start slave;
查看同步状态  
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.10.162
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 3922
               Relay_Log_File: slave-relay-bin.000004
                Relay_Log_Pos: 251
        Relay_Master_Log_File: mysql-bin.000004
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
授权其他数据库远程登录
mysql> grant all privileges on *.* to 'root'@'192.168.10.162' identified by '163';            
所有的数据库相互授权允许访问

数据库已经做完了可以进行验证了,如果验证成功了我们开始安装amoeba吧

二、安装amoeba配置负载均衡
假设amoeba的前提条件:
n         Java SE 1.5 或以上 Amoeba 框架是基于JDK1.5开发的,采用了JDK1.5的特性。
n         支持Mysql 协议版本10(mysql 4.1以后的版本)。
n         您的网络环境至少运行有一个mysql 4.1以上的服务
首先安装jdk环境此处略去

安装 Amoeba server:   192.168.10.160

[root@localhost amoeba]# unzip 101027160252.zip
解压后在目录下面的文件有主配置文件盒启动文件
[root@localhost conf]# ls
access_list.conf  amoeba.xml   function.dtd     log4j.dtd  rule.dtd             rule.xml
amoeba.dtd        amoeba.xml.  functionMap.xml  log4j.xml  ruleFunctionMap.xml
[root@localhost bin]# ls
amoeba  amoeba.bat  amoeba.classworlds
修改启动
[root@localhost bin]# vim amoeba
DEFAULT_OPTS="-server -Xms256m -Xmx256m -Xss512k"    #启动内存的大小太小会导致启动不了
修改主配置
[root@localhost conf]# vim amoeba.xml




       
               
                8066     #本机的监听端口

               
               
                192.168.10.160     #监听ip

               
                20

               
                30

               
                30

               
                128

               
                true

               
                user      #进入数据库的用户名

               
               
                123456      #密码


               
                60
       


       
       
               
                        com.meidusa.amoeba.net.AuthingableConnectionManager

                       
               

       


       
               
                                        ##定义服务1

                       
                       
                                defaultManager

                               
                                3306      

                               
                                192.168.10.161
                                mysql      #要修改的数据库

                               
                                root

                               

                               
                                123456


                       


                       
                       
                                200
                                200
                                10
                                600000
                                600000
                                true
                                true
                       

               


                                      #定义第二个服务

                       
                       
                        defaultManager

                       
                        3306

                       
                        192.168.10.162
                        mysql

                       
                        root

                       

                       
                        123456

               

                       
                       
                        200
                        200
                        10
                        600000
                        600000
                        true
                        true
                       

               


                                      #定义第三个服务

                       
                       
                        defaultManager

                       
                        3306

                       
                        192.168.10.163
                        mysql

                       
                        root

                       

                       
                        123456

               

                       
                       
                        200
                        200
                        10
                        600000
                        600000
                        true
                        true
                       

               

                
                                      #定义第四个服务

                       
                       
                        defaultManager

                       
                        3306

                       
                        192.168.10.164
                        mysql

                       
                        root

                       

                       
                        123456

               

                       
                       
                        200
                        200
                        10
                        600000
                        600000
                        true
                        true
                       

               


                               #此处配置读写负载
                       
                               
                                1

                               
                                server1,server2     #每个数据库负载次数
                       

               


                              #此处配置读写负载
                       
                               
                                1

                               
                                server3,server4     #每个数据库负载次数
                       

               


       


       
                ${amoeba.home}/conf/rule.xml
                ${amoeba.home}/conf/functionMap.xml
                ${amoeba.home}/conf/ruleFunctionMap.xml
                1500
                master1     #负载名称

                
                master1
                master2
                
                true
       


根据需求配置好负载后进行启动amoeba
[root@localhost bin]#./amoeba &
查看启动状态
[root@test3 ~]# netstat -tunpl |grep 8066
tcp        0      0 ::ffff:127.0.0.1:8066       :::*                        LISTEN      28537/java
2、进行测试数据并且和负载均衡
进入数据库
[root@test ~]# mysql -uroot -p -h127.0.0.1 -P8066
此时所有的状态已经全部同步,我们只允许两台主数据库有写入权限,我们插入一条数据
mysql> insert into wang values('1','zhang','this_is_master');
此时可以查看数据库的同步状态
mysql> select * from wang;
+------+-------+----------------+
| id   | name  | address        |
+------+-------+----------------+
|    1 | zhang | this_is_master |
+------+-------+----------------+

我们到test3服务器然后停掉test3的从服务,此时在插入数据查看
此时查看两次数据
mysql> select * from wang;
+------+-------+----------------+
| id   | name  | address        |
+------+-------+----------------+
|    1 | zhang | this_is_master |
|    2 | zhao  | this_is_master |
+------+-------+----------------+
2 rows in set (0.03 sec)
mysql> select * from wang;
+------+-------+----------------+
| id   | name  | address        |
+------+-------+----------------+
|    1 | zhang | this_is_master |
+------+-------+----------------+

如果都停掉从服务,我们插入数据都不会显示
可是测试一下
到这儿我们的amoeba基本已经做完了,希望可以帮助到大家,amoeba是mysql负载中最简单的还有一种MMM架构的自己在研究,等下一步的研究成果。

阅读(1907) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~