|
简介:
Amoeba是一个以MySQL为底层数据存储,并对应用提供MySQL协议接口的proxy。它集中地响应应用的请求,依据用户事先设置的规则,将SQL请求发送到特定的数据库上执行。基于此可以实现负载均衡、读写分离、高可用性等需求。
拓扑图:
搭建mysql的主从复制环境:
ip地址为192.168.150.135作为MASTER
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
| 修改my.cnf
[iyunv@centos-server ~]# vim/etc/my.cnf
[mysqld]
port = 3306
binlog-do-db=dragon #同步的数据库
replicate-ignore-db=mysql #不同步的数据库
replicate-ignore-db=information_schema
server-id= 1 #server-id要唯一
log-bin=mysql-bin
mysql> show variables like "server_id";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 1 |
+---------------+-------+
1 row in set (0.01 sec)
mysql> create database dragon;
Query OK, 1 row affected (0.08 sec)
mysql> grant replication slave on *.* to 'back'@'192.168.150.%' identified by '123456';
1 row in set (0.01 sec) #添加用户"back",SLAVE用于登录本机复制数据库日志文件
mysql> show master status; #查看MASTER上dragon库的日志的状态信息
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000025 | 560 | dragon | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
exit
[iyunv@centos-server ~]# cd /usr/local/mysql/var/
[iyunv@centos-server var]# tar zcf dragon.tar.gz dragon/
scp dragon.tar.gz root@192.168.150.137:/usr/local/mysql/var/ #把数据库复制到SLAVE上
|
ip192.168.150.137做为SLAVE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
| 修改my.cnf的server-id为2
[iyunv@centos-server ~]# cd /usr/local/mysql/var/
[iyunv@centos-server ~]#tar xf dragon.tar.gz #先解压缩复制过来的数据库文件
[iyunv@centos-server ~]#mysql -uroot -pqwe123
mysql> slave stop;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> change master to master_host='192.168.150.135',master_user='back',master_password='123456',master_log_file='mysql-bin.000025',master_log_pos=560; #使用用户"back"复制MASTER的"mysql-bin.000025"日志
Query OK, 0 rows affected (0.03 sec)
mysql> slave start;
Query OK, 0 rows affected (0.01 sec)
mysql> exit
Bye
[iyunv@centos-server ~]# service mysqld restart #重启SLAVE的mysql
[iyunv@drbd2 ~]# mysql -uroot -pqwe123
mysql> show slave statusG #查看SLAVE是否正常同步,主要看"Slave_IO_Running"和"Slave_SQL_Running"是否为yes
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.150.135
Master_User: back
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000025
Read_Master_Log_Pos: 560
Relay_Log_File: centos-server-relay-bin.000004
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000025
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
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: 560
Relay_Log_Space: 414
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)
|
部署amoeba:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| amoeba是基于jdk环境运行的,所以要先安装jdk
[iyunv@drbd1 ~]# mv jdk1.6.0_31/ /usr/local/
[iyunv@drbd1 ~]# cat >/etc/profile.d/jdk.sh<<end
JAVA_HOME=/usr/local/jdk1.6.0_31
PATH=$JAVA_HOME/bin:$PATH
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME PATH CLASSPATH
end
[iyunv@centos-server ~]# java -version #测试jdk安装成功
java version "1.6.0_31"
Java(TM) SE Runtime Environment (build 1.6.0_31-b04)
Java HotSpot(TM) 64-Bit Server VM (build 20.6-b01, mixed mode)
[iyunv@centos-server ~]# unzip amoeba-mysql-3.0.5-RC-distribution.zip
[iyunv@centos-server ~]# mv amoeba-mysql-3.0.5-RC /usr/local/amoeba
[iyunv@centos-server amoeba]# vim jvm.properties #做如下修改,不然启动amoeba会报错
#JVM_OPTIONS="-server -Xms256m -Xmx1024m -Xss196k -XX:PermSize=16m -XX:MaxPermSize=96m"
JVM_OPTIONS="-server -Xms256m -Xmx1024m -Xss256k -XX:PermSize=16m -XX:MaxPermSize=96m"
|
配置amoeba:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
| 主要通过"conf"目录下的dbServer.xml和amoeba.xml设置amoeba调度mysql请求:
[iyunv@centos-server conf]#vim dbServer.xml
${defaultManager}
64
128
3306 #连接mysql的默认端口
dragon #设置amoeba默认连接的数据库名
amoba #amoeba连接后端mysql的帐号和密码,后端mysql必须创建
qwe123
500 #设置最大连接数,默认是500
500 #配置最大空闲连接数
1 #配置最少空闲连接数
600000
600000
true
true
true
#定义一个dbserver,名字自定义,后面会用到
192.168.150.135 #dbserver的IP address
192.168.150.137
#定义一个dbserver组
#调度算法:1.复制均衡,2.表示权重,3.表示HA。
2
slave1 #设置dbserver成员,根据需要可加多个
[iyunv@centos-server conf]#vim amoeba.xml
8066 #amoeba负责接受请求侦听的端口
128
64
root #客户机连接ameba使用的帐号和密码
qwe123
${amoeba.home}/conf/access_list.conf
128
500
utf8
60
com.meidusa.toolkit.net.AuthingableConnectionManager
${amoeba.home}/conf/dbServers.xml
${amoeba.home}/conf/rule.xml
${amoeba.home}/conf/ruleFunctionMap.xml
${amoeba.home}/conf/functionMap.xml
1500
writedb
writedb #在dbServer定义的 writedb响应写操作
slave1 #在dbServer定义的 slave1响应读操作
true
|
在后端mysql添加amobe帐号:
1
2
3
4
| mysql> grant ALL ON *.* TO 'amoba'@'192.168.150.128' identified by 'qwe123';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
|
启动amoeba: 1
| [iyunv@centos-server ]# /usr/local/amoeba/bin/./launcer
|
测试读写:
[iyunv@drbd2 conf]# mysql -uroot -pqwe123 -h192.168.150.128 -P8066
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 714931905
Server version: 5.1.45-mysql-amoeba-proxy-3.0.4-BETA Source distribution
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
mysql>
mysql> use dragon;
Database changed
mysql> create table user(ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
-> name VARCHAR(20) NOT nULL,
-> sex BOOLEAN);
Query OK, 0 rows affected (0.05 sec)
mysql> insert into user(name,sex) VALUES ('dragon',1);
Query OK, 1 row affected (0.05 sec)
mysql> insert into user(name,sex) VALUES ('dragon',1),('bbq',1);
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from user;
+----+--------+------+
| ID | name | sex |
+----+--------+------+
| 1 | dragon | 1 |
| 2 | dragon | 1 |
| 3 | bbq | 1 |
+----+--------+------+
3 rows in set (0.03 sec)
</end
|
|