群集架构篇

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了群集架构篇相关的知识,希望对你有一定的参考价值。

——nginx反向代理+keepalived双机热备+tomcat服务器池+后端数据库

目录
第一部分 环境准备
第二部分 部署调度器—搭建Nginx+Keepalived(双机热备)
第三部分 部署服务器池—搭建Tomcat
第四部分 搭建mysql数据库
第五部分 案例应用

第一部分 环境准备

一:Nginx+keepalived服务器两台(调度器,双机热备)
系统:Linux—CentOS7.4
IP地址:192.168.40.15
192.168.40.16
软件需求:nginx安装包(nginx-1.13.9.tar.gz)
Keepalived安装包(keepalived-1.4.2.tar.gz)

二:tomcat服务器两台(服务器池)
系统:Linux—CentOS7.4
IP地址:192.168.40.18(TM01)
192.168.40.19(TM02)
软件需求:java环境jdk包(jdk-8u144-linux-x64.tar.gz)
tomcat安装包(apache-tomcat-8.5.23.tar.gz)
SL会员商城项目软件包(SLSaleSystem.tar.gz)

三:Mysql服务器一台
系统:Linux-CentOS7.4
IP地址:192.168.40.30
软件需求:mysql安装包(mysql-boost-5.7.20.tar.gz)
SL会员商场数据库文件(slsaledb-2014-4-10.sql)

四:客户端一台,以本机为例,测试验证用
IP地址:192.168.40.12
//Linux系统信息

技术分享图片

第二部分 部署调度器—搭建Nginx+Keepalived(双机热备)

//以下在两台Nginx调度服务器上操作

第一步:配置主服务器(192.168.40.15)

-------安装nginx服务-------
[[email protected] ~]# yum install -y //安装编译工具及插件

gcc
gcc-c++
make
openssl-devel
zlib-devel
pcre-devel
[[email protected] ~]# useradd -s /sbin/nologin -M nginx //添加帐号
[[email protected] ~]# tar vxf nginx-1.13.9.tar.gz //解压nginx安装包
[[email protected] ~]# cd nginx-1.13.9
[[email protected] ~ nginx-1.13.9]# ./configure //个性化配置
--user=nginx
--group=nginx
--with-file-aio
--with-http_flv_module
--with-http_stub_status_module
--with-http_ssl_module
--with-http_gzip_static_module
--with-http_realip_module
[[email protected] nginx-1.13.9]# make //编译
[[email protected] nginx-1.13.9]# make install //安装
//以下编译nginx主配置文件
[[email protected] nginx-1.13.9]# vi /usr/local/nginx/conf/nginx.conf
user nginx nginx;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;

pid logs/nginx.pid;

events {
worker_connections 1024;
}

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;
    add_header X-Server $hostname;
    server_names_hash_bucket_size 128;
    server_name_in_redirect off;

sendfile        on;
tcp_nopush     on;
tcp_nodelay on;

#keepalive_timeout  0;
keepalive_timeout  60;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 128k;
    client_max_body_size 512m;
    open_file_cache max=65535 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 1;

gzip  on;
    gzip_static on;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_min_length 1024;
    gzip_vary on;
    gzip_types text/plain text/javascript application/x-javascript text/css text/xml application/xml application/xml+rss;

server_tokens off;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 512k;
fastcgi_buffers 6 512k;
fastcgi_busy_buffers_size 512k;
fastcgi_temp_file_write_size 512k;
fastcgi_intercept_errors on;
client_body_buffer_size 128k;

    proxy_connect_timeout 600;
    proxy_read_timeout 600;
    proxy_send_timeout 600;
    proxy_buffer_size 32k;
    proxy_buffers 4 32k;
    proxy_busy_buffers_size 64k;
    proxy_temp_file_write_size 2m;
    proxy_ignore_client_abort on;
    proxy_cache_path /usr/local/nginx/cache_temp levels=2:2

