使用 shell 脚本自动执行 mysql 安全安装

使用 shell 脚本自动执行 mysql 安全安装

我正在尝试使用 Linux Shell 脚本自动执行 mysql 安全安装。下面是我从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 "$MYSQL\r"

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

  expect "New password:"
  send "$MYSQL_ROOT_PASSWORD\r"

  expect "Re-enter new password:"
  send "$MYSQL_ROOT_PASSWORD\r"

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

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

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

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

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

  expect eof
  "))

  echo "$SECURE_MYSQL"

但我收到了错误

./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) :"'

我试图找出错误,但没有成功。

答案1

(按原样尝试一下,好像管道存在一些问题,|所以我不得不逃避它们。

源代码

#!/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 "$MYSQL\r"

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

      expect "New password:"
      send "$MYSQL_ROOT_PASSWORD\r"

      expect "Re-enter new password:"
      send "$MYSQL_ROOT_PASSWORD\r"

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

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

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

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

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

      expect eof
      ")

      echo "$SECURE_MYSQL"
调试执行
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:/#

并且我已使用新密码验证了登录test@123

答案2

下面的脚本对我有用。

#!/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 \"$MYSQL\r\"
  expect \"New password:\"
  send \"$MYSQL_ROOT_PASSWORD\r\"
  expect \"Re-enter new password:\"
  send \"$MYSQL_ROOT_PASSWORD\r\"
  expect \"Change the password for root ?\ ((Press y\|Y for Yes, any other key for No) :\"
  send \"y\r\"
  send \"$MYSQL\r\"
  expect \"New password:\"
  send \"$MYSQL_ROOT_PASSWORD\r\"
  expect \"Re-enter new password:\"
  send \"$MYSQL_ROOT_PASSWORD\r\"
  expect \"Do you wish to continue with the password provided?\(Press y\|Y for Yes, any other key for No) :\"
  send \"y\r\"
  expect \"Remove anonymous users?\(Press y\|Y for Yes, any other key for No) :\"
  send \"y\r\"
  expect \"Disallow root login remotely?\(Press y\|Y for Yes, any other key for No) :\"
  send \"n\r\"
  expect \"Remove test database and access to it?\(Press y\|Y for Yes, any other key for No) :\"
  send \"y\r\"
  expect \"Reload privilege tables now?\(Press y\|Y for Yes, any other key for No) :\"
  send \"y\r\"
  expect eof
  ")

  echo $SECURE_MYSQL

相关内容