陈俊勇:部署Tomcat域名访问和连接MySQL,生产环境中很实用

[图]微软Azure上40%的虚拟主机运行Linux系统
2017年11月1日
10块钱建的网站怎么备案
2017年11月5日

这是继昨天之后的实验、若想知道跟多请看前一篇

陈俊勇:部署Tomcat域名访问和连接MySQL,生产环境中很实用

搭建基于域名的虚拟主机

进入Tomcat里面去修改配置文件,配置虚拟主机用域名访问,

陈俊勇:部署Tomcat域名访问和连接MySQL,生产环境中很实用

[root@juny-18 tomcat]# cd conf/

[root@juny-18 conf]# ls

Catalina catalina.properties jaspic-providers.xml logging.properties tomcat-users.xml web.xml

catalina.policy context.xml jaspic-providers.xsd server.xml tomcat-users.xsd

server.xml是Tomcat的主配置文件(全局),服务器设置的,例如端口设置,路径设置。

context里设置tomcat数据源,用来连接数据库。

tomcat_users主要是用户名和密码的设置。

web是默认首页等等之类的设置

首先配置主配置文件

[root@juny-18 conf]# vim server.xml

添加一个主机,就像添加一个网址一样;

在1的模块下添加一个类似于2的主机模块

陈俊勇:部署Tomcat域名访问和连接MySQL,生产环境中很实用

现在定义两个基于域名的虚拟主机

陈俊勇:部署Tomcat域名访问和连接MySQL,生产环境中很实用

查看自己写的目录是否有,如果没有增加一个目录;

appBase指定应用程序(网站)的基本路径,这里可以存放多个程序(网站),一般是相对路径,相对于tomcat的安装目录。

Context path=””为虚拟目录,如果是空,表示直接就是/,如果是如path=”aa”,那么访问的时候就是site:8080/aa

xuegod.cndocBase=”……” 为实际目录,就是可以是绝对路径,如果是相对路径就是基于appBase

[root@juny-18 conf]# ls /var/www/html/

ls: 无法访问/var/www/html/: 没有那个文件或目录

[root@juny-18 conf]# mkdir -p /var/www/html/web1

[root@juny-18 conf]# mkdir -p /var/www/html/web2

陈俊勇:部署Tomcat域名访问和连接MySQL,生产环境中很实用

在web1和web2里面写入测试文件

Web1

陈俊勇:部署Tomcat域名访问和连接MySQL,生产环境中很实用

Web2

陈俊勇:部署Tomcat域名访问和连接MySQL,生产环境中很实用

重启Tomcat做测试

[root@juny-18 conf]# /etc/init.d/tomcat stop

Using CATALINA_BASE: /usr/local/src/tomcat

Using CATALINA_HOME: /usr/local/src/tomcat

Using CATALINA_TMPDIR: /usr/local/src/tomcat/temp

Using JRE_HOME: /usr

Using CLASSPATH: /usr/local/src/tomcat/bin/bootstrap.jar:/usr/local/src/tomcat/bin/tomcat-juli.jar

NOTE: Picked up JDK_JAVA_OPTIONS: –add-opens=java.base/java.lang=ALL-UNNAMED –add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED

[root@juny-18 conf]# /etc/init.d/tomcat start

Using CATALINA_BASE: /usr/local/src/tomcat

Using CATALINA_HOME: /usr/local/src/tomcat

Using CATALINA_TMPDIR: /usr/local/src/tomcat/temp

Using JRE_HOME: /usr

Using CLASSPATH: /usr/local/src/tomcat/bin/bootstrap.jar:/usr/local/src/tomcat/bin/tomcat-juli.jar

Tomcat started.

修改主机的host文件

Host文件位置

C:\Windows\System32\drivers\etc

陈俊勇:部署Tomcat域名访问和连接MySQL,生产环境中很实用

在浏览器中输入

陈俊勇:部署Tomcat域名访问和连接MySQL,生产环境中很实用

陈俊勇:部署Tomcat域名访问和连接MySQL,生产环境中很实用

Tomcat连接MySQL

安装jsk连接mysql的工具

解压并进入到目录

