简体   繁体   中英

vagrant ssh provisioning - mysql password

I'm building a Vagrant box (CentOS 6.4) using SSH provisioning.

All's running fine, LAMP components installed and started but I've reached the step where I should secure MySql (set mysql password, etc).

There's mysql_secure_installation that could be run, but it doesn't work in a non-interactive mode.

I could run

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

but if I provision the same box multiple times, Mysql will accept a new password the first time, but then will complain.

Is there an elegant way of securing MySql, automatically, at provisioning time? (I'm not using Chef/Puppet, just simple SSH provisioning)

A possible way to go could be:

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

Explanation: the script first try to connect to mysql with the new password, and only if it fails (ie the password has not yet been set) executes the command to set it up.

In your Vagrantfile add the line as follows:

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

end
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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM