Redis cache server (nginx+tomcat+redis+mysql for session session sharing)

1. Introduction to redis

Redis is a key-value storage system.Similar to memcached, it supports storing relatively more value types, including string (string), list (list), set (set), zset (sorted set - ordered set), and hash (hash type).Like memcached, data is cached in memory for efficiency.The difference is that redis periodically writes updated data to disk, or writes modifications to appended log files, on which master-slave synchronization is based.

Redis is a high-performance key-value database.The emergence of redis largely compensates for the insufficiency of key/value storage such as memcacehd, which can complement relational databases in some cases.It provides java, C/C++, C#, php, javaScript, Perl, Object-C, python, Ruby and other clients for easy use.
If you simply compare redis to memcached, there are basically three points:
1. redis not only supports simple key/value type data, but also provides storage of list, set, zset, hash and other data structures.
2, redis supports data backup, that is, master-slave mode data backup.
3, redis supports data persistence, keeping data in memory on disk and reloading it for use on reboot.

In Reids, not all data is always stored in memory.This is the biggest difference from memcacehd.
Redis only caches information about all keys. If Reids finds that memory usage exceeds a certain threshold, it triggers swap operations. Redis calculates the value of those keys according to "swappability = age*log(size_in_memory)" which requires swap to disk.These keys are then persisted to disk and cleared from memory.This feature allows redis to hold data larger than its machine's own memory size.Of course, the memory of the machine itself must be able to maintain all eky, because these data will not be swap operated.
When reading data from Redis, if the value corresponding to the key read is no longer in memory, Redis needs to load the corresponding data from the swap file and return it to the requestor.

Comparison of memcached and redis
1, Network IO Model
memcacehd is a multi-threaded, non-blocking IO multiplexed network model, which is divided into listening main thread and worker sub-threads, listening threads listening for network connections, and after accepting requests, passing the connection descriptor pipe to the worker thread to read and write IO. The network layer uses libevent encapsulated event libraries. The multi-threaded model can play a multi-core role.
Redis uses a single-threaded IO reuse model and encapsulates a simple AeEvent event processing framework. It mainly implements epoll, kqueue and select. For IO operations alone, single-threaded can maximize the speed advantage, but Redis also provides some simple computing functions, such as sorting, aggregation, etc. For these operations, the single-threaded model actually implementsSeriously affecting overall throughput, the entire IO schedule is blocked during CPU computation.

2. Memory management
memcacehd uses pre-allocated memory pools, slab s and chunks of different sizes to manage memory, and value chooses the appropriate chunk storage according to size.
Redis uses field memory requests to store data.

3. Storage and other aspects
memcached basically only supports simple key-value storage, does not support persistence and replication, and in addition to key/value, Redis also supports list, set, sortedset, hash and many other data structures.

2. How to keep session session session

Currently, in order for the web to accommodate large-scale access, cluster deployment of applications is required.The most effective solution for clustering is load balancing. To achieve load balancing, each request of users may be allocated to an irregular server. Therefore, we must first resolve the session uniformity to ensure the normal use of users regardless of which server their requests are forwarded to, that is, we need to implement the session sharing mechanism.

There are several scenarios to unify session s under a cluster system:
1, Session sticky
For example, a hash policy based on ip access, where the current user's requests are centrally defined in one server, allows a single server to hold the user's session login information and, if down, is equivalent to a single-point deployment, will be lost and sessions will not be replicated.(not used)
2, session replication share:
For example, session sharing in tomcat refers to synchronizing sessions among multiple application servers in a clustered environment to keep sessions consistent and transparent.If one of the servers fails, according to the load balancing principle, the dispatcher traverses to find available nodes and distribute requests. Because sessions are synchronized, the user's session information is not lost and session replication occurs.
The drawbacks of this scenario are:
Must be done between the same middleware (for example, between tomcat-tomcat)
The performance penalty caused by session replication increases rapidly, especially when large objects are saved in sessions and objects change rapidly, which can lead to a significant performance degradation and a loss of system performance.This feature limits the horizontal expansion of web applications.
session content is synchronized to members via broadcasting, which can cause network traffic bottlenecks, even for intranets.Not doing well with large concurrencies.
3, session sharing based on cache DB cache
Session sharing based on memcache/redis cache, that is, using cacheDB to store session information, the application server accepts new requests to save session information in the cache DB. When the application server fails, the dispatcher traverses to find available nodes, distributes requests, and removes the cache when the application server finds that the session is not in local memoryFind in DB and copy to local machine if found, enabling session sharing and high availability.(Recommended)

