我遇到了 Java 8、MySQL Server 5.7.23、MySQL Connector for Java v5.1.45 和 Tomcat v9.0.12(在 Ubuntu Server 18.04 上运行)的问题。
我构建了一个简单的 Java 应用程序 (war),它在 MySQL 数据库中执行查询。
我的context.xml(我放了2个资源节点,一个用于生产环境,一个用于开发环境),从我从CATALINA_OPTS中读取一个名为“env”的参数并使用System.getProperty(“env"),应用程序可以选择合适的数据源来使用。
<Context>
<!-- MySQL - Production -->
<Resource
name="prod-jdbc/ds-1" auth="Container"
type="javax.sql.DataSource" factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/testdb?useSSL=false"
username="dbuser" password="dbUserPwd!"
autoReconnect="true" maxTotal="10"
minIdle="3" maxIdle="5" maxActive="10" maxWaitMillis="10000"
removeAbandonedOnBorrow="true" removeAbandonedOnMaintenance="true"
logAbandoned="true" removeAbandonedTimeout="1000"
closeMethod="close" testOnBorrow="true"
testWhileIdle="true" timeBetweenEvictionRunsMillis="60000"
validationQueryTimeout="10" validationInterval="6000"
validationQuery="SELECT 1"
/>
<!-- MySQL - Development -->
<Resource
name="dev-jdbc/ds-1" auth="Container"
type="javax.sql.DataSource" factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/localdb?useSSL=false"
username="root" password="root"
autoReconnect="true" maxTotal="10"
minIdle="3" maxIdle="5" maxActive="10"
maxWaitMillis="10000" removeAbandonedOnBorrow="true"
removeAbandonedOnMaintenance="true" logAbandoned="true"
removeAbandonedTimeout="1000" closeMethod="close"
testOnBorrow="true" testWhileIdle="true"
timeBetweenEvictionRunsMillis="60000" validationQueryTimeout="10"
validationInterval="6000" validationQuery="SELECT 1"
/>
</Context>
一个简单的测试类:
package main;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.sql.DataSource;
import javax.ws.rs.core.Application;
public class ConnectionTester extends Application implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent arg0) {
Context ctx = null; Connection con = null; Statement stmt = null; ResultSet rs = null;
try{
ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:/comp/env/prod-jdbc/ds-1");
con = ds.getConnection();
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT nome FROM Customers");
while(rs.next()) {
System.out.println(rs.getString("name"));
}
}
catch(NamingException | SQLException e){
e.printStackTrace();
}
finally{
try {
rs.close(); stmt.close(); con.close(); ctx.close();
} catch (SQLException e) {
System.out.println("Exception in closing DB resources");
} catch (NamingException e) {
System.out.println("Exception in closing Context");
}
}
} // contextInitialized
} // ConnectionTester
我已经以这种方式设置了 MySQL 权限(我还需要允许“dbuser”的外部访问,因为我有一个连接到此 MySQL 数据库的外部客户端):
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'veryStrongPwd';
CREATE USER 'dbuser'@'localhost' IDENTIFIED BY 'dbUserPwd!';
GRANT ALL PRIVILEGES ON *.* TO 'dbuser'@'localhost' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'dbuser'@'%' IDENTIFIED BY 'dbUserPwd!' WITH GRANT OPTION;
FLUSH PRIVILEGES;
Tomcat 没有任何特定的配置或资源配置,它只是将 v9.0.12 解压缩到一个文件夹中。
我尝试将“root”和“dbuser”凭据都放入 context.xml 中,但是当我启动 Tomcat 时,出现以下问题:
ConnectionPool.init Unable to create initial connections of pool.
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:873)
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1710)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1226)
...
抛出这些异常后,它会打印记录集,但为什么即使在 context.xml 中我设置了 dbuser 凭据,它仍需要 root 登录?
我读过在 StackOverflow 中搜索过的内容,我读过我必须检查 mysql.user 表,如果我运行:
SELECT user, host, plugin, authentication_string FROM mysql.user;
我明白了(“插件”列的每条记录都有“mysql_native_password”值):
如果我尝试使用以下命令从 shell 登录到 MySQL 服务器:
mysql -u root (or dbuser) -p
可以登录成功
编辑:我发现从 context.xml(为开发环境制作的那个)中删除第二个“资源”节点,问题就消失了,但我仍然不明白为什么会发生,因为在查找中我选择prod 资源 (ctx.lookup("java:/comp/env/prod-jdbc/ds-1")).
我错过了什么?提前致谢
最佳答案
在您的 context.xml 中,您有 root 用户密码作为 root。在您的 MySQL 脚本中,您已将 root 用户密码设置为 veryStrongPwd。
如果您的问题更多是关于为什么即使您不使用 Resource 也会收到错误 - 您将连接池的最小大小设置为 3。所以 Tomcat 会创建连接池,并在启动时尝试向其中添加 3 个连接。或许,如果您将 minIdle 设置为 0,它就会按您希望的方式工作。
更新
还有另一个设置 - initialSize,默认为 10。将其设置为 0 应该会阻止 Tomcat 在启动时尝试使用该 DataSource。
关于java - context.xml 中有两个数据源的用户 'root' @'localhost' 的访问被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52920253/
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
我已经像这样安装了一个新的Rails项目:$railsnewsite它执行并到达:bundleinstall但是当它似乎尝试安装依赖项时我得到了这个错误Gem::Ext::BuildError:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcheckingforlibkern/OSAtomic.h...yescreatingMakefilemake"DESTDIR="cleanmake"DESTDIR="
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/