我正在编写一个脚本,我想要启动mysql_secure_installation脚本并在每个提示符处提供相应的答案。 我尝试使用这样的回声:
echo -e "\n\n$db_pass\n$db_pass\n\n\n\n\n" | /usr/bin/mysql_secure_installation
它不完全工作。 它似乎正确回答了最后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.
当询问root密码时应该提交一个回报。 然后当被要求设置根密码时,它应该提交另一个返回。 然而,正如你从输出中看到的那样,没有发生。
为了达到预期的效果,我需要做些什么?
MySQL开发站点上的这个错误报告解决了需要交互式用户会话的mysql_secure_installation问题。
有一些很好的讨论,有一些提示和例子,使用expect作为一种解决方法。 最好的build议,似乎解决您的愿望,非交互式(即:没有直接的用户交互)运行脚本是丹尼尔范伊登提供以下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."
另外,似乎基本的MySQL安装具有非安全的东西的总体问题似乎是在MySQL 5.6.8中解决,但只适用于通过RPM安装的新安装或使用--random-password选项进行的源代码安装:
新的RPM安装操作(不是升级)使用–random-passwords选项调用mysql_install_db 。 因此,从此版本开始的RPM安装将保护其根帐户,并且不会有匿名用户帐户。 (对于Unbreakable Linux Network,使用RPM的安装操作不受影响,因为它们不使用mysql_install_db 。)
对于使用二进制.tar.gz分发或源代码分发的安装操作,您可以手动调用mysql_install_db和–random-passwords选项,以使MySQL安装更加安全。 build议这样做,特别是对于敏感数据的站点。
你不能这样做。 如果你想和一个脚本进行交互,你必须使用像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;"
所有你需要做的之后是设置一个root密码。