Nginx+Keepalived+Tomcat+MySQL负载均衡& 通过nginx调度器访问Tom

目录
第一部分 环境准备
第二部分 部署调度器—搭建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调度器
访问:http://192.168.80.100/index.jsp

三:验证keepalived双机热备

原文地址:http://blog.51cto.com/13871378/2320409

时间: 2024-10-07 09:44:17

Nginx+Keepalived+Tomcat+MySQL负载均衡& 通过nginx调度器访问Tom的相关文章

nginx+keepalived+tomcat+memcache负载均衡搭建小集群

最近一段时间一直在研究高可用高并发负载均衡分布式集群等技术,先前发布了lvs基于网络第四次协议搭建的小集群,现在用空刚好搭建了一个基于nginx搭建的小集群. 我准备了四台机器,情况如下 机器名称 机器IP 安装软件 角色 虚拟ip 描述 vmm01 192.168.252.11 Nginx.keepalived Nginx主机 192.168.252.200 反向代理到tomcat1和tomcat2 vmm04 192.168.252.14 Nginx.keepalived Nginx备机 主

Nginx 服务器 之Nginx与tomcat实现负载均衡

  本文讲解我们如何使用Nginx做反向带服务器,实现nginx与tomcat服务器集群做负载均衡. 一.nginx与tomcat实现负载均衡 1.在/usr/local/ngnix/conf  创建文件 nginx-tomcat.conf 文件内容: user  nobody; worker_processes  2; events {        worker_connections  1024;     } http{    # upstream 配置一组后端服务器,     # 请求转

配置nginx+keepalived高可用负载均衡的时候。主从服务器都出现了VIP 阿里云问题

配置nginx+keepalived高可用负载均衡的时候.主从服务器都出现了VIP 出现这问题的场景是在阿里VPS云服务器网络环境中,因为路由交换层禁用了ARP的广播限制,造成KEEPALIVE主备协议无法通过广播的方式进行通信,造成主备两台服务器都强占HAVIP地址,出现同时两台服务器都有VIP地址的情况出现,必须通过配置来指定IP的两台服务器间进行通讯(阿里说明文档中解释只能支持两台使用同一个HAVIP地址),基于以下方法可以的情况下,多备方式用同样的方式也应该可行 ,有需要的兄弟可以测试下

Tomcat(五):nginx/httpd + tomcat及负载均衡tomcat

*/ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hljs-comment, .hljs-template_comment, .diff .hljs-header, .hljs-javadoc { color: #998; font-style: italic; } .hljs-keyword, .css .rule .hljs-keyword, .h

nginx + keepalived 高可用负载均衡

虚拟IP:    192.168.2.52 nginx-1:   192.168.2.50 nginx-2:   192.168.2.51 关闭/etc/sysconfig/selinux SELINUX=disabled nginx-1 yum install keepalived vim /etc/keepalived/keepalived.conf--------------------------------------------------- ! Configuration File

Nginx+Keepalived高可用负载均衡

转自 https://www.jianshu.com/p/da26df4f7d60 Keepalived+Nginx实现高可用Web负载均衡 Master 192.168.0.69 nginx.keepalived Centos7.4 backup 192.168.0.70 nginx.keepalived Centos7.4 vip(虚拟IP) 192.168.0.180 192.168.0.181 1.两台都安装nginx,参照安装nginx文档 2. 两台都安装Keepalived yum

windows+nginx+memcached+tomcat做负载均衡

首先,我们明确目标,做Tomcat集群的目的是为了提供更高的负载能力,把访问均摊到不同的服务器上. 直观地来说,就是访问test.localhost.com时,nignx会随机将访问请求分发到tomcat1,tomcat2,为了保持session同步,使用memcached去管理session. 为此我们准备的配置清单是: windows x 1 nginx x 1 memcached x 1 tomcat x 2 mysql x 1 部署的架构图如下: 首先,我准备了一个Java Web项目.

Nginx+Keepalived主备负载均衡学习笔记

实验环境及软件版本: CentOS版本:    6.6(2.6.32.-504.el6.x86_64) nginx版本:     nginx-1.6.2 keepalived版本:keepalived-1.2.7 准备工作: 关闭防火墙:service iptables stop chkconfig iptables off 关闭selinux: sed -i 's/SELINUX=disabled/SELINUX=enforcing/g' /etc/selinux/config init 6

nginx 配置tomcat 实现负载均衡

环境:准备三台机械: 两台安装tomcat 一台安装nginx;安装步骤省略:tomcat一台的IP:http://192.168.195.132:8080另一台tomcat一台的IP:http://192.168.195.133:8080nginxIP:http://192.168.195.134主要:nginx的配置文件nginx.conf:#user  nobody;worker_processes  1; #error_log  logs/error.log;#error_log  lo