title: How to automate mysql_secure_installation in Chef
body: <p>Chef mysql recipe- in-order to setup a permanent password for root user in mysql, I did find a process which uses "bash" resource in recipe for running a bash script which automates all the steps which pop-up in the process. But after running the convergence it errors out </p>
  
  <blockquote>
    <p>"ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)". </p>
  </blockquote>
  
  <p>I understand it is because initially the temporary password is generated in mysqld.log files and I need to provide this temp passwd for running the mysql_secure_installation. But I couldn't find a way to include a step in the script where it can bring this temp passwd from the logs and use it in the script. Below is the script I'm currently running in the recipe. </p>
  
  <pre><code>root_password = node.set['mysql_user']['root']['password']
  bash "mysql_secure_installation" do
    code &lt;&lt;-EOH
      mysql -u root -e "DELETE FROM mysql.user WHERE User='';"
      mysql -u root -e "DROP DATABASE test;"
      mysql -u root -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';"
      mysql -u root -e "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');"
      mysql -u root -e "SET PASSWORD FOR 'root'@'localhost' = PASSWORD('#{root_password}');" -D mysql
      mysql -u root -p#{root_password} -e "SET PASSWORD FOR 'root'@'127.0.0.1' = PASSWORD('#{root_password}');" -D mysql
      mysql -u root -p#{root_password} -e "SET PASSWORD FOR 'root'@'::1' = PASSWORD('#{root_password}');" -D mysql
      mysql -u root -p#{root_password} -e "FLUSH PRIVILEGES;"
    EOH
  end
  </code></pre>