keys_zone=cache_temp:128m inactive=30m max_size=2g;
proxy_cache_valid 200 302 10m;
include /usr/local/nginx/conf/conf.d/*.conf;

server {
    listen       80;
    server_name  localhost;

    charset UTF-8;

    #access_log  logs/host.access.log  main;

    location / {
        root   html;
        index  index.html index.htm;
    }

}
保存退出
//以下编辑子配置文件
[[email protected] nginx-1.13.9]# cd /usr/local/nginx/conf/
[[email protected] conf]# mkdir conf.d
[[email protected] conf]# cd conf.d/
[[email protected]]# vi lvs01.conf //新建子配置文件
server {
listen 80;
server_name localhost 192.168.40.15; #服务器名称与IP地址
index index.html index.jsp;
root /usr/local/nginx/html;
access_log /usr/local/nginx/logs/tomcat.aa.com_access.log main;
location ~ ..jsp$ {
index index.jsp;
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Client-IP $remote_addr;
proxy_set_header X-For $proxy_add_x_forwarded_for;
proxy_pass http://center_pool;
}
location ~ .
.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 30d;
proxy_pass http://center_pool;
}
location ~ .*.(js|css)?$ {
expires 1h;
proxy_pass http://center_pool;

    }

}
[[email protected] conf.d]# vi pool.conf //创建服务器池
upstream center_pool { #默认轮询
server 192.168.40.18:8080;
server 192.168.40.19:8080;
}
//制作启动脚本
[[email protected] conf.d]# vi /etc/init.d/nginx
#!/bin/bash
# chkconfig: 35 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

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[[email protected] conf.d]# chmod +x /etc/init.d/nginx //增加执行权限
[[email protected] conf.d]# chkconfig --add nginx //加入系统管理服务
[[email protected] conf.d]# service nginx start //启动nginx服务
[[email protected] conf.d]# netstat -anpt | grep 80

-------------部署keepalived-----------
[[email protected] conf.d]#
yum -y install
popt-devel
kernel-devel
openssl-devel
[[email protected] ~]# tar xvf keepalived-1.4.2.tar.gz
[[email protected] ~]# cd keepalived-1.4.2
[[email protected] keepalived-1.4.2]# ./configure --prefix=/ //配置
[[email protected] keepalived-1.4.2]# make && make install //编译与安装
[[email protected] keepalived-1.4.2]# cp keepalived/etc/init.d/keepalived /etc/init.d/
[[email protected] keepalived-1.4.2]# systemctl enable keepalived //设置开机自启
//以下编辑keepalived配置文件
[[email protected] keepalived-1.4.2]# cd /etc/keepalived/
[[email protected] keepalived]# vi keepalived.conf
! Configuration File for keepalived
global_defs {
route_id NGINX-01
}
vrrp_script nginx {
script "/opt/nginx.sh"
interval 2
weight -10
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
nginx
}
virtual_ipaddress {
192.168.40.100
}
}
//注意,主备的优先级配置的相差50

//判断keepalived进程是否存在,在就启动nginx不在就关闭
[[email protected] keepalived]# vi /opt/nginx.sh
#!/bin/bash
#Filename:nginx.sh
A=$(ps -ef | grep keepalived | grep -v grep | wc -l)
if [ $A -gt 0 ]; then
/etc/init.d/nginx start
else
/etc/init.d/nginx stop
Fi
[[email protected] keepalived]# chmod +x /opt/nginx.sh
[[email protected] keepalived]# systemctl start keepalived
[[email protected] ~]# ip addr show dev ens33 //查看漂移地址是否生成

技术分享图片

[[email protected] ~]#systemctl status nginx
[[email protected] ~]#systemctl status keeplived

技术分享图片

---------------测试验证------------
[[email protected] ]# systemctl stop keepalived //关闭keepalived服务
[[email protected] ]# systemctl status keepalived //查看其状态
[[email protected] ]# ip addr show dev ens33 //查看漂移地址是否还在

技术分享图片

[[email protected] keepalived]# systemctl start keepalived //开启keepalived服务
[[email protected] keepalived]# netstat -anpt | grep 80 //nginx随keepalived启动

技术分享图片

//验证成功

第二步:配置从服务器(192.168.40.16)

-------安装nginx服务-------
[[email protected] ~]# yum install -y //安装编译工具及插件

gcc
gcc-c++
make
openssl-devel
zlib-devel
pcre-devel
[[email protected] ~]# useradd -s /sbin/nologin -M nginx //添加帐号
[[email protected] ~]# tar vxf nginx-1.13.9.tar.gz //解压nginx安装包
[[email protected] ~]# cd nginx-1.13.9
[[email protected] ~ nginx-1.13.9]# ./configure //个性化配置
--user=nginx
--group=nginx
--with-file-aio
--with-http_flv_module
--with-http_stub_status_module
--with-http_ssl_module
--with-http_gzip_static_module
--with-http_realip_module
[[email protected] nginx-1.13.9]# make //编译
[[email protected] nginx-1.13.9]# make install //安装
//以下编译nginx主配置文件
[[email protected] nginx-1.13.9]# vi /usr/local/nginx/conf/nginx.conf
user nginx nginx;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;

pid logs/nginx.pid;

events {
worker_connections 1024;
}

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;
    add_header X-Server $hostname;
    server_names_hash_bucket_size 128;
    server_name_in_redirect off;

sendfile        on;
tcp_nopush     on;
tcp_nodelay on;

#keepalive_timeout  0;
keepalive_timeout  60;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 128k;
    client_max_body_size 512m;
    open_file_cache max=65535 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 1;

gzip  on;
    gzip_static on;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_min_length 1024;
    gzip_vary on;
    gzip_types text/plain text/javascript application/x-javascript text/css text/xml application/xml application/xml+rss;

server_tokens off;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 512k;
fastcgi_buffers 6 512k;
fastcgi_busy_buffers_size 512k;
fastcgi_temp_file_write_size 512k;
fastcgi_intercept_errors on;
client_body_buffer_size 128k;

    proxy_connect_timeout 600;
    proxy_read_timeout 600;
    proxy_send_timeout 600;
    proxy_buffer_size 32k;
    proxy_buffers 4 32k;
    proxy_busy_buffers_size 64k;
    proxy_temp_file_write_size 2m;
    proxy_ignore_client_abort on;
    proxy_cache_path /usr/local/nginx/cache_temp levels=2:2

keys_zone=cache_temp:128m inactive=30m max_size=2g;
proxy_cache_valid 200 302 10m;
include /usr/local/nginx/conf/conf.d/*.conf;

server {
    listen       80;
    server_name  localhost;

    charset UTF-8;

    #access_log  logs/host.access.log  main;

    location / {
        root   html;
        index  index.html index.htm;
    }

}
保存退出
//以下编辑子配置文件
[[email protected] nginx-1.13.9]# cd /usr/local/nginx/conf/
[[email protected] conf]# mkdir conf.d
[[email protected] conf]# cd conf.d/
[[email protected]]# vi lvs01.conf //新建子配置文件
server {
listen 80;
server_name localhost 192.168.40.16; #服务器名称与IP地址
index index.html index.jsp;
root /usr/local/nginx/html;
access_log /usr/local/nginx/logs/tomcat.aa.com_access.log main;
location ~ ..jsp$ {
index index.jsp;
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Client-IP $remote_addr;
proxy_set_header X-For $proxy_add_x_forwarded_for;
proxy_pass http://center_pool;
}
location ~ .
.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 30d;
proxy_pass http://center_pool;
}
location ~ .*.(js|css)?$ {
expires 1h;
proxy_pass http://center_pool;

    }

}
[[email protected] conf.d]# vi pool.conf //创建服务器池
upstream center_pool { #默认轮询
server 192.168.40.18:8080;
server 192.168.40.19:8080;
}
//制作启动脚本
[[email protected] conf.d]# vi /etc/init.d/nginx
#!/bin/bash
# chkconfig: 35 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

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[[email protected] conf.d]# chmod +x /etc/init.d/nginx //增加执行权限
[[email protected] conf.d]# chkconfig --add nginx //加入系统管理服务
[[email protected] conf.d]# service nginx start //启动nginx服务
[[email protected] conf.d]# netstat -anpt | grep 80

-------------部署keepalived-----------
[[email protected] conf.d]#
yum -y install
popt-devel
kernel-devel
openssl-devel
[[email protected] ~]# tar xvf keepalived-1.4.2.tar.gz
[[email protected] ~]# cd keepalived-1.4.2
[[email protected] keepalived-1.4.2]# ./configure --prefix=/ //配置
[[email protected] keepalived-1.4.2]# make && make install //编译与安装
[[email protected] keepalived-1.4.2]# cp keepalived/etc/init.d/keepalived /etc/init.d/
[[email protected] keepalived-1.4.2]# systemctl enable keepalived //设置开机自启
//以下编辑keepalived配置文件(注意和主服务器不同之处)
[[email protected] keepalived-1.4.2]# cd /etc/keepalived/
[[email protected] keepalived]# vi keepalived.conf
! Configuration File for keepalived
global_defs {
route_id NGINX-02
}
vrrp_script nginx {
script "/opt/nginx.sh"
interval 2
weight -10
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
nginx
}
virtual_ipaddress {
192.168.40.100
}
}
//注意,主备的优先级配置的相差50
//判断keepalived进程是否存在,在就启动nginx不在就关闭
[[email protected] keepalived]# vi /opt/nginx.sh
#!/bin/bash
#Filename:nginx.sh
A=$(ip addr | grep 192.168.40.100/32 | grep -v grep | wc -l)
if [ $A -gt 0 ]; then
/etc/init.d/nginx start
else
/etc/init.d/nginx stop
fi
[[email protected] keepalived]# chmod +x /opt/nginx.sh
[[email protected] ~]# systemctl start keepalived

----------测试验证----------
一:查看从服务器状态
[[email protected] ~]# ip addr show dev ens33 //查看漂移地址

技术分享图片

//由于主服务器在运行,漂移地址并未同步过来

二:模拟主服务故障

  1. 主服务器
    [[email protected] ~]#systemctl stop keepalived
    [[email protected] ~]#systemctl status keepalived
    [[email protected] ~]# ip addr show dev ens33

技术分享图片

//漂流地址已经不在此服务器上

2.从服务器
[[email protected] ~]#systemctl status nginx
[[email protected] ~]#systemctl status keepalived

技术分享图片

[[email protected] ~]#ip addr show dev ens33

技术分享图片

//服务器检测到虚拟地址,nginx服务自动启动开始工作

三:模拟主服务器恢复工作

  1. 主服务器

[[email protected] ~]#systemctl start keepalived
[[email protected] ~]#ip addr show dev ens33

技术分享图片

[[email protected] ~]# netstat -anpt | grep 80

技术分享图片

//主服务器已恢复工作

  1. 从服务器
    [[email protected] ~]# ip addr show dev ens33

技术分享图片

//漂移地址自动移除
//nginx自动停止服务
//双机热备验证成功

第三部分 部署服务器池—搭建Tomcat

//以下在两台tomcat服务器上操作
第一步:部署第一个节点服务器TM01(192.168.40.18)
----------部署java环境,jdk---------
[[email protected] ~]# tar xvf jdk-8u144-linux-x64.tar.gz //解压jdk
[[email protected] ~]# cp -r jdk1.8.0_144/ /usr/local/java //创建java源目录
[[email protected] ~]# vi /etc/profile
最后添加以下内容:
export JAVA_HOME=/usr/local/java
export JRE_HOME=/usr/local/java/jre
export PATH=$PATH:/usr/local/java/bin
export CLASSPATH=./:/usr/local/java/lib:/usr/local/java/jre/lib
保存退出
[[email protected] ~]# source /etc/profile //立即生效
[[email protected] ~]# java –version //查看版本

技术分享图片

//java环境部署完成

----------部署tomcat----------
[[email protected] ~]# tar xvf apache-tomcat-8.5.23.tar.gz //解压apache-tomcat
[[email protected] ~]# cp -r apache-tomcat-8.5.23 /usr/local/tomcat8 //创建tomcat源目录
//做个软链接,使tomcat开启与关闭更加方便
[[email protected] ~]# ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup
[[email protected] ~]# ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown
[[email protected] ~]# tomcatup //开启tomcat
[[email protected] ~ ]# tomcatdown //关闭 Tomcat
[[email protected] ~]# tomcatup //再次开启tomcat
//目的是防止Tomcat没有开启
报错是在Tomcatdown上报error
[[email protected] ~]# netstat -anput | grep 80

技术分享图片

-----------验证------------

  1. 真机下访问默认主页:http://192.168.40.18:8080

技术分享图片

//tomcat部署成功

  1. 服务器池中有两台tomcat服务器,为了便于识别,主页上添加点标记
    [[email protected] ~]# vi /usr/local/tomcat8/webapps/ROOT/index.jsp
    添加一行内容:

技术分享图片

保存退出
[[email protected] ~ ]# tomcatdown //关闭 Tomcat
[[email protected] ~]# tomcatup //开启tomcat
//再次访问默认主页http://192.168.40.18:8080

技术分享图片

第二步:部署第二个节点服务器TM02(192.168.40.19)

----------部署java环境,jdk---------
[[email protected] ~]# tar xvf jdk-8u144-linux-x64.tar.gz //解压jdk
[[email protected]~]# cp -r jdk1.8.0_144/ /usr/local/java //创建java源目录
[[email protected] ~]# vi /etc/profile
最后添加以下内容:
export JAVA_HOME=/usr/local/java
export JRE_HOME=/usr/local/java/jre
export PATH=$PATH:/usr/local/java/bin
export CLASSPATH=./:/usr/local/java/lib:/usr/local/java/jre/lib
保存退出
[[email protected] ~]# source /etc/profile //立即生效
[[email protected] ~]# java –version //查看版本

技术分享图片

//java环境部署完成

----------部署tomcat----------
[[email protected] ~]# tar xvf apache-tomcat-8.5.23.tar.gz //解压apache-tomcat
[[email protected] ~]# cp -r apache-tomcat-8.5.23 /usr/local/tomcat8 //创建tomcat源目录
//做个软链接,使tomcat开启与关闭更加方便
[[email protected] ~]# ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup
[[email protected] ~]# ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown
[[email protected] ~]# tomcatup //开启tomcat
[[email protected] ~ ]# tomcatdown //关闭 Tomcat
[[email protected] ~]# tomcatup //再次开启tomcat
//目的是防止Tomcat没有开启
报错是在Tomcatdown上报error
[[email protected] ~]# netstat -anput | grep 80

技术分享图片

-----------验证------------

  1. 真机下访问默认主页:http://192.168.40.19:8080

技术分享图片

[[email protected] ~]# vi /usr/local/tomcat8/webapps/ROOT/index.jsp
添加一行内容:

技术分享图片

保存退出
[[email protected] ~ ]# tomcatdown //关闭 Tomcat
[[email protected] ~]# tomcatup //开启tomcat
//再次访问默认主页http://192.168.40.19:8080

技术分享图片

//tomcat部署成功

第三步:验证nginx调度器漂移地址轮询
真机访问http://192.168.40.100/index.jsp

技术分享图片

再次访问

技术分享图片

//轮询验证成功且不影响单独的nginx访问

技术分享图片

第四部分 搭建Mysql数据库

//以下在mysql数据库上操作(192.168.40.30)
一:安装编译工具与插件
[[email protected] ~]# yum -y install

gcc
gcc-c++
make
ncurses
ncurses-devel
bison
cmake

二:建立数据库程序用户
[[email protected] ~]# useradd -s /sbin/nologin mysql

三:解压mysql安装包
[[email protected] ~]# tar xf mysql-boost-5.7.20.tar.gz –C /opt/

四:配置(个性化配置及优化项目
[[email protected] ~]# cd /opt/mysql-5.7.20/
[[email protected] mysql-5.7.20]# cmake

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql //定义安装目录
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock //连接数据库socket路径
-DSYSCONFDIR=/etc //指定初始化参数文件目录(my.cnf)
-DSYSTEMD_PID_DIR=/usr/local/mysql //数据库目录
-DDEFAULT_CHARSET=utf8 //指定默认使用的字符集编码
-DDEFAULT_COLLATION=utf8_general_ci //指定默认使用的字符集校对规则,utf8_general_ci是适用于UTF-8字符集的通用规则
-DWITH_INNOBASE_STORAGE_ENGINE=1 //支持InnoDB引擎
-DWITH_ARCHIVE_STORAGE_ENGINE=1
-DWITH_BLACKHOLE_STORAGE_ENGINE=1
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 //安装支持数据库分区
-DMYSQL_DATADIR=/usr/local/mysql/data
-DWITH_BOOST=boost
-DWITH_SYSTEMD=1

五:编译与安装
[[email protected] mysql-5.7.20]# make
[[email protected] mysql-5.7.20]# make install

//对数据库目录进行权限配置
[[email protected] mysql-5.7.20]# chown -R mysql.mysql /usr/local/mysql/

六:编辑Mysql主配置文件
[[email protected] mysql-5.7.20]# vi /etc/my.cnf
(里面内容全部删除,替换成以下内容)
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
//保存退出

[[email protected] mysql-5.7.20]# chown mysql:mysql /etc/my.cnf //配置文件属主与属组

七:设置环境变量
[[email protected] mysql-5.7.20]# echo ‘PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH‘ >> /etc/profile
//把这两个路径添加到环境变量中,并放到profile文件中使之开机自运行,否则不生效
[email protected] mysql-5.7.20]# echo ‘export PATH‘ >> /etc/profile
//设为全局变量,使它使用环境更广
[[email protected] mysql-5.7.20]# source /etc/profile //立即生效

八:初始化数据库
[[email protected] ~]# cd /usr/local/mysql/
[[email protected] mysql]# bin/mysqld

--initialize-insecure
--user=mysql
--basedir=/usr/local/mysql
--datadir=/usr/local/mysql/data

九:开启Mysql服务
[[email protected] mysql]# cp usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
[[email protected] mysql]# systemctl daemon-reload
[[email protected] mysql]# systemctl start mysqld
[[email protected] mysql]# netstat -anpt | grep 3306 //查看服务运行状态

技术分享图片

[[email protected] mysql]# systemctl enable mysqld //设为开机启动

十:访问数据库操作
[[email protected] mysql]# mysqladmin -u root -p password "abc123"
//给root账号设置密码为abc123
注意:提示输入的是原始密码(原始没有密码,直接回车)
[[email protected] mysql]# mysql -u root -p //登录数据库
注意:有密码的加“-p”,如果没有密码不用加“-p”

技术分享图片

//数据库安装配置成功,退出则输入”quit“

第五部分 案例应用

第一步:Tomcat服务器下操作(两台tomcat都要操作)
[[email protected] ~]# cd /usr/local/tomcat8/conf/
[[email protected] conf]# vi server.xml
在/Host name下面新增一行:
<Context path="" docBase="SLSaleSystem" reloadable="true" debug="0"></Context>

技术分享图片

保存退出

第二步:在mysql服务器上操作
[[email protected] ~]# mysql -u root -p //登录数据库,输入密码abc123
create database slsaledb; //创建数据库slsaledb
GRANT all ON slsaledb.* TO ‘testuser‘@‘%‘ IDENTIFIED BY ‘admin123‘; //授权testuser用户(密码为admin123)拥有slsaledb数据库的所有权限
flush privileges; //刷新权限

技术分享图片

quit 退出
[[email protected] ~]# mysql -u root -p < slsaledb-2014-4-10.sql //上传slsaledb数据库
//用root进数据库查看

技术分享图片

第三步:在tomcat服务器上操作

  1. TOM01(192.168.40.18):
    [[email protected] ~]# tar xf SLSaleSystem.tar.gz -C /usr/local/tomcat8/webapps/
    [[email protected] ~]# cd /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes
    [[email protected] classes]# vi jdbc.properties

技术分享图片

保存退出

  1. TOM02(192.168.40.19):
    [[email protected] ~]# tar xf SLSaleSystem.tar.gz -C /usr/local/tomcat8/webapps/
    [[email protected] ~]# cd /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes
    [[email protected] classes]# vi jdbc.properties

技术分享图片

保存退出

第四步:验证
一: 验证tomcat连接
真机下访问:http://192.168.40.18:8080(TOM01

技术分享图片

输入管理员帐号admin,密码123456

技术分享图片

访问:http://192.168.40.19:8080(TOM02

技术分享图片

输入管理员帐号admin,密码123456

技术分享图片

二:验证nginx调度器

在nginx主配置文件里加上一个Tomcat地址池 在server上面加上此命令
vi /usr/local/nginx/conf/nginx.conf
upstream tomcat_pool {
server 192.168.40.18:8080;
server 192.168.40.19:8080;
ip_hash; }
访问:http://192.168.80.100/index.jsp

技术分享图片

技术分享图片
三:验证keepalived双机热备

以上是关于群集架构篇的主要内容,如果未能解决你的问题,请参考以下文章

群集架构篇——nginx反向代理+keepalived双机热备+tomcat服务器池+后端数据库

详述LVS负载均衡群集 (理论篇)

LVS负载均衡群集架构:NET模式群集部署

mysql群集架构mmm高可用群集

中级篇使用Haproxy搭建WEB群集

LVS负载均衡群集架构:DR模式群集部署