【问题标题】:Cannot Access MySQL installed in Virtual Machine无法访问安装在虚拟机中的 MySQL
【发布时间】:2015-07-14 18:28:54
【问题描述】:

我在安装在 VMware 中的 xubuntu 15.04 中安装 MySQL。
这是 MySQL 的详细信息:

mysql  Ver 14.14 Distrib 5.6.24, for debian-linux-gnu (i686) using  EditLine wrapper
Copyright (c) 2000, 2015, 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.

我已经设置了 netstat。

@ubuntu:~$ netstat -an|grep 3306
tcp     0   0 127.0.0.1:3306        0.0.0.0:*               LISTEN   

另外,我删除了 /etc/mysql/mysql.conf.d/mysqld.cnf 中的 bind_addressskip-externak-locking (我在 /etc/mysql/my.cnf 中找不到这两个。我只在 mysqld.cnf 中找到它们。)然后重新启动 mysql。

我还设置了 MySQL 的用户:

mysql> create user 'test'@'localhost' identified by 'MY_PASSWORD';
mysql> create user 'test'@'%' identified by 'MY_PASSWORD';
mysql> grant all on *.* to 'test'@'localhost';
mysql> grant all on *.* to 'test'@'%';

正如https://stackoverflow.com/a/21382716/1618596所说,我打开了mysql的端口:

sudo /sbin/iptables -A INPUT -i eth0 -p tcp --destination-port 3306 -j ACCEPT

然后我想访问我本地机器上的数据库。
我在 Hibernate 中使用 Java。这是hibernate.cfg.xml的代码(192.168.96.130是我xubuntu的ip地址)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory name="">
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://192.168.96.130/Test_DB</property>
        <property name="hibernate.connection.username">test</property>
        <property name="hibernate.connection.password">MY_PASSWORD</property>
        <property name="show_sql">true</property>
    </session-factory>
</hibernate-configuration>

但它显示错误:

WARN: HHH000277: Could not bind factory to JNDI
org.hibernate.engine.jndi.JndiException: Error parsing JNDI name []
    at org.hibernate.engine.jndi.internal.JndiServiceImpl.parseName(JndiServiceImpl.java:141)
    at org.hibernate.engine.jndi.internal.JndiServiceImpl.bind(JndiServiceImpl.java:157)
    at org.hibernate.internal.SessionFactoryRegistry.addSessionFactory(SessionFactoryRegistry.java:103)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:497)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1859)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1930)
    at M.main(M.java:14)

因此,我想知道如何访问我的数据库。

【问题讨论】:

    标签: mysql virtual-machine vmware


    【解决方案1】:

    您需要从您的休眠配置文件中的打开会话工厂标签中删除 name 属性,以便您的 hibernate.xml 看起来像这样:

      <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
        <session-factory>
            <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="hibernate.connection.url">jdbc:mysql://192.168.96.130/Test_DB</property>
            <property name="hibernate.connection.username">test</property>
            <property name="hibernate.connection.password">MY_PASSWORD</property>
            <property name="show_sql">true</property>
        </session-factory>
    </hibernate-configuration>
    

    【讨论】:

    • 谢谢。有用。令人难以置信的是问题如此简单......只是因为一个属性。
    【解决方案2】:

    发生这种情况是因为 hibernate 生成工具没有与最新版本的 hibernate 保持同步。到今天为止,您将在 Hibernate Core 4.3.10 Final 和 JBoss Tools 4.2.3 Final 版本(都考虑最新版本)中遇到此错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-17
      • 2017-04-08
      • 2014-04-08
      • 2018-01-09
      • 1970-01-01
      • 1970-01-01
      • 2017-03-24
      • 2022-10-04
      相关资源
      最近更新 更多