Third, nginx+tomcat+redis+mysql for session sharing and load balancing

1. Project environment

Host operating system ip address
nginx CentOS 7.3
tomcat-1 CentOS 7.3
tomcat-2 CentOS 7.3
mysql CentOS 7.3
Redis CentOS 7.3

2, Project Topology

In this diagram, nginx acts as a reverse proxy, separating the static from the dynamic, randomly assigning customers'dynamic requests to two tomcat servers according to their weights, redis serves as the shared session data server for two tomcats, and mysql serves as the back-end database for two tomcats.

3. Project implementation

1, nginx installation configuration
Using nginx as the load balancer of tomcat, the session session data of Tomcat is stored in redis, which can achieve the 7x24 effect of zero downtime.Because sessions are stored in redis, nginx does not have to configure a Tomcat method called stick-paste in order to truly load-balance multiple Tomcats in the background.

1)Install dependent toolkits:
[root@nginx ~]#  yum -y install gcc* pcre-devel openssl-devel zlib-devel make vim 
2)Establish nginx Program user groups and users:
[root@nginx ~]# groupadd -r nginx && useradd -r -g nginx -s /bin/false -M nginx
3)Compile Installation nginx: 
[root@nginx ~]# tar zxf nginx-1.8.0.tar.gz 
[root@nginx ~]# cd nginx-1.8.0
[root@nginx nginx-1.8.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx \
>  --with-http_stub_status_module --with-http_ssl_module  --with-http_dav_module  --with-http_flv_module \
>  --with-http_mp4_module --with-http_gzip_static_module --with-http_gzip_static_module \
> --with-http_addition_module --with-http_sub_module  --with-pcre  --with-http_realip_module 
[root@nginx nginx-1.8.0]# make && make install
4)Optimize the path and check:
[root@nginx nginx-1.8.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@nginx nginx-1.8.0]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
5)To write nginx Service script:
[root@nginx nginx-1.8.0]# vim /etc/init.d/nginx
# chkconfig: 2345 99 20
# description: Nginx Service Control Script
case "$1" in
netstat -anplt |grep ":80" &> /dev/null && pgrep "nginx" &> /dev/null
if [ $? -eq 0 ]
echo "Nginx service already running."
$PROG -t &> /dev/null
if [ $? -eq 0 ] ; then
echo "Nginx service start success."
$PROG -t
netstat -anplt |grep ":80" &> /dev/null && pgrep "nginx" &> /dev/null
if [ $? -eq 0 ]
kill -s QUIT $(cat $PIDF)
echo "Nginx service stop success."
echo "Nginx service already stop"
$0 stop
$0 start
netstat -anplt |grep ":80" &> /dev/null && pgrep "nginx" &> /dev/null
if [ $? -eq 0 ]
echo "Nginx service is running."
echo "Nginx is stop."
netstat -anplt |grep ":80" &> /dev/null && pgrep "nginx" &> /dev/null
if [ $? -eq 0 ]
$PROG -t &> /dev/null
if [ $? -eq 0 ] ; then
kill -s HUP $(cat $PIDF)
echo "reload Nginx config success."
$PROG -t
echo "Nginx service is not run."
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
#And join Startup Autostart:
[root@nginx ~]# chkconfig --add nginx
[root@nginx ~]# chkconfig nginx on
#Start nginx:
[root@nginx ~]# systemctl daemon-reload
[root@nginx ~]# systemctl start nginx
[root@nginx ~]# netstat -anput | grep nginx
tcp        0      0    *               LISTEN      21812/nginx: master 

6) Configure nginx reverse proxy (reverse proxy + load balancing + health detection)
Note: Combine proxy and upstream modules to achieve back-end web load balancing;
Combining ngx_http_proxy_module and ngx_http_upstream_module, which are the default modules of nginx, to achieve the health check of the back-end server;

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
Add the following configuration items to the http {} field:

