通过 shell 脚本使用 echo 命令自动化 mysql_secure_installation

新手上路,请多包涵

我正在尝试使用自动响应来自动化 mysql_secure_installation 脚本。我的代码如下:

 echo "& y y abc abc y y y y" | ./usr/bin/mysql_secure_installation

我正在自动化的实际问题如下:

 Enter current password for root (enter for none): <enter>
Set root password? [Y/n] y
New password: abc
Re-enter new password: abc
Remove anonymous users? [Y/n] y
Disallow root login remotely? [Y/n] y
Remove test database and access to it? [Y/n] y
Reload privilege tables now? [Y/n] y

但它给了我一个错误“对不起,你不能在这里使用空密码”,但在屏幕上我曾经按第一个问题的返回键。

原文由 cucucool 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 995
2 个回答

我偶然发现了这个问题,但决定通过 Bash 脚本手动运行查询:

 #!/bin/bash

# Make sure that NOBODY can access the server without a password
mysql -e "UPDATE mysql.user SET Password = PASSWORD('CHANGEME') WHERE User = 'root'"
# Kill the anonymous users
mysql -e "DROP USER ''@'localhost'"
# Because our hostname varies we'll use some Bash magic here.
mysql -e "DROP USER ''@'$(hostname)'"
# Kill off the demo database
mysql -e "DROP DATABASE test"
# Make our changes take effect
mysql -e "FLUSH PRIVILEGES"
# Any subsequent tries to run queries this way will get access denied because lack of usr/pwd param

原文由 Sleavely 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