码迷,mamicode.com
首页 > 数据库 > 详细

MySQL 高可用架构

时间:2015-10-13 01:51:27      阅读:377      评论:0      收藏:0      [点我收藏+]

标签:mysql

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  只负责写入数据,VIP192.168.5.200

           mysql_  只负责读取数据,VIP192.168.5.00                       

 

  • 建立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/bash

for i in 15 16 17 18 19 20 21

         do

         ssh 192.168.5.$i date 081622552015

done

  • 安装Perl工具(mha使用的是Perl语句)

#!/bin/bash

for i in 17 18 19 20 21

do

       (

       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/bash

for i in 17 18 19 20 21

do

       (   

       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"

       )&

done

  1. 2    分别修改mysqlbinlog配置文件,开启A/B复制功能

vim /etc/my.cnf

server_id=12345

log_bin=binlog

log_bin_index=binlog.index

  1. 3   开启MySQL

#!/bin/bash

for i in 17 18 19 20 21

do

       (   

       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"

       )&

done

  1. 4        分别添加MySQL A/B同步的管理账户

# mysql

mysql>grant replication slve on *.*to sko identified by ‘123‘;

mysql> flush privileges;

  1. 5        查看MySQL_A1master信息(使用的binlog日志号)

mysql -e ‘show master status \G‘

  1. 6        修改其他MySQLmaster.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"; done

5.7    分别查看slave的状态

# mysql

mysql> 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.rpm

  1. 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 

 

  1. 3    修改配置文件

vim /etc/mha/mha.cnf

[server default]

#mysql admin account and password

user=root

password=123

#mha workdir and working

manager_workdir=/etc/mha

manager_log=/etc/mha/manager_log

#mysql AB account and password

repl_user=sko

repl_password=123

#check_mha_mode time

ping_interval=1

#ssh account

ssh_user=root

[server1]

hostname=192.168.5.17

ssh_port=22

master_binlog_dir=/var/lib/mysql

candidate_master=1

[server2]

hostname=192.168.5.18

ssh_port=22

master_binlog_dir=/var/lib/mysql

candidate_master=1

[server3]

hostname=192.168.5.19

ssh_port=22

master_binlog_dir=/var/lib/mysql

candidate_master=0

[server4]

hostname=192.168.5.20

ssh_port=22

master_binlog_dir=/var/lib/mysql

candidate_master=0

[server5]

hostname=192.168.5.21

ssh_port=22

master_binlog_dir=/var/lib/mysql

candidate_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/bash

for i in 17 18 19 20 21

do

MASTER=`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. 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/haproxy

make install PREFIX=/usr/local/haproxy

cd /usr/src/haproxy-1.5.3/

cd examples/

cp haproxy.init /etc/init.d/haproxy

chmod 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

       #quiet

defaults

       log     global

       mode    http

       #option httplog

       option  dontlognull

       retries 3

       option redispatch

       maxconn 2000

       contimeout      5000

       clitimeout      50000

       srvtimeout      50000

listen 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 2

listen 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 设置“读”VIP

ifconfig eth0:1 192.168.5.100/24    

11.2 设置“写”VIP  

#!/bin/bash

VIP=192.168.5.200

NETMASK=255.255.255.0

MUSER=root

MPW=123

MYSQL_IP="192.168.5.17 192.168.5.18"

NIC=eth0

#~~~~~~main program~~~~~

#check_master_mysql

check_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

  fi

done

}

#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"

        fi

done

}

while true

  do

  check_master

  VIP

  sleep1

done

          chmod 755 check_mysql.txt

         nohup ./check_mysql.txt  &


本文出自 “运维之道” 博客,请务必保留此出处http://lrtao2010.blog.51cto.com/10425744/1702249

MySQL 高可用架构

标签:mysql

原文地址:http://lrtao2010.blog.51cto.com/10425744/1702249

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!