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 root@192.168.150.131 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 'root@192.168.150.130: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
如下:
[html] [http://www.aiuxian.com/article/p-2255343.html]( "view plain") http://www.aiuxian.com/article/p-2255343.html
<?
xml
version
=
"1.0"
encoding
=
"gbk"
?>
1.
1.
1.
<!DOCTYPE amoeba:configuration SYSTEM "amoeba.dtd"
>
1.
<
amoeba:configuration
xmlns:amoeba
1.
1.
1.
<
server
1.
<!– proxy server绑定的端口 –>
1.
<
property
name
"port"
8006
</
property
1.
1.
<!– proxy server绑定的IP –>
1.
<!–
1.
<property name="ipAddress">192.168.150.130</property>
1.
–>
1.
<!– proxy server net IO Read thread size –>
1.
<
property
name
"readThreadPoolSize"
20
</
property
1.
1.
<!– proxy server client process thread size –>
1.
<
property
name
"clientSideThreadPoolSize"
30
</
property
1.
1.
<!– mysql server data packet process thread size –>
1.
<
property
name
"serverSideThreadPoolSize"
30
</
property
1.
1.
<!– socket Send and receive BufferSize(unit:K) –>
1.
<
property
name
"netBufferSize"
128
</
property
1.
1.
<!– Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm). –>
1.
<
property
name
"tcpNoDelay"
true
</
property
1.
1.
<!– 对外验证的用户名 –>
1.
<
property
name
"user"
root
</
property
1.
1.
<!– 对外验证的密码 –>
1.
<
property
name
"password"
root
</
property
1.
1.
<!– query timeout( default: 60 second , TimeUnit:second) –>
1.
<
property
name
"queryTimeout"
60
</
property
1.
</
server
1.
1.
<!–
1.
每个ConnectionManager都将作为一个线程启动。
1.
manager负责Connection IO读写/死亡检测
1.
—
>
1.
<
connectionManagerList
>
1.
<
connectionManager
name
=
"defaultManager"
class
=
"com.meidusa.amoeba.net.MultiConnectionManagerWrapper"
>
1.
<
property
name
=
"subManagerClassName"
>
com.meidusa.amoeba.net.AuthingableConnectionManager
</
property
>
1.
1.
<!–
1.
default value is avaliable Processors
1.
<
property
name
=
"processors"
>
5
</
property
>
1.
—
>
1.
</
connectionManager
>
1.
</
connectionManagerList
>
1.
1.
<
dbServerList
>
1.
<!–
1.
一台mysqlServer 需要配置一个pool,
1.
如果多台 平等的mysql需要进行loadBalance,
1.
平台已经提供一个具有负载均衡能力的objectPool:com.meidusa.amoeba.mysql.server.MultipleServerPool
1.
简单的配置是属性加上
virtual
=
"true"
,该Pool 不允许配置factoryConfig
1.
或者自己写一个ObjectPool。
1.
—
>
1.
<
dbServer
name
=
"server1"
>
1.
1.
<!– PoolableObjectFactory实现类 –>
1.
<
factoryConfig
class
"com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory"
1.
<
property
name
"manager"
defaultManager
</
property
1.
1.
<!– 真实mysql数据库端口 –>
1.
<
property
name
"port"
3306
</
property
1.
1.
<!– 真实mysql数据库IP –>
1.
<
property
name
"ipAddress"
192.168.150.130
</
property
1.
<
property
name
"schema"
mark
</
property
1.
1.
<!– 用于登陆mysql的用户名 –>
1.
<
property
name
"user"
master
</
property
1.
1.
<!– 用于登陆mysql的密码 –>
1.
<
property
name
"password"
master
</
property
1.
1.
</
factoryConfig
1.
1.
<!– ObjectPool实现类 –>
1.
<
poolConfig
class
"com.meidusa.amoeba.net.poolable.PoolableObjectPool"
1.
<
property
name
"maxActive"
200
</
property
1.
<
property
name
"maxIdle"
200
</
property
1.
<
property
name
"minIdle"
10
</
property
1.
<
property
name
"minEvictableIdleTimeMillis"
600000
</
property
1.
<
property
name
"timeBetweenEvictionRunsMillis"
600000
</
property
1.
<
property
name
"testOnBorrow"
true
</
property
1.
<
property
name
"testWhileIdle"
true
</
property
1.
</
poolConfig
1.
</
dbServer
1.
<
dbServer
name
"server2"
1.
1.
<!– PoolableObjectFactory实现类 –>
1.
<
factoryConfig
class
"com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory"
1.
<
property
name
"manager"
defaultManager
</
property
1.
1.
<!– 真实mysql数据库端口 –>
1.
<
property
name
"port"
3306
</
property
1.
1.
<!– 真实mysql数据库IP –>
1.
<
property
name
"ipAddress"
192.168.150.131
</
property
1.
<
property
name
"schema"
mark
</
property
1.
1.
<!– 用于登陆mysql的用户名 –>
1.
<
property
name
"user"
slave
</
property
1.
1.
<!– 用于登陆mysql的密码 –>
1.
<
property
name
"password"
slave
</
property
1.
1.
1.
</
factoryConfig
1.
1.
<!– ObjectPool实现类 –>
1.
<
poolConfig
class
"com.meidusa.amoeba.net.poolable.PoolableObjectPool"
1.
<
property
name
"maxActive"
200
</
property
1.
<
property
name
"maxIdle"
200
</
property
1.
<
property
name
"minIdle"
10
</
property
1.
<
property
name
"minEvictableIdleTimeMillis"
600000
</
property
1.
<
property
name
"timeBetweenEvictionRunsMillis"
600000
</
property
1.
<
property
name
"testOnBorrow"
true
</
property
1.
<
property
name
"testWhileIdle"
true
</
property
1.
</
poolConfig
1.
</
dbServer
1.
1.
<
dbServer
name
"multiPool"
virtual
"true"
1.
<
poolConfig
class
"com.meidusa.amoeba.server.MultipleServerPool"
1.
1.
<!– 负载均衡参数 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA–>
1.
<
property
name
"loadbalance"
1
</
property
1.
1.
<!– 参与该pool负载均衡的poolName列表以逗号分割 –>
1.
<
property
name
"poolNames"
server2
</
property
1.
</
poolConfig
1.
</
dbServer
1.
</
dbServerList
1.
1.
<
queryRouter
class
"com.meidusa.amoeba.mysql.parser.MysqlQueryRouter"
1.
<
property
name
"ruleConfig"
${amoeba.home}/conf/rule.xml
</
property
1.
<
property
name
"functionConfig"
${amoeba.home}/conf/functionMap.xml
</
property
1.
<
property
name
"ruleFunctionConfig"
${amoeba.home}/conf/ruleFunctionMap.xml
</
property
1.
<
property
name
"LRUMapSize"
1500
</
property
1.
<
property
name
"defaultPool"
server1
</
property
1.
1.
<
property
name
"writePool"
server1
</
property
1.
<
property
name
"readPool"
mutilPool
</
property
1.
1.
<
property
name
"needParse"
true
</
property
1.
</
queryRouter
1.
</
amoeba:configuration
2.3、赋予权限
主数据库,赋予权限
mysql> grant all privileges on *.* to master@'192.168.150.130' identified by 'master';
从数据库,赋予权限
mysql> grant all privileges on *.* to slave@'192.168.150.130' identified by 'slave';
2.4、重启amoeba
/usr/local/amoeba/bin/amoeba stop
/usr/local/amoeba/bin/amoeba start
2.5、测试
2.5.1、编辑amoeba_insert.php
<?php
$con = mysql_connect("192.168.150.130:8006","root","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mark", $con);
$r = mysql_query("INSERT INTO emp VALUES (999,'mark999')");
if($r>0){
echo "插入了";
}
mysql_close($con);
2.5.2、编辑amoeba_select.php
<?php
$con = mysql_connect("192.168.150.130:8006","root","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mark", $con);
$result = mysql_query("select \* from emp;");
while($row = mysql_fetch_array($result)){
echo $row['id']."–".$row['name'];
echo '<br />';
}
mysql_close($con);
2.5.3、访问amoeba_insert.php和amoeba_select.php来测试
2.6、可能遇到的问题!!!!!!!
1、执行amoeba的命令,不够权限的话
方案1、chmod -R +x /usr/local/amoeba/bin/
2、遇到连接错误,如
ERROR 1130: Host '192.168.1.3' is not allowed to connect to this MySQL
或
Lost connection to MySQL server during query
方案1、赋予权限给新的用户
mysql> GRANT ALL PRIVILEGES ON \*.\* TO myuser@192.168.1.3 IDENTIFIED BY 'mypassword'
方案2、防火墙问题
vi /etc/sysconfig/iptables ,添加如下
-A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT(允许80端口通过防火墙)
-A INPUT -m state –state NEW -m tcp -p tcp –dport 3306 -j ACCEPT(允许3306端口通过防火墙)
重启网络 service network restart