#load balance Settings:
        upstream  backend_tomcat {   
                server weight=1 max_fails=2 fail_timeout=10s;
                server weight=1 max_fails=2 fail_timeout=10s;
# http_proxy Settings:
        proxy_connect_timeout 75;   #nginx connection timeout with back-end server (proxy connection timeout)
        proxy_send_timeout 75;   #Defines the data return time for the back-end server, that is, the back-end server must complete all data within the specified time, or nginx will disconnect this connection.
        proxy_read_timeout 75;  #Define timeouts for reading responses from back-end servers
        proxy_buffer_size 4k;  #Set the size of the buffer (which by default is equal to the size of a buffer set by the proxy_buffers directive, but can also be set to a smaller size)
        proxy_buffers 4 32k;    #Set the number and size of buffers per connection
        proxy_busy_buffers_size 64k;  #Buffer size under heavy load (default size is twice the size of a single block set by the proxy_buffers directive)
        proxy_temp_file_write_size 64k;  #This option limits the size of each temporary file written when the server whose cache is proxied responds to it
Explanation of parameters:
Weight: The polling weight, which can also be used with ip_hash, defaults to 1
 max_fails: The number of times a request failed to run, defaulting to 1.When the maximum number of times is exceeded, an error is returned from the proxy_next_upstream module definition.
fail_timeout: has two meanings, allowing up to two failures in a 10 s time period and not allocating requests to this server in a 10 s time period after two failures.

Add in the server module:

#proxy_pass Settings: 
        location ~* \.(jsp|do)$ {   #Matched dynamic requests will be forwarded
        proxy_pass http://backend_tomcat; #Request to go to the list of servers defined by backend, the reverse proxy, corresponding to the upstream load balancer.
        proxy_redirect off;  #Whether to define redirection rules
        proxy_set_header Host $host;  #Allow redefinition or addition of request headers to back-end servers
        proxy_set_header X-Real-IP $remote_addr;   #The web server side gets the user's real ip, or X-Forward-For ward below
        proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;    #Back-end Web server can get users'real IP through X-Forwarded-For
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
#Increase failover by automatically forwarding requests to another server in the upstream load balancing pool if the back-end server returns errors such as 502, 504, execution timeout, etc.

7) Restart the nginx service and set firewall rules:

[root@nginx ~]# systemctl restart nginx
[root@nginx ~]# firewall-cmd --add-port=80/tcp --permanent
[root@nginx ~]# firewall-cmd --reload

2, install and deploy tomcat server
JDK must be installed before tomcat can be installed. JDK is a free software development kit for Java language provided by sun Company. It contains Java virtual machine (JVM). Written java source programs are compiled to form Java byte codes. Once JDK is installed, these byte code files can be interpreted using JVM to ensure the platform of java.

1) Install the JDK and configure the java environment (tomcat 1 and tomcat2 server installations)

[root@tomcat-1 ~]# tar zxf jdk-8u211-linux-x64.tar.gz
[root@tomcat-1 ~]# mv jdk1.8.0_211/ /usr/local/java
[root@tomcat-1 ~]# vim /etc/profile
export JAVA_HOME=/usr/local/java
export PATH=$JAVA_HOME/bin:$PATH
[root@tomcat-1 ~]# source /etc/profile
[root@tomcat-1 ~]# java -version  #Ensure that the java version is consistent with the current version
java version "1.8.0_211"
Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)

2) Install Tomcat (same operation as tomcat1 and tomcat2 servers)
You can download the required version from the tomcat website:

[root@tomcat-1 ~]# tar zxf apache-tomcat-8.5.35.tar.gz 
[root@tomcat-1 ~]# mv apache-tomcat-8.5.35 /usr/local/tomcat8
[root@tomcat-1 ~]# vim /etc/profile
export CATALINA_HOME=/usr/local/tomcat8
[root@tomcat-1 ~]# source /etc/profile
#Start tomcat:
[root@tomcat-1 ~]# /usr/local/tomcat8/bin/   
#This is just the path to the startup script, as the environment variable is set above to execute the script directly
Using CATALINA_BASE:   /usr/local/tomcat8
Using CATALINA_HOME:   /usr/local/tomcat8
Using CATALINA_TMPDIR: /usr/local/tomcat8/temp
Using JRE_HOME:        /usr/local/java
Using CLASSPATH:       /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar
Tomcat started.
[root@tomcat-1 ~]# netstat -anput | grep 8080
tcp6       0      0 :::8080                 :::*                    LISTEN      15408/java
#Firewall rule configuration:
[root@tomcat-1 ~]# firewall-cmd --add-port=8080/tcp --permanent
[root@tomcat-1 ~]# firewall-cmd --reload

#Browser Test Access to Two tomcat Servers

3) Modify Tomcat configuration files (tomcat1 and tomcat2 operations)

