简体   繁体   中英

Cannot connect to MySQL - host not allowed

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

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?

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.

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

Thanks in advance!

EDIT: (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>

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

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

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

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

$ mysql -u root -p
mysql> show grants for 'root'@'localhost';
mysql> show grants for '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';

or

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

and then

mysql> flush privileges;

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

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.

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

There are severals steps which you need to do:

Step1: do some configuration on my.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))

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

use mysql;

select user, host from user;

Step4: restart your mysql

sudo /etc/init.d/mysql restart

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM