CentOS 7 configuration Tomcat9+MySQL solution

CentOS 7 configuration Tomcat9+MySQL solution

Configure Tomcat

First install Tomcat

Installing Tomcat is divided into two steps: installing Tomcat and installing JDK

JDK (Java Development Kit) is a product developed by Sun Microsystems for Java. Since the launch of Java, JDK has become the most widely used Java SDK. JDK is the core of the entire Java, which includes the Java runtime environment, Java tools and Java-based class libraries. Therefore, if you want to run Java programs, you must have the support of JDK, and the prerequisite for installing Tomcat is to install JDK.

Install JDK

: : : : : : : : : : : : : : :

Install Tomcat

# Because it is a binary package, the compilation process is omitted [root@DaMoWang ~]# tar xf apache-tomcat-9.0.10.tar.gz [root@DaMoWang ~]# mv apache-tomcat-9.0.10/ /usr/local/tomcat [root@DaMoWang ~]# cd /usr/local/tomcat [root@DaMoWang tomcat]# bin/startup.sh # Start Tomcat

Use the netstat command to check whether the startup is successful.

# Normally there will be three ports 8005, 8009 and 8080, of which 8080 is the port for providing web services, 8005 is the management port, and 8009 is the port for third-party service calls, such as the one used when httpd and Tomcat are combined [root@DaMoWang tomcat]# netstat -lntp|grep java tcp6 0 0 127.0.0.1:8005 :::* LISTEN 3430/java tcp6 0 0 :::8009 :::* LISTEN 3430/java tcp6 0 0 :::8080 :::* LISTEN 3430/java

Configure Tomcat

The installation process of Tomcat is very simple, and there are not many configurations for Tomcat.

Configure the access port of the Tomcat service

The default startup port of Tomcat is 8080. If you want to change it, you need to modify the conf/server.xml in the installation directory.

[root@DaMoWang tomcat]# vim conf/server.xml # 找到<Connector port="8080" protocol="HTTP/1.1" 的行, 比如修改为80 <Connector port="80" protocol="HTTP/1.1 保存退出# 重启Tomcat服务[root@DaMoWang tomcat]# bin/shutdown.sh Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/local/jdk10 Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar NOTE: Picked up JDK_JAVA_OPTIONS: --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED [root@DaMoWang tomcat]# bin/startup.sh Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/local/jdk10 Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started.

Tomcat Virtual Host

: : : : : : : : : : : : : : :

Testing Tomcat parsing JSP

# Create a JSP test page [root@DaMoWang tomcat]# vim /data/tomcatweb/1.jsp <html><body><center> Now time is: <%=new java.util.Date()%> </center></body></html> # View the running results [root@DaMoWang tomcat]# curl -x127.0.0.1:8080 www.damowang.cn/1.jsp <html><body><center> Now time is: Fri Jul 27 18:40:46 CST 2018 </center></body></html> # You can see that the middle code is parsed into the current system time. You can also bind hosts on the physical machine and test it with a browser

Tomcat connects to MySQL

Tomcat connects to MySQL through the JDBC driver

So you need to prepare a package

mysql-connector-java can be downloaded from the official website

First configure mysql, create test libraries, tables, and users

[root@DaMoWang ~]# mysql -uroot -p475541270 mysql> create database java_test; mysql> use java_test mysql> grant all on java_test.* to 'java'@'127.0.0.1' identified by 'damowang'; mysql> create table damowang (`id` int(4), `name` char(40)); mysql> insert into damowang values ​​(1,'abc'); mysql> insert into damowang values ​​(2,'aaa'); mysql> insert into damowang values ​​(3,'ccc'); # 退出mysql 去验证java用户有没有问题[root@DaMoWang ~]# mysql -ujava -pdamowang -h127.0.0.1 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.21 Source distribution Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>

After configuring MySQL, configure the Tomcat-related configuration files

: : : : : : : : : : : : : : :

Now we need to use the software package we just prepared.

[root@DaMoWang ~]# tar xf mysql-connector-java-8.0.12.tar.gz -C /usr/src/ [root@DaMoWang ~]# mv /usr/src/mysql-connector-java-8.0.12/mysql-connector-java-8.0.12.jar /usr/local/tomcat/lib/

test

[root@DaMoWang ~]# vim /usr/local/tomcat/webapps/ROOT/t.jsp # Code written in java This script can connect to our database<%@page import=%> <%@page import=%> <%@page import=%> <% Context ctx = new InitialContext(); DataSource ds = (DataSource) ctx .lookup(); Connection conn = ds.getConnection(); Statement state = conn.createStatement(); String sql = ; ResultSet rs = state.executeQuery(sql); (rs.next()) { out.println(rs.getString() +); out.println(rs.getString() +); } rs.close(); state.close(); conn.close(); %>
# Restart Tomcat
[root@DaMoWang ~]# /usr/local/tomcat/bin/shutdown.sh
[root@DaMoWang ~]# /usr/local/tomcat/bin/startup.sh

The database connection is successful, and the data in the damowang table in the java_test library is displayed

You may also be interested in:
  • Centos6.5 Jdk+Tomcat+Mysql environment installation graphic tutorial
  • Detailed explanation of building a Mysql container + Tomcat container connection environment through Docker
  • Simple record of Cent OS server configuration JDK+Tomcat+MySQL
  • Installation of apache+php+mysql+imap+ldap+jdk+tomcat under redhat
  • Detailed explanation of Linux installation JDK, Tomcat and MySQL (with pictures and text)
  • How to install mysql, jdk and tomcat with yum under CentOS

<<:  A brief summary of vue keep-alive

>>:  Simple examples of creating stored procedures, triggers and using cursors in Navicat (pictures and text)

Recommend

Learn about JavaScript closure functions in one article

Table of contents Variable Scope The concept of c...

An article teaches you how to use js to achieve the barrage effect

Table of contents Create a new html file: Create ...

Summary on Positioning in CSS

There are four types of positioning in CSS, which...

Solutions to Mysql index performance optimization problems

The optimization created by MySQL is to add index...

Should I abandon JQuery?

Table of contents Preface What to use if not jQue...

MySQL 5.7 and above version download and installation graphic tutorial

1. Download 1. MySQL official website download ad...

Several ways to implement inheritance in JavaScript

Table of contents Structural inheritance (impleme...

border-radius is a method for adding rounded borders to elements

border-radius:10px; /* All corners are rounded wi...

HTML table cross-row and cross-column operations (rowspan, colspan)

Generally, the colspan attribute of the <td>...

How to add java startup command to tomcat service

My first server program I'm currently learnin...

CSS3 realizes the effect of triangle continuous enlargement

1. CSS3 triangle continues to zoom in special eff...

Use of Linux sed command

1. Function Introduction sed (Stream EDitor) is a...