您的位置:首页 > 数据库 > SQL

MySQL 高可用架构

2015-10-12 20:53 501 查看
Mysql 高可用架构(基于A/B复制)
Client 192.168.5.13/24 Mha_manager 192.168.5.15/24 Mysql_A1 192.168.5.17/24 Mysql_A2 192.168.5.18/24 Haproxy 192.168.5.16/24 Mysql_B1 192.168.5.19/24 Mysql_B2 192.168.5.20/24 Mysql_B3 192.168.5.21/24 其中mysql_A 只负责写入数据,VIP为192.168.5.200。 mysql_B 只负责读取数据,VIP为192.168.5.100。 建立ssh互信认
ssh-keygen
for i in 15 16 17 18 19 20 21;do ssh-copy-id -i /root/.ssh/id_rsa.pub192.168.5.$i; done
统一时间
#!/bin/bashfor i in 15 16 17 18 19 20 21 do ssh 192.168.5.$i date 081622552015done安装Perl工具(mha使用的是Perl语句)
#!/bin/bashfor i in 17 18 19 20 21do ( ssh192.168.5.$i "yum install perl-* -y" &>/tmp/perl_install [$? -eq 0 ] && echo "192.168.5.$i perl_install ok" || echo"192.168.5.$i perl_install error" )&done安装MySQL并进行相关设置
安装MySQL
#!/bin/bashfor i in 17 18 19 20 21do ( ssh192.168.5.$i "yum install mysql* -y" &>/tmp/mysql_install [$? -eq 0 ] && echo "192.168.5.$i mysql_install ok" || echo"192.168.5.$i mysql_install error" )&done2 分别修改mysql的binlog配置文件,开启A/B复制功能
vim /etc/my.cnfserver_id=1(2、3、4、5)log_bin=binloglog_bin_index=binlog.index3 开启MySQL
#!/bin/bashfor i in 17 18 19 20 21do ( ssh192.168.5.$i /etc/init.d/mysqld start &>/tmp/mysql_install [$? -eq 0 ] && echo "192.168.5.$i mysql_start ok" || echo"192.168.5.$i mysql_start error" )&done4 分别添加MySQL A/B同步的管理账户
# mysqlmysql>grant replication slve on *.*to sko identified by '123';mysql> flush privileges;5 查看MySQL_A1的master信息(使用的binlog日志号)
mysql -e 'show master status \G'6 修改其他MySQL的master.info 的内容,保证能正常同步数据
for i in 18 19 20 21; do ssh192.168.5.$i mysql -e "slave stop"; ssh 192.168.5.$i mysql -e"change master to master_host='192.168.5.17',master_user='sko',master_password='123',master_log_file='binlog.000003',master_log_pos=1420";ssh 192.168.5.$i mysql -e "slave start"; done5.7 分别查看slave的状态# mysqlmysql> show slave status \G;Slave_IO_Running: Yes Slave_SQL_Running: Yes 5.8 分别给MySQL管理员root设置密码,清理不用的用户,并使root用户可以远程登录MySQL。 # mysql_secure_installation # mysql -u root -p123 mysql>update mysql.user set host='%' where host='127.0.0.1';mysql> flush privileges;安装mha软件
分别在mha的服务端上安装node软件
cd mha_soft/ rpm -ivh mha4mysql-node-0.54-0.el6.noarch.rpm2 在mha_manager上安装
安装依赖包 cd mha_soft/dependent/yum localinstall./* -y 安装软件rpm -ivhmha4mysql-manager-0.55-0.el6.noarch.rpm rpm -ivhmha4mysql-node-0.54-0.el6.noarch.rpm 3 修改配置文件
vim /etc/mha/mha.cnf[server default]#mysql admin account and passworduser=rootpassword=123#mha workdir and workingmanager_workdir=/etc/mhamanager_log=/etc/mha/manager_log#mysql AB account and passwordrepl_user=skorepl_password=123#check_mha_mode timeping_interval=1#ssh accountssh_user=root[server1]hostname=192.168.5.17ssh_port=22master_binlog_dir=/var/lib/mysqlcandidate_master=1[server2]hostname=192.168.5.18ssh_port=22master_binlog_dir=/var/lib/mysqlcandidate_master=1[server3]hostname=192.168.5.19ssh_port=22master_binlog_dir=/var/lib/mysqlcandidate_master=0[server4]hostname=192.168.5.20ssh_port=22master_binlog_dir=/var/lib/mysqlcandidate_master=0[server5]hostname=192.168.5.21ssh_port=22master_binlog_dir=/var/lib/mysqlcandidate_master=0进行mha检测
masterha_check_ssh --conf=/etc/mha/mha.cnf masterha_check_repl--conf=/etc/mha/mha.cnf启动mha
nohup masterha_manager--conf=/etc/mha/mha.cnf > /tmp/mha_manager.log < /dev/null 2>&1&补充脚本
查看master_Host 是那台主机
#!/bin/bashfor i in 17 18 19 20 21doMASTER=`ssh192.168.5.$i "mysql -u root -p123 -e 'show slave status \G' | grep"Master_Host""`echo-e "Host=192.168.5.$i $MASTER"done 十、 安装haproxy软件1 安装haproxy软件
cd haproxy/tar xf haproxy-1.5.3.tar -C /usr/src/cd /usr/src/cd haproxy-1.5.3/make TARGET=linux26PRETIX=/usr/local/haproxymake install PREFIX=/usr/local/haproxycd /usr/src/haproxy-1.5.3/cd examples/cp haproxy.init /etc/init.d/haproxychmod 755 /etc/init.d/haproxy 10.2设置配置文件 mkdir /etc/haproxy mkdir /usr/share/haproxy ln -s /usr/local/haproxy/sbin/* /usr/sbin/ vim /usr/local/haproxy/haproxy.cfg global log 127.0.0.1 local0 log 127.0.0.1 local1 notice #log loghost local0 info maxconn 4096 chroot /usr/share/haproxy uid 99 gid 99 daemon #debug #quietdefaults log global mode http #option httplog option dontlognull retries 3 option redispatch maxconn 2000 contimeout 5000 clitimeout 50000 srvtimeout 50000listen MySQL 0.0.0.0:3306 mode tcp maxconn 200 balance roundrobin option mysql-check user root server mysql_1 192.168.5.19:3306 inter 1s rise 2 fall 2 server mysql_2 192.168.5.20:3306 inter 1s rise 2 fall 2 server mysql_2 192.168.5.21:3306 inter 1s rise 2 fall 2listen admin_status mode http bind 0.0.0.0:8899 option httplog log global stats enable stats refresh 10s stats hide-version stats realm Haproxy\ Statistics stats uri /admin-status stats auth admin:123456 stats admin if TRUE ln -s /usr/local/haproxy/haproxy.cfg /etc/haproxy/ 10.3开启haproxy service haproxy start 十一、实现MySQL高可用和读写分离 11.1 设置“读”VIPifconfig eth0:1 192.168.5.100/24 11.2 设置“写”VIP #!/bin/bashVIP=192.168.5.200NETMASK=255.255.255.0MUSER=rootMPW=123MYSQL_IP="192.168.5.17 192.168.5.18"NIC=eth0#~~~~~~main program~~~~~#check_master_mysqlcheck_master(){for IP in $MYSQL_IP do ssh$IP "mysql -u $MUSER -p$MPW -e 'show slave status \G' | grep -w'Slave_IO_Running'"&>/dev/null if[ $? -eq 1 ];then MY_master=$IP fidone}#eheck_master#echo $MY_master#set_VIP(){#ssh $MY_master "ip add showeth0" | grep inet |grep "$VIP"# if[ $? -ne 0 ];then# ifconfig$NIC:200 $VIP netmask $NETMASK# fi#}VIP(){for IP in $MYSQL_IP do ssh$IP "ip add show eth0" | grep inet | grep "$VIP"&>/dev/null if[ $? -eq 0 ] && [ $MY_master != "$IP" ];then ssh$IP "ifconfig $NIC:200 down" elif[ $? -eq 1 ] && [ $MY_master == "$IP" ];then ssh$IP "ifconfig $NIC:200 $VIP netmask $NETMASK up" fidone}while true do check_master VIP sleep1done chmod 755 check_mysql.txt nohup ./check_mysql.txt &

本文出自 “运维之道” 博客,请务必保留此出处http://lrtao2010.blog.51cto.com/10425744/1702249
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: