1)、在两台机子上面分别安装 mysql , keepalived
2)、
keepalived 主从:
主:vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state BACKUP # 备用用 BACKUP
interface eth2 #用的是哪个网卡
virtual_router_id 59 #虚拟路由地址必须一样,才能配置成一个组
priority 100 # 备用用 90
advert_int 1 #MASTER与BACKUP负载均衡器之间同步检查的时间间隔,单位是秒
nopreempt
authentication {
auth_type PASS #(密码形式)
auth_pass 1111
}
virtual_ipaddress {
192.168.32.100
}
notify_master "/bin/bash -x /usr/local/sbin/modify_master.sh 2>&1 | tee /tmp/modi.txt"
#notify_master "/bin/echo master >> /etc/keepalived/temp.txt"
}
virtual_server 192.168.32.100 3306 {
delay_loop 6 #每隔6s 查询 realserver 状态
lb_algo rr #lvs 算法
lb_kind NAT #Direct Route
persistence_timeout 60 #会话保持时间,单位是秒(可以适当延长时间以保持session)
protocol TCP # 用 TCP协议检查 realserver 状态
real_server 192.168.32.79 3306 {
weight 1
notify_down /usr/local/sbin/keepalived.sh
TCP_CHECK {
connect_timeout 10 #10s 无响应超时
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
从:vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state BACKUP # 备用用 BACKUP
interface eth2 #用的是哪个网卡
virtual_router_id 59 #虚拟路由地址必须一样,才能配置成一个组
priority 90 # 备用用 90
advert_int 1 #MASTER与BACKUP负载均衡器之间同步检查的时间间隔,单位是秒
nopreempt
authentication {
auth_type PASS #(密码形式)
auth_pass 1111
}
virtual_ipaddress {
192.168.32.100
}
notify_master "/bin/bash -x /usr/local/sbin/modify_master.sh 2>&1 | tee /tmp/modi.txt"
#notify_master "/bin/echo master >> /etc/keepalived/temp.txt"
}
virtual_server 192.168.32.100 3306 {
delay_loop 2 #每隔2s 查询 realserver 状态
lb_algo rr #lvs 算法
lb_kind NAT #Direct Route
persistence_timeout 60 #会话保持时间,单位是秒(可以适当延长时间以保持session)
protocol TCP # 用 TCP协议检查 realserver 状态
real_server 192.168.32.87 3306 {
weight 1
notify_down /usr/local/sbin/keepalived.sh
TCP_CHECK {
connect_timeout 10 #10s 无响应超时
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
其中主从 keepalived.sh:
#!/bin/bash
service keepalived restart
service mysql start
主从modify_master.sh:
#!/bin/bash
/usr/local/mysql/bin/mysql -uroot -p1234567 -e ‘set global read_only=0;‘
/usr/local/mysql/bin/mysql -uroot -h 192.168.32.87 -p1234567 -e ‘set global read_only=1;‘ (其中 -h 后面主机要么空格,要么加引号,从修改为:192.168.32.79)
mysql 主从 (需搭建互为主从):
主服务器:
server_id = 1
log-bin=aming
binlog-do-db=db1,db2 (binlog-ignore-db=mysql)
指定权限:
>grant replication slave on *.* to ‘repl‘@‘192.168.32.87‘ identified by ‘1234567‘;
>flush privileges;
>flush tables with read lock; (unlock tables;)
查看主服务器状态:
>show master status;
从服务器:
server_id = 11
replicate-do-db = db1; (replicate-ignore-db=mysql)
接下去把主服务器的数据拷贝到从服务器中
> stop slave;
> change master to master_host=‘192.168.32.79‘, master_port=3306,master_user=‘repl‘,master_password=‘1234567‘,
> master_log_file=‘aming.00001‘,master_log_pos=106; (主服务器中 show master status 看到的数据)
> start slave;
注:配置完之后需要重启,其中是否成功主从需要查看 从服务器中 show slave status 中的 SLAVE-IO-RUNNING 跟 SLAVE-SQL-RUNNING,并且查看 LAST-IO-ERROR 跟 Last_SQL_Error中是否报错
mysql 设置只读:
1) flush tables with read lock; (退出终端失效)
2) set global read_only=1; 这个拥有super权限的仍然可以写操作
3) grant select....权限
当用 keepalived 进行主从配置的时候,应用那边需配置 keepalived 的 VIP 地址。
原文地址:http://liuyangjun.blog.51cto.com/9273686/1733756