[root@tomcat-1 ~]# vim /usr/local/tomcat8/conf/server.xml
#Set default virtual host and add jvmRoute
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat-1">
#Modify the default virtual host and point the Web site file path to/web/webapp1, adding the context segment to the host segment
 <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
                <Context docBase="/web/webapp1" path="" reloadable="true"/>   #Add the line content

#tomcat-2 server configuration file:

#The two configurations are just different jvmRoute, and the other configurations are the same:
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat-2">
<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
                  <Context docBase="/web/webapp1" path="" reloadable="true"/> 

Description: jvmRoute is a jvm identity, which is the top label of the page. In the actual production environment, all background tomcat identities should be the same. Here, for the illustrative purposes of the experiment, the identities of my two Tomcats are changed to different ones.Wait a moment to verify.

4) Create web page directory and test file: (same operation for tomcat-1 and tomcat-2)

[root@tomcat-1 ~]# mkdir -p /web/webapp1
[root@tomcat-1 ~]# vim /web/webapp1/index.jsp  #Write dynamic web page files
#The contents are as follows:
<%@page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<h1><font color="red">Session serviced by tomcat</font></h1>
<table aligh="center" border="1">
<td>Session ID</td>
<td><%=session.getId() %></td>
<% session.setAttribute("abc","abc");%>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>

#Note: To test load balancing, the title of the index.jsp file for the tomcat-2 node is modified to "tomcat-2" (the content of the web page provided by the two Tomcat servers in the production environment is the same) and the other configurations are the same.

5) Restart the tomct service and verify load balancing

#Both tomcat-1 and tomcat-2 need to be restarted
[root@tomcat-1 ~]# 
[root@tomcat-1 ~]#   
[root@tomcat-1 ~]# netstat -anput | grep 8080
tcp6       0      0 :::8080                 :::*                    LISTEN      15551/java  

#Users use browsers to verify load balancing by accessing nginx web services:
Results of the first visit:

Result of the second visit:

As you can see from the above results, nginx distributes the access requests to tomcat-1 and tomcat-2 on the back-end, respectively. The client's access requests are load balanced, but session IDs are different (i.e., session maintenance is not implemented), which can put a lot of pressure on the back-end server.

6) Verify Health Examination

#You can turn off a tomcat host, simulate downtime, and test access with a client browser
[root@tomcat-1 ~]#   
Using CATALINA_BASE:   /usr/local/tomcat8
Using CATALINA_HOME:   /usr/local/tomcat8
Using CATALINA_TMPDIR: /usr/local/tomcat8/temp
Using JRE_HOME:        /usr/local/java
Using CLASSPATH:       /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar

No matter how you refresh the page, tomcat2 always serves, indicating that the health check worked and the validation was successful.

3. Configure tomcat to maintain session through redis

The #front can be said to be preparation work, and the following is the focus of the project.
1) Install redis
Official download address:
[root@redis ~]# tar zxf redis-4.0.14.tar.gz
[root@redis ~]# cd redis-4.0.14/
[root@redis redis-4.0.14]# make && make install

From the figure above, we can easily see that redis is installed in the / usr/local,/usr/local/bin,/usr/local/share,/usr/local/include,/usr/local/lib,/usr/local/share/man directory.

#Then switch to the utils directory and execute the redis initialization script as follows:

[root@redis redis-4.0.14]# cd utils/
[root@redis utils]# ./ 
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379] 
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] 
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] 
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] 
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server] 
Selected config: 
Port           : 6379  
Config file    : /etc/redis/6379.conf  
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /usr/local/bin/redis-server  
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
#All of the above default carriage returns are fine

From the above installation process, we can see that redis configuration file after redis initialization is / etc/redis/6379.conf, log file is / var/log/redis_6379.log, data file dump.rdb is stored in / var/lib/redis/6379 directory, and startup script is / etc/init.d/redis_6379.
#If you need to use systemd, you can create a unit file named redis_6379.service under /etc/systemd/system.

[root@redis utils]# vim /etc/systemd/system/redis_6379.service
#The contents are as follows:
Description=Redis on port 6379
ExecStart=/etc/init.d/redis_6379 start
ExecStop=/etc/init.d/redis_6379 stop
#Note: Here Type=forking is the form of background operation

#Start redis
[root@redis utils]# systemctl daemon-reload
[root@redis utils]# systemctl enable redis_6379.service
[root@redis utils]# systemctl start redis_6379.service

