MySQL开启general_log跟踪数据执行过程

1. 设置general log保存路径 
css

(1)注意在Linux中只能设置到 /tmp 或 /var 文件夹下,设置其余路径出错java

mysql>set global general_log_file='/tmp/general.lg';

(2)windows:mysql

mysql>set global general_log_file='D:\general.lg';//log文件放在D盘根目录下


2.开启general log模式 
sql

mysql>set global general_log=on;


3. 关闭general log模式 数据库

mysql>set global general_log=off;


在general log模式开启过程当中,全部对数据库的操做都将被记录 general.log 文件windows


项目中遇到的问题:eclipse

在调试hibernate的时候发现设置hbm2ddl.auto设置为update是不能工做,eclipse中报以下错误:测试

Hibernate: insert into news (title, content) values (?, ?)spa

Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not insert: [com.ericsson.ewanbao.News]hibernate

at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)

at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)

...................

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'hibernate.news' doesn't exist

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

...............

问题分析:

经检查数据库发现,news表确实建立不成功,可见hbm2ddl.auto设置为update没有可以创建表

解决步骤:

1.把mysql的general-log打开,这样能够跟踪全部的sql语句;

2.再次执行程序,在general-log的最后发现以下sql语句:

   3 Query  SHOW FULL TABLES FROM `hibernate` LIKE 'news'

   3 Query  create table news (id integer not null auto_increment, title varchar(255), content varchar(255), primary key (id)) type=InnoDB

   3 Query  SHOW FULL TABLES FROM `hibernate` LIKE 'PROBABLYNOT'

   3 Query  SET autocommit=0

   3 Query  SET autocommit=1

   3 Query  SET autocommit=0

   3 Query  insert into news (title, content) values ('???????', '???????????!')

   3 Query  SHOW FULL TABLES FROM `hibernate` LIKE 'PROBABLYNOT'

3.能够发现hibernate已经发送了见表语句给数据库,为何建表没有成功了?尝试拷贝这些建表语句到sql控制台执行,获得以下结果:

mysql> create table news (id integer not null auto_increment, title varchar(255), content varchar(255), primary key (id)

) type=InnoDB;

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version

 for the right syntax to use near 'type=InnoDB' at line 1

4.能够看到是sql语句错误,经分析发现是hibernate.dialect的设置错误,原先设置为org.hibernate.dialect.MySQLInnoDBDialect,应该修改成org.hibernate.dialect.MySQL5InnoDBDialect

经测试,可以正常插入表

相关文章
相关标签/搜索