MySQL主从复制技术与读写分离技术amoeba应用
前言:目前在搭建一个人才网站,预计流量会很大,需要用到分布式数据库技术,MySQL的主从复制+读写分离技术。读写分离技术有官方的MySQL-proxy,阿里巴巴的Amoeba。Amoeba能在阿里巴巴这么大流量的平台投入使用而且运行稳定,Amoeba的性能是很优越的,相信眼前事实,所以选择了Amoeba。
一、名词解析
1. 主从复制。
将主服务器上的数据复制到从服务器上,保护数据免受意外的损失。
2.Amoeba
Amoeba(变形虫)项目,专注分布式数据库 proxy 开发。座落与Client、DB Server(s)之间。对客户端透明。具有负载均衡、高可用性、sql过滤、读写分离、可路由相关的query到目标数据库、可并发请求多台数据库合并结果。
二、环境资料
假设amoeba的前提条件:
1. 实验环境:
System: CentOS release 6.5
JDK: jdk-6u35-linux-x64.bin
Mysql版本: 5.5.38
Amoeba版本: amoeba-mysql-3.0.5-RC-distribution
2. 架构图:
主服务器,用于写数据库, Master server: 192.168.1.106
从服务器,用于读数据库,Slave server: 192.168.1.100
读写分离的代理服务器, Amoeba server: 192.168.1.105
三、架设MySQL的主从复制服务器
1. 主服务器Master server数据库配置:
我的mysql 安装在/usr/local/mysql
#vi /etc/my.cnf
设置 server-id = 1 ,此值不能和从数据库的一样
在my.cnf末尾添加如下语句,表示哪些库不同步:
binlog-ignore-db=mysql #每个不同步的库写一行
#service mysqld restart
接着分配一个数据库账号给Slave Server 和 Amoeba Server:
#mysql -uroot -p123456
mysql> grant replication slave on *.* to ‘idc719comslave’@’192.168.1.100’ identified by ‘123456’
mysql> grant all on *.* to ‘proxyuser’@’192.168.1.105’ identified by ‘123456’
mysql> flush privileges;
mysql> show master status;
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000028 | 107 | | mysql |
+------------------+----------+--------------+------------------+
1 row in set (0.01 sec)
记录File的mysql-bin.000028 与 Position的107 ,等会要用到。
2. 从服务器Slave server数据库配置:
#vi /etc/my.cnf
replicate-do-db=test
replicate-ignore-db=mysql
replicate-ignore-db=persondb
#service mysqld restart
#mysql -uroot -p123456
mysql> grant all on *.* to ‘proxyuser’@’192.168.1.105’ identified by ‘123456’;
mysql> CHANGE MASTER TO
-> Master_Host=192.168.1.106,
-> Master_User=idc719comslave,
-> Master_Password=123456,
-> Master_Port=3306,
-> Master_Log_File=mysql-bin.000028,
-> Master_Log_File=107;
mysql> start slave;
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.106
Master_User: idc719comslave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000028
Read_Master_Log_Pos: 107
Relay_Log_File: slaveserver1-relay-bin.000016
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000028
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: test
Replicate_Ignore_DB: mysql
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: 107
Relay_Log_Space: 416
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:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
1 row in set (0.00 sec)
看到Slave_IO_State: Waiting for master to send event 表示成功了!
如果不成功, 比如
Last_IO_Errno: 2003
、Last_IO_Error: error connecting to master ‘idc719comslave@192.168.1.106:3306‘ - retry-time: 60 retries: 86400
是主服务器开了防火墙,或者用户密码错误!
三、架设 Amoeba 服务器
1. 安装JDK
Amoeba是java编写的,运行需要JDK环境,可以通过#echo $JAVA_HOME看是否安装配置了JDK,如果没有,参考如下方法:
下载软件包
uname -r 检查系统内核信息
mkdir -p /tmp/src && cd /tmp/src
x86_64 请下载
wget http://docs.minunix.com/web/jdk-6u35-linux-x64.bin
i386系列 请下载
wget http://docs.minunix.com/web/jdk-6u35-linux-i586.bin
注:作者系统为X86_64 ,所以以x86_64 演示,步骤都一样的!
安装必备组件信息:
yum -y install glibc*
下载完成之后,修改文件属性,赋予可执行权限,然后执行程序予以安装:
chmod +x jdk-6u35-linux-x64.bin
./jdk-6u35-linux-x64.bin
注:此处可能会出现以下问题:
/lib/ld-linux.so.2: bad ELF interpreter: No such file or directory
此问题是因为您没有安装glibc-* ,只需要安装glibc 即可解决,
yum -y install glibc*
安装完成之后,执行下列命令:
#cp -rf jdk1.6.0_35 /usr/local/jdk
#vim /etc/profile.d/java.sh \\\输入下列内容
JAVA_HOME="/usr/local/jdk"
CLASS_PATH="$JAVA_HOME/lib:$JAVA_HOME/jre/lib"
PATH=".:$PATH:$JAVA_HOME/bin"
export JAVA_HOME
###保存退出,并执行下面的命令使配置生效
#source /etc/profile.d/java.sh
#java -version \\\检查java版本
2.安装Amoeba
下载页面:http://sourceforge.net/projects/amoeba/?source=directory
下载后,解压 #unzip amoeba-mysql-3.0.5-RC-distribution.zip -d /usr/local/
#cd /usr/local
#mv amoeba-mysql-3.0.5-RC-distribution amoeba
#cd amoeba
#ls
benchmark bin conf jvm.properties lib logs
#cd conf
#vim amoeba.xml
查找如下字段并修改红色的部分,分别是端口、amoeba主机的ip,用户密码,和读写服务器的别名,其余字段默认就可以
<property name="port">8066</property> <property name="ipAddress">192.168.1.105</property> <property name="user">root</property> <property name="password">123456</property> <property name="writePool">server1</property> <property name="readPool">server2</property> |
#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, If you need to configure multiple dbServer with load balancing that can be simplified by the following configuration: add attribute with name virtual = "true" in dbServer, but the configuration does not allow the element with name factoryConfig such as ‘multiPool‘ dbServer -->
<dbServer name="abstractServer" abstractive="true"> <factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory"> <property name="connectionManager">${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">proxyuser</property>
<property name="password">123456</property> </factoryConfig>
<poolConfig class="com.meidusa.toolkit.common.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="server1" parent="abstractServer"> <factoryConfig> <!-- mysql ip --> <property name="ipAddress">192.168.1.106</property> </factoryConfig> </dbServer>
<dbServer name="server2" parent="abstractServer"> <factoryConfig> <!-- mysql ip --> <property name="ipAddress">192.168.1.100</property> </factoryConfig> </dbServer>
<dbServer name="multiPool" 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">server1</property> </poolConfig> </dbServer>
</amoeba:dbServers>
|
文字出自:http://blog.csdn.net/ljuncong