#As we can see from the figure above, the service status is dead. The solution is to restart the service as shown in the following figure:

#Firewall rule settings:
[root@redis utils]# firewall-cmd --add-port=6379/tcp --permanent
[root@redis utils]# firewall-cmd --reload

2) Configure redis

[root@redis ~]# vim /etc/redis/6379.conf 
#Modify as follows (remove comments and modify):
bind   #Modify the monitoring address of redis to the ip of the redis host
requirepass pwd@123   #For security reasons, the requirepass parameter of redis'password verification function needs to be started.
#Restart the redis service:
[root@redis ~]# systemctl restart redis_6379.service
[root@redis ~]# netstat -anput | grep redis
tcp        0      0*               LISTEN      12261/redis-server 

After configuring the #redis configuration file, start redis and perform simple operations (tests) as follows:

[root@redis ~]# redis-cli -h -p 6379 -a pwd@123
Warning: Using a password with '-a' option on the command line interface may not be safe.> keys *
(empty list or set)> set name lisi
OK> get name
"lisi"> quit   

The above parameters are explained:

First, we use redis-cli, the command line tool for redis, to connect to the redis server with ip (tested locally), port 6379, and password pwd@123
 keys *: is to view all key-value pairs of redis
 set nam lisi: add a key (name) - value (lisi)
get name: view the contents of the key name
 quit: exit connection
 Other commands for #redis will be explained later

Now that all redis are deployed, configure session synchronization for tomcat.

3) Configure tomcat session redis synchronization (same operation for tomcat1 and tomcat2)
#Download the corresponding jar package for tomcat-redis-session-manager, which has three main components:

① Copy to $TOMCAT_HOME/lib In the catalog:
[root@tomcat-1 ~]# cp tomcat85-session-redis-1.0.jar jedis-2.9.0.jar commons-pool2-2.4.2.jar  /usr/local/tomcat8/lib/
(2) Modify the context.xml file of tomcat:
[root@tomcat-1 ~]# vim /usr/local/tomcat8/conf/context.xml 
Add the following:

The picture is as follows:

        <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
        <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
        host=""   #redis host address
        password="pwd@123"   #redis login password
        port="6379"        #Listening Port
        maxInactiveInterval="60" />    #session Invalidation Time (Units)
③ Modify, restart tomcat: 
[root@tomcat-1 ~]# 
[root@tomcat-1 ~]# 
[root@tomcat-1 ~]# netstat -anput | grep 8080
tcp6       0      0 :::8080                 :::*                    LISTEN      16931/java  

Repeat the above on the tomcat-2 host.

(4) Verify the consistency of session sessions
#Simulate user access to test page via browser
First visit:

Second visit (refresh page):

You can see that different Tomcats are visited, but the sessions are the same, indicating that sessions remain synchronized to achieve the purpose of clustering.
Note: Session persistence settings are turned on by default from tomcat6. Local session persistence can be turned off during testing. It is actually very simple. In the context.xml file in the conf directory of tomcat, uncomment the following configuration:

#Before modification:
    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <Manager pathname="" />
#After modification:
    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <Manager pathname="" />
⑤ See redis:
[root@redis ~]# redis-cli -h -p 6379 -a pwd@123
Warning: Using a password with '-a' option on the command line interface may not be safe.> keys *
1) "3585F7426EBBC83802C0407575FEAC72tMLrlI.tomcat-2"
2) "name"> quit

You can see that redis establishes key-value pairs for session persistence on tomcat2.

4, Configure tomcat to connect to the database

The session session session of tomcat remains resolved through redis, and now it's time to resolve the tomcat connection database problem.(Host acts as mysql DB server)
1) Install mysql service (binary)

[root@mysql ~]# vim
rpm -qa | grep mariadb  &> /dev/null
if [ $? -eq 0 ]
    rpm -e mariadb-libs --nodeps
