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

Detailed tutorial on installing Hbase 2.3.5 on Vmware + Ubuntu18.04

Preface The previous article installed Hadoop, an...

Mysql5.6.36 script compilation, installation and initialization tutorial

Overview This article is a script for automatical...

How to use JavaScript strategy pattern to validate forms

Table of contents Overview Form validation withou...

Using js to implement the two-way binding function of data in Vue2.0

Object.defineProperty Understanding grammar: Obje...

Ideas and methods for incremental backup of MySQL database

To perform incremental backup of the MySQL databa...

Summary of Git commit log modification methods

Case 1: Last submission and no push Execute the f...

Analyze Mysql transactions and data consistency processing issues

This article analyzes the consistency processing ...

Solution to MySQL root password error number 1045

Stop MySQL Service Windows can right-click My Com...

js to achieve simple product screening function

This article example shares the specific code of ...

Introduction to the use of MySQL official performance testing tool mysqlslap

Table of contents Introduction Instructions Actua...

How to publish static resources in nginx

step Place the prepared static resource files in ...

Example of configuring multiple SSL certificates for a single Nginx IP address

By default, Nginx supports only one SSL certifica...

Detailed tutorial on Docker pulling Oracle 11g image configuration

Without further ado Start recording docker pullin...

Detailed process of decompressing and installing mysql5.7.17 zip

1. Download address https://dev.mysql.com/downloa...