18 #define CHECK_VERSION "2.5.1"
20 #include "client_priv.h"
21 #include "my_default.h"
23 #include <mysql_version.h>
24 #include <mysqld_error.h>
25 #include <sslopt-vars.h>
26 #include <welcome_copyright_notice.h>
34 #define MAX_ALTER_STR_SIZE 128 * 1024
35 #define KEY_PARTITIONING_CHANGED_STR "KEY () partitioning changed"
37 static MYSQL mysql_connection, *sock = 0;
38 static my_bool opt_alldbs = 0, opt_check_only_changed = 0, opt_extended = 0,
39 opt_compress = 0, opt_databases = 0, opt_fast = 0,
40 opt_medium_check = 0, opt_quick = 0, opt_all_in_1 = 0,
41 opt_silent = 0, opt_auto_repair = 0, ignore_errors = 0,
42 tty_password= 0, opt_frm= 0, debug_info_flag= 0, debug_check_flag= 0,
43 opt_fix_table_names= 0, opt_fix_db_names= 0, opt_upgrade= 0,
45 static uint verbose = 0, opt_mysql_port=0;
46 static int my_end_arg;
47 static char * opt_mysql_unix_port = 0;
48 static char *opt_password = 0, *current_user = 0,
49 *default_charset= 0, *current_host= 0;
50 static char *opt_plugin_dir= 0, *opt_default_auth= 0;
51 static int first_error = 0;
52 static char *opt_skip_database;
53 DYNAMIC_ARRAY tables4repair, tables4rebuild, alter_table_cmds;
55 static char *shared_memory_base_name=0;
57 static uint opt_protocol=0;
58 static char *opt_bind_addr = NULL;
60 enum operations { DO_CHECK=1, DO_REPAIR, DO_ANALYZE, DO_OPTIMIZE, DO_UPGRADE };
62 static struct my_option my_long_options[] =
64 {
"all-databases",
'A',
65 "Check all the databases. This is the same as --databases with all databases selected.",
66 &opt_alldbs, &opt_alldbs, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
68 {
"analyze",
'a',
"Analyze given tables.", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0,
71 "Instead of issuing one query for each table, use one query per database, naming all tables in the database in a comma-separated list.",
72 &opt_all_in_1, &opt_all_in_1, 0, GET_BOOL, NO_ARG, 0, 0, 0,
74 {
"auto-repair", OPT_AUTO_REPAIR,
75 "If a checked table is corrupted, automatically fix it. Repairing will be done after all tables have been checked, if corrupted ones were found.",
76 &opt_auto_repair, &opt_auto_repair, 0, GET_BOOL, NO_ARG, 0,
78 {
"bind-address", 0,
"IP address to bind to.",
79 (uchar**) &opt_bind_addr, (uchar**) &opt_bind_addr, 0, GET_STR,
80 REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
81 {
"character-sets-dir", OPT_CHARSETS_DIR,
82 "Directory for character set files.", &charsets_dir,
83 &charsets_dir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
84 {
"check",
'c',
"Check table for errors.", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0,
86 {
"check-only-changed",
'C',
87 "Check only tables that have changed since last check or haven't been closed properly.",
88 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
89 {
"check-upgrade",
'g',
90 "Check tables for version-dependent changes. May be used with --auto-repair to correct tables requiring version-dependent updates.",
91 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
92 {
"compress", OPT_COMPRESS,
"Use compression in server/client protocol.",
93 &opt_compress, &opt_compress, 0, GET_BOOL, NO_ARG, 0, 0, 0,
96 "Check several databases. Note the difference in usage; in this case no tables are given. All name arguments are regarded as database names.",
97 &opt_databases, &opt_databases, 0, GET_BOOL, NO_ARG,
100 {
"debug",
'#',
"This is a non-debug version. Catch this and exit.",
101 0, 0, 0, GET_DISABLED, OPT_ARG, 0, 0, 0, 0, 0, 0},
103 {
"debug",
'#',
"Output debug log. Often this is 'd:t:o,filename'.",
104 0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
106 {
"debug-check", OPT_DEBUG_CHECK,
"Check memory and open file usage at exit.",
107 &debug_check_flag, &debug_check_flag, 0,
108 GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
109 {
"debug-info", OPT_DEBUG_INFO,
"Print some debug info at exit.",
110 &debug_info_flag, &debug_info_flag,
111 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
112 {
"default-character-set", OPT_DEFAULT_CHARSET,
113 "Set the default character set.", &default_charset,
114 &default_charset, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
115 {
"default_auth", OPT_DEFAULT_AUTH,
116 "Default authentication client-side plugin to use.",
117 &opt_default_auth, &opt_default_auth, 0,
118 GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
119 {
"fast",
'F',
"Check only tables that haven't been closed properly.",
120 &opt_fast, &opt_fast, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0,
122 {
"fix-db-names", OPT_FIX_DB_NAMES,
"Fix database names.",
123 &opt_fix_db_names, &opt_fix_db_names,
124 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
125 {
"fix-table-names", OPT_FIX_TABLE_NAMES,
"Fix table names.",
126 &opt_fix_table_names, &opt_fix_table_names,
127 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
128 {
"force",
'f',
"Continue even if we get an SQL error.",
129 &ignore_errors, &ignore_errors, 0, GET_BOOL, NO_ARG, 0, 0,
132 "If you are using this option with CHECK TABLE, it will ensure that the table is 100 percent consistent, but will take a long time. If you are using this option with REPAIR TABLE, it will force using old slow repair with keycache method, instead of much faster repair by sorting.",
133 &opt_extended, &opt_extended, 0, GET_BOOL, NO_ARG, 0, 0, 0,
135 {
"help",
'?',
"Display this help message and exit.", 0, 0, 0, GET_NO_ARG,
136 NO_ARG, 0, 0, 0, 0, 0, 0},
137 {
"host",
'h',
"Connect to host.", ¤t_host,
138 ¤t_host, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
139 {
"medium-check",
'm',
140 "Faster than extended-check, but only finds 99.99 percent of all errors. Should be good enough for most cases.",
141 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
142 {
"write-binlog", OPT_WRITE_BINLOG,
143 "Log ANALYZE, OPTIMIZE and REPAIR TABLE commands. Use --skip-write-binlog "
144 "when commands should not be sent to replication slaves.",
145 &opt_write_binlog, &opt_write_binlog, 0, GET_BOOL, NO_ARG,
147 {
"optimize",
'o',
"Optimize table.", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0,
150 "Password to use when connecting to server. If password is not given, it's solicited on the tty.",
151 0, 0, 0, GET_PASSWORD, OPT_ARG, 0, 0, 0, 0, 0, 0},
153 {
"pipe",
'W',
"Use named pipes to connect to server.", 0, 0, 0, GET_NO_ARG,
154 NO_ARG, 0, 0, 0, 0, 0, 0},
156 {
"plugin_dir", OPT_PLUGIN_DIR,
"Directory for client-side plugins.",
157 &opt_plugin_dir, &opt_plugin_dir, 0,
158 GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
159 {
"port",
'P',
"Port number to use for connection or 0 for default to, in "
160 "order of preference, my.cnf, $MYSQL_TCP_PORT, "
161 #if MYSQL_PORT_DEFAULT == 0
164 "built-in default (" STRINGIFY_ARG(MYSQL_PORT)
").",
165 &opt_mysql_port, &opt_mysql_port, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0,
167 {
"protocol", OPT_MYSQL_PROTOCOL,
"The protocol to use for connection (tcp, socket, pipe, memory).",
168 0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
170 "If you are using this option with CHECK TABLE, it prevents the check from scanning the rows to check for wrong links. This is the fastest check. If you are using this option with REPAIR TABLE, it will try to repair only the index tree. This is the fastest repair method for a table.",
171 &opt_quick, &opt_quick, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0,
174 "Can fix almost anything except unique keys that aren't unique.",
175 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
177 {
"shared-memory-base-name", OPT_SHARED_MEMORY_BASE_NAME,
178 "Base name of shared memory.", &shared_memory_base_name, &shared_memory_base_name,
179 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
181 {
"silent",
's',
"Print only error messages.", &opt_silent,
182 &opt_silent, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
183 {
"skip_database", 0,
"Don't process the database specified as argument",
184 &opt_skip_database, &opt_skip_database, 0, GET_STR, REQUIRED_ARG,
186 {
"socket",
'S',
"The socket file to use for connection.",
187 &opt_mysql_unix_port, &opt_mysql_unix_port, 0, GET_STR,
188 REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
189 #include <sslopt-longopts.h>
190 {
"tables", OPT_TABLES,
"Overrides option --databases (-B).", 0, 0, 0,
191 GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
193 "When used with REPAIR, get table structure from .frm file, so the table can be repaired even if .MYI header is corrupted.",
194 &opt_frm, &opt_frm, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0,
196 #ifndef DONT_ALLOW_USER_CHANGE
197 {
"user",
'u',
"User for login if not current user.", ¤t_user,
198 ¤t_user, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
200 {
"verbose",
'v',
"Print info about the various stages.", 0, 0, 0, GET_NO_ARG,
201 NO_ARG, 0, 0, 0, 0, 0, 0},
202 {
"version",
'V',
"Output version information and exit.", 0, 0, 0, GET_NO_ARG,
203 NO_ARG, 0, 0, 0, 0, 0, 0},
204 {0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
207 static const char *load_default_groups[] = {
"mysqlcheck",
"client", 0 };
210 static void print_version(
void);
211 static void usage(
void);
212 static int get_options(
int *argc,
char ***argv);
213 static int process_all_databases();
214 static int process_databases(
char **db_names);
215 static int process_selected_tables(
char *db,
char **table_names,
int tables);
216 static int process_all_tables_in_db(
char *database);
217 static int process_one_db(
char *database);
218 static int use_db(
char *database);
219 static int handle_request_for_tables(
char *tables, uint length);
220 static int dbConnect(
char *host,
char *user,
char *passwd);
221 static void dbDisconnect(
char *host);
222 static void DBerror(
MYSQL *mysql,
const char *when);
223 static void safe_exit(
int error);
224 static void print_result();
225 static uint fixed_name_length(
const char *
name);
226 static char *fix_table_name(
char *dest,
char *src);
230 static void print_version(
void)
232 printf(
"%s Ver %s Distrib %s, for %s (%s)\n", my_progname, CHECK_VERSION,
233 MYSQL_SERVER_VERSION, SYSTEM_TYPE, MACHINE_TYPE);
237 static void usage(
void)
240 puts(ORACLE_WELCOME_COPYRIGHT_NOTICE(
"2000"));
241 puts(
"This program can be used to CHECK (-c, -m, -C), REPAIR (-r), ANALYZE (-a),");
242 puts(
"or OPTIMIZE (-o) tables. Some of the options (like -e or -q) can be");
243 puts(
"used at the same time. Not all options are supported by all storage engines.");
244 puts(
"Please consult the MySQL manual for latest information about the");
245 puts(
"above. The options -c, -r, -a, and -o are exclusive to each other, which");
246 puts(
"means that the last option will be used, if several was specified.\n");
247 puts(
"The option -c will be used by default, if none was specified. You");
248 puts(
"can change the default behavior by making a symbolic link, or");
249 puts(
"copying this file somewhere with another name, the alternatives are:");
250 puts(
"mysqlrepair: The default option will be -r");
251 puts(
"mysqlanalyze: The default option will be -a");
252 puts(
"mysqloptimize: The default option will be -o\n");
253 printf(
"Usage: %s [OPTIONS] database [tables]\n", my_progname);
254 printf(
"OR %s [OPTIONS] --databases DB1 [DB2 DB3...]\n",
256 printf(
"OR %s [OPTIONS] --all-databases\n", my_progname);
257 print_defaults(
"my", load_default_groups);
258 my_print_help(my_long_options);
259 my_print_variables(my_long_options);
264 get_one_option(
int optid,
const struct my_option *opt __attribute__((unused)),
267 int orig_what_to_do= what_to_do;
271 what_to_do = DO_ANALYZE;
274 what_to_do = DO_CHECK;
277 what_to_do = DO_CHECK;
278 opt_check_only_changed = 1;
285 what_to_do = DO_CHECK;
286 opt_medium_check = 1;
289 what_to_do = DO_OPTIMIZE;
291 case OPT_FIX_DB_NAMES:
292 what_to_do= DO_UPGRADE;
295 case OPT_FIX_TABLE_NAMES:
296 what_to_do= DO_UPGRADE;
299 if (argument == disabled_my_option)
300 argument= (
char*)
"";
303 char *start = argument;
304 my_free(opt_password);
305 opt_password = my_strdup(argument, MYF(MY_FAE));
306 while (*argument) *argument++=
'x';
315 what_to_do = DO_REPAIR;
318 what_to_do= DO_CHECK;
323 opt_protocol = MYSQL_PROTOCOL_PIPE;
327 DBUG_PUSH(argument ? argument :
"d:t:o");
330 #include <sslopt-case.h>
337 case 'V': print_version(); exit(0);
338 case OPT_MYSQL_PROTOCOL:
339 opt_protocol= find_type_or_exit(argument, &sql_protocol_typelib,
344 if (orig_what_to_do && (what_to_do != orig_what_to_do))
346 fprintf(stderr,
"Error: %s doesn't support multiple contradicting commands.\n",
354 static int get_options(
int *argc,
char ***argv)
364 my_getopt_use_args_separator= TRUE;
365 if ((ho_error= load_defaults(
"my", load_default_groups, argc, argv)) ||
366 (ho_error=handle_options(argc, argv, my_long_options, get_one_option)))
368 my_getopt_use_args_separator= FALSE;
372 size_t pnlen= strlen(my_progname);
375 what_to_do = DO_CHECK;
376 else if (!strcmp(
"repair", my_progname + pnlen - 6))
377 what_to_do = DO_REPAIR;
378 else if (!strcmp(
"analyze", my_progname + pnlen - 7))
379 what_to_do = DO_ANALYZE;
380 else if (!strcmp(
"optimize", my_progname + pnlen - 8))
381 what_to_do = DO_OPTIMIZE;
383 what_to_do = DO_CHECK;
390 if (!default_charset)
392 if (opt_fix_db_names || opt_fix_table_names)
393 default_charset= (
char*)
"utf8";
395 default_charset= (
char*) MYSQL_AUTODETECT_CHARSET_NAME;
397 if (strcmp(default_charset, MYSQL_AUTODETECT_CHARSET_NAME) &&
398 !get_charset_by_csname(default_charset, MY_CS_PRIMARY, MYF(MY_WME)))
400 printf(
"Unsupported character set: %s\n", default_charset);
403 if (*argc > 0 && opt_alldbs)
405 printf(
"You should give only options, no arguments at all, with option\n");
406 printf(
"--all-databases. Please see %s --help for more information.\n",
411 if (*argc < 1 && !opt_alldbs)
413 printf(
"You forgot to give the arguments! Please see %s --help\n",
415 printf(
"for more information.\n");
419 opt_password = get_tty_password(NullS);
421 my_end_arg= MY_CHECK_ERROR | MY_GIVE_INFO;
422 if (debug_check_flag)
423 my_end_arg= MY_CHECK_ERROR;
428 static int process_all_databases()
434 if (mysql_query(sock,
"SHOW DATABASES") ||
435 !(tableres = mysql_store_result(sock)))
437 my_printf_error(0,
"Error: Couldn't execute 'SHOW DATABASES': %s",
438 MYF(0), mysql_error(sock));
441 while ((row = mysql_fetch_row(tableres)))
443 if (process_one_db(row[0]))
451 static int process_databases(
char **db_names)
454 for ( ; *db_names ; db_names++)
456 if (process_one_db(*db_names))
463 static int process_selected_tables(
char *db,
char **table_names,
int tables)
467 if (opt_all_in_1 && what_to_do != DO_UPGRADE)
474 char *table_names_comma_sep, *end;
475 size_t tot_length= 0;
478 for (i = 0; i < tables; i++)
479 tot_length+= fixed_name_length(*(table_names + i)) + 2;
481 if (!(table_names_comma_sep = (
char *)
482 my_malloc((
sizeof(
char) * tot_length) + 4, MYF(MY_WME))))
485 for (end = table_names_comma_sep + 1; tables > 0;
486 tables--, table_names++)
488 end= fix_table_name(end, *table_names);
492 handle_request_for_tables(table_names_comma_sep + 1, (uint) (tot_length - 1));
493 my_free(table_names_comma_sep);
496 for (; tables > 0; tables--, table_names++)
497 handle_request_for_tables(*table_names, fixed_name_length(*table_names));
502 static uint fixed_name_length(
const char *
name)
505 uint extra_length= 2;
507 for (p= name; *p; p++)
514 return (uint) ((p -
name) + extra_length);
518 static char *fix_table_name(
char *dest,
char *src)
541 static int process_all_tables_in_db(
char *database)
548 if (use_db(database))
550 if ((mysql_query(sock,
"SHOW /*!50002 FULL*/ TABLES") &&
551 mysql_query(sock,
"SHOW TABLES")) ||
552 !(res= mysql_store_result(sock)))
554 my_printf_error(0,
"Error: Couldn't get table list for database %s: %s",
555 MYF(0), database, mysql_error(sock));
559 num_columns= mysql_num_fields(res);
561 if (opt_all_in_1 && what_to_do != DO_UPGRADE)
572 while ((row = mysql_fetch_row(res)))
573 tot_length+= fixed_name_length(row[0]) + 2;
574 mysql_data_seek(res, 0);
576 if (!(tables=(
char *) my_malloc(
sizeof(
char)*tot_length+4, MYF(MY_WME))))
578 mysql_free_result(res);
581 for (end = tables + 1; (row = mysql_fetch_row(res)) ;)
583 if ((num_columns == 2) && (strcmp(row[1],
"VIEW") == 0))
586 end= fix_table_name(end, row[0]);
591 handle_request_for_tables(tables + 1, tot_length - 1);
596 while ((row = mysql_fetch_row(res)))
599 if ((what_to_do != DO_UPGRADE) && (num_columns == 2) && (strcmp(row[1],
"VIEW") == 0))
602 handle_request_for_tables(row[0], fixed_name_length(row[0]));
605 mysql_free_result(res);
610 static int run_query(
const char *
query)
612 if (mysql_query(sock, query))
614 fprintf(stderr,
"Failed to %s\n", query);
615 fprintf(stderr,
"Error: %s\n", mysql_error(sock));
622 static int fix_table_storage_name(
const char *name)
624 char qbuf[100 + NAME_LEN*4];
626 if (strncmp(name,
"#mysql50#", 9))
628 sprintf(qbuf,
"RENAME TABLE `%s` TO `%s`", name, name + 9);
631 printf(
"%-50s %s\n", name, rc ?
"FAILED" :
"OK");
635 static int fix_database_storage_name(
const char *name)
637 char qbuf[100 + NAME_LEN*4];
639 if (strncmp(name,
"#mysql50#", 9))
641 sprintf(qbuf,
"ALTER DATABASE `%s` UPGRADE DATA DIRECTORY NAME", name);
644 printf(
"%-50s %s\n", name, rc ?
"FAILED" :
"OK");
648 static int rebuild_table(
char *name)
652 query= (
char*)my_malloc(
sizeof(
char) * (12 + fixed_name_length(name) + 6 + 1),
656 ptr= strmov(query,
"ALTER TABLE ");
657 ptr= fix_table_name(ptr, name);
658 ptr= strxmov(ptr,
" FORCE", NullS);
659 if (mysql_real_query(sock, query, (uint)(ptr - query)))
661 fprintf(stderr,
"Failed to %s\n", query);
662 fprintf(stderr,
"Error: %s\n", mysql_error(sock));
669 static int process_one_db(
char *database)
671 if (opt_skip_database && opt_alldbs &&
672 !strcmp(database, opt_skip_database))
675 if (what_to_do == DO_UPGRADE)
678 if (opt_fix_db_names && !strncmp(database,
"#mysql50#", 9))
680 rc= fix_database_storage_name(database);
683 if (rc || !opt_fix_table_names)
686 return process_all_tables_in_db(database);
690 static int use_db(
char *database)
692 if (mysql_get_server_version(sock) >= FIRST_INFORMATION_SCHEMA_VERSION &&
693 !my_strcasecmp(&my_charset_latin1, database, INFORMATION_SCHEMA_DB_NAME))
695 if (mysql_get_server_version(sock) >= FIRST_PERFORMANCE_SCHEMA_VERSION &&
696 !my_strcasecmp(&my_charset_latin1, database, PERFORMANCE_SCHEMA_DB_NAME))
698 if (mysql_select_db(sock, database))
700 DBerror(sock,
"when selecting the database");
706 static int disable_binlog()
708 const char *stmt=
"SET SQL_LOG_BIN=0";
709 return run_query(stmt);
712 static int handle_request_for_tables(
char *tables, uint length)
714 char *query, *end, options[100],
message[100];
715 uint query_length= 0;
720 switch (what_to_do) {
723 if (opt_quick) end = strmov(end,
" QUICK");
724 if (opt_fast) end = strmov(end,
" FAST");
725 if (opt_medium_check) end = strmov(end,
" MEDIUM");
726 if (opt_extended) end = strmov(end,
" EXTENDED");
727 if (opt_check_only_changed) end = strmov(end,
" CHANGED");
728 if (opt_upgrade) end = strmov(end,
" FOR UPGRADE");
731 op= (opt_write_binlog) ?
"REPAIR" :
"REPAIR NO_WRITE_TO_BINLOG";
732 if (opt_quick) end = strmov(end,
" QUICK");
733 if (opt_extended) end = strmov(end,
" EXTENDED");
734 if (opt_frm) end = strmov(end,
" USE_FRM");
737 op= (opt_write_binlog) ?
"ANALYZE" :
"ANALYZE NO_WRITE_TO_BINLOG";
740 op= (opt_write_binlog) ?
"OPTIMIZE" :
"OPTIMIZE NO_WRITE_TO_BINLOG";
743 return fix_table_storage_name(tables);
746 if (!(query =(
char *) my_malloc((
sizeof(
char)*(length+110)), MYF(MY_WME))))
751 query_length= sprintf(query,
"%s TABLE %s %s", op, tables, options);
757 ptr= strmov(strmov(query, op),
" TABLE ");
758 ptr= fix_table_name(ptr, tables);
759 ptr= strxmov(ptr,
" ", options, NullS);
760 query_length= (uint) (ptr - query);
762 if (mysql_real_query(sock, query, query_length))
764 sprintf(message,
"when executing '%s TABLE ... %s'", op, options);
765 DBerror(sock, message);
774 static void print_result()
778 char prev[NAME_LEN*2+2];
779 char prev_alter[MAX_ALTER_STR_SIZE];
781 my_bool found_error=0, table_rebuild=0;
783 res = mysql_use_result(sock);
787 for (i = 0; (row = mysql_fetch_row(res)); i++)
789 int changed = strcmp(prev, row[0]);
790 my_bool status = !strcmp(row[2],
"status");
799 if (found_error && opt_auto_repair && what_to_do != DO_REPAIR &&
805 insert_dynamic(&alter_table_cmds, (uchar*) prev_alter);
807 insert_dynamic(&tables4rebuild, (uchar*) prev);
810 insert_dynamic(&tables4repair, prev);
818 if (status && changed)
819 printf(
"%-50s %s", row[0], row[3]);
820 else if (!status && changed)
822 printf(
"%s\n%-9s: %s", row[0], row[2], row[3]);
823 if (opt_auto_repair && strcmp(row[2],
"note"))
825 const char *alter_txt= strstr(row[3],
"ALTER TABLE");
830 if (!strncmp(row[3], KEY_PARTITIONING_CHANGED_STR,
831 strlen(KEY_PARTITIONING_CHANGED_STR)) &&
832 strstr(alter_txt,
"PARTITION BY"))
834 if (strlen(alter_txt) >= MAX_ALTER_STR_SIZE)
836 printf(
"Error: Alter command too long (>= %d),"
837 " please do \"%s\" or dump/reload to fix it!\n",
844 strcpy(prev_alter, alter_txt);
850 printf(
"%-9s: %s", row[2], row[3]);
851 strmov(prev, row[0]);
855 if (found_error && opt_auto_repair && what_to_do != DO_REPAIR)
860 insert_dynamic(&alter_table_cmds, (uchar*) prev_alter);
862 insert_dynamic(&tables4rebuild, (uchar*) prev);
865 insert_dynamic(&tables4repair, prev);
867 mysql_free_result(res);
871 static int dbConnect(
char *host,
char *user,
char *passwd)
873 DBUG_ENTER(
"dbConnect");
876 fprintf(stderr,
"# Connecting to %s...\n", host ? host :
"localhost");
878 mysql_init(&mysql_connection);
880 mysql_options(&mysql_connection, MYSQL_OPT_COMPRESS, NullS);
884 mysql_ssl_set(&mysql_connection, opt_ssl_key, opt_ssl_cert, opt_ssl_ca,
885 opt_ssl_capath, opt_ssl_cipher);
886 mysql_options(&mysql_connection, MYSQL_OPT_SSL_CRL, opt_ssl_crl);
887 mysql_options(&mysql_connection, MYSQL_OPT_SSL_CRLPATH, opt_ssl_crlpath);
891 mysql_options(&mysql_connection,MYSQL_OPT_PROTOCOL,(
char*)&opt_protocol);
893 mysql_options(&mysql_connection, MYSQL_OPT_BIND, opt_bind_addr);
895 if (shared_memory_base_name)
896 mysql_options(&mysql_connection,MYSQL_SHARED_MEMORY_BASE_NAME,shared_memory_base_name);
899 if (opt_plugin_dir && *opt_plugin_dir)
900 mysql_options(&mysql_connection, MYSQL_PLUGIN_DIR, opt_plugin_dir);
902 if (opt_default_auth && *opt_default_auth)
903 mysql_options(&mysql_connection, MYSQL_DEFAULT_AUTH, opt_default_auth);
905 mysql_options(&mysql_connection, MYSQL_SET_CHARSET_NAME, default_charset);
906 mysql_options(&mysql_connection, MYSQL_OPT_CONNECT_ATTR_RESET, 0);
907 mysql_options4(&mysql_connection, MYSQL_OPT_CONNECT_ATTR_ADD,
908 "program_name",
"mysqlcheck");
909 if (!(sock = mysql_real_connect(&mysql_connection, host, user, passwd,
910 NULL, opt_mysql_port, opt_mysql_unix_port, 0)))
912 DBerror(&mysql_connection,
"when trying to connect");
915 mysql_connection.reconnect= 1;
920 static void dbDisconnect(
char *host)
923 fprintf(stderr,
"# Disconnecting from %s...\n", host ? host :
"localhost");
928 static void DBerror(
MYSQL *mysql,
const char *when)
930 DBUG_ENTER(
"DBerror");
931 my_printf_error(0,
"Got error: %d: %s %s", MYF(0),
932 mysql_errno(mysql), mysql_error(mysql), when);
933 safe_exit(EX_MYSQLERR);
938 static void safe_exit(
int error)
950 int main(
int argc,
char **argv)
956 if (get_options(&argc, &argv))
961 if (dbConnect(current_host, current_user, opt_password))
964 if (!opt_write_binlog)
966 if (disable_binlog()) {
972 if (opt_auto_repair &&
973 (my_init_dynamic_array(&tables4repair,
sizeof(
char)*(NAME_LEN*2+2),16,64) ||
974 my_init_dynamic_array(&tables4rebuild,
sizeof(
char)*(NAME_LEN*2+2),16,64) ||
975 my_init_dynamic_array(&alter_table_cmds, MAX_ALTER_STR_SIZE, 0, 1)))
982 process_all_databases();
984 else if (argc > 1 && !opt_databases)
985 process_selected_tables(*argv, (argv + 1), (argc - 1));
988 process_databases(argv);
993 if (!opt_silent && (tables4repair.elements || tables4rebuild.elements))
994 puts(
"\nRepairing tables");
995 what_to_do = DO_REPAIR;
996 for (i = 0; i < tables4repair.elements ; i++)
998 char *name= (
char*) dynamic_array_ptr(&tables4repair, i);
999 handle_request_for_tables(name, fixed_name_length(name));
1001 for (i = 0; i < tables4rebuild.elements ; i++)
1002 rebuild_table((
char*) dynamic_array_ptr(&tables4rebuild, i));
1003 for (i = 0; i < alter_table_cmds.elements ; i++)
1004 run_query((
char*) dynamic_array_ptr(&alter_table_cmds, i));
1007 dbDisconnect(current_host);
1008 if (opt_auto_repair)
1010 delete_dynamic(&tables4repair);
1011 delete_dynamic(&tables4rebuild);
1013 my_free(opt_password);
1015 my_free(shared_memory_base_name);
1018 return(first_error!=0);