1

我正在使用 SSH 配置构建一个 Vagrant 盒子(CentOS 6.4)。

一切运行良好,LAMP 组件已安装并启动,但我已经到了应该保护 MySql 的步骤(设置 mysql 密码等)。

有 mysql_secure_installation 可以运行,但它不能在非交互模式下工作。

我可以跑

 /usr/bin/mysqladmin -u root password 'newpassword' 

但是如果我多次提供同一个框,Mysql 将第一次接受新密码,但随后会抱怨。

是否有一种优雅的方法可以在配置时自动保护 MySql?(我没有使用 Chef/Puppet,只是简单的 SSH 配置)

4

3 回答 3

5

一种可能的方法是:

/usr/bin/mysql -uroot -pnewpassword -e 'SELECT CURDATE();' || /usr/bin/mysqladmin -u root password 'newpassword'

说明:脚本首先尝试使用新密码连接mysql,只有在失败(即密码尚未设置)时才执行命令设置。

于 2013-09-16T10:16:02.277 回答
0

在您Vagrantfile添加如下行:

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
   <other code>
   ...
    config.vm.provision "shell", 
       inline: "mysqladmin -uroot -pcurrentpass password newpassword"

end
于 2016-01-28T07:19:36.027 回答
-1
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password password root'
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password root'
于 2014-01-17T05:39:11.793 回答