赞
踩
Tomcat+MySql+jdbc
Linux下安装配置 Jdk+Tomcat+MySql+jdbc
1、安装JDK
下载 jdk-7-linux-i586.rpm
http://java.sun.com/javase/downloads/index.jsp
# rpm –ivh jdk-7-linux-i586.rpm
# vi ~/.bash_profile
JAVA_HOME=/usr/java/jdk1.7.0
CLASSPATH=$JAVA_HOME/lib:$JAVA_HOME/jre/lib
PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
export PATH CLASSPATH JAVA_HOME
使环境变量立刻生效
# source ~/.bash_profile
# source /etc/profile
# java -version (jdk的版本为jdk1.4.2则表示jdk已成功安装)。
2、安装Tomcat
下载apache-tomcat-6.0.10.tar.gz
http://jakarta.apache.org/tomcat
# tar zxf apache-tomcat-6.0.10.tar.gz -C /usr/src
# mv /usr/src/apache-tomcat-6.0.10 /usr/src/tomcat
# /usr/src/tomcat/bin/startup.sh (启动tomcat)
或
# /usr/src/tomcat/bin/catalina.sh start
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TEMDIR: /usr/local/tomcat/temp
Using JAVA_HOME: /usr/java/jdk1.7.0
# /usr/src/tomcat/bin/catalina.sh start/stop (启动/关闭tomcat)
到此tomcat已经安装完成,现在使用浏览器访问 http://localhost:8080,出现tomcat默认页面,
系统重启后自动启动Tomcat:
[root@tpwb ~]# vi /etc/rc.d/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don’t
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
export JDK_HOME=/usr/java/jdk1.7.0
export JAVA_HOME=/usr/java/jdk1.7.0
/usr/src/tomcat/bin/catalina.sh start
设置虚拟主机:
# vi /usr/src/tomcat/conf/server.xml
autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
prefix="www.benet.com_access_log." pattern="common" suffix=".txt"
timestamp="true"/>
或
autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
prefix="www.benet.com_access_log." pattern="common" suffix=".txt"
timestamp="true"/>
一个jsp页面:
# mkdir /usr/src/tomcat/webapps/ROOT/test
# cd /usr/src/tomcat/webapps/ROOT/test
# vi hello.jsp
:wq
访问:http://localhost:8080/test/hello.jsp
附:
tomcat默认是不开启目录浏览的,可以通过修改web.xml里的listings的值来设定,改false为true。如
下:
listings
true
默认不开启servlet直接访问,可以修改web.xml和context.xml的配置来开启,如下:
web.xml。找到这段,去掉注释 ()
invoker
org.apache.catalina.servlets.InvokerServlet
还有这段,也去掉注释
invoker
context.xml
在Context里增加privileged
3、安装mysql
# useradd -M -s /sbin/nologin mysql
# tar zxf mysql-5.0.56.tar.gz -C /usr/src
# cd /usr/src/mysql-5.0.56
# ./configure --prefix=/usr/local/mysql --with-unix-sock-path=/var/lib/mysql/mysql.sock
# make
# make install
# cp support-files/my-medium.cnf /etc/my.cnf
# vi /etc/my.cnf
socket=/var/lib/mysql/mysql.sock
:wq
# /usr/local/mysql/bin/mysql_install_db --user=mysql
# chown -R root.mysql /usr/local/mysql/
# chown -R mysql /usr/local/mysql/var
# echo "/usr/local/mysql/lib/mysql" >> /etc/ld.so.conf
# ldconfig
# /usr/local/mysql/bin/mysqld_safe --user=mysql &
# 回车(Enter键)
# netstat -ntpl | grep 3306
# cp support-files/mysql.server /etc/init.d/mysqld
# chmod +x /etc/init.d/mysqld
# chkconfig --add mysqld
# chkconfig --level 35 mysqld on
# export PATH=$PATH:/usr/local/mysql/bin
# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
# mysqladmin -u root password "123456"
# mysql -u root -p
4. mysql-connector-java安装
http://dev.mysql.com/downloads/connector/j/5.1.html
4.1 解压缩,把jar文件移到tomcat lib目录下
# tar -zxvf mysql-connector-java-5.1.6.tar.gz
# cp mysql-connector-java-5.1.6/mysql-connector-java-5.1.6-bin.jar /usr/src/local/tomcat/lib/
4.2 配置数据池连接
4.2 .1 修改tomcat/conf/server.xml,对原来test项目进行修改,添加数据池配置
autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
auth="Container"
type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password=""
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/test_db_name?
autoReconnect=true&useUnicode=true&characterEncoding=utf-8"/>
4.2 .2 在/usr/src/tomcat/webapps/ROOT/WEB-INF/web.xml文件,加入如下内容:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
My Test App
DB Connection
jdbc/test
javax.sql.DataSource
Container
4.2.3 mysql.jsp 测试 (先在mysql数据库里建个数据库phpbb,表phpbb,用户phpbb,密码phpbb)
# mysql -u root -p
> create database phpbb;
> use phpbb;
> create table phpbb(user_name char(30) not null,user_passwd char(20) not null);
> insert into phpbb.phpbb(user_name,user_passwd) values('phpbb',encrypt('123456'));
> grant all on phpbb.* to phpbb@'localhost' identified by 'phpbb';
> quit
# vi mysql.jsp
String url ="jdbc:mysql://localhost/phpbb?
user=phpbb&password=phpbb&useUnicode=true&characterEncoding=8859_1";
Connection conn= DriverManager.getConnection(url);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="show tables";
ResultSet rs=stmt.executeQuery(sql);%>
stmt.close();
conn.close();
%>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。