Skip to content

Instantly share code, notes, and snippets.

@Ramonymous
Forked from coderua/mysql_secure.sh
Last active August 24, 2019 09:22
Show Gist options
  • Save Ramonymous/0f37f10478aa2f7675f3bbb8c3413bf7 to your computer and use it in GitHub Desktop.
Save Ramonymous/0f37f10478aa2f7675f3bbb8c3413bf7 to your computer and use it in GitHub Desktop.

Revisions

  1. Ramonymous revised this gist Aug 24, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mysql_secure.sh
    Original file line number Diff line number Diff line change
    @@ -54,7 +54,7 @@ fi
    #
    if [ $(dpkg-query -W -f='${Status}' expect 2>/dev/null | grep -c "ok installed") -eq 0 ]; then
    echo "Can't find expect. Trying install it..."
    aptitude -y install expect
    yum -y install expect

    fi

  2. Ramonymous revised this gist Aug 24, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mysql_secure.sh
    Original file line number Diff line number Diff line change
    @@ -97,7 +97,7 @@ echo "${SECURE_MYSQL}"

    if [ "${PURGE_EXPECT_WHEN_DONE}" -eq 1 ]; then
    # Uninstalling expect package
    aptitude -y purge expect
    yum -y remove expect
    fi

    exit 0
  3. @coderua coderua revised this gist Aug 12, 2014. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions mysql_secure.sh
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,17 @@
    #!/bin/bash

    #
    # Automate mysql secure installation
    #
    # Automate mysql secure installation for debian-baed systems
    #
    # - You can set a password for root accounts.
    # - You can remove root accounts that are accessible from outside the local host.
    # - You can remove anonymous-user accounts.
    # - You can remove the test database (which by default can be accessed by all users, even anonymous users),
    # and privileges that permit anyone to access databases with names that start with test_.
    # For details see documentation: http://dev.mysql.com/doc/refman/5.7/en/mysql-secure-installation.html
    #
    # @version 13.08.2014 00:41 +03:00
    # @version 13.08.2014 00:39 +03:00
    # Tested on Debian 7.6 (wheezy)
    #
    # Usage:
    # Setup mysql root password: ./mysql_secure.sh 'your_new_root_password'
  4. @coderua coderua revised this gist Aug 12, 2014. 1 changed file with 75 additions and 9 deletions.
    84 changes: 75 additions & 9 deletions mysql_secure.sh
    Original file line number Diff line number Diff line change
    @@ -1,20 +1,78 @@
    #!/bin/bash

    aptitude -y install expect
    #
    # Automate mysql secure installation
    #
    # - You can set a password for root accounts.
    # - You can remove root accounts that are accessible from outside the local host.
    # - You can remove anonymous-user accounts.
    # - You can remove the test database (which by default can be accessed by all users, even anonymous users),
    # and privileges that permit anyone to access databases with names that start with test_.
    # For details see documentation: http://dev.mysql.com/doc/refman/5.7/en/mysql-secure-installation.html
    #
    # @version 13.08.2014 00:41 +03:00
    #
    # Usage:
    # Setup mysql root password: ./mysql_secure.sh 'your_new_root_password'
    # Change mysql root password: ./mysql_secure.sh 'your_old_root_password' 'your_new_root_password'"
    #

    // Not required in actual script
    MYSQL_ROOT_PASSWORD=abcd1234
    # Delete package expect when script is done
    # 0 - No;
    # 1 - Yes.
    PURGE_EXPECT_WHEN_DONE=0

    #
    # Check the bash shell script is being run by root
    #
    if [[ $EUID -ne 0 ]]; then
    echo "This script must be run as root" 1>&2
    exit 1
    fi

    #
    # Check input params
    #
    if [ -n "${1}" -a -z "${2}" ]; then
    # Setup root password
    CURRENT_MYSQL_PASSWORD=''
    NEW_MYSQL_PASSWORD="${1}"
    elif [ -n "${1}" -a -n "${2}" ]; then
    # Change existens root password
    CURRENT_MYSQL_PASSWORD="${1}"
    NEW_MYSQL_PASSWORD="${2}"
    else
    echo "Usage:"
    echo " Setup mysql root password: ${0} 'your_new_root_password'"
    echo " Change mysql root password: ${0} 'your_old_root_password' 'your_new_root_password'"
    exit 1
    fi

    #
    # Check is expect package installed
    #
    if [ $(dpkg-query -W -f='${Status}' expect 2>/dev/null | grep -c "ok installed") -eq 0 ]; then
    echo "Can't find expect. Trying install it..."
    aptitude -y install expect

    fi

    SECURE_MYSQL=$(expect -c "
    set timeout 10
    set timeout 3
    spawn mysql_secure_installation
    expect \"Enter current password for root (enter for none):\"
    send \"$MYSQL\r\"
    send \"$CURRENT_MYSQL_PASSWORD\r\"
    expect \"Change the root password?\"
    send \"n\r\"
    expect \"root password?\"
    send \"y\r\"
    expect \"New password:\"
    send \"$NEW_MYSQL_PASSWORD\r\"
    expect \"Re-enter new password:\"
    send \"$NEW_MYSQL_PASSWORD\r\"
    expect \"Remove anonymous users?\"
    send \"y\r\"
    @@ -31,6 +89,14 @@ send \"y\r\"
    expect eof
    ")

    echo "$SECURE_MYSQL"
    #
    # Execution mysql_secure_installation
    #
    echo "${SECURE_MYSQL}"

    if [ "${PURGE_EXPECT_WHEN_DONE}" -eq 1 ]; then
    # Uninstalling expect package
    aptitude -y purge expect
    fi

    aptitude -y purge expect
    exit 0
  5. Mins revised this gist Jan 23, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mysql_secure.sh
    Original file line number Diff line number Diff line change
    @@ -33,4 +33,4 @@ expect eof

    echo "$SECURE_MYSQL"

    aptitude purge expect
    aptitude -y purge expect
  6. Mins renamed this gist Jan 23, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. Mins created this gist Jan 23, 2013.
    36 changes: 36 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    #!/bin/bash

    aptitude -y install expect

    // Not required in actual script
    MYSQL_ROOT_PASSWORD=abcd1234

    SECURE_MYSQL=$(expect -c "
    set timeout 10
    spawn mysql_secure_installation
    expect \"Enter current password for root (enter for none):\"
    send \"$MYSQL\r\"
    expect \"Change the root password?\"
    send \"n\r\"
    expect \"Remove anonymous users?\"
    send \"y\r\"
    expect \"Disallow root login remotely?\"
    send \"y\r\"
    expect \"Remove test database and access to it?\"
    send \"y\r\"
    expect \"Reload privilege tables now?\"
    send \"y\r\"
    expect eof
    ")

    echo "$SECURE_MYSQL"

    aptitude purge expect