List:Internals« Previous MessageNext Message »
From:Jeremy Zawodny Date:April 27 2002 2:58am
Subject:[PATCH] client/mysqlbinlog.cc -- adds "-d <database" command-line opt
View as plain text  
Sasha & others,

This patch adds a "-d" command-line option to mysqlbinlog so that on a
local log, you can output only the entries from a single database.  I
needed this earlier when trying to track down what is causing
duplicate key problems on my 4.0.2 slave.

I spent a lot of time in mysqlbinlog.cc, sql/log_event.h, and
sql/log_event.cc today.  I don't know if this the "right" way to make
the change, since my C++ is rather rusty.  But it does the job.

Please consider the patch below (or something like it) by sig for
inclusion in 4.0.

Needless to say, I know a heck of a lot more about the binary log
now. :-)

Thanks!

Jeremy
-- 
Jeremy D. Zawodny, <jzawodn@stripped>
Technical Yahoo - Yahoo Finance
Desk: (408) 349-7878   Fax: (408) 349-5454   Cell: (408) 685-5936

MySQL 3.23.47-max: up 78 days, processed 2,053,155,650 queries (302/sec. avg)

--- mysqlbinlog.cc-orig	Fri Apr 26 15:00:41 2002
+++ mysqlbinlog.cc	Fri Apr 26 19:29:28 2002
@@ -43,6 +43,7 @@
 #ifndef DBUG_OFF
   {"debug", 	  optional_argument, 	0, '#'},
 #endif
+  {"database",    required_argument,    0, 'd'},
   {"help", 	  no_argument, 		0, '?'},
   {"host", 	  required_argument,	0, 'h'},
   {"offset", 	  required_argument,	0, 'o'},
@@ -59,6 +60,8 @@
 
 void sql_print_error(const char *format,...);
 
+static bool one_database = 0;
+static const char* database;
 static bool short_form = 0;
 static ulonglong offset = 0;
 static const char* host = "localhost";
@@ -103,7 +106,7 @@
 
 static void print_version()
 {
-  printf("%s  Ver 1.8 for %s at %s\n",my_progname,SYSTEM_TYPE, MACHINE_TYPE);
+  printf("%s  Ver 1.9 for %s at %s\n",my_progname,SYSTEM_TYPE, MACHINE_TYPE);
 }
 
 
@@ -127,6 +130,7 @@
 -?, --help		Display this help and exit\n\
 -s, --short-form	Just show the queries, no extra info\n\
 -o, --offset=N		Skip the first N entries\n\
+-d, --database=database List entries for just this database (local log only)\n\
 -h, --host=server	Get the binlog from server\n\
 -P, --port=port         Use port to connect to the remote server\n\
 -u, --user=username     Connect to the remote server as username\n\
@@ -173,7 +177,7 @@
   int c, opt_index = 0;
 
   result_file = stdout;
-  while((c = getopt_long(*argc, *argv, "so:#::h:j:u:p:P:r:t:?V", long_options,
+  while((c = getopt_long(*argc, *argv, "so:#::d:h:j:u:p:P:r:t:?V", long_options,
 			 &opt_index)) != EOF)
   {
     switch(c)
@@ -183,6 +187,11 @@
       DBUG_PUSH(optarg ? optarg : default_dbug_option);
       break;
 #endif
+    case 'd':
+      one_database = 1;
+      database = my_strdup(optarg, MYF(0));
+      break;
+
     case 's':
       short_form = 1;
       break;
@@ -473,6 +482,37 @@
     }
     if (rec_count >= offset)
     {
+      // see if we should skip this event (only care about queries for now)
+      if (one_database)
+      {
+        if (ev->get_type_code() == QUERY_EVENT)
+        {
+          //const char * log_dbname = ev->get_db();
+          const char * log_dbname = ((Query_log_event*)ev)->db;
+          //printf("entry: %llu, database: %s\n", rec_count, log_dbname);
+
+          if ((log_dbname != NULL) && (strcmp(log_dbname, database)))
+          {
+            //printf("skipping, %s is not %s\n", log_dbname, database);
+            rec_count++;
+            delete ev;
+            continue; // next
+          }
+#ifndef DBUG_OFF
+          else
+          {
+            printf("no skip\n");
+          }
+#endif
+        }
+#ifndef DBUG_OFF
+        else
+        {
+          const char * query_type = ev->get_type_str();
+          printf("not query -- %s\n", query_type);
+        }
+#endif
+      }
       if (!short_form)
         fprintf(result_file, "# at %s\n",llstr(old_off,llbuff));
 
Thread
[PATCH] client/mysqlbinlog.cc -- adds "-d <database" command-line optJeremy Zawodny27 Apr
  • [PATCH] client/mysqlbinlog.cc -- adds "-d <database" command-line optMichael Widenius16 May