mysql - 通过 shell 脚本使用 echo 命令自动化 mysql_secure_inst

我正在尝试通过自动响应来自动化 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

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

最佳答案

我偶然发现了这个问题,但决定通过 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

关于mysql - 通过 shell 脚本使用 echo 命令自动化 mysql_secure_installation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24270733/

相关文章:

linux - 在 Linux 中将多个 JPG 合并为单个 PDF

python - 如何跳过循环中的迭代?

linux - 在 shell 脚本中缩进多行输出

c - Linux共享内存: shmget() vs mmap()?

c - Linux 中的 getch() 和 getche() 等价于什么?

linux - objdump 如何发出 intel 语法

python - 使用 Pandas 计算每个组的唯一值

python - 在 Python 中查找数字的所有因数的最有效方法是什么?

python - 使用 python 和 BeautifulSoup 从网页中检索链接

android - 如何在 Ubuntu 上安装 Android SDK?