use*_*013 7 mysql bash centos6
我正在编写一个脚本,我想在其中启动 mysql_secure_installation脚本并在每个提示处提供适当的答案。我尝试使用这样的回声:
echo -e "\n\n$db_pass\n$db_pass\n\n\n\n\n" | /usr/bin/mysql_secure_installation
Run Code Online (Sandbox Code Playgroud)
它不完全起作用。它似乎正确回答了最后 5 个问题,但没有正确回答前 3 个问题(这是在一个CentOS 6.5盒子上)。这是输出:
In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
stty: standard input: Invalid argument
Enter current password for root (enter for none):
stty: standard input: Invalid argument
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
Set root password? [Y/n] ... skipping.
Run Code Online (Sandbox Code Playgroud)
当被要求输入 root 密码时,它应该提交一个返回值。然后它应该在被要求设置 root 密码时提交另一个返回。但是,正如您从没有发生的输出中看到的那样。
我需要做什么才能让它按预期工作?
你不能那样做。如果您想与脚本进行交互,您必须使用诸如expect(1) 之类的东西——SF 和更广泛的互联网上有关于如何执行此操作的示例。
您可以效仿许多的mysql_secure_Installation与
/usr/bin/mysql -uroot -e "DELETE FROM mysql.user WHERE User=\'\'; DELETE FROM mysql.user WHERE User=\'root\' AND Host NOT IN (\'localhost\', \'127.0.0.1\', \'::1\'); DROP DATABASE IF EXISTS test; FLUSH PRIVILEGES;"
Run Code Online (Sandbox Code Playgroud)
之后你需要做的就是设置一个root密码。
MySQL 开发站点上的此错误报告解决了mysql_secure_installation需要交互式用户会话的问题。
那里有一些很好的讨论,其中有一些技巧和示例可以expect用作解决方法。Daniël van Eeden 提供了以下expect脚本,似乎可以解决您希望以非交互方式运行脚本(即:无需用户直接交互)的最佳建议:
#!/usr/bin/expect --
spawn /usr/local/mysql/bin/mysql_secure_installation
expect "Enter current password for root (enter for none):"
send "\r"
expect "Set root password?"
send "y\r"
expect "New password:"
send "password\r"
expect "Re-enter new password:"
send "password\r"
expect "Remove anonymous users?"
send "y\r"
expect "Disallow root login remotely?"
send "y\r"
expect "Remove test database and access to it?"
send "y\r"
expect "Reload privilege tables now?"
send "y\r"
puts "Ended expect script."
Run Code Online (Sandbox Code Playgroud)
此外,似乎在 MySQL 5.6.8 中解决了具有非安全内容的基本 MySQL 安装的首要问题,但仅适用于通过 RPM 或使用该--random-password选项进行的源代码安装的新安装:
新的 RPM 安装操作(不是升级) 使用 --random-passwords 选项调用mysql_install_db。因此,从此版本开始安装的 RPM 将保护其 root 帐户,并且没有匿名用户帐户。(使用 RPMs for Unbreakable Linux Network 的安装操作不受影响,因为它们不使用 mysql_install_db。)
对于使用二进制 .tar.gz 发行版或源发行版的安装操作,您可以使用 --random-passwords 选项手动调用mysql_install_db以使您的 MySQL 安装更安全。建议这样做,特别是对于具有敏感数据的站点。
| 归档时间: |
|
| 查看次数: |
5897 次 |
| 最近记录: |