|
一、环境
系统 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解析,内容如下
1
2
3
4
5
6
7
| [iyunv@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:
1
2
3
4
| [iyunv@manager ~]# ssh-keygen
[iyunv@manager ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@master
[iyunv@manager ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@slave1
[iyunv@manager ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@slave2
|
master:
1
2
3
4
| [iyunv@master ~]# ssh-keygen
[iyunv@master ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@manager
[iyunv@master ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@slave1
[iyunv@master ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@slave2
|
slave1:
1
2
3
4
| [iyunv@slave1 ~]# ssh-keygen
[iyunv@slave1 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@manager
[iyunv@slave1 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@master
[iyunv@slave1 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@slave2
|
slave2:
1
2
3
4
| [iyunv@slave2 ~]# ssh-keygen
[iyunv@slave2 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@manager
[iyunv@slave2 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@master
[iyunv@slave2 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@slave1
|
四、安装mysql
在master,slave1,slave2上安装mysql服务。这里安装的是mysql-5.5.37.tar.gz,使用脚本进行安装
脚本内容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
| [iyunv@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,确保是唯一的
1
2
3
4
5
6
7
8
9
10
11
| #master的server-id
[iyunv@master ~]# grep server-id /etc/my.cnf
server-id = 1
#slave1的server-id
[iyunv@slave1 ~]# grep server-id /etc/my.cnf
server-id = 53
#slave2的server-id
[iyunv@slave2 ~]# grep server-id /etc/my.cnf
server-id = 53
|
在master,slave1上配置主从同步用的账号。slave1是备用的master,这个也需要进行授权。
|
|