转载

Tomcat学习总结(17)—— Tomcat生产环境中域名访问和连接MySQL

搭建基于域名的虚拟主机
进入Tomcat里面去修改配置文件,配置虚拟主机用域名访问,
[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的主机模块
现在定义两个基于域名的虚拟主机
查看自己写的目录是否有,如果没有增加一个目录;
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
在web1和web2里面写入测试文件
Web1
Web2
重启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:WindowsSystem32driversetc
在浏览器中输入
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
<%@ 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>
浏览器访问成功
文章最后发布于: 2017-11-05 17:08:18
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览