web架构 之 MySQL负载均衡 主从复制 读写分离

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





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
$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
$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 '
';


}
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 [email protected] 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



你可能感兴趣的:(MySQL)