2012-11-14 93 views
7

我剛開始使用Hibernate而當我終於嘗試推出的網絡應用程序,我得到以下錯誤:無法連接到MySQL - 主機不允許

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?我可以授予像類似問題的答案一樣的權限,但爲什麼我不是本地主機?

我明白這裏的問題,hbm2ddl設置設置爲創建,所以它想創建給定的表,但沒有從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> 

回答

10

看看你的hibernate配置文件。有一個條目

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

您必須更改此項以連接到本地主機上的mysql。

接下來看看mysql。連接到MySQL和檢查權限:

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

如果沒有獲取相應的數據庫或表沒有補助,加上他們:

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

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

然後

mysql> flush privileges;

+1

jdbc:mysql:// localhost:3306/clothes已經是localhost,你可以看到 –

+0

好了,其他屬性也設置爲正確的值? –

+0

檢查上面的問題的編輯,我不認爲錯誤是在那裏,但請檢查出來! –

6

運行在你的MySQL實例此命令(替換標記)

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

這將允許你從任何主機連接到MySQL 。 192.168.1.7是您電腦的IP地址。

如果你想保護你的數據庫實例,請閱讀mysql documentation的這一位。

+1

什麼你的意思是我的電腦的IP?我的電腦在我的家庭網絡中的IP?我有沒有辦法以localhost身份連接? –

0

有幾個步驟wh ich你需要做的:

第1步:做一些配置我的。CONF

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

第二步:去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; 

第三步:您可以檢查mysql的其配置

use mysql; 

select user, host from user; 

第四步:重新啓動你的mysql

sudo /etc/init.d/mysql restart 
相關問題