MySQL负载均衡架构图如下:
一、分别在192.168.150.130和192.168.150.131上安装mysql
二、配置master(192.168.150.130)
1、编辑/etc/my.cnf,添加如下(如etc没my.cnf,cp /usr/local/mysql/support-files/my-medium.cnf /etc/my.cnf)
log-bin=mysql-bin #slave会基于此log-bin来做replication
server-id=1 #master的标识,说明是主数据库
binlog-do-db = mark #用于主从同步数据库
2、赋予权限给slave服务器的slave数据库
mysql> GRANT REPLICATION SLAVE ON *.* TO [email protected] IDENTIFIED BY '111111';
3、重启mysql,使得配置生效
/usr/local/mysql/bin/mysqladmin shutdown
/usr/local/mysql/bin/mysqld_safe --user=mysql& 或
/usr/local/mysql/bin/safe_mysqld
4、查看master状态
mysql>show master status;
File Position Binlog_Do_DB Binlog_Ignore_DB
mysql-bin.000003 98 mark
三、配置slave(192.168.150.131)
1、编辑/etc/my.cnf,添加以下配置:(如etc没my.cnf,cp /usr/local/mysql/support-files/my-medium.cnf /etc/my.cnf)
server-id=2 #slave的标识
2、连接主数据库(master),并启动
mysql> stop slave;
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.150.130',
-> MASTER_USER='root',
-> MASTER_PASSWORD='111111',
-> MASTER_LOG_FILE='mysql-bin.000003',
-> MASTER_LOG_POS=98;
mysql> start slave;
mysql> show slave status\G # 查看slave状态
其中MASTER_HOST是master机的ip,
MASTER_USER和MASTER_PASSWORD就是我们刚才在master上添加的用户,
MASTER_LOG_FILE和MASTER_LOG_POS对应与master status里的信息
3、如果出现类似字样,如
the server is not configured as slave。。。。
解决方案:
mysql> set global server_id = 2;
4、验证master-slave搭建生效
4.1、通过查看slave机的log(/var/log/mysqld.log 或 /usr/local/mysql/data/localhost.localdomain.err):
100703 10:51:42 [Note] Slave I/O thread: connected to master '[email protected]:3306', replication started in log 'mysql-bin.000003' at position 98
如看到以上信息则证明搭建成功,如果有问题也可通过此log找原因
4.2、在master
mysql>create database mark;
use mark;
create table emp(id int,name varchar(100));
4.3、在slave
mysql>create database mark;
use mark;
create table emp(id int,name varchar(100));
4.4、在master
insert into emp values(1,'mark1');
insert into emp values(2,'mark2');
select * from emp;
4.5、查看slave数据库
select * from emp;
与master数据一致表示同步成功!!
四、安装配置amoeba,实现负载均衡
1、amoeba是基于java运行的,所以要先装jdk
# chmod +x jdk-1_5_0_16-linux-i586-rpm.bin
# ./ jdk-1_5_0_16-linux-i586-rpm.bin
# vi /etc/profile //在文件里面添加jdk的环境变量,添加内容如下
JAVA_HOME=/usr/java/jdk1.5.0_16
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$CLASSPATH
export CLASSPATH
# source /etc/profile
2、安装amoeba,并配置(注意:我的amoeba装在192.168.150.130,因为内存不够,开不起三个虚拟机 T_T,所以amoeba和master数据库装在同一服务器上)
2.1、安装
tar -zxvf amoeba-mysql-1.2.1-GA.tar.gz
cp -vRp /root/amoeba-mysql-1.2.1-GA /usr/local/amoeba
2.2、配置
vi /usr/local/amoeba/conf/amoeba.xml
如下:
8006
20
30
30
128
true
root
root
60
com.meidusa.amoeba.net.AuthingableConnectionManager
defaultManager
3306
192.168.150.130
mark
master
master
200
200
10
600000
600000
true
true
defaultManager
3306
192.168.150.131
mark
slave
slave
200
200
10
600000
600000
true
true
1
server2
${amoeba.home}/conf/rule.xml
${amoeba.home}/conf/functionMap.xml
${amoeba.home}/conf/ruleFunctionMap.xml
1500
server1
server1
mutilPool
true