码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2018-11-22 21:05:46      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:max   mkdir   pipe   添加帐号   负载均衡   request   echo   主服务器   ack   

目录
第一部分 环境准备
第二部分 部署调度器—搭建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服务-------
[root@lvs01 ~]# yum install -y \ //安装编译工具及插件

gcc \
gcc-c++ \
make \
openssl-devel \
zlib-devel \
pcre-devel
[root@localhost ~]# useradd -s /sbin/nologin -M nginx //添加帐号
[root@localhost ~]# tar vxf nginx-1.13.9.tar.gz //解压nginx安装包
[root@localhost ~]# cd nginx-1.13.9
[root@localhost ~ 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
[root@localhost nginx-1.13.9]# make //编译
[root@localhost nginx-1.13.9]# make install //安装
//以下编译nginx主配置文件
[root@localhost 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;
    }

}
保存退出
//以下编辑子配置文件
[root@localhost nginx-1.13.9]# cd /usr/local/nginx/conf/
[root@localhost conf]# mkdir conf.d
[root@localhost conf]# cd conf.d/
[root@localhostconf.d]# 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;

    }

}
[root@localhost conf.d]# vi pool.conf //创建服务器池
upstream center_pool { #默认轮询
server 192.168.40.18:8080;
server 192.168.40.19:8080;
}
//制作启动脚本
[root@localhost 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/
[root@localhost conf.d]# chmod +x /etc/init.d/nginx //增加执行权限
[root@localhost conf.d]# chkconfig --add nginx //加入系统管理服务
[root@localhost conf.d]# service nginx start //启动nginx服务
[root@localhost conf.d]# netstat -anpt | grep 80

-------------部署keepalived-----------
[root@localhost conf.d]#
yum -y install \
popt-devel \
kernel-devel \
openssl-devel
[root@localhost ~]# tar xvf keepalived-1.4.2.tar.gz
[root@localhost ~]# cd keepalived-1.4.2
[root@localhost keepalived-1.4.2]# ./configure --prefix=/ //配置
[root@localhost keepalived-1.4.2]# make && make install //编译与安装
[root@localhost keepalived-1.4.2]# cp keepalived/etc/init.d/keepalived /etc/init.d/
[root@localhost keepalived-1.4.2]# systemctl enable keepalived //设置开机自启
//以下编辑keepalived配置文件
[root@localhost keepalived-1.4.2]# cd /etc/keepalived/
[root@localhost 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不在就关闭
[root@localhost 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
[root@localhost keepalived]# chmod +x /opt/nginx.sh
[root@localhost keepalived]# systemctl start keepalived
[root@localhost ~]# ip addr show dev ens33 //查看漂移地址是否生成

技术分享图片

[root@localhost ~]#systemctl status nginx
[root@localhost ~]#systemctl status keeplived

技术分享图片

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

技术分享图片

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

技术分享图片

//验证成功

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

-------安装nginx服务-------
[root@lvs01 ~]# yum install -y \ //安装编译工具及插件

gcc \
gcc-c++ \
make \
openssl-devel \
zlib-devel \
pcre-devel
[root@localhost ~]# useradd -s /sbin/nologin -M nginx //添加帐号
[root@localhost ~]# tar vxf nginx-1.13.9.tar.gz //解压nginx安装包
[root@localhost ~]# cd nginx-1.13.9
[root@localhost ~ 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
[root@localhost nginx-1.13.9]# make //编译
[root@localhost nginx-1.13.9]# make install //安装
//以下编译nginx主配置文件
[root@localhost 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;
    }

}
保存退出
//以下编辑子配置文件
[root@localhost nginx-1.13.9]# cd /usr/local/nginx/conf/
[root@localhost conf]# mkdir conf.d
[root@localhost conf]# cd conf.d/
[root@localhostconf.d]# 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;

    }

}
[root@localhost conf.d]# vi pool.conf //创建服务器池
upstream center_pool { #默认轮询
server 192.168.40.18:8080;
server 192.168.40.19:8080;
}
//制作启动脚本
[root@localhost 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/
[root@localhost conf.d]# chmod +x /etc/init.d/nginx //增加执行权限
[root@localhost conf.d]# chkconfig --add nginx //加入系统管理服务
[root@localhost conf.d]# service nginx start //启动nginx服务
[root@localhost conf.d]# netstat -anpt | grep 80

-------------部署keepalived-----------
[root@localhost conf.d]#
yum -y install \
popt-devel \
kernel-devel \
openssl-devel
[root@localhost ~]# tar xvf keepalived-1.4.2.tar.gz
[root@localhost ~]# cd keepalived-1.4.2
[root@localhost keepalived-1.4.2]# ./configure --prefix=/ //配置
[root@localhost keepalived-1.4.2]# make && make install //编译与安装
[root@localhost keepalived-1.4.2]# cp keepalived/etc/init.d/keepalived /etc/init.d/
[root@localhost keepalived-1.4.2]# systemctl enable keepalived //设置开机自启
//以下编辑keepalived配置文件(注意和主服务器不同之处)
[root@localhost keepalived-1.4.2]# cd /etc/keepalived/
[root@localhost 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不在就关闭
[root@localhost 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
[root@localhost keepalived]# chmod +x /opt/nginx.sh
[root@localhost ~]# systemctl start keepalived

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

技术分享图片

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

二:模拟主服务故障

  1. 主服务器
    [root@localhost ~]#systemctl stop keepalived
    [root@localhost ~]#systemctl status keepalived
    [root@localhost ~]# ip addr show dev ens33

技术分享图片

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

2.从服务器
[root@localhost ~]#systemctl status nginx
[root@localhost ~]#systemctl status keepalived

技术分享图片

[root@localhost ~]#ip addr show dev ens33

技术分享图片

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

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

  1. 主服务器

[root@localhost ~]#systemctl start keepalived
[root@localhost ~]#ip addr show dev ens33

技术分享图片

[root@localhost ~]# netstat -anpt | grep 80

技术分享图片

//主服务器已恢复工作

  1. 从服务器
    [root@localhost ~]# ip addr show dev ens33

技术分享图片

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

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

//以下在两台tomcat服务器上操作
第一步:部署第一个节点服务器TM01(192.168.40.18)
----------部署java环境,jdk---------
[root@localhost ~]# tar xvf jdk-8u144-linux-x64.tar.gz //解压jdk
[root@localhost ~]# cp -r jdk1.8.0_144/ /usr/local/java //创建java源目录
[root@localhost ~]# 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
保存退出
[root@localhost ~]# source /etc/profile //立即生效
[root@localhost ~]# java –version //查看版本

技术分享图片

//java环境部署完成

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

技术分享图片

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

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

技术分享图片

//tomcat部署成功

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

技术分享图片

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

技术分享图片

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

----------部署java环境,jdk---------
[root@localhost ~]# tar xvf jdk-8u144-linux-x64.tar.gz //解压jdk
[root@localhost~]# cp -r jdk1.8.0_144/ /usr/local/java //创建java源目录
[root@localhost ~]# 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
保存退出
[root@localhost ~]# source /etc/profile //立即生效
[root@localhost ~]# java –version //查看版本

技术分享图片

//java环境部署完成

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

技术分享图片

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

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

技术分享图片

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

技术分享图片

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

技术分享图片

//tomcat部署成功

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

技术分享图片

再次访问

技术分享图片

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

技术分享图片

第四部分 搭建Mysql数据库

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

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

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

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

四:配置(个性化配置及优化项目
[root@localhost ~]# cd /opt/mysql-5.7.20/
[root@localhost 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

五:编译与安装
[root@localhost mysql-5.7.20]# make
[root@localhost mysql-5.7.20]# make install

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

六:编辑Mysql主配置文件
[root@localhost 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
//保存退出

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

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

八:初始化数据库
[root@localhost ~]# cd /usr/local/mysql/
[root@localhost mysql]# bin/mysqld \

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

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

技术分享图片

[root@localhost mysql]# systemctl enable mysqld //设为开机启动

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

技术分享图片

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

第五部分 案例应用

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

技术分享图片

保存退出

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

技术分享图片

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

技术分享图片

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

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

技术分享图片

保存退出

  1. TOM02(192.168.40.19):
    [root@localhost ~]# tar xf SLSaleSystem.tar.gz -C /usr/local/tomcat8/webapps/
    [root@localhost ~]# cd /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes
    [root@localhost 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双机热备

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

标签:max   mkdir   pipe   添加帐号   负载均衡   request   echo   主服务器   ack   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!