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

經測試,可以正常插入表

相關文章
相關標籤/搜索