Я пытаюсь автоматизировать mysql, безопасная установка с помощью Linux окружает scriping. Я имею ниже кода, полученного от 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) :"'
Я попытался выяснить ошибку, но никакой успех.
Попробуйте это, как это, кажется, что существует некоторая проблема с (
и канал |
таким образом, я должен был выйти из них.
#!/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
.
Ниже сценария, работавшего для меня.
#!/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