Tomcat我们在centos7部署java-web项目时无法连接mariadb
Tomcat can't connect to mariadb when we deployed the java-web project in centos7
当我在 IDEA 中启动 web 项目时,一切都很好,包括与 mariadb 的连接。但是,当我将输出文件 (war) 放入 /usr/share/tomcat/webapps/ 并使用命令 "systemctl restart tomcat" 重新启动 tomcat 服务时。打开 firefox 并输入网站 "localhost:8080/online"。当我输入帐户和密码时,浏览器抛出一个无指针异常,这意味着我无法连接到 mariadb。
为什么以及如何处理这个问题?
环境:
$ uname -a
Linux bogon 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
$ mysql --version
mysql Ver 15.1 Distrib 5.5.60-MariaDB, for Linux (x86_64) using readline 5.1
$ tomcat version
Server version: Apache Tomcat/7.0.76
Server built: Mar 12 2019 10:11:36 UTC
Server number: 7.0.76.0
OS Name: Linux
OS Version: 3.10.0-957.el7.x86_64
Architecture: amd64
JVM Version: 1.8.0_212-b04
JVM Vendor: Oracle Corporation
Tomcat状态:
$ systemctl status tomcat
● tomcat.service - Apache Tomcat Web Application Container
Loaded: loaded (/usr/lib/systemd/system/tomcat.service; disabled; vendor preset: disabled)
Active: active (running) since 二 2019-09-03 09:25:46 CST; 6s ago
Main PID: 31464 (java)
Tasks: 46
CGroup: /system.slice/tomcat.service
└─31464 /usr/lib/jvm/jre/bin/java -classpath /usr/share/tomcat/bin...
MariaDB 状态:
# systemctl status mariadb
● mariadb.service - MariaDB database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
Active: active (running) since 二 2019-09-03 07:15:24 CST; 2h 28min ago
Process: 17937 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
Process: 17905 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
Main PID: 17936 (mysqld_safe)
Tasks: 20
CGroup: /system.slice/mariadb.service
├─17936 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
└─18112 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysq...
9月 03 07:15:20 bogon systemd[1]: Starting MariaDB database server...
9月 03 07:15:20 bogon mariadb-prepare-db-dir[17905]: Database MariaDB is pro...
9月 03 07:15:20 bogon mariadb-prepare-db-dir[17905]: If this is not the case...
9月 03 07:15:20 bogon mysqld_safe[17936]: 190903 07:15:20 mysqld_safe unkno...'
9月 03 07:15:20 bogon mysqld_safe[17936]: 190903 07:15:20 mysqld_safe Loggi....
9月 03 07:15:21 bogon mysqld_safe[17936]: 190903 07:15:21 mysqld_safe Start...l
9月 03 07:15:24 bogon systemd[1]: Started MariaDB database server.
Hint: Some lines were ellipsized, use -l to show in full.
网络应用程序:
# cd /usr/share/tomcat/webapps/
# ll
总用量 19880
drwxr-xr-x. 4 tomcat tomcat 4096 9月 3 07:35 online
-rw-r--r--. 1 root root 20350442 9月 3 07:34 online.war
Tomcat 日志信息:(/var/log/tomcat)
# cat localhost.2019-09-03.log
九月 03, 2019 7:17:51 上午 org.apache.catalina.core.ApplicationContext log
信息: No Spring WebApplicationInitializer types detected on classpath
九月 03, 2019 7:17:52 上午 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring FrameworkServlet 'spring'
九月 03, 2019 7:18:06 上午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [spring] in context with path [/online] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at bright.spring.dao.LoginDao.login(LoginDao.java:20)
at bright.spring.controller.LoginController.login(LoginController.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1087)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2549)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2538)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
LoginDao.java
package bright.spring.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
import bright.spring.jdbc.JDBC;
import bright.spring.pojo.User;
public class LoginDao {
/**/
Map<String,String> u = new HashMap<>();
/**/
ResultSet rs = null;
public Map<String,String> login(User user){
try {
Connection conn = JDBC.getConnection();
Statement s = conn.createStatement();//LoginDao.java:20
if(user.getIdentity().equals("student")){
rs = s.executeQuery("select * from student where username ='"+user.getUsername()+"' and password ='"+user.getPassword()+"'");
if(rs.next()){
u.put("name",rs.getString("studentName") );
}else{
u.put("name","isnull" );
}
}else if(user.getIdentity().equals("teacher")){
rs = s.executeQuery("select * from teacher where username ='"+user.getUsername()+"' and password ='"+user.getPassword()+"'");
if(rs.next()){
u.put("name",rs.getString("teacherName") );
}else{
u.put("name","isnull" );
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return u;
}
}
Class JDBC:
package bright.spring.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JDBC {
static Connection conn;
static{
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("");
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException{
try {
/*conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/onlineexp?useUnicode=true&characterEncoding=utf-8", "root", "ch96");*/
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/onlineexp?useUnicode=true&characterEncoding=utf-8", "root", "ch96");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
}
我可以和你分享我的经验。我也面临同样类型的问题。在我的情况下,我使用 tomcat、Mysql、centOS。
经过长时间的研发,我发现了三个问题。
- 一个是我使用 MySql 8 个服务器。它有不同的驱动程序 class "com.mysql.cj.jdbc.Driver"
- 第二个是 CentOS 防火墙不允许从主机 127.0.0.1
访问端口 3306/tcp 上的 MySQL 服务器
- 最后一个是 Mysql 8 个默认安全未来。 (用户级权限。)
你应该在你的案例中检查所有这些问题......
我认为您必须更新 MariaDB 的代码。
MySQL 驱动程序不适用于 MariaDB。
您必须执行以下操作:
MariaDBJDBC 驱动 :
Class.forName("org.mariadb.jdbc.Driver")
如何获取连接:
Connection connection = DriverManager.getConnection("jdbc:mariadb://127.0.0.1:3306/onlineexp?useUnicode=true&characterEncoding=utf-8", "root", "ch96");
访问 here 了解更多信息。
希望对您有所帮助。 :)
当我在 IDEA 中启动 web 项目时,一切都很好,包括与 mariadb 的连接。但是,当我将输出文件 (war) 放入 /usr/share/tomcat/webapps/ 并使用命令 "systemctl restart tomcat" 重新启动 tomcat 服务时。打开 firefox 并输入网站 "localhost:8080/online"。当我输入帐户和密码时,浏览器抛出一个无指针异常,这意味着我无法连接到 mariadb。 为什么以及如何处理这个问题?
环境:
$ uname -a
Linux bogon 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
$ mysql --version
mysql Ver 15.1 Distrib 5.5.60-MariaDB, for Linux (x86_64) using readline 5.1
$ tomcat version
Server version: Apache Tomcat/7.0.76
Server built: Mar 12 2019 10:11:36 UTC
Server number: 7.0.76.0
OS Name: Linux
OS Version: 3.10.0-957.el7.x86_64
Architecture: amd64
JVM Version: 1.8.0_212-b04
JVM Vendor: Oracle Corporation
Tomcat状态:
$ systemctl status tomcat
● tomcat.service - Apache Tomcat Web Application Container
Loaded: loaded (/usr/lib/systemd/system/tomcat.service; disabled; vendor preset: disabled)
Active: active (running) since 二 2019-09-03 09:25:46 CST; 6s ago
Main PID: 31464 (java)
Tasks: 46
CGroup: /system.slice/tomcat.service
└─31464 /usr/lib/jvm/jre/bin/java -classpath /usr/share/tomcat/bin...
MariaDB 状态:
# systemctl status mariadb
● mariadb.service - MariaDB database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
Active: active (running) since 二 2019-09-03 07:15:24 CST; 2h 28min ago
Process: 17937 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
Process: 17905 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
Main PID: 17936 (mysqld_safe)
Tasks: 20
CGroup: /system.slice/mariadb.service
├─17936 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
└─18112 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysq...
9月 03 07:15:20 bogon systemd[1]: Starting MariaDB database server...
9月 03 07:15:20 bogon mariadb-prepare-db-dir[17905]: Database MariaDB is pro...
9月 03 07:15:20 bogon mariadb-prepare-db-dir[17905]: If this is not the case...
9月 03 07:15:20 bogon mysqld_safe[17936]: 190903 07:15:20 mysqld_safe unkno...'
9月 03 07:15:20 bogon mysqld_safe[17936]: 190903 07:15:20 mysqld_safe Loggi....
9月 03 07:15:21 bogon mysqld_safe[17936]: 190903 07:15:21 mysqld_safe Start...l
9月 03 07:15:24 bogon systemd[1]: Started MariaDB database server.
Hint: Some lines were ellipsized, use -l to show in full.
网络应用程序:
# cd /usr/share/tomcat/webapps/
# ll
总用量 19880
drwxr-xr-x. 4 tomcat tomcat 4096 9月 3 07:35 online
-rw-r--r--. 1 root root 20350442 9月 3 07:34 online.war
Tomcat 日志信息:(/var/log/tomcat)
# cat localhost.2019-09-03.log
九月 03, 2019 7:17:51 上午 org.apache.catalina.core.ApplicationContext log
信息: No Spring WebApplicationInitializer types detected on classpath
九月 03, 2019 7:17:52 上午 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring FrameworkServlet 'spring'
九月 03, 2019 7:18:06 上午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [spring] in context with path [/online] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at bright.spring.dao.LoginDao.login(LoginDao.java:20)
at bright.spring.controller.LoginController.login(LoginController.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1087)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2549)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2538)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
LoginDao.java
package bright.spring.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
import bright.spring.jdbc.JDBC;
import bright.spring.pojo.User;
public class LoginDao {
/**/
Map<String,String> u = new HashMap<>();
/**/
ResultSet rs = null;
public Map<String,String> login(User user){
try {
Connection conn = JDBC.getConnection();
Statement s = conn.createStatement();//LoginDao.java:20
if(user.getIdentity().equals("student")){
rs = s.executeQuery("select * from student where username ='"+user.getUsername()+"' and password ='"+user.getPassword()+"'");
if(rs.next()){
u.put("name",rs.getString("studentName") );
}else{
u.put("name","isnull" );
}
}else if(user.getIdentity().equals("teacher")){
rs = s.executeQuery("select * from teacher where username ='"+user.getUsername()+"' and password ='"+user.getPassword()+"'");
if(rs.next()){
u.put("name",rs.getString("teacherName") );
}else{
u.put("name","isnull" );
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return u;
}
}
Class JDBC:
package bright.spring.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JDBC {
static Connection conn;
static{
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("");
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException{
try {
/*conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/onlineexp?useUnicode=true&characterEncoding=utf-8", "root", "ch96");*/
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/onlineexp?useUnicode=true&characterEncoding=utf-8", "root", "ch96");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
}
我可以和你分享我的经验。我也面临同样类型的问题。在我的情况下,我使用 tomcat、Mysql、centOS。
经过长时间的研发,我发现了三个问题。
- 一个是我使用 MySql 8 个服务器。它有不同的驱动程序 class "com.mysql.cj.jdbc.Driver"
- 第二个是 CentOS 防火墙不允许从主机 127.0.0.1 访问端口 3306/tcp 上的 MySQL 服务器
- 最后一个是 Mysql 8 个默认安全未来。 (用户级权限。)
你应该在你的案例中检查所有这些问题......
我认为您必须更新 MariaDB 的代码。
MySQL 驱动程序不适用于 MariaDB。
您必须执行以下操作:
MariaDBJDBC 驱动 :
Class.forName("org.mariadb.jdbc.Driver")
如何获取连接:
Connection connection = DriverManager.getConnection("jdbc:mariadb://127.0.0.1:3306/onlineexp?useUnicode=true&characterEncoding=utf-8", "root", "ch96");
访问 here 了解更多信息。
希望对您有所帮助。 :)