Hibernate之Mysql或Mariadb数据库连接问题 博客分类: MavenJavaWeb hibernatemariadbmysql
Hibernate之Mysql或Mariadb数据库连接问题
项目是用Maven做的build,配置主要需要注意一下几个地方:
1. 数据库Mysql或Mariadb
yum repository:
http://mirrors.neusoft.edu.cn/mariadb/mariadb-10.0.16/
Mariadb:
https://mariadb.com/kb/en/mariadb/
<dependency> <groupId>org.mariadb.jdbc</groupId> <artifactId>mariadb-java-client</artifactId> <version>1.1.8</version> <scope>compile</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.9</version> </dependency>
2. Hibernate 版本 4.3.8.Final
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.3.8.Final</version> <scope>compile</scope> </dependency>
3. src\main\resources\hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">org.mariadb.jdbc.Driver</property> <property name="hibernate.bytecode.use_reflection_optimizer">false</property> <property name="hibernate.connection.password">passw0rd</property> <property name="hibernate.connection.url">jdbc:mariadb://9.114.194.77:3306/cody</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="use_sql_comments">false</property> <mapping resource="com/mkyong/customer/hibernate/Customer.hbm.xml" /> </session-factory> </hibernate-configuration>
错误:Error calling Driver#connect,connect refused...
xxx is not allowed to connect to this MariaDB server
类似这样的错。
需要注意以下几个地方:
1. hibernate.cfg.xml的配置
<property name="hibernate.connection.driver_class">org.mariadb.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mariadb://9.114.194.77:3306/cody</property>
2. 检查SessionFactory 是不是正常
参考官方文档:
http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch01.html#tutorial-firstapp-firstclass
public class HibernateListener implements ServletContextListener{ private Configuration config; private SessionFactory factory; private String path = "/hibernate.cfg.xml"; private static Class clazz = HibernateListener.class; public static final String KEY_NAME = clazz.getName(); public void contextDestroyed(ServletContextEvent event) { // } public void contextInitialized(ServletContextEvent event) { try { URL url = HibernateListener.class.getResource(path); config = new Configuration().configure(url); System.out.println("Hibernate Configuration loaded"); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build(); System.out.println("Hibernate serviceRegistry created"); factory = config.buildSessionFactory(serviceRegistry); System.out.println("Session Factory:" + factory.toString()); //save the Hibernate session factory into serlvet context event.getServletContext().setAttribute(KEY_NAME, factory); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } }
3. 数据库的授权:
通常这种修改都是web server 和 db在两个机器上,所以有个访问权限的问题。
通常访问的时候会以 root@<web server ip>的形式,这就需要授权和添加相关的用户密码才能访问db。
可以参考这篇文章:
http://www.cnblogs.com/xyzdw/archive/2011/08/11/2135227.html
一、在安装mysql/MariaDB的机器上运行:
1、mysql -h localhost -u root -p //这样应该可以进入MySQL/MariaDB服务器
2、mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION //赋予任何主机访问数据的权限
3、mysql>FLUSH PRIVILEGES //修改生效
4、mysql>EXIT //退出DB
二、配置mysql数据库的user 表的‘%’用户信息:
MariaDB [mysql]> UPDATE user SET Password=PASSWORD('newpwd') WHERE User='root';
MariaDB [mysql]>FLUSH PRIVILEGES;
MariaDB [mysql]>exit;
错误:
hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
参考文档:
http://www.tuicool.com/articles/7FZrYzE