需求:
在mysql abb 复制的基础上,实现apache对数据库的读写分离,写给maseter,读通过LVS 给�z台slave
实验环境:
客户端192.168.11.20
路由器一台机子两个网卡192.168.11.1和192.168.18.1
Apache 192.168.18.241
mysql 的master 192.168.18.81
分发器一台机子 192.168.18.250 vip:192.168.18.253
Mysql-b1 一台机子 192.168.18.161 vip:192.168.18.253
Mysql-b2 一台机子 192.168.18.201 vip:192.168.18.253
详细:
一、客户端,
配置好ip,192.168.11.20,并将网关指向192.168.11.1
二、route,
两个网卡分别为192.168.11.1和 192.168.18.1
echo 1 > /proc/sys/net/ipv4/ip_forward
三、配置mysql a-b-b
(1)在3台mysql上
yum install mysql mysql-server
(2)根据以前的mysql主辅ab配置好mysql的a-b-b模式
master:
vim /etc/my.cnf
[mysqld]
server_id = 1
log-bin=binlog
log-bin-index=binlog.index
slave:
vim /etc/my.cnf
[mysqld]
server_id = 2
relay_log = /var/lib/mysql/mysql-relay-bin
relay_log_index=/var/lib/mysql/mysql-relay-bin.index
master && slave
service mysqld restart
master:
mysql> GRANT replication slave ON *.* TO 'kyo'@'%' identified by '123';
Query OK, 0 rows affected (0.05 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
#################################################
slave:
cd /var/lib/mysql
rm -fr *
service mysqld restart
master
mysqldump -A -x > /tmp/full.sql
scp /tmp/full.sql root@salveip:/tmp/
slave:
mysql < /tmp/full.sql
master:
mysql> flush tables with read lock;
mysql> show master status;
+---------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------+----------+--------------+------------------+
| binlog.000003 | 1780 | | |
mysql>unlock tables;
slave:
mysql> change master to master_host='192.168.18.125', master_port=3306, master_user='kyo', master_password='123', master_log_file='binlog.000003',master_log_pos=295;
mysql> start slave;
slave:
mysql> show slave status \G
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
slave2 上的配置可以同slave1
如果要求在配置slave2的时候不允许master 停服务或者锁表,那么请参阅http://zhangshujian.blog.51cto.com/5670152/1076801
四、根据dr模式配置分发器和myb1、myb2
(1)配置分发器
a、ip地址:ifconfig eth0 192.168.18.250
b、绑定ip地址:ifconfig eth0:0 192.168.18.253/32 broadcast 192.168.18.253 up
c、指定主机路由:route add –host 192.168.18.253 dev eth0:0
d、设定分发规则:
rpm –ivh ipvsadm-1.24-10.i386.rpm
设置分发规则:ipvsadm –A –t 192.168.18.253:3306 –s rr
Ipvsadm –a –t 192.168.18.253:3306 –r 192.168.18.161 -g
Ipvsadm –a –t 192.168.18.253:3306 –r 192.168.18.201 -g
查看: ipvsadm –ln
250ffmysql#ipvsadm -ln
I P Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.18.253:3306 rr
-> 192.168.18.201:3306 Route 1 0 0
-> 192.168.18.161:3306 Route 1 0 0
(2)配置slave1
a、绑定vip:ifconfig lo:0 192.168.18.253/32 broadcast 192.16818.253 up
b、设置主机路由:route add –host 192.168.18.253 dev lo:0
c、设置默认路由:route add default gw 192.168.18.1
d、保证路由在arp
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
e、重启下mysql服务
(3)配置slave2同上slave1
五、实现读写分离,写的时候指向mysql master192.168.18.81,读的时候指向分发器192.168.18.253
(1)使用mysql-w-r.tar.gz
解压文件:tar fvxz mysql-w-r.tar.gz,并将解压的文件拷贝靠/var/www/html下
(2)修改文件的ip地址和用户以实现读写分离
a、新建一个mysql帐号使其分别有写权限、读权限。
GRANT all privileges ON *.* TO 'aa'@'%' identified by '123';
GRANT all privileges ON *.* TO 'aa'@'%' identified by '123';
flush privileges;
b、修改mysql-c.php
$con = mysql_connect("192.168.18.81","aa","123");
修改insert.php
$con = mysql_connect("192.168.18.81","aa","123");
修改mysql-r.php
$con = mysql_connect("192.168.18.253","aa","123");
测试:
Lost connection to MySQL server at 'reading initial communication packet' system erro 113
最后确定是分发器的防火墙没有关闭