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

时间:2022-09-23 20:29:17

MySQL负载均衡架构图如下:

web架构 之 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
如下:

<?xml version="1.0" encoding="gbk"?>


<!DOCTYPE amoeba:configuration SYSTEM "amoeba.dtd">
<amoeba:configuration xmlns:amoeba="http://amoeba.meidusa.com/">


<server>
<!-- proxy server绑定的端口 -->
<property name="port">8006</property>

<!-- proxy server绑定的IP -->
<!--
<property name="ipAddress">192.168.150.130</property>
-->
<!-- proxy server net IO Read thread size -->
<property name="readThreadPoolSize">20</property>

<!-- proxy server client process thread size -->
<property name="clientSideThreadPoolSize">30</property>

<!-- mysql server data packet process thread size -->
<property name="serverSideThreadPoolSize">30</property>

<!-- socket Send and receive BufferSize(unit:K) -->
<property name="netBufferSize">128</property>

<!-- Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm). -->
<property name="tcpNoDelay">true</property>

<!-- 对外验证的用户名 -->
<property name="user">root</property>

<!-- 对外验证的密码 -->
<property name="password">root</property>

<!-- query timeout( default: 60 second , TimeUnit:second) -->
<property name="queryTimeout">60</property>
</server>

<!--
每个ConnectionManager都将作为一个线程启动。
manager负责Connection IO读写/死亡检测
-->
<connectionManagerList>
<connectionManager name="defaultManager" class="com.meidusa.amoeba.net.MultiConnectionManagerWrapper">
<property name="subManagerClassName">com.meidusa.amoeba.net.AuthingableConnectionManager</property>

<!--
default value is avaliable Processors
<property name="processors">5</property>
-->
</connectionManager>
</connectionManagerList>

<dbServerList>
<!--
一台mysqlServer 需要配置一个pool,
如果多台 平等的mysql需要进行loadBalance,
平台已经提供一个具有负载均衡能力的objectPool:com.meidusa.amoeba.mysql.server.MultipleServerPool
简单的配置是属性加上 virtual="true",该Pool 不允许配置factoryConfig
或者自己写一个ObjectPool。
-->
<dbServer name="server1">

<!-- PoolableObjectFactory实现类 -->
<factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
<property name="manager">defaultManager</property>

<!-- 真实mysql数据库端口 -->
<property name="port">3306</property>

<!-- 真实mysql数据库IP -->
<property name="ipAddress">192.168.150.130</property>
<property name="schema">mark</property>

<!-- 用于登陆mysql的用户名 -->
<property name="user">master</property>

<!-- 用于登陆mysql的密码 -->
<property name="password">master</property>

</factoryConfig>

<!-- ObjectPool实现类 -->
<poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">
<property name="maxActive">200</property>
<property name="maxIdle">200</property>
<property name="minIdle">10</property>
<property name="minEvictableIdleTimeMillis">600000</property>
<property name="timeBetweenEvictionRunsMillis">600000</property>
<property name="testOnBorrow">true</property>
<property name="testWhileIdle">true</property>
</poolConfig>
</dbServer>
<dbServer name="server2">

<!-- PoolableObjectFactory实现类 -->
<factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
<property name="manager">defaultManager</property>

<!-- 真实mysql数据库端口 -->
<property name="port">3306</property>

<!-- 真实mysql数据库IP -->
<property name="ipAddress">192.168.150.131</property>
<property name="schema">mark</property>

<!-- 用于登陆mysql的用户名 -->
<property name="user">slave</property>

<!-- 用于登陆mysql的密码 -->
<property name="password">slave</property>


</factoryConfig>

<!-- ObjectPool实现类 -->
<poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">
<property name="maxActive">200</property>
<property name="maxIdle">200</property>
<property name="minIdle">10</property>
<property name="minEvictableIdleTimeMillis">600000</property>
<property name="timeBetweenEvictionRunsMillis">600000</property>
<property name="testOnBorrow">true</property>
<property name="testWhileIdle">true</property>
</poolConfig>
</dbServer>

<dbServer name="multiPool" virtual="true">
<poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">

<!-- 负载均衡参数 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
<property name="loadbalance">1</property>

<!-- 参与该pool负载均衡的poolName列表以逗号分割 -->
<property name="poolNames">server2</property>
</poolConfig>
</dbServer>
</dbServerList>

<queryRouter class="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter">
<property name="ruleConfig">${amoeba.home}/conf/rule.xml</property>
<property name="functionConfig">${amoeba.home}/conf/functionMap.xml</property>
<property name="ruleFunctionConfig">${amoeba.home}/conf/ruleFunctionMap.xml</property>
<property name="LRUMapSize">1500</property>
<property name="defaultPool">server1</property>

<property name="writePool">server1</property>
<property name="readPool">mutilPool</property>

<property name="needParse">true</property>
</queryRouter>
</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