Stay organized with collections Save and categorize content based on your preferences.
This page includes scripts that help with debugging and using MySQL.
Find write activities#!/bin/bash
# Tail the binlog and look for insert / update / delete
# The goal is to help the user understand which writes are happening
MYSQL=$(which mysql)
MYSQLBINLOG=$(which mysqlbinlog)
DATE=$(which date)
if [[ -z "${MYSQL}" ]]
then
echo "ERROR: Could not find mysql shell"
exit 1
fi
if [[ -z "${MYSQLBINLOG}" ]]
then
echo "ERROR: Could not find mysqlbinlog utility"
exit 1
fi
if [[ -z "$1" ]]
then
echo "Usage: $0 [mysql connection parameters]"
exit 1
fi
last_log=$("${MYSQL}" "$@" -N -B -e "SHOW BINARY LOGS;" | tail -n 1 | 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"
Find ALTER TABLE
commands
#!/bin/bash
# Search for DDL Commands in the last 24 hours. Should help the user understand
# which DDL commands they are performing.
MYSQL=$(which mysql)
MYSQLBINLOG=$(which mysqlbinlog)
DATE=$(which date)
if [[ -z "${MYSQL}" ]]
then
echo "ERROR: Could not find mysql shell"
exit 1
fi
if [[ -z "${MYSQLBINLOG}" ]]
then
echo "ERROR: Could not find mysqlbinlog utility"
exit 1
fi
if [[ -z "$1" ]]
then
echo "Usage: $0 [mysql connection parameters]"
exit 1
fi
log_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}"
for file in ${log_files}
do
echo "Log file: ${file}"
"${MYSQLBINLOG}" --start-datetime "${yesterday}" --read-from-remote-server "$@" "${file}" | grep -B 2 "ALTER TABLE\|CREATE TABLE\|TRUNCATE TABLE\|RENAME TABLE\|DROP TABLE"
done
Lock all 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="$(which mysql)"
if [[ -z "${MYSQL}" ]]
then
echo "ERROR: Could not find mysql shell"
exit 1
fi
if [[ -z "$1" ]]
then
echo "Usage: $0 [mysql connection parameters]"
exit 1
fi
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');"
QUERY="$("${MYSQL}" "$@" -N -B -e "${LOCK_TABLES_STMT}")"
(
echo "${QUERY}"
read -n 1 -r -s -p $'Tables locked, press any key to stop the session and UNLOCK TABLES\n'
echo "UNLOCK TABLES;"
) | "${MYSQL}" "$@"
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-07 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-07 UTC."],[[["This page provides debugging scripts specifically designed for use with MySQL databases."],["One script helps users identify write activities, such as inserts, updates, and deletes, by monitoring the binlog."],["Another script enables users to search for Data Definition Language (DDL) commands like `ALTER TABLE` within the past 24 hours."],["A 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."]]],[]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4