20 #include "client_priv.h"
21 #include "my_default.h"
23 #include <my_pthread.h>
26 #include <sql_common.h>
27 #include <welcome_copyright_notice.h>
28 #include <mysqld_error.h>
30 #define ADMIN_VERSION "8.42"
31 #define MAX_MYSQL_VAR 512
32 #define SHUTDOWN_DEF_TIMEOUT 3600
33 #define MAX_TRUNC_LENGTH 3
35 char *host= NULL, *user= 0, *opt_password= 0,
36 *default_charset= (
char*) MYSQL_AUTODETECT_CHARSET_NAME;
37 char truncated_var_names[MAX_MYSQL_VAR][MAX_TRUNC_LENGTH];
38 char ex_var_names[MAX_MYSQL_VAR][FN_REFLEN];
39 ulonglong last_values[MAX_MYSQL_VAR];
40 static int interval=0;
41 static my_bool option_force=0,interrupted=0,new_line=0,
42 opt_compress=0, opt_relative=0, opt_verbose=0, opt_vertical=0,
43 tty_password= 0, opt_nobeep;
44 static my_bool debug_info_flag= 0, debug_check_flag= 0;
45 static uint tcp_port = 0, option_wait = 0, option_silent=0, nr_iterations;
46 static uint opt_count_iterations= 0, my_end_arg;
47 static char *opt_bind_addr = NULL;
48 static ulong opt_connect_timeout, opt_shutdown_timeout;
49 static char * unix_port=0;
50 static char *opt_plugin_dir= 0, *opt_default_auth= 0;
51 static uint opt_enable_cleartext_plugin= 0;
52 static my_bool using_opt_enable_cleartext_plugin= 0;
55 static char *shared_memory_base_name=0;
57 static uint opt_protocol=0;
58 static myf error_flags;
66 static uint ex_val_max_len[MAX_MYSQL_VAR];
67 static my_bool ex_status_printed = 0;
68 static uint ex_var_count, max_var_length, max_val_length;
70 #include <sslopt-vars.h>
72 static void print_version(
void);
73 static void usage(
void);
74 extern "C" my_bool get_one_option(
int optid,
const struct my_option *opt,
76 static my_bool sql_connect(
MYSQL *mysql, uint wait);
77 static int execute_commands(
MYSQL *mysql,
int argc,
char **argv);
78 static int drop_db(
MYSQL *mysql,
const char *db);
79 extern "C" sig_handler endprog(
int signal_number);
80 static void nice_time(ulong sec,
char *buff);
81 static void print_header(
MYSQL_RES *result);
83 static void print_row(
MYSQL_RES *result,MYSQL_ROW cur, uint row);
84 static void print_relative_row(
MYSQL_RES *result, MYSQL_ROW cur, uint row);
85 static void print_relative_row_vert(
MYSQL_RES *result, MYSQL_ROW cur, uint row);
86 static void print_relative_header();
87 static void print_relative_line();
88 static void truncate_names();
89 static my_bool get_pidfile(
MYSQL *mysql,
char *pidfile);
90 static my_bool wait_pidfile(
char *pidfile, time_t last_modified,
91 struct stat *pidfile_status);
92 static void store_values(
MYSQL_RES *result);
100 ADMIN_CREATE, ADMIN_DROP, ADMIN_SHUTDOWN,
101 ADMIN_RELOAD, ADMIN_REFRESH, ADMIN_VER,
102 ADMIN_PROCESSLIST, ADMIN_STATUS, ADMIN_KILL,
103 ADMIN_DEBUG, ADMIN_VARIABLES, ADMIN_FLUSH_LOGS,
104 ADMIN_FLUSH_HOSTS, ADMIN_FLUSH_TABLES, ADMIN_PASSWORD,
105 ADMIN_PING, ADMIN_EXTENDED_STATUS, ADMIN_FLUSH_STATUS,
106 ADMIN_FLUSH_PRIVILEGES, ADMIN_START_SLAVE, ADMIN_STOP_SLAVE,
107 ADMIN_FLUSH_THREADS, ADMIN_OLD_PASSWORD
109 static const char *command_names[]= {
110 "create",
"drop",
"shutdown",
111 "reload",
"refresh",
"version",
112 "processlist",
"status",
"kill",
113 "debug",
"variables",
"flush-logs",
114 "flush-hosts",
"flush-tables",
"password",
115 "ping",
"extended-status",
"flush-status",
116 "flush-privileges",
"start-slave",
"stop-slave",
117 "flush-threads",
"old-password",
121 static TYPELIB command_typelib=
122 { array_elements(command_names)-1,
"commands", command_names, NULL};
124 static struct my_option my_long_options[] =
126 {
"bind-address", 0,
"IP address to bind to.",
127 (uchar**) &opt_bind_addr, (uchar**) &opt_bind_addr, 0, GET_STR,
128 REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
130 "Number of iterations to make. This works with -i (--sleep) only.",
131 &nr_iterations, &nr_iterations, 0, GET_UINT,
132 REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
134 {
"debug",
'#',
"Output debug log. Often this is 'd:t:o,filename'.",
135 0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
137 {
"debug-check", OPT_DEBUG_CHECK,
"Check memory and open file usage at exit.",
138 &debug_check_flag, &debug_check_flag, 0,
139 GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
140 {
"debug-info", OPT_DEBUG_INFO,
"Print some debug info at exit.",
141 &debug_info_flag, &debug_info_flag,
142 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
144 "Don't ask for confirmation on drop database; with multiple commands, "
145 "continue even if an error occurs.",
146 &option_force, &option_force, 0, GET_BOOL, NO_ARG, 0, 0,
148 {
"compress",
'C',
"Use compression in server/client protocol.",
149 &opt_compress, &opt_compress, 0, GET_BOOL, NO_ARG, 0, 0, 0,
151 {
"character-sets-dir", OPT_CHARSETS_DIR,
152 "Directory for character set files.", &charsets_dir,
153 &charsets_dir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
154 {
"default-character-set", OPT_DEFAULT_CHARSET,
155 "Set the default character set.", &default_charset,
156 &default_charset, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
157 {
"help",
'?',
"Display this help and exit.", 0, 0, 0, GET_NO_ARG,
158 NO_ARG, 0, 0, 0, 0, 0, 0},
159 {
"host",
'h',
"Connect to host.", &host, &host, 0, GET_STR,
160 REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
161 {
"no-beep",
'b',
"Turn off beep on error.", &opt_nobeep,
162 &opt_nobeep, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
164 "Password to use when connecting to server. If password is not given it's asked from the tty.",
165 0, 0, 0, GET_PASSWORD, OPT_ARG, 0, 0, 0, 0, 0, 0},
167 {
"pipe",
'W',
"Use named pipes to connect to server.", 0, 0, 0, GET_NO_ARG,
168 NO_ARG, 0, 0, 0, 0, 0, 0},
170 {
"port",
'P',
"Port number to use for connection or 0 for default to, in "
171 "order of preference, my.cnf, $MYSQL_TCP_PORT, "
172 #if MYSQL_PORT_DEFAULT == 0
175 "built-in default (" STRINGIFY_ARG(MYSQL_PORT)
").",
176 &tcp_port, &tcp_port, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
177 {
"protocol", OPT_MYSQL_PROTOCOL,
"The protocol to use for connection (tcp, socket, pipe, memory).",
178 0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
180 "Show difference between current and previous values when used with -i. "
181 "Currently only works with extended-status.",
182 &opt_relative, &opt_relative, 0, GET_BOOL, NO_ARG, 0, 0, 0,
185 {
"shared-memory-base-name", OPT_SHARED_MEMORY_BASE_NAME,
186 "Base name of shared memory.", &shared_memory_base_name, &shared_memory_base_name,
187 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
189 {
"silent",
's',
"Silently exit if one can't connect to server.",
190 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
191 {
"socket",
'S',
"The socket file to use for connection.",
192 &unix_port, &unix_port, 0, GET_STR, REQUIRED_ARG, 0, 0, 0,
194 {
"sleep",
'i',
"Execute commands repeatedly with a sleep between.",
195 &interval, &interval, 0, GET_INT, REQUIRED_ARG, 0, 0, 0, 0,
197 #include <sslopt-longopts.h>
198 #ifndef DONT_ALLOW_USER_CHANGE
199 {
"user",
'u',
"User for login if not current user.", &user,
200 &user, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
202 {
"verbose",
'v',
"Write more information.", &opt_verbose,
203 &opt_verbose, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
204 {
"version",
'V',
"Output version information and exit.", 0, 0, 0, GET_NO_ARG,
205 NO_ARG, 0, 0, 0, 0, 0, 0},
207 "Print output vertically. Is similar to --relative, but prints output vertically.",
208 &opt_vertical, &opt_vertical, 0, GET_BOOL, NO_ARG, 0, 0, 0,
210 {
"wait",
'w',
"Wait and retry if connection is down.", 0, 0, 0, GET_UINT,
211 OPT_ARG, 0, 0, 0, 0, 0, 0},
212 {
"connect_timeout", OPT_CONNECT_TIMEOUT,
"", &opt_connect_timeout,
213 &opt_connect_timeout, 0, GET_ULONG, REQUIRED_ARG, 3600*12, 0,
215 {
"shutdown_timeout", OPT_SHUTDOWN_TIMEOUT,
"", &opt_shutdown_timeout,
216 &opt_shutdown_timeout, 0, GET_ULONG, REQUIRED_ARG,
217 SHUTDOWN_DEF_TIMEOUT, 0, 3600*12, 0, 1, 0},
218 {
"plugin_dir", OPT_PLUGIN_DIR,
"Directory for client-side plugins.",
219 &opt_plugin_dir, &opt_plugin_dir, 0,
220 GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
221 {
"default_auth", OPT_DEFAULT_AUTH,
222 "Default authentication client-side plugin to use.",
223 &opt_default_auth, &opt_default_auth, 0,
224 GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
225 {
"enable_cleartext_plugin", OPT_ENABLE_CLEARTEXT_PLUGIN,
226 "Enable/disable the clear text authentication plugin.",
227 &opt_enable_cleartext_plugin, &opt_enable_cleartext_plugin,
228 0, GET_BOOL, OPT_ARG, 0, 0, 0, 0, 0, 0},
229 { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
233 static const char *load_default_groups[]= {
"mysqladmin",
"client",0 };
236 get_one_option(
int optid,
const struct my_option *opt __attribute__((unused)),
243 opt_count_iterations= 1;
246 if (argument == disabled_my_option)
247 argument= (
char*)
"";
250 char *start=argument;
251 my_free(opt_password);
252 opt_password=my_strdup(argument,MYF(MY_FAE));
253 while (*argument) *argument++=
'x';
266 opt_protocol = MYSQL_PROTOCOL_PIPE;
270 DBUG_PUSH(argument ? argument :
"d:t:o,/tmp/mysqladmin.trace");
272 #include <sslopt-case.h>
280 if ((option_wait=atoi(argument)) <= 0)
284 option_wait= ~(uint)0;
290 case OPT_CHARSETS_DIR:
291 #if MYSQL_VERSION_ID > 32300
292 charsets_dir = argument;
295 case OPT_MYSQL_PROTOCOL:
296 opt_protocol= find_type_or_exit(argument, &sql_protocol_typelib,
299 case OPT_ENABLE_CLEARTEXT_PLUGIN:
300 using_opt_enable_cleartext_plugin= TRUE;
312 int main(
int argc,
char *argv[])
314 int error= 0, ho_error;
316 my_bool can_handle_passwords;
318 char **commands, **save_argv;
322 my_getopt_use_args_separator= TRUE;
323 if (load_defaults(
"my",load_default_groups,&argc,&argv))
325 my_getopt_use_args_separator= FALSE;
328 if ((ho_error=handle_options(&argc, &argv, my_long_options, get_one_option)))
330 free_defaults(save_argv);
334 my_end_arg= MY_CHECK_ERROR | MY_GIVE_INFO;
335 if (debug_check_flag)
336 my_end_arg= MY_CHECK_ERROR;
345 opt_password = get_tty_password(NullS);
347 (void) signal(SIGINT,endprog);
348 (void) signal(SIGTERM,endprog);
351 mysql_options(&mysql,MYSQL_OPT_BIND,opt_bind_addr);
353 mysql_options(&mysql,MYSQL_OPT_COMPRESS,NullS);
354 if (opt_connect_timeout)
356 uint tmp=opt_connect_timeout;
357 mysql_options(&mysql,MYSQL_OPT_CONNECT_TIMEOUT, (
char*) &tmp);
362 mysql_ssl_set(&mysql, opt_ssl_key, opt_ssl_cert, opt_ssl_ca,
363 opt_ssl_capath, opt_ssl_cipher);
364 mysql_options(&mysql, MYSQL_OPT_SSL_CRL, opt_ssl_crl);
365 mysql_options(&mysql, MYSQL_OPT_SSL_CRLPATH, opt_ssl_crlpath);
367 mysql_options(&mysql,MYSQL_OPT_SSL_VERIFY_SERVER_CERT,
368 (
char*)&opt_ssl_verify_server_cert);
371 mysql_options(&mysql,MYSQL_OPT_PROTOCOL,(
char*)&opt_protocol);
373 if (shared_memory_base_name)
374 mysql_options(&mysql,MYSQL_SHARED_MEMORY_BASE_NAME,shared_memory_base_name);
376 mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, default_charset);
377 error_flags= (myf)(opt_nobeep ? 0 : ME_BELL);
379 if (opt_plugin_dir && *opt_plugin_dir)
380 mysql_options(&mysql, MYSQL_PLUGIN_DIR, opt_plugin_dir);
382 if (opt_default_auth && *opt_default_auth)
383 mysql_options(&mysql, MYSQL_DEFAULT_AUTH, opt_default_auth);
385 mysql_options(&mysql, MYSQL_OPT_CONNECT_ATTR_RESET, 0);
386 mysql_options4(&mysql, MYSQL_OPT_CONNECT_ATTR_ADD,
387 "program_name",
"mysqladmin");
388 if (using_opt_enable_cleartext_plugin)
389 mysql_options(&mysql, MYSQL_ENABLE_CLEARTEXT_PLUGIN,
390 (
char*) &opt_enable_cleartext_plugin);
392 first_command= find_type(argv[0], &command_typelib, FIND_TYPE_BASIC);
393 can_handle_passwords=
394 (first_command == ADMIN_PASSWORD || first_command == ADMIN_OLD_PASSWORD) ?
396 mysql_options(&mysql, MYSQL_OPT_CAN_HANDLE_EXPIRED_PASSWORDS,
397 &can_handle_passwords);
399 if (sql_connect(&mysql, option_wait))
406 unsigned int err= mysql_errno(&mysql);
407 if (err >= CR_MIN_ERROR && err <= CR_MAX_ERROR)
412 for (; argc > 0; argv++, argc--)
414 if (find_type(argv[0], &command_typelib, FIND_TYPE_BASIC) !=
434 while (!interrupted && (!opt_count_iterations || nr_iterations))
438 if ((error= execute_commands(&mysql,argc,commands)))
459 if (opt_count_iterations && --nr_iterations == 0)
468 if (mysql.net.vio == 0)
470 if (option_wait && !interrupted)
473 sql_connect(&mysql, option_wait);
501 my_free(opt_password);
504 my_free(shared_memory_base_name);
506 free_defaults(save_argv);
513 sig_handler endprog(
int signal_number __attribute__((unused)))
530 static my_bool sql_connect(
MYSQL *mysql, uint wait)
536 if (mysql_real_connect(mysql,host,user,opt_password,NullS,tcp_port,
537 unix_port, CLIENT_REMEMBER_OPTIONS))
543 (void) fflush(stderr);
553 host= (
char*) LOCAL_HOST;
554 my_printf_error(0,
"connect to server at '%s' failed\nerror: '%s'",
555 error_flags, host, mysql_error(mysql));
556 if (mysql_errno(mysql) == CR_CONNECTION_ERROR)
559 "Check that mysqld is running and that the socket: '%s' exists!\n",
560 unix_port ? unix_port : mysql_unix_port);
562 else if (mysql_errno(mysql) == CR_CONN_HOST_ERROR ||
563 mysql_errno(mysql) == CR_UNKNOWN_HOST)
565 fprintf(stderr,
"Check that mysqld is running on %s",host);
566 fprintf(stderr,
" and that the port is %d.\n",
567 tcp_port ? tcp_port: mysql_port);
568 fprintf(stderr,
"You can check this by doing 'telnet %s %d'\n",
569 host, tcp_port ? tcp_port: mysql_port);
575 if (wait != (uint) ~0)
578 if ((mysql_errno(mysql) != CR_CONN_HOST_ERROR) &&
579 (mysql_errno(mysql) != CR_CONNECTION_ERROR))
586 fprintf(stderr,
"Got error: %s\n", mysql_error(mysql));
590 else if (!option_silent)
595 fputs(
"Waiting for MySQL server to answer",stderr);
596 (void) fflush(stderr);
601 (void) fflush(stderr);
623 static int execute_commands(
MYSQL *mysql,
int argc,
char **argv)
636 for (; argc > 0 ; argv++,argc--)
639 switch (option= find_type(argv[0], &command_typelib, FIND_TYPE_BASIC)) {
642 char buff[FN_REFLEN+20];
645 my_printf_error(0,
"Too few arguments to create", error_flags);
648 sprintf(buff,
"create database `%.*s`",FN_REFLEN,argv[1]);
649 if (mysql_query(mysql,buff))
651 my_printf_error(0,
"CREATE DATABASE failed; error: '%-.200s'",
652 error_flags, mysql_error(mysql));
662 my_printf_error(0,
"Too few arguments to drop", error_flags);
665 if (drop_db(mysql,argv[1]))
672 char pidfile[FN_REFLEN];
673 my_bool got_pidfile= 0;
674 time_t last_modified= 0;
675 struct stat pidfile_status;
681 if (mysql->unix_socket && (got_pidfile= !get_pidfile(mysql, pidfile)) &&
682 !stat(pidfile, &pidfile_status))
683 last_modified= pidfile_status.st_mtime;
685 if (mysql_shutdown(mysql, SHUTDOWN_DEFAULT))
687 my_printf_error(0,
"shutdown failed; error: '%s'", error_flags,
695 printf(
"Shutdown signal sent to server; Waiting for pid file to disappear\n");
698 if (wait_pidfile(pidfile, last_modified, &pidfile_status))
703 case ADMIN_FLUSH_PRIVILEGES:
705 if (mysql_query(mysql,
"flush privileges"))
707 my_printf_error(0,
"reload failed; error: '%s'", error_flags,
713 if (mysql_refresh(mysql,
714 (uint) ~(REFRESH_GRANT | REFRESH_STATUS |
715 REFRESH_READ_LOCK | REFRESH_SLAVE |
718 my_printf_error(0,
"refresh failed; error: '%s'", error_flags,
723 case ADMIN_FLUSH_THREADS:
724 if (mysql_refresh(mysql,(uint) REFRESH_THREADS))
726 my_printf_error(0,
"refresh failed; error: '%s'", error_flags,
734 puts(ORACLE_WELCOME_COPYRIGHT_NOTICE(
"2000"));
735 printf(
"Server version\t\t%s\n", mysql_get_server_info(mysql));
736 printf(
"Protocol version\t%d\n", mysql_get_proto_info(mysql));
737 printf(
"Connection\t\t%s\n",mysql_get_host_info(mysql));
738 if (mysql->unix_socket)
739 printf(
"UNIX socket\t\t%s\n", mysql->unix_socket);
741 printf(
"TCP port\t\t%d\n", mysql->port);
742 status=mysql_stat(mysql);
746 pos= (
char*) strchr(status,
' ');
748 printf(
"%s\t\t\t",status);
749 if ((status=str2int(pos,10,0,LONG_MAX,(
long*) &sec)))
753 while (*status ==
' ') status++;
760 case ADMIN_PROCESSLIST:
765 if (mysql_query(mysql, (opt_verbose ?
"show full processlist" :
766 "show processlist")) ||
767 !(result = mysql_store_result(mysql)))
769 my_printf_error(0,
"process list failed; error: '%s'", error_flags,
773 print_header(result);
774 while ((row=mysql_fetch_row(result)))
775 print_row(result,row,0);
777 mysql_free_result(result);
782 status=mysql_stat(mysql);
792 my_printf_error(0,
"Too few arguments to 'kill'", error_flags);
800 out= strxmov(buff,
"KILL ", NullS);
801 ullstr(strtoull(pos, NULL, 0), out);
803 if (mysql_query(mysql, buff))
806 my_printf_error(0,
"kill failed on %s; error: '%s'", error_flags,
807 out, mysql_error(mysql));
810 if (!(pos=strchr(pos,
',')))
820 if (mysql_dump_debug_info(mysql))
822 my_printf_error(0,
"debug failed; error: '%s'", error_flags,
827 case ADMIN_VARIABLES:
833 if (mysql_query(mysql,
"show /*!40003 GLOBAL */ variables") ||
834 !(res=mysql_store_result(mysql)))
836 my_printf_error(0,
"unable to show variables; error: '%s'", error_flags,
841 while ((row=mysql_fetch_row(res)))
842 print_row(res,row,0);
844 mysql_free_result(res);
847 case ADMIN_EXTENDED_STATUS:
852 void (*func) (
MYSQL_RES*, MYSQL_ROW, uint);
855 if (mysql_query(mysql,
"show /*!50002 GLOBAL */ status") ||
856 !(res = mysql_store_result(mysql)))
858 my_printf_error(0,
"unable to show status; error: '%s'", error_flags,
863 DBUG_ASSERT(mysql_num_rows(res) < MAX_MYSQL_VAR);
869 if (!ex_status_printed)
876 print_relative_line();
877 print_relative_header();
878 print_relative_line();
883 if (opt_relative && !opt_vertical)
884 func = print_relative_row;
885 else if (opt_vertical)
886 func = print_relative_row_vert;
890 while ((row = mysql_fetch_row(res)))
891 (*func)(res, row, rownr++);
894 if (ex_status_printed)
897 print_relative_line();
903 ex_status_printed = 1;
904 mysql_free_result(res);
907 case ADMIN_FLUSH_LOGS:
909 if (mysql_refresh(mysql,REFRESH_LOG))
911 my_printf_error(0,
"refresh failed; error: '%s'", error_flags,
917 case ADMIN_FLUSH_HOSTS:
919 if (mysql_query(mysql,
"flush hosts"))
921 my_printf_error(0,
"refresh failed; error: '%s'", error_flags,
927 case ADMIN_FLUSH_TABLES:
929 if (mysql_query(mysql,
"flush tables"))
931 my_printf_error(0,
"refresh failed; error: '%s'", error_flags,
937 case ADMIN_FLUSH_STATUS:
939 if (mysql_query(mysql,
"flush status"))
941 my_printf_error(0,
"refresh failed; error: '%s'", error_flags,
947 case ADMIN_OLD_PASSWORD:
950 char buff[128],crypted_pw[64];
952 char *typed_password= NULL, *verified= NULL;
953 bool log_off=
true, err=
false;
956 bool old= (option == ADMIN_OLD_PASSWORD);
959 start_time=time((time_t*) 0);
960 randominit(&rand_st,(ulong) start_time,(ulong) start_time/2);
964 my_printf_error(0,
"Too few arguments to change password", error_flags);
970 typed_password= get_tty_password(
"New password: ");
971 verified= get_tty_password(
"Confirm new password: ");
972 if (strcmp(typed_password, verified) != 0)
974 my_printf_error(0,
"Passwords don't match",MYF(ME_BELL));
980 typed_password= argv[1];
982 if (typed_password[0])
985 size_t pw_len= strlen(typed_password);
986 if (pw_len > 1 && typed_password[0] ==
'\'' &&
987 typed_password[pw_len-1] ==
'\'')
988 printf(
"Warning: single quotes were not trimmed from the password by"
989 " your command\nline client, as you might have expected.\n");
997 if (mysql_query(mysql,
"SHOW VARIABLES LIKE 'old_passwords'"))
999 bool fatal= (mysql_errno(mysql) != ER_MUST_CHANGE_PASSWORD);
1000 if (fatal || opt_verbose)
1001 my_printf_error(0,
"Could not determine old_passwords setting "
1002 "from server; error: '%s'.", error_flags,
1003 mysql_error(mysql));
1012 MYSQL_RES *res= mysql_store_result(mysql);
1016 "Could not get old_passwords setting from "
1017 "server; error: '%s'.",
1018 error_flags, mysql_error(mysql));
1022 if (!mysql_num_rows(res))
1026 MYSQL_ROW row= mysql_fetch_row(res);
1027 old= (!strncmp(row[1],
"ON", 2) || !strncmp(row[1],
"1", 1));
1029 mysql_free_result(res);
1033 if (mysql_query(mysql,
"set sql_log_off=1"))
1036 fprintf(stderr,
"Note: Can't turn off logging; '%s'", mysql_error(mysql));
1048 make_scrambled_password_323(crypted_pw, typed_password);
1050 make_scrambled_password(crypted_pw, typed_password);
1055 sprintf(buff,
"set password='%s'", crypted_pw);
1057 if (mysql_query(mysql,buff))
1059 if ((mysql_errno(mysql) == ER_PASSWD_LENGTH) &&
1060 !(option == ADMIN_OLD_PASSWORD) && !retry_count)
1063 memset(crypted_pw, 0, 64);
1069 if (mysql_errno(mysql)!=1290)
1071 my_printf_error(0,
"unable to change password; error: '%s'",
1072 error_flags, mysql_error(mysql));
1082 my_printf_error(0,
"\n"
1083 "You cannot use 'password' command as mysqld runs\n"
1084 " with grant tables disabled (was started with"
1085 " --skip-grant-tables).\n"
1086 "Use: \"mysqladmin flush-privileges password '*'\""
1087 " instead", error_flags);
1092 if (log_off && mysql_query(mysql,
"set sql_log_off=0"))
1095 fprintf(stderr,
"Note: Can't turn on logging; '%s'", mysql_error(mysql));
1099 if (typed_password != argv[1])
1101 my_free(typed_password);
1111 case ADMIN_START_SLAVE:
1112 if (mysql_query(mysql,
"START SLAVE"))
1114 my_printf_error(0,
"Error starting slave: %s", error_flags,
1115 mysql_error(mysql));
1119 puts(
"Slave started");
1121 case ADMIN_STOP_SLAVE:
1122 if (mysql_query(mysql,
"STOP SLAVE"))
1124 my_printf_error(0,
"Error stopping slave: %s", error_flags,
1125 mysql_error(mysql));
1129 puts(
"Slave stopped");
1134 if (!mysql_ping(mysql))
1136 if (option_silent < 2)
1137 puts(
"mysqld is alive");
1141 if (mysql_errno(mysql) == CR_SERVER_GONE_ERROR)
1144 if (!mysql_ping(mysql))
1145 puts(
"connection was down, but mysqld is now alive");
1149 my_printf_error(0,
"mysqld doesn't answer to ping, error: '%s'",
1150 error_flags, mysql_error(mysql));
1157 my_printf_error(0,
"Unknown command: '%-.60s'", error_flags, argv[0]);
1165 static void print_version(
void)
1167 printf(
"%s Ver %s Distrib %s, for %s on %s\n",my_progname,ADMIN_VERSION,
1168 MYSQL_SERVER_VERSION,SYSTEM_TYPE,MACHINE_TYPE);
1172 static void usage(
void)
1175 puts(ORACLE_WELCOME_COPYRIGHT_NOTICE(
"2000"));
1176 puts(
"Administration program for the mysqld daemon.");
1177 printf(
"Usage: %s [OPTIONS] command command....\n", my_progname);
1178 my_print_help(my_long_options);
1179 my_print_variables(my_long_options);
1180 print_defaults(
"my",load_default_groups);
1181 puts(
"\nWhere command is a one or more of: (Commands may be shortened)\n\
1182 create databasename Create a new database\n\
1183 debug Instruct server to write debug information to log\n\
1184 drop databasename Delete a database and all its tables\n\
1185 extended-status Gives an extended status message from the server\n\
1186 flush-hosts Flush all cached hosts\n\
1187 flush-logs Flush all logs\n\
1188 flush-status Clear status variables\n\
1189 flush-tables Flush all tables\n\
1190 flush-threads Flush the thread cache\n\
1191 flush-privileges Reload grant tables (same as reload)\n\
1192 kill id,id,... Kill mysql threads");
1193 #if MYSQL_VERSION_ID >= 32200
1195 password [new-password] Change old password to new-password in current format\n\
1196 old-password [new-password] Change old password to new-password in old format");
1199 ping Check if mysqld is alive\n\
1200 processlist Show list of active threads in server\n\
1201 reload Reload grant tables\n\
1202 refresh Flush all tables and close and open logfiles\n\
1203 shutdown Take server down\n\
1204 status Gives a short status message from the server\n\
1205 start-slave Start slave\n\
1206 stop-slave Stop slave\n\
1207 variables Prints variables available\n\
1208 version Get version info from server");
1212 static int drop_db(
MYSQL *mysql,
const char *db)
1214 char name_buff[FN_REFLEN+20],
buf[10];
1219 puts(
"Dropping the database is potentially a very bad thing to do.");
1220 puts(
"Any data stored in the database will be destroyed.\n");
1221 printf(
"Do you really want to drop the '%s' database [y/N] ",db);
1223 input= fgets(buf,
sizeof(buf)-1, stdin);
1224 if (!input || ((*input !=
'y') && (*input !=
'Y')))
1226 puts(
"\nOK, aborting database drop!");
1230 sprintf(name_buff,
"drop database `%.*s`",FN_REFLEN,db);
1231 if (mysql_query(mysql,name_buff))
1233 my_printf_error(0,
"DROP DATABASE %s failed;\nerror: '%s'", error_flags,
1234 db,mysql_error(mysql));
1237 printf(
"Database \"%s\" dropped\n",db);
1242 static void nice_time(ulong sec,
char *buff)
1246 if (sec >= 3600L*24)
1250 buff=int10_to_str(tmp, buff, 10);
1251 buff=strmov(buff,tmp > 1 ?
" days " :
" day ");
1257 buff=int10_to_str(tmp, buff, 10);
1258 buff=strmov(buff,tmp > 1 ?
" hours " :
" hour ");
1264 buff=int10_to_str(tmp, buff, 10);
1265 buff=strmov(buff,
" min ");
1267 strmov(int10_to_str(sec, buff, 10),
" sec");
1271 static void print_header(
MYSQL_RES *result)
1276 mysql_field_seek(result,0);
1278 while ((field = mysql_fetch_field(result)))
1280 printf(
" %-*s|",(
int) field->max_length+1,field->name);
1287 static void print_top(
MYSQL_RES *result)
1293 mysql_field_seek(result,0);
1294 while((field = mysql_fetch_field(result)))
1296 if ((length=(uint) strlen(field->name)) > field->max_length)
1297 field->max_length=length;
1299 length=field->max_length;
1300 for (i=length+2 ; i--> 0 ; )
1309 static void print_row(
MYSQL_RES *result, MYSQL_ROW cur,
1310 uint row __attribute__((unused)))
1316 mysql_field_seek(result,0);
1317 for (i=0 ; i < mysql_num_fields(result); i++)
1319 field = mysql_fetch_field(result);
1320 length=field->max_length;
1321 printf(
" %-*s|",length+1,cur[i] ? (
char*) cur[i] :
"");
1327 static void print_relative_row(
MYSQL_RES *result, MYSQL_ROW cur, uint row)
1333 mysql_field_seek(result, 0);
1334 field = mysql_fetch_field(result);
1335 printf(
"| %-*s|", (
int) field->max_length + 1, cur[0]);
1337 field = mysql_fetch_field(result);
1338 tmp = cur[1] ? strtoull(cur[1], NULL, 10) : (ulonglong) 0;
1339 printf(
" %-*s|\n", (
int) field->max_length + 1,
1340 llstr((tmp - last_values[row]), buff));
1341 last_values[row] = tmp;
1345 static void print_relative_row_vert(
MYSQL_RES *result __attribute__((unused)),
1347 uint row __attribute__((unused)))
1356 tmp = cur[1] ? strtoull(cur[1], NULL, 10) : (ulonglong) 0;
1357 printf(
" %-*s|", ex_val_max_len[row] + 1,
1358 llstr((tmp - last_values[row]), buff));
1361 if ((length=(uint) strlen(buff) > ex_val_max_len[row]) && ex_status_printed)
1362 ex_val_max_len[row] = length;
1363 last_values[row] = tmp;
1367 static void store_values(
MYSQL_RES *result)
1373 field = mysql_fetch_field(result);
1374 max_var_length = field->max_length;
1375 field = mysql_fetch_field(result);
1376 max_val_length = field->max_length;
1378 for (i = 0; (row = mysql_fetch_row(result)); i++)
1380 strmov(ex_var_names[i], row[0]);
1381 last_values[
i]=strtoull(row[1],NULL,10);
1382 ex_val_max_len[
i]=2;
1389 static void print_relative_header()
1394 for (i = 0; i < ex_var_count; i++)
1395 printf(
" %-*s|", ex_val_max_len[i] + 1, truncated_var_names[i]);
1400 static void print_relative_line()
1405 for (i = 0; i < ex_var_count; i++)
1408 for (j = 0; j < ex_val_max_len[
i] + 2; j++)
1416 static void truncate_names()
1419 char *ptr,top_line[MAX_TRUNC_LENGTH+4+NAME_LEN+22+1],buff[22];
1423 ptr=strfill(ptr,max_var_length+2,
'-');
1425 ptr=strfill(ptr,MAX_TRUNC_LENGTH+2,
'-');
1427 ptr=strfill(ptr,max_val_length+2,
'-');
1432 for (i = 0 ; i < ex_var_count; i++)
1435 printf(
"| %-*s|", max_var_length + 1, ex_var_names[i]);
1436 ptr = ex_var_names[
i];
1438 for (j = 0; j <
i; j++)
1439 if (*truncated_var_names[j] == *ptr)
1442 truncated_var_names[
i][0]= *ptr;
1443 int10_to_str(sfx, truncated_var_names[i]+1,10);
1444 printf(
" %-*s|", MAX_TRUNC_LENGTH + 1, truncated_var_names[i]);
1445 printf(
" %-*s|\n", max_val_length + 1, llstr(last_values[i],buff));
1452 static my_bool get_pidfile(
MYSQL *mysql,
char *pidfile)
1456 if (mysql_query(mysql,
"SHOW VARIABLES LIKE 'pid_file'"))
1458 my_printf_error(0,
"query failed; error: '%s'", error_flags,
1459 mysql_error(mysql));
1461 result = mysql_store_result(mysql);
1464 MYSQL_ROW row=mysql_fetch_row(result);
1466 strmov(pidfile, row[1]);
1467 mysql_free_result(result);
1477 static my_bool wait_pidfile(
char *pidfile, time_t last_modified,
1478 struct stat *pidfile_status)
1480 char buff[FN_REFLEN];
1483 DBUG_ENTER(
"wait_pidfile");
1485 system_filename(buff, pidfile);
1489 if ((fd= my_open(buff, O_RDONLY, MYF(0))) < 0)
1494 (void) my_close(fd,MYF(0));
1495 if (last_modified && !stat(pidfile, pidfile_status))
1497 if (last_modified != pidfile_status->st_mtime)
1501 printf(
"pid file '%s' changed while waiting for it to disappear!\nmysqld did probably restart\n",
1507 if (count++ == opt_shutdown_timeout)
1510 }
while (!interrupted);
1514 DBUG_PRINT(
"warning",(
"Pid file didn't disappear"));
1516 "Warning; Aborted waiting on pid file: '%s' after %d seconds\n",