【问题标题】:couldn't connect to mysql from hibernate as admin using mysqlworkbench无法使用 mysqlworkbench 从休眠以管理员身份连接到 mysql
【发布时间】:2019-02-23 04:03:51
【问题描述】:

我正在尝试通过休眠连接到 Mysql。我可以以管理员身份在 mysqlworkbench 中创建数据库,但是在尝试通过休眠连接时出现以下错误

ERROR: Access denied for user 'root'@'localhost' (using password: YES)
Initial sessionfactory creationfailedorg.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
Exception in thread "main" java.lang.ExceptionInInitializerError
    at hibernate.HibernateUtil.<clinit>(HibernateUtil.java:21)
    at hibernate.CustomerDAO.addCustomer(CustomerDAO.java:14)
    at hibernate.ClientDemo.main(ClientDemo.java:20)

以下是我的代码 hibernate.cfg.xml

<?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>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:Mysql://127.0.0.1:3306/myschema</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">Hariharan@1604</property>
        <property name="show_sql">true</property>
        <mapping class="hibernate.Customer" />
    </session-factory>
</hibernate-configuration>

HibernateUtil.java

package hibernate;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtil {
    private static final SessionFactory sessionFactory;
    static
    {
        try
        {
            Configuration cfg=new Configuration().configure("config/Hibernate.cfg.xml");
            ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
            sessionFactory=cfg.buildSessionFactory();
        }
        catch(Throwable ex)
        {
            System.err.println("Initial sessionfactory creationfailed"+ex);
            throw new ExceptionInInitializerError();
        }
    }
    public static SessionFactory getSessionFactory()
    {
        return sessionFactory;
    }

}

Icustomer.java

package hibernate;

public interface ICustomer {
    public void addCustomer(Customer custDAO);


}

CustomerDAO.java

package hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class CustomerDAO implements ICustomer
{

    @Override
    public void addCustomer(Customer custDAO) {
        // TODO Auto-generated method stub
        Session session=HibernateUtil.getSessionFactory().openSession();
        Transaction tx=session.beginTransaction();
        session.save(tx);
        tx.commit();
        session.close();

    }

}

客户.java

package hibernate;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="CUSTOMER")
public class Customer {
    @Id

    private int customerId;
    private String customerName;

    public Customer()
    {}
    public int getCustomerId() {
        return customerId;
    }
    public void setCustomerId(int customerId) {
        this.customerId = customerId;
    }
    public String getCustomerName() {
        return customerName;
    }
    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }
}

ClientDemo.java

package hibernate;
import java.util.Scanner;
import org.hibernate.HibernateException;
public class ClientDemo {
    public static void main(String[] args) {
        CustomerDAO custdao=new CustomerDAO();
        try
        {
            System.out.println("Create");
            System.out.println("Enter the data");
            Scanner sc=new Scanner(System.in);
            System.out.println("ID");
            int id=sc.nextInt();
            System.out.println("Name");
            String str=sc.next();
            Customer cust=new Customer();
            cust.setCustomerId(id);
            cust.setCustomerName(str);
            custdao.addCustomer(cust);
            System.out.println("One record created");
            sc.close();

        }
        catch (HibernateException e) {
            // TODO: handle exception
            System.out.println(e);
        }
    }
}

这是我的连接详情

数据库截图

【问题讨论】:

  • 这个问题一般发生在hibernate属性文件中传入的密码与你的数据库密码不匹配时。重新验证您的密码。
  • 能否提供“user”表的输出

标签: java hibernate mysql-workbench


【解决方案1】:

注意 127.0.0.1 与 MySQL 安全方案的 localhost 不同。

尝试将hibernate.connection.url设置为localhost,或者授予特定权限,例如(但不限于此示例):

GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1';

【讨论】:

  • 当我尝试运行上述 10:21:41 GRANT ALL PRIVILEGES ON . TO 'root'@'127.0.0.1' 时出现错误错误代码:1410 . 不允许创建 GRANT 0.037 sec 的用户
  • 将 hibernate.connection.url 设置为 localhost 我得到同样的错误
  • 我相信您的 root 用户可能没有授予权限。请检查此线程:stackoverflow.com/questions/19237475/…
【解决方案2】:
 <property name="hibernate.connection.url">jdbc:Mysql://127.0.0.1:3306/myschema</property>

除了用户名和密码的正确性之外,您确定您的架构名称是myschema 吗? Mysql 的大 M 也可能是个问题。

【讨论】:

  • 是的,将 Mysql 更改为 mysql,是的,我的架构名称是 myschema
  • 在同一台机器上同时做数据库和应用服务器吗?
【解决方案3】:

root 用户没有权限。所以通过以下查询更改了权限

UPDATE mysql.user SET Grant_priv='Y', Super_priv='Y' WHERE User='root';
FLUSH PRIVILEGES;
GRANT ALL ON *.* TO 'root'@'localhost';

关闭我的连接并再次打开。

【讨论】:

    猜你喜欢
    • 2014-01-02
    • 2016-09-12
    • 2020-07-23
    • 2021-02-12
    • 1970-01-01
    • 2017-05-19
    • 2016-06-07
    • 2012-07-05
    • 1970-01-01
    相关资源
    最近更新 更多