CentOS6.4 build mysql high-availability architecture MHA
I. Environment
Minimal installation of CentOS 6.4x64
Manager 192.168.3.51
Master 192.168.3.52 (standby master)
Slave1 192.168.3.53
Slave2 192.168.3.54
Ii. Configure local hosts resolution
Configure the same hosts resolution for all four hosts. The content is as follows:
[root@manager~]
#cat/etc/hosts
127.0.0.1localhostlocalhost.localdomainlocalhost4localhost4.localdomain4
::1localhostlocalhost.localdomainlocalhost6localhost6.localdomain6
192.168.3.51 manager
192.168.3.52 master
192.168.3.53 slave1
192.168.3.54 slave2
3. Configure ssh key-free login between four hosts
Manager:
[root@manager~]
#ssh-keygen
[root@manager~]
#ssh-copy-id-i~/.ssh/id_rsa.pubroot@master
[root@manager~]
#ssh-copy-id-i~/.ssh/id_rsa.pubroot@slave1
[root@manager~]
#ssh-copy-id-i~/.ssh/id_rsa.pubroot@slave2
Master:
[root@master~]
#ssh-keygen
[root@master~]
#ssh-copy-id-i~/.ssh/id_rsa.pubroot@manager
[root@master~]
#ssh-copy-id-i~/.ssh/id_rsa.pubroot@slave1
[root@master~]
#ssh-copy-id-i~/.ssh/id_rsa.pubroot@slave2
Slave1:
[root@slave1~]
#ssh-keygen
[root@slave1~]
#ssh-copy-id-i~/.ssh/id_rsa.pubroot@manager
[root@slave1~]
#ssh-copy-id-i~/.ssh/id_rsa.pubroot@master
[root@slave1~]
#ssh-copy-id-i~/.ssh/id_rsa.pubroot@slave2
Slave2:
[root@slave2~]
#ssh-keygen
[root@slave2~]
#ssh-copy-id-i~/.ssh/id_rsa.pubroot@manager
[root@slave2~]
#ssh-copy-id-i~/.ssh/id_rsa.pubroot@master
[root@slave2~]
#ssh-copy-id-i~/.ssh/id_rsa.pubroot@slave1
4. Install mysql
Install mysql on master, slave1, and slave2. Mysql-5.5.37.tar.gz is installed here, and the script is used for installation.
The script content is as follows:
[root@master~]
#catmysql_install.sh
#!/bin/bash
DATADIR=
'/data/mysql/data'
VERSION=
'mysql-5.5.37'
export
LANG=zh_CN.UTF-8
#Sourcefunctionlibrary.
.
/etc/init
.d
/functions
#camkeinstallmysql5.5.X
install_mysql(){
read
-p
"pleaseinputapasswordforroot:"
PASSWD
if
[!-d$DATADIR];
then
mkdir
-p$DATADIR
fi
yum
install
cmake
make
gcc-c++bison-develncurses-devel-y
id
mysql&>
/dev/null
if
[$?-
ne
0];
then
useradd
mysql-s
/sbin/nologin
-M
fi
#useraddmysql-s/sbin/nologin-M
#changedatadirownertomysql
chown
-Rmysql.mysql$DATADIR
cd
#wgethttp://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
"installmysqlisfailed!"
/bin/false
exit
$?
fi
sleep
2
#link
ln
-s
/usr/local/
$VERSION/
/usr/local/mysql
ln
-s
/usr/local/mysql/bin/
*
/usr/bin/
#copyconfigandstartfile
/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
#initmysql
/usr/local/mysql/scripts/mysql_install_db
--basedir=
/usr/local/mysql
--datadir=$DATADIR--user=mysql
if
[$?-
ne
0];
then
action
"installmysqlisfailed!"
/bin/false
exit
$?
fi
#checkmysql
/etc/init
.d
/mysqld
start
if
[$?-
ne
0];
then
action
"mysqlstartisfailed!"
/bin/false
exit
$?
fi
chkconfig--addmysqld
chkconfigmysqldon
/usr/local/mysql/bin/mysql
-e
"updatemysql.usersetpassword=password('$PASSWD')wherehost='localhost'anduser='root';"
/usr/local/mysql/bin/mysql
-e
"updatemysql.usersetpassword=password('$PASSWD')wherehost='127.0.0.1'anduser='root';"
/usr/local/mysql/bin/mysql
-e
"deletefrommysql.userwherepassword='';"
/usr/local/mysql/bin/mysql
-e
"flushprivileges;"
#/usr/local/mysql/bin/mysql-e"selectversion();">/dev/null2>&1
if
[$?-
eq
0];
then
echo
"+---------------------------+"
echo
"+ ------ Mysql installation completed -------- +"
echo
"+---------------------------+"
fi
#/etc/init.d/mysqldstop
}
install_mysql
Create master-slave replication between master, slave1, and slave2
Modify the server-id of the three machines to ensure they are unique.
# Master server-id
[root@master~]
#grepserver-id/etc/my.cnf
server-
id
=1
# Server-id of slave1
[root@slave1~]
#grepserver-id/etc/my.cnf
server-
id
=53
# Server-id of slave2
[root@slave2~]
#grepserver-id/etc/my.cnf
server-
id
=53
Configure the account for master-slave synchronization on master and slave1. Slave1 is a slave master, which also requires authorization.
# Create an account for master-slave Synchronization