[MySQL集群架构] mysql主主+lvs
测试机2台,分别为A,B,系统均为AS4。A的IP:192.168.17.21,B的IP:192.168.17.53。
1:安装mysql。
分别在A,B上执行
[root@data1 ~]# cd /home
[root@data1 ~]# tar zxvfmysql-5.1.32.tar.gz
[root@data1 ~]# cd mysql-5.1.32
[root@data1 ~]# ./configure --prefix=/usr/local/mysql--with-extra-charsets=all --enable-thread-safe-client --with-plugin-partition
[root@data1 ~]# make
[root@data1 ~]# make install
[root@data1 ~]#/usr/local/mysql/bin/mysql_install_db --user=mysql
[root@data1 ~]# cp support-files/my-medium.cnf/etc/my.cnf
[root@data1 ~]# cd /usr/local/mysql/
[root@data1 ~]# chown -R root .
[root@data1 ~]# chown -R mysql var
[root@data1 ~]# chgrp -R mysql .
[root@data1 ~]#/usr/local/mysql/bin/mysqld_safe --user=mysql &
[root@data1 ~]# /usr/local/mysql/bin/mysqladmin-uroot password newpasswd
复制代码
2:A,B互为主从
AB上分别添加复制用户,方便起见,用root测试。
mysql> GRANT ALL PRIVILEGES ON *.* TO[email=root@]root@"%[/email]" IDENTIFIED BY"newpasswd" WITH GRANT OPTION;
mysql> flush privileges;
A上,修改my.cnf文件,[mysqld]下添加:
.
.
.
auto-increment-increment = 2
auto-increment-offset = 2
server-id = 10
log-error = /usr/local/mysql/var/data1.err
log-bin =/usr/local/mysql/var/mysql-master-bin.log
binlog-do-db = db1
binlog-do-db = db2
binlog-do-db = db3
server-id = 2
master-host = 192.168.17.21
master-user = root
master-password = newpasswd
master-port = 3306
master-connect-retry=60
replicate-do-db=db1
replicate-do-db=db2
replicate-do-db=db3
log-bin=/usr/local/mysql/var/mysql-slave-bin.log
复制代码
B上,修改my.cnf文件,[mysqld]下添加:
auto-increment-increment = 2
auto-increment-offset = 1
server-id = 1
log-error = /usr/local/mysql/var/data2.err
log-bin =/usr/local/mysql/var/mysql-master-bin.log
binlog-do-db = db1
binlog-do-db = db2
binlog-do-db = db3
server-id = 20
master-host = 192.168.17.21
master-user = root
master-password = newpasswd
master-port = 3306
master-connect-retry=60
replicate-do-db=db1
replicate-do-db=db2
replicate-do-db=db3
log-bin=/usr/local/mysql/var/mysql-slave-bin.log
复制代码
A,B上重新启动mysql。
[root@data1 ~]#mysqladmin -uroot-pnewpasswd shutdown
[root@data1~]#/usr/local/mysql/bin/mysqld_safe &
A上进入mysql,并查看master状态
[root@data1 ~]# mysql -uroot -pnewpasswd
mysql> show master status;
+------------------------+----------+--------------------------------+------------------+
| File | Position |Binlog_Do_DB |Binlog_Ignore_DB |
+------------------------+----------+--------------------------------+------------------+
| mysql-slave-bin.000018 | 44305519 |db1,db2,db3 | |
+------------------------+----------+--------------------------------+------------------+
进入B,设置B为A的从。
[root@data2 ~]# mysql -uroot -pnewpasswd
mysql>slave stop;
mysql>change master to
mysql>master_host='192.168.17.21',
mysql>master_user='root',
mysql>master_password='newpasswd',
mysql>master_port=3308,
mysql>master_log_file='mysql-slave-bin.000018',
mysql>master_log_pos=44305519;
mysql>slave start;
mysql> show master status;
+------------------------+----------+--------------------------------+------------------+
| File | Position |Binlog_Do_DB |Binlog_Ignore_DB |
+------------------------+----------+--------------------------------+------------------+
| mysql-slave-bin.000010 | 42354594 |db1,db2,db3 | |
+------------------------+----------+--------------------------------+------------------+
1 row in set (0.00 sec)
进入A:
mysql>slave stop;
mysql>change master to
mysql>master_host='192.168.17.53',
mysql>master_user='root',
mysql>master_password='newpasswd',
mysql>master_port=3308,
mysql>master_log_file='mysql-slave-bin.000010',
mysql>master_log_pos=42354594;
mysql>slave start;
复制代码
3:安装LVS
A上虚拟一个网卡,IP地址为192.168.17.88,即VIP。
[root@data1 ~] touch /etc/sysconfig/network-scripts/ifcfg-eth0:1
[root@data1 ~] vi/etc/sysconfig/network-scripts/ifcfg-eth0:1
DEVICE=eth0:1
BOOTPROTO=none
HWADDR=
ONBOOT=yes
TYPE=Ethernet
IPADDR=192.168.17.88
NETMASK=255.255.255.0
USERCTL=no
IPV6INIT=no
PEERDNS=no
复制代码
[root@data1 ~] ifup eth0:1
AB上分别执行lvs.sh,内容为:
#!/bin/bash
#description : start realserver
VIP=203.110.169.25
/sbin/ifconfig lo:0 $VIP broadcast $VIPnetmask 255.255.255.255 up
/sbin/route add -host $VIP dev lo:0
echo "1">/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1">/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2">/proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p
复制代码
A,B上分别安装ipvsadm和piranha:
[root@data1 ~] cd /home/lvs
[root@data1 lvs]# rpm -ivhipvsadm-1.24-6.1.i386.rpm
[root@data1 lvs]# rpm -ivhpiranha-0.8.2-1.i386.rpm
[root@data1 lvs]#/usr/sbin/piranha-passwd
New Password:
Verify:
Updating password for user piranha
[root@data1 lvs]# service piranha-guirestart
Shutting down piranha-gui: [ OK ]
Starting piranha-gui: [ OK ]
[root@data1 lvs]# chkconfig piranha-gui on
复制代码
登陆A的piranha进行配置,http://192.168.17.21:3636。
把A的/etc/sysconfig/ha/lvs.cf复制到B上。
具体配置如下:
serial_no = 14
primary = 192.168.17.21
service = lvs
backup_active = 1
backup = 192.168.17.53
heartbeat = 1
heartbeat_port = 539
keepalive = 6
deadtime = 18
network = direct
debug_level = NONE
monitor_links = 0
virtual mysql {
active = 1
address = 192.168.17.88 eth0:1
vip_nmask = 255.255.255.0
port = 3306
use_regex = 0
load_monitor = none
scheduler = wlc
protocol = tcp
timeout = 6
reentry = 15
quiesce_server = 0
server 21 {
address = 192.168.17.21
active = 1
weight = 1
}
server 53 {
address = 192.168.17.53
active = 1
weight = 1
}
}
复制代码
AB上分别执行:
[root@data1 lvs]# service pulse start
至此,over。
测试了几天,进行了大量的数据库操作,至今没有报错。
方法虽然很简单,但是感觉蛮实用。
测试到月底,如果可行就开始上线。