此网络拓扑,大致可分为前方nginx+keepalived的调度器和后端的LAMP的web网站架构,在后方的LAMP的网站架构中的mysql被分离出去,成为一个相对独立的整体,修改配置文件记得备份。
配置大致步骤如下:
网卡配置
主机 | ip | 操作系统 | 安装软件 |
---|---|---|---|
nginx01+keepalived | ens33:20.0.0.50/24 ens33:1:20.0.0.8/24 | centos7 | nginx-1.12.0.tar.gz keepalived |
nginx02+keepalived | ens33:20.0.0.60/24 ens33:1:20.0.0.8/24 | centos7 | nginx-1.12.0.tar.gz keepalived |
mha_manager | ens33:20.0.0.10/24 | centos7 | mha4mysql-node-0.57.tar.gz mha4mysql-manager-0.57.tar.gz |
mysql1 | ens33:20.0.0.20/24 ens33:1:20.0.0.113/24 | centos7 | cmake-2.8.6.tar.gz mysql-5.6.36.tar.gz mha4mysql-node-0.57.tar.gz |
mysql2 | ens33:20.0.0.30/24 ens33:1:20.0.0.113/24 | centos7 | cmake-2.8.6.tar.gz mysql-5.6.36.tar.gz mha4mysql-node-0.57.tar.gz |
mysql3 | ens33:20.0.0.40/24 | centos7 | cmake-2.8.6.tar.gz mysql-5.6.36.tar.gz mha4mysql-node-0.57.tar.gz |
两台nginx服务器先安装nginx
两台Apache服务器安装Apache
阶段验证:可通过访问nginx访问Apache
两台nginx服务器开启keepalived功能
阶段验证:keepalived的双机热备
MHA高可用
阶段验证:mysql主从复制与高可用
PHP
阶段验证:测试PHP网页,验证与mysql的连接
论坛搭建
性能测试
服务器配置
改名
[root@localhost ~]#hostname set-hostname nginx01
[root@localhost ~]#su
防火墙设置
[root@nginx01 ~]#iptables -F
[root@nginx01 ~]#setenforce 0
nginx安装
安装nginx-1.12.0.tar.gz版本
[root@nginx01 ~]#yum -y install pcre-devel zlib-devel gcc gcc-c++
[root@nginx01 ~]#useradd -M -s /sbin/nologin nginx
[root@nginx01 ~]#tar zxvf nginx-1.12.0.tar.gz -C /usr/local/
[root@nginx01 ~]#cd /usr/local/nginx-1.12.0/
[root@nginx01 nginx-1.12.0]#./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
[root@nginx01 nginx-1.12.0]#make && make install
[root@nginx01 nginx-1.12.0]#ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@nginx01 nginx-1.12.0]#ln -s /usr/local/nginx/conf/nginx.conf /etc/nginx.conf
[root@nginx01 nginx-1.12.0]#nginx -t
[root@nginx01 nginx-1.12.0]#vim /etc/init.d/nginx
#!/bin/bash
#chkconfig:- 99 20
#description:Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage:$0 {start|stop|restart|reload}"
exit 1
esac
exit 0
[root@nginx01 nginx-1.12.0]#chmod +x nginx
[root@nginx01 nginx-1.12.0]#chkconfig --add nginx
[root@nginx01 nginx-1.12.0]#chkconfig --level 35 nginx on
[root@nginx01 nginx-1.12.0]#chkconfig --list
[root@nginx01 nginx-1.12.0]#service nginx start
修改nginx配置文件,使来自客户的请求web请求转发至后端
截取其中修改过的部分
[root@nginx01 ~]#cp -p /usr/local/nginx/conf/nginx.conf /usr/local/conf/nginx.conf.bak
[root@nginx01 ~]# vim /etc/nginx.conf
...
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
#添加下面三句,后端Apache的地址,apacheserver为自己取的名字,如果有下划线可能会出错
upstream apacheserver {
server 20.0.0.70;
server 20.0.0.80;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
添加下面的代理
proxy_pass http://apacheserver;
#proxy_set_header Host $host;
}
...
nginx02也像nginx01这样配置
[root@nginx01 ~]# yum -y install keepalived
[root@nginx01 ~]#cp -p /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
[root@nginx01 ~]#vim /etc/keepalived/keepalived.conf
#有些地方是要删除的
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_01
#这里删除了4行
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
#这个就是虚拟IP,客户访问的唯一入口
virtual_ipaddress {
20.0.0.8/24 dev ens33 label ens33:1
}
}
nginx02的配置也是这样的具体配置文件为
[root@nginx02 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_02
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
20.0.0.8/24 dev ens33 label ens33:1
}
}
查看当主keepalived故障后漂移地址的转移情况
[root@nginx01 ~]#service nginx stop
[root@nginx01 ~]#service nginx start
[root@nginx01 ~]#systemcl start keepalived
这时我们在nginx01上应该可以看到漂移地址20.0.0.8/24,在nginx02上是看不到的
[root@nginx01 ~]# ifconfig
ens33: flags=4163 mtu 1500
inet 20.0.0.50 netmask 255.255.255.0 broadcast 20.0.0.255
inet6 fe80::7c4f:bfde:e758:8b91 prefixlen 64 scopeid 0x20
ether 00:0c:29:53:5d:bc txqueuelen 1000 (Ethernet)
RX packets 11236 bytes 786457 (768.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 24554 bytes 1561779 (1.4 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:1: flags=4163 mtu 1500
inet 20.0.0.8 netmask 255.255.255.0 broadcast 0.0.0.0
ether 00:0c:29:53:5d:bc txqueuelen 1000 (Ethernet)
当我们把nginx01关机或者是关闭keepalived是漂移地址就可以到nginx02中
[root@nginx02 ~]# ifconfig
ens33: flags=4163 mtu 1500
inet 20.0.0.60 netmask 255.255.255.0 broadcast 20.0.0.255
inet6 fe80::58d2:f009:bf23:12f5 prefixlen 64 scopeid 0x20
ether 00:0c:29:e8:6c:85 txqueuelen 1000 (Ethernet)
RX packets 44701 bytes 2863318 (2.7 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5790 bytes 475084 (463.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:1: flags=4163 mtu 1500
inet 20.0.0.8 netmask 255.255.255.0 broadcast 0.0.0.0
ether 00:0c:29:e8:6c:85 txqueuelen 1000 (Ethernet)
这样keepalived的功能就验证成功,想要漂移地址回到nginx01 上的话,启动nginx01上的keepalived,没有漂移过去的话再停再起
服务器改名
[root@localhost ~]#hostnamectl set-hostname apache01
[root@localhost ~]#su
防火墙设置
[root@apache01 ~]# iptables -F
[root@apache01 ~]# setenforce 0
[root@apache01 ~]# systemctl stop firewalld
Apache安装
使用版本apr-1.6.2.tar.gz apr-util-1.6.0.tar.gz httpd-2.4.29.tar.bz2
在当前目录下解压
[root@apache01 ~]# tar zxvf apr-1.6.2.tar.gz ##apr软件包用于跨平台
[root@apache01 ~]# tar zxvf apr-util-1.6.0.tar.gz ##
[root@apache01 ~]# tar jxvf httpd.2.4.29.tar.bz2
移动apr和apr-util
[root@apache01 ~]# mv apr-1.6.2 httpd-2.4.29/srclib/apr
[root@apache01 ~]# mv apr-util-1.6.0 httpd-2.4.29/srclib/apr-until
配置安装环境
[root@apache01 apache]# yum -y install \
gcc \
gcc-c++ \
make \ ##把源码配置文件编译二进制机器可执行的二进制文件
pcre-devel \
expat-devel \ ##识别标签性语言,html xml
perl
yum -y install gcc gcc-c++ make pcre-devel expat-devel perl
编译安装
[root@apache01 apache]# cd httpd-2.4.29/
[root@apache01 apache]#./configure \
--prefix=/usr/local/httpd \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi
##指定安装位置,可自己指定
##开启核心模块
##重写功能,可实现防盗链等操作
##字符集
##通用网关接口,编码的规范
[root@apache01 apache]#make &&make install
启动优化
启动管理
[root@apache01 httpd-2.4.29]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd ##Apachectl启动脚本
[root@localhost httpd-2.4.29]# vim /etc/init.d/httpd
#!/bin/sh
#chkconfig:35 85 15 ##35级别自动启动,第85个开启,15个关闭
#description:Apache is a World Wide Web
#
...省略部分信息
保持退出
[root@apache01 httpd-2.4.29]# chkconfig --add httpd ##将httpd加到service管理器中
[root@apache01 httpd-2.4.29]# chkconfig httpd on
[root@apache01 httpd-2.4.29]# chkconfig --list
....
httpd 0:关 1:关 2:开 3:开 4:开 5:开 6:关
命令识别
[root@apache01 httpd-2.4.29]# ln -s /usr/local/httpd/bin/* /usr/local/bin/
配置文件
[root@apache01 httpd-2.4.29]# ln -s /usr/local/httpd/conf/httpd.conf /etc/http.conf
[root@apache01 httpd-2.4.29]# vim /etc/httpd.conf
...
Listen 0.0.0.0:80
#Listen 80
...
#
ServerName www.family.com:80
#
...
可用httpd -t 检查语法错误
[root@apache01 ~]#vim /usr/local/httpd/htdocs/index.html
apache1
启动
[root@apache01 ~]#service httpd start
apache02配置也是这样的,在Apache中还需要配置PHP和MySQL我们放到后面
先配置主mysql
改名
[root@mysql1 ~]# hostnamectl set-hostname mysql1
[root@mysql1 ~]# su
防火墙配置
[root@mysql1 ~]# iptables -F
[root@mysql1 ~]# setenforce 0
[root@mysql1 ~]# systemclt stop firewalld
MySQL安装
cmake-2.8.6.tar.gz mysql-5.6.36.tar.gz mha4mysql-node-0.57.tar.gz mha4mysql-manager-0.57.tar.gz
1.安装编译环境
[root@mysql1 ~]# yum -y install ncurses-devel gcc gcc-c++ perl-Module-Install
软件准备
cmake-2.8.6.tar.gz mysql-5.6.36.tar.gz mha4mysql-node-0.57.tar.gz
2.安装cmake编译软件
[root@mysql1 ~]# tar zxvf cmake-2.8.6.tar.gz
[root@mysql1 ~]# cd cmake-2.8.6/
[root@mysql1 cmake-2.8.6]# ./configure 配置它
[root@msyql1 cmake-2.8.6]# gmake && gmake install
3.安装MySQL数据库
[root@mysql1 ~]# tar zxvf mysql-5.6.36.tar.gz
[root@mysql1 ~]# cd mysql-5.6.36/
[root@mysql1 mysql-5.6.36]# cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DSYSCONFDIR=/etc
[root@mysql1 mysql-5.6.36]#make && make install
[root@mysql1 mysql-5.6.36]# cp support-files/my-default.cnf /etc/my.cnf
cp:是否覆盖"/etc/my.cnf"? yes
[root@mysql1 mysql-5.6.36]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@mysql1 mysql-5.6.36]# chmod +x /etc/rc.d/init.d/mysqld
[root@mysql1 mysql-5.6.36]# chkconfig --add mysqld
[root@mysql1 mysql-5.6.36]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
[root@mysql1 mysql-5.6.36]#source /etc/profile
[root@mysql1 mysql-5.6.36]# groupadd mysql
[root@mysql1 mysql-5.6.36]# useradd -M -s /sbin/nologin mysql -g mysql
[root@mysql1 mysql-5.6.36]# chown -R mysql.mysql /usr/local/mysql
[root@mysql1 mysql-5.6.36]#/usr/local/mysql/scripts/mysql_install_db \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data \
--user=mysql
4.修改master的主配置文件/etc/my.cnf文件,三台服务器的server-id不能一样
[root@mysql1 ~]#vim /etc/my.cnf
#复制到相应位置即可
[mysqld]
server-id = 1
log_bin = master-bin
log-slave-updates = true
配置从服务器
[root@mysql2 ~]#vim /etc/my.cnf
server-id = 2
log_bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
[root@mysql3 ~]#vim /etc/my.cnf
server-id = 3
log_bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
5.mysql1,mysql2,mysql3分别做两个软链接,方便使用
[root@mysql1~]#ln -s /usr/local/mysql/bin/mysql /usr/sbin/
[root@mysql1~]#ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
6.mysql1,mysql2,mysql3 启动mysql
[root@mysql1~]# /usr/local/mysql/bin/mysqld_safe --user=mysql &
[root@mysql1~]# netstat -antp |grep 3306
tcp6 0 0 :::3306 :::* LISTEN 23904/mysqld
[root@mysql1~]# mysql -uroot -p
7.配置MySQL 一主两从
(1)MySQL主从配置相对比较简单,需要注意的是授权。步骤如下:
在所有数据库节点上都需要授权两个用户,一个是从库同步使用,另一个是manager监控管理数据库使用
mysql>grant replication slave on *.* to 'myslave'@'20.0.0.%' identified by 'myslave123';
mysql>grant all privileges on *.* to 'mha'@'20.0.0.%' identified by 'mha123';
mysql>flush privileges;
(2)下面三条授权按理论是不用添加的,但是做案例实验环境时候通过MHA检查MySQL主从有报错,
报两个从库通过主机名连接不上主库,所以所有数据库加上下面的授权,这样mha也可使用主机名进行访问了。
mysql>grant all privileges on *.* to 'mha'@'mysql1' identified by 'mha123';
mysql>grant all privileges on *.* to 'mha'@'mysql2' identified by 'mha123';
mysql>grant all privileges on *.* to 'mha'@'mysql3' identified by 'mha123';
(3)在mysql1主机上查看二进制文件和同步点
mysql>show master status;
(4)接下来在mysql2和mysql3上分别执行同步
mysql>change master to master_host='20.0.0.20',master_user='myslave',master_password='myslave123',master_log_file='master-bin.000004',master_log_pos=120;
mysql>start slave;
(5)查看IO和SQL线程都是yes代表同步是否正常
mysql>show slave status\G;
slave_IO_Running:Yes
slave_SQL_Running:Yes
必须设置两个从库为只读模式:
mysql>set global read_only=1;
设置完成即可验证主从复制
mha_manager安装MHA软件
1.所有的服务器上都安装MHA依赖的环境,首先安装epel源
[root@mha_manager ~]#yum -y install epel-release --nogpgcheck
##perl组件
[root@mha_manager ~]#yum -y install perl-DBD-MySQL \
perl-Config-Tiny \
perl-Log-Dispatch \
perl-Parellel-ForkManager \
perl-ExtUtils-CBuilder \
perl-ExtUtils-MakeMaker \
perl-CPAN
2.MHA软件包对于每个操作系统版本都不一样,这里centos7.4必须选择0.57版本,
在所有服务器上必须安装node组件,最后在MHA-manager节点上安装manager组件,因为manager依赖node组件
,下面都是在mysql1上演示安装node组件
[root@mysql1 ~]# tar zxvf mha4mysql-node-0.57.tar.gz
[root@mysql1 ~]# cd mha4mysql-node-0.57
[root@mysql1 ~]# perl Makefile.PL
[root@mysql1 ~]# make
[root@mysql1 ~]# make install
3.在MHA-manager上安装manager组件(注意:一定要先安装node组件才能安装manager组件)
[root@mha_manager ~]#tar zxvf mha4mysql-manager-0.57.tar.gz
[root@mha_manager ~]#cd mha4mysql-manager-0.57
[root@mha_manager ~]#perl Makefile.PL
[root@mha_manager ~]#make
[root@mha_manager ~]#make install ##我装这个跳出来一大堆东西
以上2和3两步可能会跳出不同的操作
manager安装后再/usr/local/bin先会生成几个工具,主要包括以下一个:
masterha_check_ssh检查MHA的SSH 配置状况
masterha_check_repl检查MySQL复制状况
masterha_manger启动manager的脚本
masterha_check_status 检测当前MHA运行状态,检查节点状态
masterha_master_monitor 检测master是否宕机
masterha_master_switch 控制故障转移(自动或者手动)
masterha_conf_host 添加或删除配置的server信息
masterha_stop 关闭manager
4.node安装后也会在/usrl/local/bin下会生成几个脚本(这些工具通常由MHA Manager的脚本触发,无需认为操作)
主要如下:
save_binary_logs 保存和复制master的二进制日志
apply_diff_relay_logs 识别差异的中继日志事件并将其差异的事件应用于其他的slave
filter_mysqlbinlog 去除不必要的ROLLBACK事件(MHA已不再使用这个工具)
purge_relay_logs 清除中继日志(不会阻塞SQL线程)
6.配置无密码认证(秘钥对)
(1)在manager在配置到所有数据库节点的无密码认证
[root@mha_manager ~]#ssh-keygen -t rsa //一路按回车
[root@mha_manager ~]#ssh-copy-id 20.0.0.20 //输入相应root密码即可
[root@mha_manager ~]#ssh-copy-id 20.0.0.30
[root@mha_manager ~]#ssh-copy-id 20.0.0.40
(2)在mysql1上配置到数据库节点mysql2和mysql3的无密码认证
[root@mysql1 ~]# ssh-keygen -t rsa
[root@mysql1 ~]# ssh-copy-id 20.0.0.30
[root@mysql1 ~]# ssh-copy-id 20.0.0.40
(3)在mysql2上配置到数据库节点mysql1和mysql3的无密码认证
[root@mysql2 ~]# ssh-keygen -t rsa
[root@mysql2 ~]# ssh-copy-id 20.0.0.20
[root@mysql2 ~]# ssh-copy-id 20.0.0.40
(4)在mysql3上配置到数据库节点mysql1和mysql2的无密码认证
[root@mysql3 ~]#ssh-keygen -t rsa
[root@mysql3 ~]#ssh-copy-id 20.0.0.20
[root@mysql3 ~]#ssh-copy-id 20.0.0.30
6.配置MHA
(1)在manager节点复制相关脚本到/usr/local/bin目录
[root@mha_manager ~]# cp -ra /root/mha4mysql-manager-0.57/samples/scripts/ /usr/local/bin
//拷贝后会有四个执行文件
ll /usr/local/bin/scripts/
-rwXr-Xr-x 1 mysql mysql 36485月312015 master_ip_failover#自动切换时VIP管理的脚本
-rWXr-Xr-x 1 mysql mysql 98725月2509:07 master_ip_online_change#在线切换时vip的管理
-rWXr-Xr-x 1 mysql mysql 118675月312015 power_manager#故障发生后关闭主机的脚本
-rWxr-xr-x 1 mysql mysql 1360 5月312015 send_report #因故障切换后发送报警的脚本
(2)复制上述的自动切换时VIP管理的脚本到/usr/local/bin目录,这里使用的脚本管理VIP
[root@mha_manager ~]#cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin
(3)修改内容如下:(删除原有内容,直接复制),故障转移的配置文件
[root@mha_manager ~]#vim /usr/local/bin/master_ip_failover
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use Getopt::Long;
my (
$command, $ssh_user, $orig_master_host, $orig_master_ip,
$orig_master_port, $new_master_host, $new_master_ip, $new_master_port
);
#############################添加内容部分#########################################
my $vip = '20.0.0.113';
my $brdc = '20.0.0.255';
my $ifdev = 'ens33';
my $key = '1';
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down";
my $exit_code = 0;
#my $ssh_start_vip = "/usr/sbin/ip addr add $vip/24 brd $brdc dev $ifdev label $ifdev:$key;/usr/sbin/arping -q -A -c 1 -I $ifdev $vip;iptables -F;";
#my $ssh_stop_vip = "/usr/sbin/ip addr del $vip/24 dev $ifdev label $ifdev:$key";
##################################################################################
GetOptions(
'command=s' => \$command,
'ssh_user=s' => \$ssh_user,
'orig_master_host=s' => \$orig_master_host,
'orig_master_ip=s' => \$orig_master_ip,
'orig_master_port=i' => \$orig_master_port,
'new_master_host=s' => \$new_master_host,
'new_master_ip=s' => \$new_master_ip,
'new_master_port=i' => \$new_master_port,
);
exit &main();
sub main {
print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";
if ( $command eq "stop" || $command eq "stopssh" ) {
my $exit_code = 1;
eval {
print "Disabling the VIP on old master: $orig_master_host \n";
&stop_vip();
$exit_code = 0;
};
if ($@) {
warn "Got Error: $@\n";
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "start" ) {
my $exit_code = 10;
eval {
print "Enabling the VIP - $vip on the new master - $new_master_host \n";
&start_vip();
$exit_code = 0;
};
if ($@) {
warn $@;
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "status" ) {
print "Checking the Status of the script.. OK \n";
exit 0;
}
else {
&usage();
exit 1;
}
}
sub start_vip() {
`ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
# A simple system call that disable the VIP on the old_master
sub stop_vip() {
`ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}
sub usage {
print
"Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n";
}
(4)创建MHA软件目录并拷贝配置文件
[root@mha_manager ~]#mkdir /etc/masterha
[root@mha_manager ~]# cp /root/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha/
[root@manager ~]# vim /etc/masterha/app1.cnf
[server default]
manager_workdir=/var/log/masterha/app1.log
manager_log=/var/log/masterha/app1/manager.log
master_binlog_dir=/usr/local/mysql/data/
master_ip_failover_script=/usr/local/bin/master_ip_failover
master_ip_online_change_script=/usr/local/bin/master_ip_online_change
password=mha123
user=mha
ping_interval=1
remote_workdir=/tmp
repl_password=myslave123
repl_user=myslave
report_script=/usr/local/send_report
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 20.0.0.31 -s 20.0.0.100
shutdown_script=""
ssh_user=root
[server1]
hostname=20.0.0.61
port=3306
[server2]
hostname=20.0.0.31
port=3306
candidate_master=1
check_repl_delay=0
[server3]
hostname=20.0.0.100
port=3306
(5)测试SSH无密码认证,如果正常最后会输出successful
[root@mha_manager ~]#masterha_check_ssh -conf=/etc/masterha/app1.cnf
(6)健康检查,成功会显示is ok要是is not ok的话,那自己改去
[root@mha_manager ~]#masterha_check_repl -conf=/etc/masterha/app1.cnf
(7)第一次配置需要去master上手动开启虚拟ip
[root@mysql1 ~]# /sbin/ifconfig ens33:1 20.0.0.113/24
(8)启动MHA
[root@mha_manager ~]#nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha/app1/manager.log 2>&1 &
或者
[root@mha_manager ~]#nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf < /dev/null > /var/log/masterha/app1/manager.log 2>&1 &
我创建了一个日志目录/var/log/masterha/app1/manager.log
(9)查看数据库主从状态
[root@mha_manager ~]# masterha_check_status --conf=/etc/masterha/app1.cnf
app1 (pid:10602) is running(0:PING_OK), master:20.0.0.61
主从复制就是在mysql1上创建数据库在mysql2和mysql3上都可以看到
MHA就是正常工作时漂移地址20.0.0.113在mysql1上,mysql1宕机都漂移到mysql2上且不影响使用
编译安装
php-5.6.11.tar.bz2
[root@apache01 ~]# tar zxvf php-5.6.11.tar.bz2
[root@apache01 ~]# yum -y install \
libjpeg \
libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 \
libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel
[root@apache01 ~]# cd php-5.6.11/
[root@apache01 ~]#./configure \
--prefix=/usr/local/php5 \
--with-config-file-path=/usr/local/php5 \
--with-apxs2=/usr/local/httpd/bin/apxs \
--enable-fpm \
--with-fpm-user=php-fpm \
--with-fpm-group=php-fpm \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip
[root@apache01 ~]# make &&make install
[root@apache01 ~]#useradd -s /sbin/nologin php-fpm
[root@apache01 php-5.6.11]# cp php.ini-development /usr/local/php5/lib/php.ini
[root@apache01 php-5.6.11]# cd /usr/local/php5/etc/
[root@apache01 etc]# cp php-fpm.conf.default php-fpm.conf
[root@apache01 etc]# vim php-fpm.conf
...
pid = run/php-fpm.pid
...
listen 0.0.0.0:9000
[root@apache01 php-fpm.d]# /usr/local/php5/sbin/php-fpm -c /usr/local/php/lib/php.ini
[root@apache01 php-fpm.d]# netstat -antp |grep 9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 44041/php-fpm: mast
修改Apache配置文件
[root@apache01 ~]# vim /etc/httpd.conf
...
#添加PHP类型的网页
DirectoryIndex index.html index.php
#查看是否有这个模块,没有的话,重来吧
LoadModule php5_module modules/libphp5.so
...
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
#添加下面两端配置,支持PHP源码
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
#找到下面两段配置,去掉前面的#注释
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
修改nginx配置文件
[root@nginx01 ~]# vim /etc/nginx.conf
...
#配置文件中本来就有这段配置,将#注释去掉,在如下修改即可,20.0.0.70为后方的Apache服务器
location ~ \.php$ {
proxy_pass http://20.0.0.70;
}
编写PHP网页
[root@apache01 ~]# vim /usr/local/httpd/htdocs/index.php
编写连接测试mysql的PHP网页
[root@apache01 ~]# vim /usr/local/httpd/htdocs/testmysql.php
进到所以mysql服务器上去授权允许Apache服务器连接
mysql>grant all privileges on *.* to 'root'@'%' identified by '123456'
客户端访问20.0.0.8/index.php测试PHP的功能
和20.0.0.8/testmysql.php测试能否连接上后后方的mysql
为论坛创建一个数据库
在mysql1上
[root@msyql1 dis]# mysql -u root -p
Enter password:
mysql> CREATE DATABASE bbs;
mysql> GRANT all ON bbs.* TO 'bbsuser'@'%' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
安装包准备
[root@apache01 ~]# mkdir dis
[root@apache01 dis]# ls
Discuz_X2.5_SC_UTF8.zip
[root@apache01 dis]#
[root@apache01 dis]# unzip Discuz_X2.5_SC_UTF8.zip
[root@apache01 dis]# ls
Discuz_X2.5_SC_UTF8.zip readme upload utility
捞出upload
[root@apache01 dis]# cp -r upload/ /usr/local/httpd/htdocs/bbs
[root@apache01 dis]# cd /usr/local/httpd/htdocs/bbs ##这里面由论坛的所有文件
[root@localhost bbs]# ls
admin.php config data home.php misc.php search.php uc_client
api connect.php favicon.ico index.php plugin.php source uc_server
api.php cp.php forum.php install portal.php static userapp.php
archiver crossdomain.xml group.php member.php robots.txt template
修改权限
[root@localhost bbs]# chown -R daemon ./config
[root@localhost bbs]# chown -R daemon ./data
[root@localhost bbs]# chown -R daemon ./uc_client/
[root@localhost bbs]# chown -R daemon ./uc_server/data/
修改nginx配置文件
[root@nginx01 ~]# vim /etc/nginx.conf
location / {
root html;
index index.html index.htm;
proxy_pass http://apacheserver;
}
#添加以下配置
location /bbs {
rewrite (.+) http://20.0.0.70/$1 permanent;
}
去浏览器登 http://20.0.0.8/bbs,即可安装论坛
服务器地址改为20.0.0.113(mysql的漂移地址) 数据库名(bbs)用户名(bbsuser) 用户密码 (123456) 管理员账号和密码(自己填即可)
这些都是我们之前授权过的
整体架构配置完成后,大致功能是用户请求访问keepalived漂移地址20.0.0.8不管是动态还是静态都会转移给Apache服务器(不过我只在apache01上配置了PHP和论坛),论坛数据全部由mysql提供支持。不管是nginx01宕机还是mysql1宕机都不会影响整个架构的运行。