Hands-off Scripted Install of MySQL 5.7 on Linux I'm a long time user of MySQL, and have historically used Puppet or Ansible to install it onto boxes. I can automatically remove the test accounts/db, set a root password, add some application and monitoring users and databases and then hand the system over for application deployments, knowing everything's setup and ready to go for them.

Since MySQL 5.7, I'm not sure how to approach this. On installation it generates a random root password which it writes into a log file. Obviously, getting that out and working with the restricted account to get it set to the required password is difficult with a script (or config management system).

I've had a go at hacking the mysqld_pre_systemd script (on Centos 7) and have that create the data files, then start up mysql with a --init-file option, which I point to a bit of SQL that sets the password as I want. Once it's running, I then connect to it and run 'SHUTDOWN' to stop it, then let systemd carry on as it was to start it up normally.

This all seems like a crazy amount of work (although quite possible with Ansible etc). As I've now got to get it going on Centos 6, is there a better way?

One thought: Is there a way to specify the root password before installation?
This isn't much of a solution, but it's possible to put the data files in place on the system before installing the RPM packages.

That is, on a dev system, install MySQL and set the root password as you want it. Shutdown Mysql and make a copy of the files/directories in /var/lib/mysql (maybe put them in a tar file or something).

On a new system, before installing the MySQL RPM packages, take a look and see if /var/lib/mysql exists - if not, create it and untar your files into it. When the RPM installs it won't overwrite those files, and so will start up with your previously set root password.

This seems like quite a cheesy option, and probably won't work when versions of the RPMs change (even dot release changes might break because they'll need an upgrade process on the archived data files). However, it's a possible way forward.

Another (less than ideal) way forward is something like this:


Check to see if /var/lib/mysqld exists. Remember this.
Install Mysql RPMs (the post-installs automatically create /var/lib/mysql if it doesn't exist - you can't really stop this from happening)
If (1) was "doesn't exist":


Ensure Mysql is stopped (the RPMs don't start it, but just be sure)
Delete /var/lib/mysql/*
run /usr/sbin/mysqld --initialize-insecure --datadir="/var/lib/mysql" --user=mysql (which is what the init.d script does, if /var/lib/mysql is empty, except we're using 'insecure' which means no root password is generated)
Start mysql
Use mysql client to log on with empty password and set the root password to whatever you want.
I'm really not a fan of using Ansible or whatever to delete database data files. Even though this method has suitable checks to make sure it doesn't delete a live database, it still seems like you're creating a dangerous situation here. However, it's another possible approach.



I guess the last approach is to petition Oracle to allow setting a root password during installation (maybe via an environment variable that the RPM post-install picks up, or a magic file somewhere that contains the password that the post-install looks for. That would make life so much easier ;-)