tar zxf  mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz 
mv mysql-5.7.28-linux-glibc2.12-x86_64 /usr/local/mysql
ln -s /usr/local/mysql/bin/* /usr/local/bin
groupadd -r mysql && useradd -r -g mysql -s /bin/false -M mysql
mkdir /usr/local/mysql/data
chown -R mysql:mysql /usr/local/mysql
cat > /etc/my.cnf <<EOF
mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
chown -R mysql:mysql /usr/local/mysql
cd /usr/local/mysql/
cp support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
service mysqld start
mypwd=`grep password /usr/local/mysql/data/mysql.err | awk -F'root@localhost: ' '{print $2}'`
mysql -uroot -p${mypwd} -e 'alter user root@localhost identified by""' --connect-expired-password
[root@mysql ~]# sh 
Starting MySQL. SUCCESS! 
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@mysql ~]# ss -anput | grep 3306
tcp    LISTEN     0      80       :::3306                 :::*                   users:(("mysqld",pid=16753,fd=12))
#Firewall rule settings:
[root@mysql ~]# firewall-cmd --add-port=3306/tcp --permanent
[root@mysql ~]# firewall-cmd --reload

2) Create data in the database

[root@mysql ~]# mysql -uroot
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> grant all on *.* to javauser@'172.16.1.%' identified by 'pwd@123';   //Authorize a user
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;   
Query OK, 0 rows affected (0.00 sec)

mysql> create database javadb;   //Create Library
Query OK, 1 row affected (0.00 sec)

mysql> use javadb
Database changed
mysql> create table testtb(id int primary key auto_increment,
    -> name varchar(25),
    -> age int(3));   //Create Table
Query OK, 0 rows affected (0.00 sec)

mysql> insert into testtb(name,age) values('zhangsan','20'),('lisi','21'),('sunqian','22');   //insert data
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from testtb;  #Check it out
| id | name     | age  |
|  1 | zhangsan |   20 |
|  2 | lisi     |   21 |
|  3 | sunqian  |   22 |
3 rows in set (0.00 sec)

3) Configure the tomcat server to connect to the mysql database (both tomcat operations are the same)

(1)download mysql-connector-java-5.1.22-bin.jar And copy to $CATALINA_HOME/lib Under Catalog:
[root@tomcat-1 ~]# cp mysql-connector-java-5.1.22.jar  /usr/local/tomcat8/lib/
(2)Context File settings:
[root@tomcat-1 ~]# vim /usr/local/tomcat8/conf/context.xml

#The figure above adds the following to <Context>
        <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
        maxActive="100" maxIdle="30" maxWait="10000"
        username="javauser" password="pwd@123" driverClassName="com.mysql.jdbc.Driver"

(3) Create a new directory under the / web/webapp1/root directory to store the site xml configuration file for tomcat connection to mysql database

[root@tomcat-1 ~]# mkdir /web/webapp1/WEB-INF
[root@tomcat-1 ~]# vim /web/webapp1/WEB-INF/web.xml
//Add the following:
<web-app xmlns=""
<description>MySQL Test App</description>
<description>DB Connection</description>
#Save the changes and exit, restart the tomcat service:
[root@tomcat-1 ~]# 
Using CATALINA_BASE:   /usr/local/tomcat8
Using CATALINA_HOME:   /usr/local/tomcat8
Using CATALINA_TMPDIR: /usr/local/tomcat8/temp
Using JRE_HOME:        /usr/local/java
Using CLASSPATH:       /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar
[root@tomcat-1 ~]# 
Using CATALINA_BASE:   /usr/local/tomcat8
Using CATALINA_HOME:   /usr/local/tomcat8
Using CATALINA_TMPDIR: /usr/local/tomcat8/temp
Using JRE_HOME:        /usr/local/java
Using CLASSPATH:       /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar
Tomcat started.

tomcat-2 does the same thing as tomcat-1.
(4) Test code
#Now create a simple test.jsp page to test the connectivity between tomcat and mysql

[root@tomcat-1 ~]# vim /web/webapp1/test.jsp
#The contents are as follows: (Modify the corresponding parameters according to your environment)
<%@ page language="java" import="java.sql.*" pageEncoding="GB2312"%>
connect MySQL<br>
String driverClass="com.mysql.jdbc.Driver";
String url="jdbc:mysql://";
String username = "javauser";
String password = "pwd@123";
Connection conn=DriverManager.getConnection(url, username, password);
Statement stmt=conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from testtb");

Note: The same test code was created on the tomcat-2 node, but the title was changed to "MySQL-2" for better validation.

(5) Access the test page through a browser, URL: Or tomcat server via nginx reverse proxy backend.

As you can see from the diagram above, tomcat-1 and tomcat-2 are now ready to connect to the database.
At this point, the project is completed.

Installation packages and jar packages have been uploaded to the disk: Link:
Extraction Code: 2fj1

Tags: Linux Tomcat Redis Nginx MySQL

Posted on Thu, 12 Mar 2020 15:13:42 -0700 by jwilliam