[英]How do I log SQL statements from JDBC calls without mySQL server running?
So, I'm trying to figure out how to log the SQL statements that would be run, without actually having an active mySQL server. 因此,我试图弄清楚如何记录将要运行的SQL语句,而实际上没有活动的mySQL服务器。
The problem I'm trying to solve is right now we're writing data to both a remote mySQL instance, as well as a local (archive data). 我要解决的问题是现在我们正在将数据写入远程mySQL实例和本地(归档数据)中。 We're logging to a local as a backup in case the remote becomes unreachable/goes down/etc.
我们正在登录到本地作为备份,以防远程无法访问/宕机等。
So, what we'd like to do instead is log the SQL statements locally (we're going through Spring JDBC Template w/variable replacement), so not quite as easy as taking the SQL we piece together ourselves and write it to a file. 因此,我们要做的是在本地记录SQL语句(我们正在通过带有可变替换的Spring JDBC模板进行日志记录),而不是像将SQL拼凑在一起并将其写入文件中那样容易。
I did find log4jdbc which looks great, except we'd still need a local mySQL instance active, even if it's just using the blackhole engine. 我确实找到了看起来不错的log4jdbc ,除了我们仍然需要一个活动的本地mySQL实例,即使它只是使用黑洞引擎也是如此。 I did think maybe we could just use the log4jdbc on the remote server, but if the connection goes away, will the JDBCTemplate even try and run the queries on the underlying JDBC driver objects before getting the failure?
我确实认为也许我们可以只在远程服务器上使用log4jdbc,但是如果连接断开,JDBCTemplate会在失败之前甚至尝试在基础JDBC驱动程序对象上运行查询吗? Most pooling mechanisms will validate the connection before returning it, so it'd still just fail before the query had a chance to run & get logged.
大多数缓冲机制都会在返回连接之前对其进行验证,因此在查询有机会运行并被记录之前,连接仍然会失败。
So, who has any bright ideas? 那么,谁有什么好主意?
Run the remote MySQL instance with bin logging enabled . 在启用bin日志记录的情况下运行远程MySQL实例。
The binary logs can be backed up and if necessary converted into SQL using the mysqlbinlog command, to later restore a database. 可以备份二进制日志 ,并在必要时使用mysqlbinlog命令将其转换为SQL,以便以后还原数据库。
一般而言,Mark O'Connors解决方案可能是最好的解决方案,但是我们解决此问题的方法是将数据简单地写为CSV文件格式,以便可以通过加载数据infile语句导入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.