Cette page inclut des scripts qui aident à déboguer et à utiliser MySQL.
Afficher les activités de saisie
#!/bin/bash# Tail the binlog and look for insert / update / delete# The goal is to help the user understand which writes are happeningMYSQL=$(whichmysql)MYSQLBINLOG=$(whichmysqlbinlog)DATE=$(whichdate)if[[-z"${MYSQL}"]]thenecho"ERROR: Could not find mysql shell"exit1fiif[[-z"${MYSQLBINLOG}"]]thenecho"ERROR: Could not find mysqlbinlog utility"exit1fiif[[-z"$1"]]thenecho"Usage: $0 [mysql connection parameters]"exit1filast_log=$("${MYSQL}""$@"-N-B-e"SHOW BINARY LOGS;"|tail-n1|cut-f1)time=$("${DATE}"'+%Y-%m-%d %H:%M:%S')echo"Continuously reading from ${last_log} starting from ${time}""${MYSQLBINLOG}"--base64-output=DECODE-ROWS--verbose--start-datetime="${time}"--read-from-remote-server"$@""${last_log}"--stop-never|grep"INSERT\|UPDATE\|DELETE"
Rechercher des commandes ALTER TABLE
#!/bin/bash# Search for DDL Commands in the last 24 hours. Should help the user understand# which DDL commands they are performing.MYSQL=$(whichmysql)MYSQLBINLOG=$(whichmysqlbinlog)DATE=$(whichdate)if[[-z"${MYSQL}"]]thenecho"ERROR: Could not find mysql shell"exit1fiif[[-z"${MYSQLBINLOG}"]]thenecho"ERROR: Could not find mysqlbinlog utility"exit1fiif[[-z"$1"]]thenecho"Usage: $0 [mysql connection parameters]"exit1filog_files=$("${MYSQL}""$@"-N-B-e"SHOW BINARY LOGS;"|cut-f1)yesterday=$("${DATE}"--date="-1 day"'+%Y-%m-%d %H:%M:%S')echo"Searching for DDL commands, starting at ${yesterday}"forfilein${log_files}doecho"Log file: ${file}""${MYSQLBINLOG}"--start-datetime"${yesterday}"--read-from-remote-server"$@""${file}"|grep-B2"ALTER TABLE\|CREATE TABLE\|TRUNCATE TABLE\|RENAME TABLE\|DROP TABLE"done
Verrouiller toutes les tables
#!/bin/bash# This script locks all non-system tables on a MySQL database.# Helps for the case where we cannot acquire read lock with flush.MYSQL="$(whichmysql)"if[[-z"${MYSQL}"]]thenecho"ERROR: Could not find mysql shell"exit1fiif[[-z"$1"]]thenecho"Usage: $0 [mysql connection parameters]"exit1fiLOCK_TABLES_STMT="select concat('LOCK TABLES ', group_concat(concat('\`',table_schema,'\`.\`',table_name,'\` READ')),';') as stmt from information_schema.tables where table_schema not in ('mysql', 'sys', 'performance_schema', 'information_schema');"QUERY="$("${MYSQL}""$@"-N-B-e"${LOCK_TABLES_STMT}")"(echo"${QUERY}"read-n1-r-s-p$'Tables locked, press any key to stop the session and UNLOCK TABLES\n'echo"UNLOCK TABLES;")|"${MYSQL}""$@"
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/08/18 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/08/18 (UTC)."],[[["\u003cp\u003eThis page provides debugging scripts specifically designed for use with MySQL databases.\u003c/p\u003e\n"],["\u003cp\u003eOne script helps users identify write activities, such as inserts, updates, and deletes, by monitoring the binlog.\u003c/p\u003e\n"],["\u003cp\u003eAnother script enables users to search for Data Definition Language (DDL) commands like \u003ccode\u003eALTER TABLE\u003c/code\u003e within the past 24 hours.\u003c/p\u003e\n"],["\u003cp\u003eA third script facilitates locking all non-system tables in a MySQL database, which can be useful when a read lock cannot be acquired with flush.\u003c/p\u003e\n"]]],[],null,["# Debugging and other tools\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nMySQL \\| [PostgreSQL](/database-migration/docs/postgres/debugging-tools \"View this page for the PostgreSQL version of Database Migration Service.\") \\| [PostgreSQL to AlloyDB](/database-migration/docs/postgresql-to-alloydb/debugging-tools \"View this page for the PostgreSQL to AlloyDB version of Database Migration Service.\")\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nOverview\n--------\n\nThis page includes scripts that help with debugging and using MySQL.\n\nFind write activities\n---------------------\n\n #!/bin/bash\n\n # Tail the binlog and look for insert / update / delete\n # The goal is to help the user understand which writes are happening\n\n\n MYSQL=$(which mysql)\n MYSQLBINLOG=$(which mysqlbinlog)\n DATE=$(which date)\n\n if [[ -z \"${MYSQL}\" ]]\n then\n echo \"ERROR: Could not find mysql shell\"\n exit 1\n fi\n if [[ -z \"${MYSQLBINLOG}\" ]]\n then\n echo \"ERROR: Could not find mysqlbinlog utility\"\n exit 1\n fi\n\n if [[ -z \"$1\" ]]\n then\n echo \"Usage: $0 [mysql connection parameters]\"\n exit 1\n fi\n\n last_log=$(\"${MYSQL}\" \"$@\" -N -B -e \"SHOW BINARY LOGS;\" | tail -n 1 | cut -f1)\n time=$(\"${DATE}\" '+%Y-%m-%d %H:%M:%S')\n\n echo \"Continuously reading from ${last_log} starting from ${time}\"\n\n \"${MYSQLBINLOG}\" --base64-output=DECODE-ROWS --verbose --start-datetime=\"${time}\" --read-from-remote-server \"$@\" \"${last_log}\" --stop-never | grep \"INSERT\\|UPDATE\\|DELETE\"\n\nFind `ALTER TABLE` commands\n---------------------------\n\n #!/bin/bash\n\n # Search for DDL Commands in the last 24 hours. Should help the user understand\n # which DDL commands they are performing.\n\n MYSQL=$(which mysql)\n MYSQLBINLOG=$(which mysqlbinlog)\n DATE=$(which date)\n\n if [[ -z \"${MYSQL}\" ]]\n then\n echo \"ERROR: Could not find mysql shell\"\n exit 1\n fi\n\n if [[ -z \"${MYSQLBINLOG}\" ]]\n then\n echo \"ERROR: Could not find mysqlbinlog utility\"\n exit 1\n fi\n\n if [[ -z \"$1\" ]]\n then\n echo \"Usage: $0 [mysql connection parameters]\"\n exit 1\n fi\n\n log_files=$(\"${MYSQL}\" \"$@\" -N -B -e \"SHOW BINARY LOGS;\" | cut -f1)\n\n yesterday=$(\"${DATE}\" --date=\"-1 day\" '+%Y-%m-%d %H:%M:%S')\n\n echo \"Searching for DDL commands, starting at ${yesterday}\"\n for file in ${log_files}\n do\n echo \"Log file: ${file}\"\n \"${MYSQLBINLOG}\" --start-datetime \"${yesterday}\" --read-from-remote-server \"$@\" \"${file}\" | grep -B 2 \"ALTER TABLE\\|CREATE TABLE\\|TRUNCATE TABLE\\|RENAME TABLE\\|DROP TABLE\"\n done\n\nLock all tables\n---------------\n\n #!/bin/bash\n\n # This script locks all non-system tables on a MySQL database.\n # Helps for the case where we cannot acquire read lock with flush.\n\n MYSQL=\"$(which mysql)\"\n\n if [[ -z \"${MYSQL}\" ]]\n then\n echo \"ERROR: Could not find mysql shell\"\n exit 1\n fi\n\n if [[ -z \"$1\" ]]\n then\n echo \"Usage: $0 [mysql connection parameters]\"\n exit 1\n fi\n\n LOCK_TABLES_STMT=\"select concat('LOCK TABLES ', group_concat(concat('\\`',table_schema,'\\`.\\`',table_name,'\\` READ')),';') as stmt from information_schema.tables where table_schema not in ('mysql', 'sys', 'performance_schema', 'information_schema');\"\n QUERY=\"$(\"${MYSQL}\" \"$@\" -N -B -e \"${LOCK_TABLES_STMT}\")\"\n\n (\n echo \"${QUERY}\"\n read -n 1 -r -s -p $'Tables locked, press any key to stop the session and UNLOCK TABLES\\n'\n echo \"UNLOCK TABLES;\"\n ) | \"${MYSQL}\" \"$@\""]]