Automate the mysql secure installation using shell script












1















I am trying to automate mysql secure installation using linux shell scriping. I have below code got from https://gist.github.com/Mins/4602864.



#!/bin/bash

MYSQL=$(grep 'temporary password' /var/log/mysqld.log | awk '{print $11}')
MYSQL_ROOT_PASSWORD="test@123"

SECURE_MYSQL=$(expect -c "

set timeout 10
spawn mysql_secure_installation

expect "Enter password for user root:"
send "$MYSQLr"

expect "Change the password for root ? ((Press y|Y for Yes, any other key for No) :"
send "yr"

expect "New password:"
send "$MYSQL_ROOT_PASSWORDr"

expect "Re-enter new password:"
send "$MYSQL_ROOT_PASSWORDr"

expect "Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :"
send "yr"

expect "Remove anonymous users? (Press y|Y for Yes, any other key for No) :"
send "yr"

expect "Disallow root login remotely? (Press y|Y for Yes, any other key for No) :"
send "yr"

expect "Remove test database and access to it? (Press y|Y for Yes, any other key for No) :"
send "yr"

expect "Reload privilege tables now? (Press y|Y for Yes, any other key for No) :"
send "yr"

expect eof
"))

echo "$SECURE_MYSQL"


But i am getting error



./sql.sh: command substitution: line 48: syntax error near unexpected token `('
./sql.sh: command substitution: line 48: ` expect "Change the password for root ? ((Press y|Y for Yes, any other key for No) :"'


I have tried to figure out the error but no success.










share|improve this question





























    1















    I am trying to automate mysql secure installation using linux shell scriping. I have below code got from https://gist.github.com/Mins/4602864.



    #!/bin/bash

    MYSQL=$(grep 'temporary password' /var/log/mysqld.log | awk '{print $11}')
    MYSQL_ROOT_PASSWORD="test@123"

    SECURE_MYSQL=$(expect -c "

    set timeout 10
    spawn mysql_secure_installation

    expect "Enter password for user root:"
    send "$MYSQLr"

    expect "Change the password for root ? ((Press y|Y for Yes, any other key for No) :"
    send "yr"

    expect "New password:"
    send "$MYSQL_ROOT_PASSWORDr"

    expect "Re-enter new password:"
    send "$MYSQL_ROOT_PASSWORDr"

    expect "Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :"
    send "yr"

    expect "Remove anonymous users? (Press y|Y for Yes, any other key for No) :"
    send "yr"

    expect "Disallow root login remotely? (Press y|Y for Yes, any other key for No) :"
    send "yr"

    expect "Remove test database and access to it? (Press y|Y for Yes, any other key for No) :"
    send "yr"

    expect "Reload privilege tables now? (Press y|Y for Yes, any other key for No) :"
    send "yr"

    expect eof
    "))

    echo "$SECURE_MYSQL"


    But i am getting error



    ./sql.sh: command substitution: line 48: syntax error near unexpected token `('
    ./sql.sh: command substitution: line 48: ` expect "Change the password for root ? ((Press y|Y for Yes, any other key for No) :"'


    I have tried to figure out the error but no success.










    share|improve this question



























      1












      1








      1








      I am trying to automate mysql secure installation using linux shell scriping. I have below code got from https://gist.github.com/Mins/4602864.



      #!/bin/bash

      MYSQL=$(grep 'temporary password' /var/log/mysqld.log | awk '{print $11}')
      MYSQL_ROOT_PASSWORD="test@123"

      SECURE_MYSQL=$(expect -c "

      set timeout 10
      spawn mysql_secure_installation

      expect "Enter password for user root:"
      send "$MYSQLr"

      expect "Change the password for root ? ((Press y|Y for Yes, any other key for No) :"
      send "yr"

      expect "New password:"
      send "$MYSQL_ROOT_PASSWORDr"

      expect "Re-enter new password:"
      send "$MYSQL_ROOT_PASSWORDr"

      expect "Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :"
      send "yr"

      expect "Remove anonymous users? (Press y|Y for Yes, any other key for No) :"
      send "yr"

      expect "Disallow root login remotely? (Press y|Y for Yes, any other key for No) :"
      send "yr"

      expect "Remove test database and access to it? (Press y|Y for Yes, any other key for No) :"
      send "yr"

      expect "Reload privilege tables now? (Press y|Y for Yes, any other key for No) :"
      send "yr"

      expect eof
      "))

      echo "$SECURE_MYSQL"


      But i am getting error



      ./sql.sh: command substitution: line 48: syntax error near unexpected token `('
      ./sql.sh: command substitution: line 48: ` expect "Change the password for root ? ((Press y|Y for Yes, any other key for No) :"'


      I have tried to figure out the error but no success.










      share|improve this question
















      I am trying to automate mysql secure installation using linux shell scriping. I have below code got from https://gist.github.com/Mins/4602864.



      #!/bin/bash

      MYSQL=$(grep 'temporary password' /var/log/mysqld.log | awk '{print $11}')
      MYSQL_ROOT_PASSWORD="test@123"

      SECURE_MYSQL=$(expect -c "

      set timeout 10
      spawn mysql_secure_installation

      expect "Enter password for user root:"
      send "$MYSQLr"

      expect "Change the password for root ? ((Press y|Y for Yes, any other key for No) :"
      send "yr"

      expect "New password:"
      send "$MYSQL_ROOT_PASSWORDr"

      expect "Re-enter new password:"
      send "$MYSQL_ROOT_PASSWORDr"

      expect "Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :"
      send "yr"

      expect "Remove anonymous users? (Press y|Y for Yes, any other key for No) :"
      send "yr"

      expect "Disallow root login remotely? (Press y|Y for Yes, any other key for No) :"
      send "yr"

      expect "Remove test database and access to it? (Press y|Y for Yes, any other key for No) :"
      send "yr"

      expect "Reload privilege tables now? (Press y|Y for Yes, any other key for No) :"
      send "yr"

      expect eof
      "))

      echo "$SECURE_MYSQL"


      But i am getting error



      ./sql.sh: command substitution: line 48: syntax error near unexpected token `('
      ./sql.sh: command substitution: line 48: ` expect "Change the password for root ? ((Press y|Y for Yes, any other key for No) :"'


      I have tried to figure out the error but no success.







      bash scripts expect






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 12 at 22:10









      ivan_onys

      1032




      1032










      asked Jan 10 at 5:05









      JanithJanith

      748




      748






















          2 Answers
          2






          active

          oldest

          votes


















          1














          Try this as it is, seems like there is a some issue with ( and pipe | so I had to escape them.



          Source Code



          #!/bin/bash

          MYSQL=$(grep 'temporary password' /var/log/mysqld.log | awk '{print $11}')
          MYSQL_ROOT_PASSWORD="test@123"

          SECURE_MYSQL=$(expect -c "

          set timeout 10
          spawn mysql_secure_installation

          expect "Enter password for user root:"
          send "$MYSQLr"

          expect "Change the password for root ?(Press y|Y for Yes, any other key for No) :"
          send "yr"

          expect "New password:"
          send "$MYSQL_ROOT_PASSWORDr"

          expect "Re-enter new password:"
          send "$MYSQL_ROOT_PASSWORDr"

          expect "Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :"
          send "yr"

          expect "Remove anonymous users?(Press y|Y for Yes, any other key for No) :"
          send "yr"

          expect "Disallow root login remotely?(Press y|Y for Yes, any other key for No) :"
          send "yr"

          expect "Remove test database and access to it?(Press y|Y for Yes, any other key for No) :"
          send "yr"

          expect "Reload privilege tables now?(Press y|Y for Yes, any other key for No) :"
          send "yr"

          expect eof
          ")

          echo "$SECURE_MYSQL"


          Debug execution

          root@5015a2757ac4:/# bash -x secure.sh
          ++ grep 'temporary password' /var/log/mysqld.log
          ++ awk '{print $11}'
          grep: /var/log/mysqld.log: No such file or directory
          + MYSQL=
          + MYSQL_ROOT_PASSWORD=test@123
          ++ expect -c '

          set timeout 10
          spawn mysql_secure_installation

          expect Enter' password for user 'root:
          send r

          expect Change' the password for root '?(Press' 'y|Y' for Yes, any other key for 'No)' ':
          send yr

          expect New' 'password:
          send test@123r

          expect Re-enter' new 'password:
          send test@123r

          expect Do' you wish to continue with the password 'provided?(Press' 'y|Y' for Yes, any other key for 'No)' ':
          send yr

          expect Remove' anonymous 'users?(Press' 'y|Y' for Yes, any other key for 'No)' ':
          send yr

          expect Disallow' root login 'remotely?(Press' 'y|Y' for Yes, any other key for 'No)' ':
          send yr

          expect Remove' test database and access to 'it?(Press' 'y|Y' for Yes, any other key for 'No)' ':
          send yr

          expect Reload' privilege tables 'now?(Press' 'y|Y' for Yes, any other key for 'No)' ':
          send yr

          expect eof
          '

          couldn't read file "password": no such file or directory
          + SECURE_MYSQL='spawn mysql_secure_installation

          Securing the MySQL server deployment.

          Connecting to MySQL using a blank password.

          VALIDATE PASSWORD PLUGIN can be used to test passwords
          and improve security. It checks the strength of password
          and allows the users to set only those passwords which are
          secure enough. Would you like to setup VALIDATE PASSWORD plugin?

          Press y|Y for Yes, any other key for No: '
          + echo 'spawn mysql_secure_installation

          Securing the MySQL server deployment.

          Connecting to MySQL using a blank password.

          VALIDATE PASSWORD PLUGIN can be used to test passwords
          and improve security. It checks the strength of password
          and allows the users to set only those passwords which are
          secure enough. Would you like to setup VALIDATE PASSWORD plugin?

          Press y|Y for Yes, any other key for No: '
          spawn mysql_secure_installation

          Securing the MySQL server deployment.

          Connecting to MySQL using a blank password.

          VALIDATE PASSWORD PLUGIN can be used to test passwords
          and improve security. It checks the strength of password
          and allows the users to set only those passwords which are
          secure enough. Would you like to setup VALIDATE PASSWORD plugin?

          Press y|Y for Yes, any other key for No:
          root@5015a2757ac4:/#


          And I have verified the login with new password test@123.






          share|improve this answer


























          • I am getting error ./secure.sh: line 39: expect: command not found

            – Janith
            Jan 10 at 8:37











          • @Janith, that means you have not installed expect command. try apt-get install expect -y and try again.

            – rɑːdʒɑ
            Jan 10 at 8:59











          • thank you for your support. Still getting the error couldn't read file "password": no such file or directory spawn mysql_secure_installation Securing the MySQL server deployment. Enter password for user root:

            – Janith
            Jan 10 at 9:13













          • @Janith, Are you sure that you exactly executing as given ? cause Its worked for me.

            – rɑːdʒɑ
            Jan 10 at 9:27











          • Yes I copied exact script which you are given

            – Janith
            Jan 10 at 9:44



















          0














          Below script worked for me.



          #!/bin/bash

          MYSQL_ROOT_PASSWORD='Password@123'
          MYSQL=$(grep 'temporary password' /var/log/mysqld.log | awk '{print $11}')

          SECURE_MYSQL=$(expect -c "

          set timeout 10
          spawn mysql_secure_installation

          expect "Enter password for user root:"
          send "$MYSQLr"
          expect "New password:"
          send "$MYSQL_ROOT_PASSWORDr"
          expect "Re-enter new password:"
          send "$MYSQL_ROOT_PASSWORDr"
          expect "Change the password for root ? ((Press y|Y for Yes, any other key for No) :"
          send "yr"
          send "$MYSQLr"
          expect "New password:"
          send "$MYSQL_ROOT_PASSWORDr"
          expect "Re-enter new password:"
          send "$MYSQL_ROOT_PASSWORDr"
          expect "Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :"
          send "yr"
          expect "Remove anonymous users?(Press y|Y for Yes, any other key for No) :"
          send "yr"
          expect "Disallow root login remotely?(Press y|Y for Yes, any other key for No) :"
          send "nr"
          expect "Remove test database and access to it?(Press y|Y for Yes, any other key for No) :"
          send "yr"
          expect "Reload privilege tables now?(Press y|Y for Yes, any other key for No) :"
          send "yr"
          expect eof
          ")

          echo $SECURE_MYSQL





          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "89"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1108475%2fautomate-the-mysql-secure-installation-using-shell-script%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Try this as it is, seems like there is a some issue with ( and pipe | so I had to escape them.



            Source Code



            #!/bin/bash

            MYSQL=$(grep 'temporary password' /var/log/mysqld.log | awk '{print $11}')
            MYSQL_ROOT_PASSWORD="test@123"

            SECURE_MYSQL=$(expect -c "

            set timeout 10
            spawn mysql_secure_installation

            expect "Enter password for user root:"
            send "$MYSQLr"

            expect "Change the password for root ?(Press y|Y for Yes, any other key for No) :"
            send "yr"

            expect "New password:"
            send "$MYSQL_ROOT_PASSWORDr"

            expect "Re-enter new password:"
            send "$MYSQL_ROOT_PASSWORDr"

            expect "Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :"
            send "yr"

            expect "Remove anonymous users?(Press y|Y for Yes, any other key for No) :"
            send "yr"

            expect "Disallow root login remotely?(Press y|Y for Yes, any other key for No) :"
            send "yr"

            expect "Remove test database and access to it?(Press y|Y for Yes, any other key for No) :"
            send "yr"

            expect "Reload privilege tables now?(Press y|Y for Yes, any other key for No) :"
            send "yr"

            expect eof
            ")

            echo "$SECURE_MYSQL"


            Debug execution

            root@5015a2757ac4:/# bash -x secure.sh
            ++ grep 'temporary password' /var/log/mysqld.log
            ++ awk '{print $11}'
            grep: /var/log/mysqld.log: No such file or directory
            + MYSQL=
            + MYSQL_ROOT_PASSWORD=test@123
            ++ expect -c '

            set timeout 10
            spawn mysql_secure_installation

            expect Enter' password for user 'root:
            send r

            expect Change' the password for root '?(Press' 'y|Y' for Yes, any other key for 'No)' ':
            send yr

            expect New' 'password:
            send test@123r

            expect Re-enter' new 'password:
            send test@123r

            expect Do' you wish to continue with the password 'provided?(Press' 'y|Y' for Yes, any other key for 'No)' ':
            send yr

            expect Remove' anonymous 'users?(Press' 'y|Y' for Yes, any other key for 'No)' ':
            send yr

            expect Disallow' root login 'remotely?(Press' 'y|Y' for Yes, any other key for 'No)' ':
            send yr

            expect Remove' test database and access to 'it?(Press' 'y|Y' for Yes, any other key for 'No)' ':
            send yr

            expect Reload' privilege tables 'now?(Press' 'y|Y' for Yes, any other key for 'No)' ':
            send yr

            expect eof
            '

            couldn't read file "password": no such file or directory
            + SECURE_MYSQL='spawn mysql_secure_installation

            Securing the MySQL server deployment.

            Connecting to MySQL using a blank password.

            VALIDATE PASSWORD PLUGIN can be used to test passwords
            and improve security. It checks the strength of password
            and allows the users to set only those passwords which are
            secure enough. Would you like to setup VALIDATE PASSWORD plugin?

            Press y|Y for Yes, any other key for No: '
            + echo 'spawn mysql_secure_installation

            Securing the MySQL server deployment.

            Connecting to MySQL using a blank password.

            VALIDATE PASSWORD PLUGIN can be used to test passwords
            and improve security. It checks the strength of password
            and allows the users to set only those passwords which are
            secure enough. Would you like to setup VALIDATE PASSWORD plugin?

            Press y|Y for Yes, any other key for No: '
            spawn mysql_secure_installation

            Securing the MySQL server deployment.

            Connecting to MySQL using a blank password.

            VALIDATE PASSWORD PLUGIN can be used to test passwords
            and improve security. It checks the strength of password
            and allows the users to set only those passwords which are
            secure enough. Would you like to setup VALIDATE PASSWORD plugin?

            Press y|Y for Yes, any other key for No:
            root@5015a2757ac4:/#


            And I have verified the login with new password test@123.






            share|improve this answer


























            • I am getting error ./secure.sh: line 39: expect: command not found

              – Janith
              Jan 10 at 8:37











            • @Janith, that means you have not installed expect command. try apt-get install expect -y and try again.

              – rɑːdʒɑ
              Jan 10 at 8:59











            • thank you for your support. Still getting the error couldn't read file "password": no such file or directory spawn mysql_secure_installation Securing the MySQL server deployment. Enter password for user root:

              – Janith
              Jan 10 at 9:13













            • @Janith, Are you sure that you exactly executing as given ? cause Its worked for me.

              – rɑːdʒɑ
              Jan 10 at 9:27











            • Yes I copied exact script which you are given

              – Janith
              Jan 10 at 9:44
















            1














            Try this as it is, seems like there is a some issue with ( and pipe | so I had to escape them.



            Source Code



            #!/bin/bash

            MYSQL=$(grep 'temporary password' /var/log/mysqld.log | awk '{print $11}')
            MYSQL_ROOT_PASSWORD="test@123"

            SECURE_MYSQL=$(expect -c "

            set timeout 10
            spawn mysql_secure_installation

            expect "Enter password for user root:"
            send "$MYSQLr"

            expect "Change the password for root ?(Press y|Y for Yes, any other key for No) :"
            send "yr"

            expect "New password:"
            send "$MYSQL_ROOT_PASSWORDr"

            expect "Re-enter new password:"
            send "$MYSQL_ROOT_PASSWORDr"

            expect "Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :"
            send "yr"

            expect "Remove anonymous users?(Press y|Y for Yes, any other key for No) :"
            send "yr"

            expect "Disallow root login remotely?(Press y|Y for Yes, any other key for No) :"
            send "yr"

            expect "Remove test database and access to it?(Press y|Y for Yes, any other key for No) :"
            send "yr"

            expect "Reload privilege tables now?(Press y|Y for Yes, any other key for No) :"
            send "yr"

            expect eof
            ")

            echo "$SECURE_MYSQL"


            Debug execution

            root@5015a2757ac4:/# bash -x secure.sh
            ++ grep 'temporary password' /var/log/mysqld.log
            ++ awk '{print $11}'
            grep: /var/log/mysqld.log: No such file or directory
            + MYSQL=
            + MYSQL_ROOT_PASSWORD=test@123
            ++ expect -c '

            set timeout 10
            spawn mysql_secure_installation

            expect Enter' password for user 'root:
            send r

            expect Change' the password for root '?(Press' 'y|Y' for Yes, any other key for 'No)' ':
            send yr

            expect New' 'password:
            send test@123r

            expect Re-enter' new 'password:
            send test@123r

            expect Do' you wish to continue with the password 'provided?(Press' 'y|Y' for Yes, any other key for 'No)' ':
            send yr

            expect Remove' anonymous 'users?(Press' 'y|Y' for Yes, any other key for 'No)' ':
            send yr

            expect Disallow' root login 'remotely?(Press' 'y|Y' for Yes, any other key for 'No)' ':
            send yr

            expect Remove' test database and access to 'it?(Press' 'y|Y' for Yes, any other key for 'No)' ':
            send yr

            expect Reload' privilege tables 'now?(Press' 'y|Y' for Yes, any other key for 'No)' ':
            send yr

            expect eof
            '

            couldn't read file "password": no such file or directory
            + SECURE_MYSQL='spawn mysql_secure_installation

            Securing the MySQL server deployment.

            Connecting to MySQL using a blank password.

            VALIDATE PASSWORD PLUGIN can be used to test passwords
            and improve security. It checks the strength of password
            and allows the users to set only those passwords which are
            secure enough. Would you like to setup VALIDATE PASSWORD plugin?

            Press y|Y for Yes, any other key for No: '
            + echo 'spawn mysql_secure_installation

            Securing the MySQL server deployment.

            Connecting to MySQL using a blank password.

            VALIDATE PASSWORD PLUGIN can be used to test passwords
            and improve security. It checks the strength of password
            and allows the users to set only those passwords which are
            secure enough. Would you like to setup VALIDATE PASSWORD plugin?

            Press y|Y for Yes, any other key for No: '
            spawn mysql_secure_installation

            Securing the MySQL server deployment.

            Connecting to MySQL using a blank password.

            VALIDATE PASSWORD PLUGIN can be used to test passwords
            and improve security. It checks the strength of password
            and allows the users to set only those passwords which are
            secure enough. Would you like to setup VALIDATE PASSWORD plugin?

            Press y|Y for Yes, any other key for No:
            root@5015a2757ac4:/#


            And I have verified the login with new password test@123.






            share|improve this answer


























            • I am getting error ./secure.sh: line 39: expect: command not found

              – Janith
              Jan 10 at 8:37











            • @Janith, that means you have not installed expect command. try apt-get install expect -y and try again.

              – rɑːdʒɑ
              Jan 10 at 8:59











            • thank you for your support. Still getting the error couldn't read file "password": no such file or directory spawn mysql_secure_installation Securing the MySQL server deployment. Enter password for user root:

              – Janith
              Jan 10 at 9:13













            • @Janith, Are you sure that you exactly executing as given ? cause Its worked for me.

              – rɑːdʒɑ
              Jan 10 at 9:27











            • Yes I copied exact script which you are given

              – Janith
              Jan 10 at 9:44














            1












            1








            1







            Try this as it is, seems like there is a some issue with ( and pipe | so I had to escape them.



            Source Code



            #!/bin/bash

            MYSQL=$(grep 'temporary password' /var/log/mysqld.log | awk '{print $11}')
            MYSQL_ROOT_PASSWORD="test@123"

            SECURE_MYSQL=$(expect -c "

            set timeout 10
            spawn mysql_secure_installation

            expect "Enter password for user root:"
            send "$MYSQLr"

            expect "Change the password for root ?(Press y|Y for Yes, any other key for No) :"
            send "yr"

            expect "New password:"
            send "$MYSQL_ROOT_PASSWORDr"

            expect "Re-enter new password:"
            send "$MYSQL_ROOT_PASSWORDr"

            expect "Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :"
            send "yr"

            expect "Remove anonymous users?(Press y|Y for Yes, any other key for No) :"
            send "yr"

            expect "Disallow root login remotely?(Press y|Y for Yes, any other key for No) :"
            send "yr"

            expect "Remove test database and access to it?(Press y|Y for Yes, any other key for No) :"
            send "yr"

            expect "Reload privilege tables now?(Press y|Y for Yes, any other key for No) :"
            send "yr"

            expect eof
            ")

            echo "$SECURE_MYSQL"


            Debug execution

            root@5015a2757ac4:/# bash -x secure.sh
            ++ grep 'temporary password' /var/log/mysqld.log
            ++ awk '{print $11}'
            grep: /var/log/mysqld.log: No such file or directory
            + MYSQL=
            + MYSQL_ROOT_PASSWORD=test@123
            ++ expect -c '

            set timeout 10
            spawn mysql_secure_installation

            expect Enter' password for user 'root:
            send r

            expect Change' the password for root '?(Press' 'y|Y' for Yes, any other key for 'No)' ':
            send yr

            expect New' 'password:
            send test@123r

            expect Re-enter' new 'password:
            send test@123r

            expect Do' you wish to continue with the password 'provided?(Press' 'y|Y' for Yes, any other key for 'No)' ':
            send yr

            expect Remove' anonymous 'users?(Press' 'y|Y' for Yes, any other key for 'No)' ':
            send yr

            expect Disallow' root login 'remotely?(Press' 'y|Y' for Yes, any other key for 'No)' ':
            send yr

            expect Remove' test database and access to 'it?(Press' 'y|Y' for Yes, any other key for 'No)' ':
            send yr

            expect Reload' privilege tables 'now?(Press' 'y|Y' for Yes, any other key for 'No)' ':
            send yr

            expect eof
            '

            couldn't read file "password": no such file or directory
            + SECURE_MYSQL='spawn mysql_secure_installation

            Securing the MySQL server deployment.

            Connecting to MySQL using a blank password.

            VALIDATE PASSWORD PLUGIN can be used to test passwords
            and improve security. It checks the strength of password
            and allows the users to set only those passwords which are
            secure enough. Would you like to setup VALIDATE PASSWORD plugin?

            Press y|Y for Yes, any other key for No: '
            + echo 'spawn mysql_secure_installation

            Securing the MySQL server deployment.

            Connecting to MySQL using a blank password.

            VALIDATE PASSWORD PLUGIN can be used to test passwords
            and improve security. It checks the strength of password
            and allows the users to set only those passwords which are
            secure enough. Would you like to setup VALIDATE PASSWORD plugin?

            Press y|Y for Yes, any other key for No: '
            spawn mysql_secure_installation

            Securing the MySQL server deployment.

            Connecting to MySQL using a blank password.

            VALIDATE PASSWORD PLUGIN can be used to test passwords
            and improve security. It checks the strength of password
            and allows the users to set only those passwords which are
            secure enough. Would you like to setup VALIDATE PASSWORD plugin?

            Press y|Y for Yes, any other key for No:
            root@5015a2757ac4:/#


            And I have verified the login with new password test@123.






            share|improve this answer















            Try this as it is, seems like there is a some issue with ( and pipe | so I had to escape them.



            Source Code



            #!/bin/bash

            MYSQL=$(grep 'temporary password' /var/log/mysqld.log | awk '{print $11}')
            MYSQL_ROOT_PASSWORD="test@123"

            SECURE_MYSQL=$(expect -c "

            set timeout 10
            spawn mysql_secure_installation

            expect "Enter password for user root:"
            send "$MYSQLr"

            expect "Change the password for root ?(Press y|Y for Yes, any other key for No) :"
            send "yr"

            expect "New password:"
            send "$MYSQL_ROOT_PASSWORDr"

            expect "Re-enter new password:"
            send "$MYSQL_ROOT_PASSWORDr"

            expect "Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :"
            send "yr"

            expect "Remove anonymous users?(Press y|Y for Yes, any other key for No) :"
            send "yr"

            expect "Disallow root login remotely?(Press y|Y for Yes, any other key for No) :"
            send "yr"

            expect "Remove test database and access to it?(Press y|Y for Yes, any other key for No) :"
            send "yr"

            expect "Reload privilege tables now?(Press y|Y for Yes, any other key for No) :"
            send "yr"

            expect eof
            ")

            echo "$SECURE_MYSQL"


            Debug execution

            root@5015a2757ac4:/# bash -x secure.sh
            ++ grep 'temporary password' /var/log/mysqld.log
            ++ awk '{print $11}'
            grep: /var/log/mysqld.log: No such file or directory
            + MYSQL=
            + MYSQL_ROOT_PASSWORD=test@123
            ++ expect -c '

            set timeout 10
            spawn mysql_secure_installation

            expect Enter' password for user 'root:
            send r

            expect Change' the password for root '?(Press' 'y|Y' for Yes, any other key for 'No)' ':
            send yr

            expect New' 'password:
            send test@123r

            expect Re-enter' new 'password:
            send test@123r

            expect Do' you wish to continue with the password 'provided?(Press' 'y|Y' for Yes, any other key for 'No)' ':
            send yr

            expect Remove' anonymous 'users?(Press' 'y|Y' for Yes, any other key for 'No)' ':
            send yr

            expect Disallow' root login 'remotely?(Press' 'y|Y' for Yes, any other key for 'No)' ':
            send yr

            expect Remove' test database and access to 'it?(Press' 'y|Y' for Yes, any other key for 'No)' ':
            send yr

            expect Reload' privilege tables 'now?(Press' 'y|Y' for Yes, any other key for 'No)' ':
            send yr

            expect eof
            '

            couldn't read file "password": no such file or directory
            + SECURE_MYSQL='spawn mysql_secure_installation

            Securing the MySQL server deployment.

            Connecting to MySQL using a blank password.

            VALIDATE PASSWORD PLUGIN can be used to test passwords
            and improve security. It checks the strength of password
            and allows the users to set only those passwords which are
            secure enough. Would you like to setup VALIDATE PASSWORD plugin?

            Press y|Y for Yes, any other key for No: '
            + echo 'spawn mysql_secure_installation

            Securing the MySQL server deployment.

            Connecting to MySQL using a blank password.

            VALIDATE PASSWORD PLUGIN can be used to test passwords
            and improve security. It checks the strength of password
            and allows the users to set only those passwords which are
            secure enough. Would you like to setup VALIDATE PASSWORD plugin?

            Press y|Y for Yes, any other key for No: '
            spawn mysql_secure_installation

            Securing the MySQL server deployment.

            Connecting to MySQL using a blank password.

            VALIDATE PASSWORD PLUGIN can be used to test passwords
            and improve security. It checks the strength of password
            and allows the users to set only those passwords which are
            secure enough. Would you like to setup VALIDATE PASSWORD plugin?

            Press y|Y for Yes, any other key for No:
            root@5015a2757ac4:/#


            And I have verified the login with new password test@123.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 10 at 7:29

























            answered Jan 10 at 7:14









            rɑːdʒɑrɑːdʒɑ

            57.5k85217301




            57.5k85217301













            • I am getting error ./secure.sh: line 39: expect: command not found

              – Janith
              Jan 10 at 8:37











            • @Janith, that means you have not installed expect command. try apt-get install expect -y and try again.

              – rɑːdʒɑ
              Jan 10 at 8:59











            • thank you for your support. Still getting the error couldn't read file "password": no such file or directory spawn mysql_secure_installation Securing the MySQL server deployment. Enter password for user root:

              – Janith
              Jan 10 at 9:13













            • @Janith, Are you sure that you exactly executing as given ? cause Its worked for me.

              – rɑːdʒɑ
              Jan 10 at 9:27











            • Yes I copied exact script which you are given

              – Janith
              Jan 10 at 9:44



















            • I am getting error ./secure.sh: line 39: expect: command not found

              – Janith
              Jan 10 at 8:37











            • @Janith, that means you have not installed expect command. try apt-get install expect -y and try again.

              – rɑːdʒɑ
              Jan 10 at 8:59











            • thank you for your support. Still getting the error couldn't read file "password": no such file or directory spawn mysql_secure_installation Securing the MySQL server deployment. Enter password for user root:

              – Janith
              Jan 10 at 9:13













            • @Janith, Are you sure that you exactly executing as given ? cause Its worked for me.

              – rɑːdʒɑ
              Jan 10 at 9:27











            • Yes I copied exact script which you are given

              – Janith
              Jan 10 at 9:44

















            I am getting error ./secure.sh: line 39: expect: command not found

            – Janith
            Jan 10 at 8:37





            I am getting error ./secure.sh: line 39: expect: command not found

            – Janith
            Jan 10 at 8:37













            @Janith, that means you have not installed expect command. try apt-get install expect -y and try again.

            – rɑːdʒɑ
            Jan 10 at 8:59





            @Janith, that means you have not installed expect command. try apt-get install expect -y and try again.

            – rɑːdʒɑ
            Jan 10 at 8:59













            thank you for your support. Still getting the error couldn't read file "password": no such file or directory spawn mysql_secure_installation Securing the MySQL server deployment. Enter password for user root:

            – Janith
            Jan 10 at 9:13







            thank you for your support. Still getting the error couldn't read file "password": no such file or directory spawn mysql_secure_installation Securing the MySQL server deployment. Enter password for user root:

            – Janith
            Jan 10 at 9:13















            @Janith, Are you sure that you exactly executing as given ? cause Its worked for me.

            – rɑːdʒɑ
            Jan 10 at 9:27





            @Janith, Are you sure that you exactly executing as given ? cause Its worked for me.

            – rɑːdʒɑ
            Jan 10 at 9:27













            Yes I copied exact script which you are given

            – Janith
            Jan 10 at 9:44





            Yes I copied exact script which you are given

            – Janith
            Jan 10 at 9:44













            0














            Below script worked for me.



            #!/bin/bash

            MYSQL_ROOT_PASSWORD='Password@123'
            MYSQL=$(grep 'temporary password' /var/log/mysqld.log | awk '{print $11}')

            SECURE_MYSQL=$(expect -c "

            set timeout 10
            spawn mysql_secure_installation

            expect "Enter password for user root:"
            send "$MYSQLr"
            expect "New password:"
            send "$MYSQL_ROOT_PASSWORDr"
            expect "Re-enter new password:"
            send "$MYSQL_ROOT_PASSWORDr"
            expect "Change the password for root ? ((Press y|Y for Yes, any other key for No) :"
            send "yr"
            send "$MYSQLr"
            expect "New password:"
            send "$MYSQL_ROOT_PASSWORDr"
            expect "Re-enter new password:"
            send "$MYSQL_ROOT_PASSWORDr"
            expect "Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :"
            send "yr"
            expect "Remove anonymous users?(Press y|Y for Yes, any other key for No) :"
            send "yr"
            expect "Disallow root login remotely?(Press y|Y for Yes, any other key for No) :"
            send "nr"
            expect "Remove test database and access to it?(Press y|Y for Yes, any other key for No) :"
            send "yr"
            expect "Reload privilege tables now?(Press y|Y for Yes, any other key for No) :"
            send "yr"
            expect eof
            ")

            echo $SECURE_MYSQL





            share|improve this answer




























              0














              Below script worked for me.



              #!/bin/bash

              MYSQL_ROOT_PASSWORD='Password@123'
              MYSQL=$(grep 'temporary password' /var/log/mysqld.log | awk '{print $11}')

              SECURE_MYSQL=$(expect -c "

              set timeout 10
              spawn mysql_secure_installation

              expect "Enter password for user root:"
              send "$MYSQLr"
              expect "New password:"
              send "$MYSQL_ROOT_PASSWORDr"
              expect "Re-enter new password:"
              send "$MYSQL_ROOT_PASSWORDr"
              expect "Change the password for root ? ((Press y|Y for Yes, any other key for No) :"
              send "yr"
              send "$MYSQLr"
              expect "New password:"
              send "$MYSQL_ROOT_PASSWORDr"
              expect "Re-enter new password:"
              send "$MYSQL_ROOT_PASSWORDr"
              expect "Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :"
              send "yr"
              expect "Remove anonymous users?(Press y|Y for Yes, any other key for No) :"
              send "yr"
              expect "Disallow root login remotely?(Press y|Y for Yes, any other key for No) :"
              send "nr"
              expect "Remove test database and access to it?(Press y|Y for Yes, any other key for No) :"
              send "yr"
              expect "Reload privilege tables now?(Press y|Y for Yes, any other key for No) :"
              send "yr"
              expect eof
              ")

              echo $SECURE_MYSQL





              share|improve this answer


























                0












                0








                0







                Below script worked for me.



                #!/bin/bash

                MYSQL_ROOT_PASSWORD='Password@123'
                MYSQL=$(grep 'temporary password' /var/log/mysqld.log | awk '{print $11}')

                SECURE_MYSQL=$(expect -c "

                set timeout 10
                spawn mysql_secure_installation

                expect "Enter password for user root:"
                send "$MYSQLr"
                expect "New password:"
                send "$MYSQL_ROOT_PASSWORDr"
                expect "Re-enter new password:"
                send "$MYSQL_ROOT_PASSWORDr"
                expect "Change the password for root ? ((Press y|Y for Yes, any other key for No) :"
                send "yr"
                send "$MYSQLr"
                expect "New password:"
                send "$MYSQL_ROOT_PASSWORDr"
                expect "Re-enter new password:"
                send "$MYSQL_ROOT_PASSWORDr"
                expect "Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :"
                send "yr"
                expect "Remove anonymous users?(Press y|Y for Yes, any other key for No) :"
                send "yr"
                expect "Disallow root login remotely?(Press y|Y for Yes, any other key for No) :"
                send "nr"
                expect "Remove test database and access to it?(Press y|Y for Yes, any other key for No) :"
                send "yr"
                expect "Reload privilege tables now?(Press y|Y for Yes, any other key for No) :"
                send "yr"
                expect eof
                ")

                echo $SECURE_MYSQL





                share|improve this answer













                Below script worked for me.



                #!/bin/bash

                MYSQL_ROOT_PASSWORD='Password@123'
                MYSQL=$(grep 'temporary password' /var/log/mysqld.log | awk '{print $11}')

                SECURE_MYSQL=$(expect -c "

                set timeout 10
                spawn mysql_secure_installation

                expect "Enter password for user root:"
                send "$MYSQLr"
                expect "New password:"
                send "$MYSQL_ROOT_PASSWORDr"
                expect "Re-enter new password:"
                send "$MYSQL_ROOT_PASSWORDr"
                expect "Change the password for root ? ((Press y|Y for Yes, any other key for No) :"
                send "yr"
                send "$MYSQLr"
                expect "New password:"
                send "$MYSQL_ROOT_PASSWORDr"
                expect "Re-enter new password:"
                send "$MYSQL_ROOT_PASSWORDr"
                expect "Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :"
                send "yr"
                expect "Remove anonymous users?(Press y|Y for Yes, any other key for No) :"
                send "yr"
                expect "Disallow root login remotely?(Press y|Y for Yes, any other key for No) :"
                send "nr"
                expect "Remove test database and access to it?(Press y|Y for Yes, any other key for No) :"
                send "yr"
                expect "Reload privilege tables now?(Press y|Y for Yes, any other key for No) :"
                send "yr"
                expect eof
                ")

                echo $SECURE_MYSQL






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 10 at 11:21









                JanithJanith

                748




                748






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Ask Ubuntu!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1108475%2fautomate-the-mysql-secure-installation-using-shell-script%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Human spaceflight

                    Can not write log (Is /dev/pts mounted?) - openpty in Ubuntu-on-Windows?

                    張江高科駅