当前位置: > 数据库 > MySQL >

CentOS6.4搭建mysql高可用架构MHA

时间:2015-07-08 21:57来源:linux.it.net.cn 作者:IT

一、环境

系统      CentOS 6.4x64最小化安装

manager     192.168.3.51

master     192.168.3.52 (备用master)

slave1     192.168.3.53

slave2     192.168.3.54

二、配置hosts本地解析

4台机都配置相同的hosts解析,内容如下

[root@manager ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.3.51    manager
192.168.3.52    master
192.168.3.53    slave1
192.168.3.54    slave2

三、配置四台主机之间ssh免秘钥登陆

manager:

[root@manager ~]# ssh-keygen
[root@manager ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@master
[root@manager ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@slave1
[root@manager ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@slave2

master:

[root@master ~]# ssh-keygen
[root@master ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@manager
[root@master ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@slave1
[root@master ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@slave2

slave1:

[root@slave1 ~]# ssh-keygen
[root@slave1 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@manager
[root@slave1 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@master
[root@slave1 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@slave2

slave2:

[root@slave2 ~]# ssh-keygen
[root@slave2 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@manager
[root@slave2 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@master
[root@slave2 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@slave1

四、安装mysql

在master,slave1,slave2上安装mysql服务。这里安装的是mysql-5.5.37.tar.gz,使用脚本进行安装

脚本内容如下

[root@master ~]# cat mysql_install.sh 
#!/bin/bash
 
DATADIR='/data/mysql/data'
VERSION='mysql-5.5.37'
export LANG=zh_CN.UTF-8
 
#Source function library.
/etc/init.d/functions
 
#camke install mysql5.5.X
install_mysql(){
        read -p "please input a password for root: " PASSWD
        if [ ! -d $DATADIR ];then
                mkdir -p $DATADIR
        fi
        yum install cmake make gcc-c++ bison-devel ncurses-devel -y
        id mysql &>/dev/null
        if [ $? -ne 0 ];then
                useradd mysql -s /sbin/nologin -M
        fi
        #useradd mysql -s /sbin/nologin -M
        #change datadir owner to mysql
        chown -R mysql.mysql $DATADIR
        cd
        #wget http://mirrors.sohu.com/mysql/MySQL-5.5/mysql-5.5.38.tar.gz
        tar xf $VERSION.tar.gz
        cd $VERSION
        cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/$VERSION \
        -DMYSQL_DATADIR=$DATADIR \
        -DMYSQL_UNIX_ADDR=$DATADIR/mysql.sock \
        -DDEFAULT_CHARSET=utf8 \
        -DDEFAULT_COLLATION=utf8_general_ci \
        -DENABLED_LOCAL_INFILE=ON \
        -DWITH_INNOBASE_STORAGE_ENGINE=1 \
        -DWITH_FEDERATED_STORAGE_ENGINE=1 \
        -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
        -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
        -DWITHOUT_PARTITION_STORAGE_ENGINE=1
        make && make install
        if [ $? -ne 0 ];then
                action "install mysql is failed!"  /bin/false
                exit $?
        fi
        sleep 2
        #link
        ln -s /usr/local/$VERSION/ /usr/local/mysql
        ln -s /usr/local/mysql/bin//usr/bin/
        #copy config and start file
        /bin/cp /usr/local/mysql/support-files/my-small.cnf /etc/my.cnf
        cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
        chmod 700 /etc/init.d/mysqld
        #init mysql
        /usr/local/mysql/scripts/mysql_install_db  --basedir=/usr/local/mysql --datadir=$DATADIR --user=mysql
        if [ $? -ne 0 ];then
                action "install mysql is failed!"  /bin/false
                exit $?
        fi
        #check mysql
        /etc/init.d/mysqld start
        if [ $? -ne 0 ];then
                action "mysql start is failed!"  /bin/false
                exit $?
        fi
        chkconfig --add mysqld
        chkconfig mysqld on
        /usr/local/mysql/bin/mysql -e "update mysql.user set password=password('$PASSWD') where host='localhost' and user='root';"
        /usr/local/mysql/bin/mysql -e "update mysql.user set password=password('$PASSWD') where host='127.0.0.1' and user='root';"
        /usr/local/mysql/bin/mysql -e "delete from mysql.user where password='';"
        /usr/local/mysql/bin/mysql -e "flush privileges;"
        #/usr/local/mysql/bin/mysql -e "select version();" >/dev/null 2>&1
        if [ $? -eq 0 ];then
                echo "+---------------------------+"
                echo "+------mysql安装完成--------+"
                echo "+---------------------------+"
        fi
        #/etc/init.d/mysqld stop
}
 
install_mysql

建立master,slave1,slave2之间的主从复制

修改3台机的server-id,确保是唯一的

#master的server-id
[root@master ~]# grep server-id /etc/my.cnf 
server-id   = 1
 
#slave1的server-id
[root@slave1 ~]# grep server-id /etc/my.cnf 
server-id   = 53
 
#slave2的server-id
[root@slave2 ~]# grep server-id /etc/my.cnf 
server-id   = 53

在master,slave1上配置主从同步用的账号。slave1是备用的master,这个也需要进行授权。

 
#创建主从同步用的账号



(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容