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

时间:2022-09-01 21:47:46

I've just started working with Hibernate and when I'm finally trying to launch the web-app I get the following error:

我刚开始使用Hibernate,当我最终尝试启动web应用时,我得到了以下错误:

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"

Which is really frustrating. Why aren't I a localhost? What is 192.168.1.7? I could just grant privileges like the answers to similar questions suggest but why aren't I localhost?

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

I understand the problem here, the hbm2ddl setting is set to create, so it wants to create the given tables but doesn't get permission from the MySQL-server.

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

I'm developing a struts2/hibernate app in Eclipse and I use Tomcat and of course MYSQL.

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

Thanks in advance!

提前谢谢!

EDIT: (hibernate.cfg.xml)

编辑(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 个解决方案

#1


10  

Look into your hibernate configuration file. There is an entry with

查看hibernate配置文件。有一个条目

<property name="connection.url"> ... </property>

<属性名= "连接。url "> …< /属性>

You must change this entry to connect to mysql at localhost.

您必须更改此条目以连接到本地主机的mysql。

Next look into mysql. Connect to mysql and check privileges:

下一个mysql。连接到mysql并检查权限:

$ mysql -u root -p
mysql> show grants for 'root'@'localhost';
mysql> show grants for 'root'@'192.168.1.7';

$ mysql -u root -p mysql>表示“root”@“localhost”;mysql>表示“root”@“192.168.1.7”;

If there are no grants for the appropriate database or tables, add them:

如果对适当的数据库或表没有赠款,则添加:

mysql> grant all privileges on clothes.* to 'root'@'localhost' identified by 'password';

mysql>授予所有服装特权。*以“密码”识别“root”@“localhost”;

or

mysql> grant all privileges on clothes.* to 'root'@'192.168.1.7' identified by 'password';

mysql>授予所有服装特权。*由“密码”识别至“root”@“192.168.1.7”;

and then

然后

mysql> flush privileges;

mysql >冲洗特权;

#2


6  

run this command on your MySQL instance (replace the tokens)

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

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

this will allow you to connect to mysql from any host. 192.168.1.7 is the ip of your computer.

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

If you want to secure your db instance, read the this bit of the mysql documentation.

如果您想要保护db实例,请阅读mysql文档的这一部分。

#3


0  

There are severals steps which you need to do:

有几个步骤你需要做:

Step1: do some configuration on my.conf

步骤1:对我的conf做一些配置

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

Step2: go to mysql command(("your_remote_ip" is the pc which wants to access your mysql))

步骤2:转到mysql命令(“your_remote_ip”是希望访问mysql的pc)

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

Step3: you can check its configuration on mysql

步骤3:你可以在mysql上检查它的配置

use mysql;

select user, host from user;

Step4: restart your mysql

目的:重新启动mysql

sudo /etc/init.d/mysql restart

#1


10  

Look into your hibernate configuration file. There is an entry with

查看hibernate配置文件。有一个条目

<property name="connection.url"> ... </property>

<属性名= "连接。url "> …< /属性>

You must change this entry to connect to mysql at localhost.

您必须更改此条目以连接到本地主机的mysql。

Next look into mysql. Connect to mysql and check privileges:

下一个mysql。连接到mysql并检查权限:

$ mysql -u root -p
mysql> show grants for 'root'@'localhost';
mysql> show grants for 'root'@'192.168.1.7';

$ mysql -u root -p mysql>表示“root”@“localhost”;mysql>表示“root”@“192.168.1.7”;

If there are no grants for the appropriate database or tables, add them:

如果对适当的数据库或表没有赠款,则添加:

mysql> grant all privileges on clothes.* to 'root'@'localhost' identified by 'password';

mysql>授予所有服装特权。*以“密码”识别“root”@“localhost”;

or

mysql> grant all privileges on clothes.* to 'root'@'192.168.1.7' identified by 'password';

mysql>授予所有服装特权。*由“密码”识别至“root”@“192.168.1.7”;

and then

然后

mysql> flush privileges;

mysql >冲洗特权;

#2


6  

run this command on your MySQL instance (replace the tokens)

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

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

this will allow you to connect to mysql from any host. 192.168.1.7 is the ip of your computer.

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

If you want to secure your db instance, read the this bit of the mysql documentation.

如果您想要保护db实例,请阅读mysql文档的这一部分。

#3


0  

There are severals steps which you need to do:

有几个步骤你需要做:

Step1: do some configuration on my.conf

步骤1:对我的conf做一些配置

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

Step2: go to mysql command(("your_remote_ip" is the pc which wants to access your mysql))

步骤2:转到mysql命令(“your_remote_ip”是希望访问mysql的pc)

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

Step3: you can check its configuration on mysql

步骤3:你可以在mysql上检查它的配置

use mysql;

select user, host from user;

Step4: restart your mysql

目的:重新启动mysql

sudo /etc/init.d/mysql restart