简体   繁体   English

如何在没有运行mySQL服务器的情况下从JDBC调用记录SQL语句?

[英]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.

相关问题 如何在 Dropwizard 中记录 SQL 语句 - How do I log SQL Statements in Dropwizard 如何使用 JDBC 连接到 SQL Server 2008 数据库? - How do I connect to a SQL Server 2008 database using JDBC? 如何在不使用预准备语句的情况下清理SQL - How do I sanitize SQL without using prepared statements 在没有预准备语句的情况下防止SQL注入(JDBC) - Preventing SQL injection without prepared statements (JDBC) 当将最新的jdbc驱动程序用于SQL Server 2005/2008时,准备好的语句,视图和存储过程如何比较性能? - When using the latest jdbc driver for SQL Server 2005/2008 how do prepared statements, views, and stored procedures compare regarding performance? 针对 Hyper-V VM 中运行的 SQL Server 诊断慢速 JDBC 选择语句 - Diagnose slow JDBC select statements against SQL Server running in a Hyper-V VM 如何在不执行SQL查询的情况下验证我的jdbc连接是否数据库正在运行 - How can I validate my jdbc connection whether the data base is up running without executing sql query 如何配置log4j来记录mybatis的sql语句 - how to configure log4j to log sql statements from mybatis 如何在不崩溃的情况下处理来自jni的AudioTrack调用? - How do I handle calls to AudioTrack from jni without crashing? 如何使用JDBC获取正在运行的语句的列表 - How to get a list of running statements using JDBC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM