java JBoss:无法连接到 MySQL 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13978767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:37:19  来源:igfitidea点击:

JBoss: can not connect to MySQL database

javamysqljbossjboss7.xmysql-connector

提问by daydreamer

I am running JBossas standalone version. I would like to connect to my local mysql database

JBoss作为独立版本运行。我想连接到我的本地mysql database

I copied the mysql connector in modules folder

我复制了模块文件夹中的 mysql 连接器

me@air~/Downloads/jboss/standalone/configuration - 10:56:18  $ ls ../../modules/org/mysql/main/
mysql-connector-java-5.1.22-bin.jar

Next I changed the standalone.xmlin jboss/standalone/configurationas

接下来我将standalone.xmlin更改jboss/standalone/configuration

                <drivers>
                    <driver name="com.mysql" module="org.mysql" />
                    <driver name="h2" module="com.h2database.h2"> <!-- default provided-->
                        <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
                    </driver>
                </drivers>

Then I added the datasource as

然后我将数据源添加为

              <datasource jndi-name="java:/bb" pool-name="bb-pool" enabled="true" use-java-context="true" use-ccm="true">
                    <connection-url>jdbc:mysql://127.0.0.1:3306/mydb</connection-url>
                    <driver>mysql</driver>
                    <pool>
                        <min-pool-size>10</min-pool-size>
                        <max-pool-size>100</max-pool-size>
                        <prefill>true</prefill>
                    </pool>
                    <security>
                        <user-name>bb</user-name>
                        <password>bb</password>
                    </security>
                    <statement>
                        <prepared-statement-cache-size>100</prepared-statement-cache-size>
                        <share-prepared-statements>true</share-prepared-statements>
                    </statement>
                </datasource>

I am using JBoss AS 7.1. When I start the server on console, I see errors

我正在使用JBoss AS 7.1. 当我在控制台上启动服务器时,我看到错误

10:54:47,458 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-1) JBAS010400: Bound data source [java:jboss/datasources/ExampleDS]
10:54:47,671 INFO  [org.jboss.as.controller] (Controller Boot Thread) JBAS014774: Service status report
JBAS014775:    New missing/unsatisfied dependencies:
      service jboss.jdbc-driver.mysql (missing) dependents: [service jboss.data-source.java:/bb] 

10:54:47,676 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015951: Admin console listening on http://127.0.0.1:9990
10:54:47,676 ERROR [org.jboss.as] (Controller Boot Thread) JBAS015875: JBoss AS 7.1.1.Final "Brontes" started (with errors) in 2000ms - Started 134 of 212 services (2 services failed or missing dependencies, 74 services are passive or on-demand)

and when I hit Ctrl+C, I see

当我击中时Ctrl+C,我看到

^C10:54:54,998 INFO  [org.jboss.as.osgi] (MSC service thread 1-8) JBAS011942: Stopping OSGi Framework
10:54:55,004 INFO  [org.jboss.as.logging] JBAS011503: Restored bootstrap log handlers
10:54:55,007 INFO  [com.arjuna.ats.jbossatx] ARJUNA032018: Destroying TransactionManagerService
10:54:55,008 INFO  [com.arjuna.ats.jbossatx] ARJUNA032014: Stopping transaction recovery manager
10:54:55,010 INFO  [org.apache.catalina.core.StandardContext] Container org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/] has not been started
10:54:55,039 INFO  [org.apache.coyote.http11.Http11Protocol] Pausing Coyote HTTP/1.1 on http--127.0.0.1-8080
10:54:55,039 INFO  [org.apache.coyote.http11.Http11Protocol] Stopping Coyote HTTP/1.1 on http--127.0.0.1-8080
10:54:55,043 INFO  [org.jboss.as.controller] JBAS014774: Service status report
JBAS014776:    Newly corrected services:
      service jboss.jdbc-driver.mysql (new available)

10:54:55,044 INFO  [org.jboss.as] JBAS015950: JBoss AS 7.1.1.Final "Brontes" stopped in 47ms

I am new to JBoss and don't know how to fix this issue or to understand what is that I am doing wrong here

我是 JBoss 的新手,不知道如何解决这个问题,也不知道我在这里做错了什么

回答by jyore

I think in your datasource you are referencing the driver mysql, but you named the driver com.mysql.

我认为在您的数据源中您引用了驱动程序mysql,但您命名了驱动程序com.mysql

Try changing one the driver name to mysql.

尝试将驱动程序名称更改为mysql.

Just a note: it is best to use the CLI to install the drivers and use the CLI or Web Console to build your datasources. The intention with AS7 is that these tools are used for all of your config and there are very few reasons why you should touch the XML yourself.

请注意:最好使用 CLI 安装驱动程序并使用 CLI 或 Web 控制台构建数据源。AS7 的目的是将这些工具用于您的所有配置,并且您应该自己接触 XML 的原因很少。

回答by Caina Santos

I had exactly the same mistake. Took me hours to find out how dumb it was.

我犯了完全相同的错误。我花了几个小时才发现它有多愚蠢。

In my case I was running jboss as a service under the jboss-as user, nice and easy. Except that when I created the mysql/main directory, along with the files within, I was under another user. No permissions there for jboss-as guy.

就我而言,我在 jboss-as 用户下将 jboss 作为服务运行,这很好很简单。除了当我创建 mysql/main 目录以及其中的文件时,我在另一个用户下。jboss-as 没有权限。

A simple chown on modules/ directory did the job.

模块/目录上的一个简单 chown 完成了这项工作。

回答by Radu

First of all, your driver definition name doesn't match the driver name under the <datasource>definition. Replace <driver name="com.mysql" ...with <driver name="mysql" ...for it to work.

首先,您的驱动程序定义名称与<datasource>定义下的驱动程序名称不匹配。更换<driver name="com.mysql" ...<driver name="mysql" ...为它工作。

Second, have you added the module.xmlfile? It's not enough to copy the MySQL connector jar in the ./modules/com/mysql/mainfolder, you also have to add the module.xmlfile:

其次,你有没有添加module.xml文件?将 MySQL 连接器 jar 复制到./modules/com/mysql/main文件夹中是不够的,您还必须添加module.xml文件:

<?xml version="1.0" encoding="UTF-8"?>

<module xmlns="urn:jboss:module:1.1" name="com.mysql">
  <resources>
    <resource-root path="mysql-connector-java-5.1.26-bin.jar"/>
  </resources>
  <dependencies>
    <module name="javax.api"/>
    <module name="javax.transaction.api"/>
  </dependencies>
</module>

Also, I had a strage similar problem when using the MySQL connector version 5.0.8, where the driver was not found / recognized. It disappeared when I switched to the newer 5.1.26 version.

此外,在使用 MySQL 连接器 5.0.8 版时,我遇到了一个类似的问题,其中未找到/识别驱动程序。当我切换到较新的 5.1.26 版本时它消失了。

回答by Indra Yadav

Make sure you are using latest version of mysql jdbc connector, i solve same issue of mine with replacing latest mysql connetor

确保您使用的是最新版本mysql jdbc connector,我通过替换最新的 mysql 连接器解决了我的相同问题