[root@juny-18 ~]# tar zxf mysql-connector-java-5.1.44.tar.gz -C /usr/local/src ; cd /usr/local/src/mysql-connector-java-5.1.44

[root@juny-18 mysql-connector-java-5.1.44]# ls

build.xml CHANGES COPYING mysql-connector-java-5.1.44-bin.jar README README.txt src

[root@juny-18 mysql-connector-java-5.1.44]#

直接将mysql-connector-java-5.1.44-bin.jar Tomcat的库文件里面就ok

[root@juny-18 mysql-connector-java-5.1.44]# cp mysql-connector-java-5.1.44-bin.jar /usr/local/src/tomcat/lib/

重启Tomcat

[root@juny-18 mysql-connector-java-5.1.44]# /etc/init.d/tomcat stop

Using CATALINA_BASE: /usr/local/src/tomcat

Using CATALINA_HOME: /usr/local/src/tomcat

Using CATALINA_TMPDIR: /usr/local/src/tomcat/temp

Using JRE_HOME: /usr

Using CLASSPATH: /usr/local/src/tomcat/bin/bootstrap.jar:/usr/local/src/tomcat/bin/tomcat-juli.jar

NOTE: Picked up JDK_JAVA_OPTIONS: –add-opens=java.base/java.lang=ALL-UNNAMED –add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED

[root@juny-18 mysql-connector-java-5.1.44]# /etc/init.d/tomcat start

Using CATALINA_BASE: /usr/local/src/tomcat

Using CATALINA_HOME: /usr/local/src/tomcat

Using CATALINA_TMPDIR: /usr/local/src/tomcat/temp

Using JRE_HOME: /usr

Using CLASSPATH: /usr/local/src/tomcat/bin/bootstrap.jar:/usr/local/src/tomcat/bin/tomcat-juli.jar

Tomcat started.

写一个测试页面来测试Tomcat与MySQL的连接

创建一个Tomcat的数据库

[root@juny-18 mysql-connector-java-5.1.44]# mysql -uroot -pwoDEmiMA01!

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 5

Server version: 5.7.20 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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> create database tomcat; #创建数据库

Query OK, 1 row affected (0.00 sec)

mysql> use tomcat;

Database changed

mysql> create table tt(id int,name varchar(128)); #创建表

Query OK, 0 rows affected (0.12 sec)

mysql> insert into tt values (1,”come on boy”),(2,”come on girl”); #插入测试数据

Query OK, 2 rows affected (0.00 sec)

Records: 2 Duplicates: 0 Warnings: 0

mysql> grant all on tomcar.* to tomcat@localhost identified by ‘Tomcat01!’; #授权账号登录

Query OK, 0 rows affected, 1 warning (0.12 sec)

mysql> flush privileges; #刷新权限表

Query OK, 0 rows affected (0.15 sec)

本机测试、完全没问题

[root@juny-18 mysql-connector-java-5.1.44]# mysql -utomcat -pTomcat01!

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 6

Server version: 5.7.20 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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>

建立测试页面

[root@juny-18 ~]# vim /usr/local/src/tomcat/webapps/ROOT/mysql.jsp

陈俊勇:部署Tomcat域名访问和连接MySQL,生产环境中很实用

<%@ page contentType=”text/html;charset=utf-8″%>

<%@ page import=”java.sql.*”%>

<html>

<body>

<%

Class.forName(“org.gjt.mm.mysql.Driver”).newInstance();

String url =”jdbc:mysql://localhost/tomcat?user=tomcat&password=Tomcat01!&useUnicode=true&characterEncoding=utf-8″;

Connection conn= DriverManager.getConnection(url);

Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

String sql=”select * from tt”;

ResultSet rs=stmt.executeQuery(sql);

while(rs.next()){%>

step:<%=rs.getString(1)%>

context:<%=rs.getString(2)%><br><br>

<%}%>

<%out.print(“Congratulations!!! JSP connect MYSQL IS OK!!”);%>

<%rs.close();

stmt.close();

conn.close();

%>

</body>

</html>

浏览器访问成功

陈俊勇:部署Tomcat域名访问和连接MySQL,生产环境中很实用

实验完成

有什么问题可以关注一起探讨

发表评论

电子邮件地址不会被公开。 必填项已用*标注