Last active
August 7, 2024 10:15
-
-
Save annibuliful/7505d7286899ea2354e39d16b978a171 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Variables | |
MYSQL_PLUGIN_DIR="/usr/lib64/mysql/plugin" | |
AUDIT_LOG_PLUGIN="audit_log.so" | |
AUDIT_LOG_URL="https://github.com/zhaopinglu/mysql-audit/archive/refs/heads/master.zip" | |
MYSQL_CONF="/etc/my.cnf" | |
MYSQL_AUDIT_DIR="/var/lib/mysql" | |
MYSQL_USER="root" | |
MYSQL_PASS="your_password" | |
# Function to install dependencies | |
install_dependencies() { | |
echo "Installing necessary dependencies..." | |
yum install -y wget unzip cmake make gcc gcc-c++ mysql-server | |
} | |
# Function to download, modify, and compile the MySQL Audit Plugin | |
download_modify_compile_plugin() { | |
echo "Downloading MySQL Audit Plugin..." | |
wget $AUDIT_LOG_URL -O master.zip | |
unzip master.zip | |
cd mysql-audit-master || exit | |
echo "Modifying source code as per instructions..." | |
sed -i 's/my_charset_utf8_general_ci/my_charset_utf8mb3_general_ci/g' src/audit_handler.cc | |
sed -i 's/my_charset_utf8_bin/my_charset_utf8mb3_bin/g' src/audit_handler.cc | |
echo "Building the MySQL Audit Plugin..." | |
cmake . | |
make | |
if [ -f "$AUDIT_LOG_PLUGIN" ]; then | |
echo "Compilation successful." | |
else | |
echo "Compilation failed. Exiting..." | |
exit 1 | |
fi | |
} | |
# Function to install the plugin | |
install_plugin() { | |
echo "Installing the MySQL Audit Plugin..." | |
cp $AUDIT_LOG_PLUGIN $MYSQL_PLUGIN_DIR/ | |
echo "Loading the plugin into MySQL..." | |
mysql -u $MYSQL_USER -p$MYSQL_PASS -e "INSTALL PLUGIN audit_log SONAME '$AUDIT_LOG_PLUGIN';" | |
if [ $? -eq 0 ]; then | |
echo "Plugin installed successfully." | |
else | |
echo "Failed to install the plugin. Exiting..." | |
exit 1 | |
fi | |
} | |
# Function to configure the plugin | |
configure_plugin() { | |
echo "Configuring the MySQL Audit Plugin..." | |
if grep -q "audit_log_format" $MYSQL_CONF; then | |
echo "Audit log configuration already exists in $MYSQL_CONF." | |
else | |
echo -e "[mysqld]\naudit_log_format = JSON\naudit_log_policy = ALL" >> $MYSQL_CONF | |
fi | |
echo "Restarting MySQL service..." | |
systemctl restart mysqld | |
if [ $? -eq 0 ]; then | |
echo "MySQL restarted successfully." | |
else | |
echo "Failed to restart MySQL. Exiting..." | |
exit 1 | |
fi | |
} | |
# Function to verify installation | |
verify_installation() { | |
echo "Verifying plugin installation..." | |
mysql -u $MYSQL_USER -p$MYSQL_PASS -e "SHOW PLUGINS;" | grep -i "audit_log" | |
if [ $? -eq 0 ]; then | |
echo "Plugin verification successful." | |
else | |
echo "Plugin verification failed. Exiting..." | |
exit 1 | |
fi | |
} | |
# Function to verify functionality | |
verify_functionality() { | |
echo "Verifying plugin functionality..." | |
tail -f $MYSQL_AUDIT_DIR/audit.log & | |
TAIL_PID=$! | |
sleep 5 | |
kill $TAIL_PID | |
if [ $? -eq 0 ]; then | |
echo "Plugin functionality verified." | |
else | |
echo "Plugin functionality verification failed. Exiting..." | |
exit 1 | |
fi | |
} | |
# Main script execution | |
install_dependencies | |
download_modify_compile_plugin | |
install_plugin | |
configure_plugin | |
verify_installation | |
verify_functionality | |
echo "MySQL Audit Plugin installation and configuration for MySQL 8.0.36 completed successfully." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment