YoushouldhavereceivedacopyoftheGNUGeneralPublicLicense alongwiththisprogram;ifnot,writetotheFreeSoftware
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
/* maintenance of mysql databases */
#define VER "10.0" #include"client_priv.h" #include <signal.h> #include <my_pthread.h> /* because of signal() */ #include <sys/stat.h> #include <mysql.h> #include <mysql_version.h> #include <welcome_copyright_notice.h> #include <my_rnd.h> #include <password.h> #include <my_sys.h>
#define MAX_MYSQL_VAR 512 #define SHUTDOWN_DEF_TIMEOUT 3600/* Wait for shutdown */
for (;;)
{ if (mysql_real_connect(mysql,host,user,opt_password,NullS,opt_mysql_port,
unix_port, CLIENT_REMEMBER_OPTIONS))
{
my_bool reconnect= 1;
mysql_options(mysql, MYSQL_OPT_RECONNECT, &reconnect); if (info)
{
fputs("\n",stderr);
(void) fflush(stderr);
} return0;
}
if (!wait) // was or reached 0, fail
{ if (!option_silent) // print diagnostics
{ if (!host)
host= (char*) LOCAL_HOST;
my_printf_error(0,"connect to server at '%s' failed\nerror: '%s'",
error_flags, host, mysql_error(mysql)); if (mysql_errno(mysql) == CR_CONNECTION_ERROR)
{
fprintf(stderr, "Check that mariadbd is running and that the socket: '%s' exists!\n",
unix_port ? unix_port : mysql_unix_port);
} elseif (mysql_errno(mysql) == CR_CONN_HOST_ERROR ||
mysql_errno(mysql) == CR_UNKNOWN_HOST)
{
fprintf(stderr,"Check that mariadbd is running on %s",host);
fprintf(stderr," and that the port is %d.\n",
opt_mysql_port ? opt_mysql_port: mysql_port);
fprintf(stderr,"You can check this by doing 'telnet %s %d'\n",
host, opt_mysql_port ? opt_mysql_port: mysql_port);
}
} return1;
}
if (wait != (uint) ~0)
wait--; /* count down, one less retry */
if ((mysql_errno(mysql) != CR_CONN_HOST_ERROR) &&
(mysql_errno(mysql) != CR_CONNECTION_ERROR))
{ /* Errorisworsethan"serverdoesn'tanswer(yet?)"; failevenifwestillhave"wait-coins"unless--force wasalsogiven.
*/
fprintf(stderr,"Got error: %s\n", mysql_error(mysql)); if (!option_force) return1;
} elseif (!option_silent)
{ if (!info)
{
info=1;
fputs("Waiting for MariaDB server to answer",stderr);
(void) fflush(stderr);
} else
{
putc('.',stderr);
(void) fflush(stderr);
}
}
sleep(5);
}
}
staticint maybe_disable_binlog(MYSQL *mysql)
{ if (opt_local && !sql_log_bin_off)
{ if (mysql_query(mysql, "set local sql_log_bin=0"))
{
my_printf_error(0, "SET LOCAL SQL_LOG_BIN=0 failed; error: '%-.200s'",
error_flags, mysql_error(mysql)); return -1;
}
}
sql_log_bin_off= true; return0;
}
if (opt_shutdown_wait_for_slaves)
{
snprintf(buff, sizeof(buff), "SHUTDOWN WAIT FOR ALL SLAVES"); if (mysql_query(mysql, buff))
{
my_printf_error(0, "%s failed; error: '%-.200s'",
error_flags, buff, mysql_error(mysql)); return -1;
}
} elseif (mysql_shutdown(mysql, SHUTDOWN_DEFAULT))
{
my_printf_error(0, "shutdown failed; error: '%s'", error_flags,
mysql_error(mysql)); return -1;
}
argc=1; /* force SHUTDOWN to be the last command */ if (got_pidfile)
{ if (opt_verbose)
printf("Shutdown signal sent to server; Waiting for pid file to disappear\n");
/* Wait until pid file is gone */ if (wait_pidfile(pidfile, last_modified, &pidfile_status)) return -1;
} break;
} case ADMIN_FLUSH_PRIVILEGES: case ADMIN_RELOAD: if (flush(mysql, "privileges")) return -1; break; case ADMIN_REFRESH: if (mysql_refresh(mysql,
(uint) ~(REFRESH_GRANT | REFRESH_STATUS |
REFRESH_READ_LOCK | REFRESH_SLAVE |
REFRESH_MASTER)))
{
my_printf_error(0, "refresh failed; error: '%s'", error_flags,
mysql_error(mysql)); return -1;
} break; case ADMIN_FLUSH_THREADS: if (mysql_refresh(mysql,(uint) REFRESH_THREADS))
{
my_printf_error(0, "refresh failed; error: '%s'", error_flags,
mysql_error(mysql)); return -1;
} break; case ADMIN_VER:
new_line=1;
print_version();
puts(ORACLE_WELCOME_COPYRIGHT_NOTICE("2000"));
printf("Server version\t\t%s\n", mysql_get_server_info(mysql));
printf("Protocol version\t%d\n", mysql_get_proto_info(mysql));
printf("Connection\t\t%s\n",mysql_get_host_info(mysql)); if (mysql->unix_socket)
printf("UNIX socket\t\t%s\n", mysql->unix_socket); else
printf("TCP port\t\t%d\n", mysql->port);
status=mysql_stat(mysql);
{ char *pos,buff[40];
ulong sec;
pos= (char*) strchr(status,' ');
*pos++=0;
printf("%s\t\t\t",status); /* print label */ if ((status=str2int(pos,1000,10,&sec)))
{
nice_time(sec,buff);
puts(buff); /* print nice time */ while (*status == ' ') status++; /* to next info */
}
}
putc('\n',stdout); if (status)
puts(status); break; case ADMIN_PROCESSLIST:
{
MYSQL_RES *result;
MYSQL_ROW row; constchar *query;
if (!opt_verbose)
query= "show processlist"; elseif (opt_verbose == 1)
query= "show full processlist"; else
query= "select * from information_schema.processlist where id != connection_id()";
if (mysql_query(mysql, query) ||
!(result = mysql_store_result(mysql)))
{
my_printf_error(0, "process list failed; error: '%s'", error_flags,
mysql_error(mysql)); return -1;
}
print_header(result); while ((row=mysql_fetch_row(result)))
print_row(result,row,0);
print_top(result);
mysql_free_result(result);
new_line=1; break;
} case ADMIN_STATUS:
status=mysql_stat(mysql); if (status)
puts(status); break; case ADMIN_KILL:
{
uint error=0; char *pos; if (argc < 2)
{
my_printf_error(0, "Too few arguments to 'kill'", error_flags); return1;
}
pos=argv[1]; for (;;)
{ /* We don't use mysql_kill(), since it only handles 32-bit IDs. */ char buff[26], *out; /* "KILL " + max 20 digs + NUL */
out= strmov(buff, "KILL ");
ullstr(strtoull(pos, NULL, 0), out);
if (mysql_query(mysql, buff))
{ /* out still points to just the number */
my_printf_error(0, "kill failed on %s; error: '%s'", error_flags,
out, mysql_error(mysql));
error=1;
} if (!(pos=strchr(pos,','))) break;
pos++;
}
argc--; argv++; if (error) return -1; break;
} case ADMIN_DEBUG: if (mysql_dump_debug_info(mysql))
{
my_printf_error(0, "debug failed; error: '%s'", error_flags,
mysql_error(mysql)); return -1;
} break; case ADMIN_VARIABLES:
{
MYSQL_RES *res;
MYSQL_ROW row;
new_line=1; if (mysql_query(mysql,"show /*!40003 GLOBAL */ variables") ||
!(res=mysql_store_result(mysql)))
{
my_printf_error(0, "unable to show variables; error: '%s'", error_flags,
mysql_error(mysql)); return -1;
}
print_header(res); while ((row=mysql_fetch_row(res)))
print_row(res,row,0);
print_top(res);
mysql_free_result(res); break;
} case ADMIN_EXTENDED_STATUS:
{
MYSQL_RES *res;
MYSQL_ROW row;
uint rownr = 0; void (*func) (MYSQL_RES*, MYSQL_ROW, uint);
new_line = 1; if (mysql_query(mysql, "show /*!50002 GLOBAL */ status") ||
!(res = mysql_store_result(mysql)))
{
my_printf_error(0, "unable to show status; error: '%s'", error_flags,
mysql_error(mysql)); return -1;
}
while ((row = mysql_fetch_row(res)))
(*func)(res, row, rownr++);
print_top(res);
ex_status_printed = 1; /* From now on the output will be relative */
mysql_free_result(res); break;
} case ADMIN_FLUSH_LOGS:
{ if (flush(mysql, "logs")) return -1; break;
} case ADMIN_FLUSH_BINARY_LOG:
{ if (flush(mysql, "binary logs")) return -1; break;
} case ADMIN_FLUSH_ENGINE_LOG:
{ if (flush(mysql, "engine logs")) return -1; break;
} case ADMIN_FLUSH_ERROR_LOG:
{ if (flush(mysql, "error logs")) return -1; break;
} case ADMIN_FLUSH_GENERAL_LOG:
{ if (flush(mysql, "general logs")) return -1; break;
} case ADMIN_FLUSH_RELAY_LOG:
{ if (flush(mysql, "relay logs")) return -1; break;
} case ADMIN_FLUSH_SLOW_LOG:
{ if (flush(mysql, "slow logs")) return -1; break;
} case ADMIN_FLUSH_HOSTS:
{ if (flush(mysql, "hosts")) return -1; break;
} case ADMIN_FLUSH_TABLES:
{ if (flush(mysql, "tables")) return -1; break;
} case ADMIN_FLUSH_STATUS:
{ if (flush(mysql, "/*!110500 global */ status")) return -1; break;
} case ADMIN_FLUSH_TABLE_STATISTICS:
{ if (flush(mysql, "table_statistics")) return -1; break;
} case ADMIN_FLUSH_INDEX_STATISTICS:
{ if (flush(mysql, "index_statistics")) return -1; break;
} case ADMIN_FLUSH_SSL:
{ if (flush(mysql, "ssl")) return -1; break;
} case ADMIN_FLUSH_USER_STATISTICS:
{ if (flush(mysql, "user_statistics")) return -1; break;
} case ADMIN_FLUSH_USER_RESOURCES:
{ if (flush(mysql, "user_resources")) return -1; break;
} case ADMIN_FLUSH_CLIENT_STATISTICS:
{ if (flush(mysql, "client_statistics")) return -1; break;
} case ADMIN_FLUSH_ALL_STATISTICS:
{ if (flush(mysql, "table_statistics,index_statistics," "user_statistics,client_statistics")) return -1; break;
} case ADMIN_FLUSH_ALL_STATUS:
{ if (flush(mysql, "/*!110500 global */ status,table_statistics," "index_statistics, user_statistics,client_statistics")) return -1; break;
} case ADMIN_OLD_PASSWORD: case ADMIN_PASSWORD:
{ char buff[128],crypted_pw[64];
time_t start_time; char *typed_password= NULL, *verified= NULL; /* Do initialization the same way as we do in mysqld */
start_time=time((time_t*) 0);
my_rnd_init(&rand_st,(ulong) start_time,(ulong) start_time/2);
if (maybe_disable_binlog(mysql)) return -1; if (argc < 1)
{
my_printf_error(0, "Too few arguments to change password", error_flags); return1;
} elseif (argc == 1)
{ /* prompt for password */
typed_password= my_get_tty_password("New password: ");
verified= my_get_tty_password("Confirm new password: "); if (strcmp(typed_password, verified) != 0)
{
my_printf_error(0,"Passwords don't match",MYF(ME_BELL));
ret = -1; goto password_done;
}
} else
typed_password= argv[1];
if (typed_password[0])
{ bool old= (find_type(argv[0], &command_typelib, FIND_TYPE_BASIC) ==
ADMIN_OLD_PASSWORD); #ifdef _WIN32
size_t pw_len= strlen(typed_password); if (pw_len > 1 && typed_password[0] == '\'' &&
typed_password[pw_len-1] == '\'')
printf("Warning: single quotes were not trimmed from the password by" " your command\nline client, as you might have expected.\n"); #endif /* Ifwedon'talreadyknowtouseanold-stylepassword,seewhat theserverisusing
*/ if (!old)
{ if (mysql_query(mysql, "SHOW VARIABLES LIKE 'old_passwords'"))
{
my_printf_error(0, "Could not determine old_passwords setting from server; error: '%s'",
error_flags, mysql_error(mysql));
ret = -1; goto password_done;
} else
{
MYSQL_RES *res= mysql_store_result(mysql); if (!res)
{
my_printf_error(0, "Could not get old_passwords setting from " "server; error: '%s'",
error_flags, mysql_error(mysql));
ret = -1; goto password_done;
} if (!mysql_num_rows(res))
old= 1; else
{
MYSQL_ROW row= mysql_fetch_row(res);
old= !strncmp(row[1], "ON", 2);
}
mysql_free_result(res);
}
} if (old)
my_make_scrambled_password_323(crypted_pw, typed_password, strlen(typed_password)); else
my_make_scrambled_password(crypted_pw, typed_password, strlen(typed_password));
} else
crypted_pw[0]=0; /* No password */
snprintf(buff, sizeof(buff), "set password='%s',sql_log_off=0",crypted_pw);
if (mysql_query(mysql,"set sql_log_off=1"))
{
my_printf_error(0, "Can't turn off logging; error: '%s'",
error_flags, mysql_error(mysql));
ret = -1;
} else if (mysql_query(mysql,buff))
{
my_printf_error(0,"unable to change password; error: '%s'",
error_flags, mysql_error(mysql));
ret = -1;
}
password_done: /* free up memory from prompted password */ if (typed_password != argv[1])
{
my_free(typed_password);
my_free(verified);
}
argc--; argv++; break;
}
case ADMIN_START_SLAVE: case ADMIN_START_ALL_SLAVES:
{
my_bool many_slaves= 0; constchar *query= "START SLAVE"; if (command == ADMIN_START_ALL_SLAVES && mariadb_connection(mysql) &&
mysql_get_server_version(mysql) >= 100000)
{
query="START ALL SLAVES";
many_slaves= 1;
}
if (mysql_query(mysql, query))
{
my_printf_error(0, "Error starting slave: %s", error_flags,
mysql_error(mysql)); return -1;
} elseif (!many_slaves || mysql_warning_count(mysql) > 0)
{ if (!option_silent)
puts("Slave('s) started");
} else
{ if (!option_silent)
puts("No slaves to start");
} break;
} case ADMIN_STOP_SLAVE: case ADMIN_STOP_ALL_SLAVES:
{ constchar *query= "STOP SLAVE";
my_bool many_slaves= 0;
if (command == ADMIN_STOP_ALL_SLAVES && mariadb_connection(mysql) &&
mysql_get_server_version(mysql) >= 100000)
{
query="STOP ALL SLAVES";
many_slaves= 1;
}
if (mysql_query(mysql, query))
{
my_printf_error(0, "Error stopping slave: %s", error_flags,
mysql_error(mysql)); return -1;
} elseif (!many_slaves || mysql_warning_count(mysql) > 0)
{ /* We can't detect if there was any slaves to stop with STOP SLAVE */ if (many_slaves && !option_silent)
puts("Slave('s) stopped");
} else
{ if (!option_silent)
puts("All slaves was already stopped");
} break;
} case ADMIN_PING:
{
my_bool reconnect= 0;
mysql_options(mysql, MYSQL_OPT_RECONNECT, &reconnect); if (!mysql_ping(mysql))
{ if (option_silent < 2)
puts("mysqld is alive");
} else
{ if (mysql_errno(mysql) == CR_SERVER_GONE_ERROR)
{
reconnect= 1;
mysql_options(mysql, MYSQL_OPT_RECONNECT, &reconnect); if (!mysql_ping(mysql))
puts("connection was down, but mysqld is now alive");
} else
{
my_printf_error(0,"mariadbd doesn't answer to ping, error: '%s'",
error_flags, mysql_error(mysql)); return -1;
}
}
reconnect=1; /* Automatic reconnect is default */
mysql_options(mysql, MYSQL_OPT_RECONNECT, &reconnect); break;
} default:
my_printf_error(0, "Unknown command: '%-.60s'", error_flags, argv[0]); return1;
}
} return ret;
}
staticvoid usage(void)
{
print_version();
puts(ORACLE_WELCOME_COPYRIGHT_NOTICE("2000"));
puts("Administration program for the mysqld daemon.");
printf("Usage: %s [OPTIONS] command command....\n", my_progname);
print_defaults("my",load_default_groups);
puts("");
my_print_help(my_long_options);
my_print_variables(my_long_options);
puts("\nWhere command is a one or more of: (Commands may be shortened)\n\
create databasename Create a new database\n\
debug Instruct server to write debug information to log\n\
drop databasename Delete a database and all its tables\n\
extended-status Gives an extended status message from the server\n\
flush-all-statistics Flush all statistics tables\n\
flush-all-status Flush status and statistics\n\
flush-client-statistics Flush client statistics\n\
flush-hosts Flush all cached hosts\n\
flush-index-statistics Flush index statistics\n\
flush-logs Flush all logs\n\
flush-privileges Reload grant tables (same as reload)\n\
flush-binary-log Flush binary log\n\
flush-engine-log Flush engine log(s)\n\
flush-error-log Flush error log\n\
flush-general-log Flush general log\n\
flush-relay-log Flush relay log\n\
flush-slow-log Flush slow query log\n\
flush-ssl Flush SSL certificates\n\
flush-status Clear status variables\n\
flush-table-statistics Clear table statistics\n\
flush-tables Flush all tables\n\
flush-threads Flush the thread cache\n\
flush-user-statistics Flush user statistics\n\
flush-user-resources Flush user resources\n\
kill id,id,... Kill mysql threads\n\
password [new-password] Change old password to new-password in current format\n\
old-password [new-password] Change old password to new-password in old format\n\
ping Check if mysqld is alive\n\
processlist Show list of active threads in server\n\
reload Reload grant tables\n\
refresh Flush all tables and close and open logfiles\n\
shutdown Take server down\n\
status Gives a short status message from the server\n\
start-all-slaves Start all slaves\n\
start-slave Start slave\n\
stop-all-slaves Stop all slaves\n\
stop-slave Stop slave\n\
variables Prints variables available\n\
version Get version info from server");
}
if (!option_force)
{
puts("Dropping the database is potentially a very bad thing to do.");
puts("Any data stored in the database will be destroyed.\n");
printf("Do you really want to drop the '%s' database [y/N] ",db);
fflush(stdout);
input= fgets(buf, sizeof(buf)-1, stdin); if (!input || ((*input != 'y') && (*input != 'Y')))
{
puts("\nOK, aborting database drop!"); return -1;
}
}
snprintf(name_buff, sizeof(name_buff), "drop database `%.*s`",FN_REFLEN,db); if (mysql_query(mysql,name_buff))
{
my_printf_error(0, "DROP DATABASE %s failed;\nerror: '%s'", error_flags,
db,mysql_error(mysql)); return1;
}
printf("Database \"%s\" dropped\n",db); return0;
}
system_filename(buff, pidfile); do
{ int fd; if ((fd= my_open(buff, O_RDONLY, MYF(0))) < 0)
{
error= 0; break;
}
(void) my_close(fd,MYF(0)); if (last_modified && !stat(pidfile, pidfile_status))
{ if (last_modified != pidfile_status->st_mtime)
{ /* File changed; Let's assume that mysqld did restart */ if (opt_verbose)
printf("pid file '%s' changed while waiting for it to disappear!\nmysqld did probably restart\n",
buff);
error= 0; break;
}
} if (count++ == opt_shutdown_timeout) break;
sleep(1);
} while (!interrupted);
if (error)
{
DBUG_PRINT("warning",("Pid file didn't disappear"));
fprintf(stderr, "Warning; Aborted waiting on pid file: '%s' after %d seconds\n",
buff, count-1);
}
DBUG_RETURN(error);
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.34 Sekunden
(vorverarbeitet am 2026-10-08)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.