Skip to content

Instantly share code, notes, and snippets.

@holly
Created August 1, 2021 07:16
Show Gist options
  • Save holly/5355af3ea447283aac899950ae589e9d to your computer and use it in GitHub Desktop.
Save holly/5355af3ea447283aac899950ae589e9d to your computer and use it in GitHub Desktop.
oneliner mysql_secure_install
#!/bin/bash
set -e
if [ $# != 1 ]; then
echo "Usage: mysql_secure_install.sh \$password"
exit 1
fi
MYSQL_CMD="/usr/bin/mysql -B"
ROOT_PASSWORD=$1
res=$($MYSQL_CMD -e "SELECT component_urn FROM mysql.component WHERE component_urn = 'file://component_validate_password'")
if [ -z "$res" ] ; then
$MYSQL_CMD -e "INSTALL COMPONENT 'file://component_validate_password'"
fi
cat <<EOF | $MYSQL_CMD
SET GLOBAL validate_password.policy = 'LOW';
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '$ROOT_PASSWORD';
DELETE FROM mysql.user WHERE User='';
DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
DROP DATABASE IF EXISTS test;
DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';
FLUSH PRIVILEGES;
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment