无法连接到MySQL - 不允许主机

时间:2012-11-14 12:09:26

标签: mysql

我刚刚开始使用Hibernate,当我最终尝试启动网络应用时,我收到以下错误:

2012-nov-14 12:54:23 org.hibernate.tool.hbm2ddl.SchemaExport execute
ERROR: HHH000231: Schema export unsuccessful
java.sql.SQLException: null,  message from server: "Host '192.168.1.7' is not allowed to
connect to this MySQL server"

这真令人沮丧。为什么我不是本地主机?什么是192.168.1.7?我可以授予像类似问题的答案所说的特权,但为什么我不是localhost呢?

我理解这里的问题,hbm2ddl设置被设置为create,所以它想要创建给定的表但是没有得到MySQL服务器的许可。

我正在Eclipse中开发一个struts2 / hibernate应用程序,我使用Tomcat,当然还有MYSQL。

提前致谢!

编辑:(hibernate.cfg.xml)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
    <property name="connection.driver_class"> com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/clothes</property>
    <property name="connection.username">root</property>
    <property name="connection.password">root</property>

    <property name="connection.pool_size">1</property>
    <property name="dialect"> org.hibernate.dialect.MySQLDialect</property>

    <property name="show_sql">true</property>

    <!-- Needs to be disabled in production! -->
    <property name="hbm2ddl.auto">create</property>

    <mapping class="johanstenberg.clothes.serverobjects.AdEntity"/>
    <mapping class="johanstenberg.clothes.serverobjects.AuthenticationEntity"/>
    <mapping class="johanstenberg.clothes.serverobjects.UserEntity"/>
    <mapping class="johanstenberg.clothes.serverobjects.VerificationKeyEntity"/>
</session-factory>
</hibernate-configuration>

3 个答案:

答案 0 :(得分:11)

查看你的hibernate配置文件。

有一个条目
  

&lt; property name =“connection.url”&gt; ...&lt; / property&gt;

您必须更改此条目才能在localhost连接到mysql。

接下来看看mysql。连接到mysql并检查权限:

  

$ mysql -u root -p
  MySQL的&GT;显示'root'@'localhost'的授权;
  MySQL的&GT;显示'root'@ 192.168.1.7'的补助金;

如果没有相应数据库或表的授权,请添加它们:

  

的MySQL&GT;在衣服上授予所有特权。*由'密码'标识的'root'@'localhost';

  

的MySQL&GT;给予衣服上的所有特权。*''root'@'192.168.1.7'由'密码'标识;

然后

  

的MySQL&GT;冲洗权限;

答案 1 :(得分:6)

在MySQL实例上运行此命令(替换标记)

CREATE USER '${username}'@'%' IDENTIFIED BY '${password}';
GRANT ALL ON *.* TO '${username}'@'%';

这将允许您从任何主机连接到mysql。 192.168.1.7是您计算机的IP。

如果要保护数据库实例,请阅读mysql documentation

的这一位

答案 2 :(得分:0)

您需要执行以下几个步骤:

步骤1:在my.conf上进行一些配置

sudo vi /etc/mysql/my.cnf
bind-address = 0.0.0.0

Step2:转到mysql命令((&#34; your_remote_ip&#34;是想要访问你的mysql的电脑))

mysql -u root -p
GRANT ALL PRIVILEGES ON *.* TO 'your_mysql_username'@'your_remote_ip'IDENTIFIED BY 'your_mysql_password' WITH GRANT OPTION;

Step3:你可以在mysql上检查它的配置

use mysql;

select user, host from user;

Step4:重新启动你的mysql

sudo /etc/init.d/mysql restart
相关问题