23

I need to write a script that will build my server from a fresh Ubuntu server install. Among things like Apache and PHP it needs to install MySQL. The only problem here is that when I install MySQL with apt-get, at some point the installation will bring up a dialog that allows me to type my root password. I.e., human interaction is required.

How can I bypass this screen during installation and avoid human interaction while still using apt-get to install MySQL?

1

5 Answers 5

31

You need to preseed the debconf database. debconf needs to be installed first before you try this.

The version of mysql and ubuntu could change the line:

echo mysql-server mysql-server/root_password select PASSWORD | debconf-set-selections
echo mysql-server mysql-server/root_password_again select PASSWORD | debconf-set-selections

For example you made need this instead:

echo mysql-server-5.0 mysql-server/root_password password PASSWORD | debconf-set-selections
echo mysql-server-5.0 mysql-server/root_password_again password PASSWORD | debconf-set-selections
3
  • Yes, it works, but after installed I can't login into mysql(mysql -u root, it displays permission denied).
    – Samnang
    Oct 4, 2010 at 9:20
  • 3
    How about mysql -u root -p
    – PhilT
    Apr 16, 2012 at 20:47
  • Also need 'seen' flag as show below to disable showing the password prompt or you'll be prompted for password again. Nov 23, 2014 at 19:20
6

While FAI and similar systems are helpful in an enterprise environment, (and he should know about them), they're unfortunately not a panacea.

For instance, what if he is working on remote machines, perhaps hosted, rented servers, colocated servers without a trusted or dedicated network, or a cloud environment like EC2? No sort of PXE netboot will work there. Puppet and Chef seem to be more general purpose tools that could help him, however.

cat << EOF | DEBIAN_FRONTEND=noninteractive DEBCONF_DB_FALLBACK=Pipe /usr/bin/apt-get install somepackage

Name: ...
Template: ...
Value: ...
Owners: ...
Variables:
   ....
   ....
EOF

You can get the correct values from the debconf DB on a system that's already been set up. This technique lets you stick the whole thing in a single shell script. For example, from /var/cache/debconf/passwords.dat (note the ability to work with both 5.0 on Debian and 5.1 and Ubuntu, extra entries don't hurt anything):

cat << EOF | DEBIAN_FRONTEND=noninteractive DEBCONF_DB_FALLBACK=Pipe /usr/bin/apt-get -qq -y install mysql-server
Name: mysql-server/root_password
Template: mysql-server/root_password
Value: YOURPASSWORD
Owners: mysql-server-5.1
Flags: seen

Name: mysql-server/root_password_again
Template: mysql-server/root_password_again
Value: YOURPASSWORD
Owners: mysql-server-5.1
Flags: seen

Name: mysql-server/root_password
Template: mysql-server/root_password
Value: YOURPASSWORD
Owners: mysql-server-5.0
Flags: seen

Name: mysql-server/root_password_again
Template: mysql-server/root_password_again
Value: YOURPASSWORD
Owners: mysql-server-5.0
Flags: seen

EOF

Another method, perhaps easier: (gather answers from debconf-get-selections or debconf-show from the debconf-utils package):

echo "Installing MySQL 5.0.."
sudo apt-get install -qqy debconf-utils
cat << EOF | debconf-set-selections
mysql-server-5.0 mysql-server/root_password password YOURPASSWORD
mysql-server-5.0 mysql-server/root_password_again password YOURPASSWORD
mysql-server-5.0 mysql-server/root_password seen true
mysql-server-5.0 mysql-server/root_password_again seen true
EOF
/usr/bin/apt-get -y install mysql-server-5.0 mysql-server

After you run debconf-set-selections, check your answers: cat /var/cache/debconf/passwords.dat

And, while testing, be sure to remove and purge all databases (especially the mysql database, where this is stored) and fix your config DB in case they got corrupted:

apt-get --purge remove mysql-server*; /usr/share/debconf/fix_db.pl 

A few tips with this: 1) You MUST have the seen flag and the _again password. 2) You MUST not put quotes around the password. It will go into a MySQL query unquoted, so you have to quote it yourself if needed. That also means no spaces (I think) 3) If you have trouble, do the preseeding by hand and then run the MySQL install directly yourself (in a normal terminal/tty) and see what debconf tells you. It will tell you warnings and errors there.

3

If you have multiple servers you need to rebuild, I highly recommend you look at something like Puppet instead of writing a shell script. Puppet has a descriptive language, so you define how you want the system to look, not how to get it there. The package type has a responsefile option that allows you to define the debconf answers in Steven's answer.

1

May I also suggest you automate the server staging process from the raw iron?

FAI nowadays has profiles to install most major Debian-based systems, including Ubuntu, from the network. This of course is just an extension to the puppet answer, which is an extension to the debconf answer.

The combination of staging from FAI, then customizing with puppet into server roles (where puppet will use the proper tools for your system, like debconf for Debian (-likes) rather automatically), is rather popular these days and it shouldn't be too hard to find examples that match what you want to do rather closely.

A major added benefit of having a staging/configuration server is that it's also a logical place to keep your configuration version-controlled, e.g. with git (which has the benefit of naming repository states by checksum, so that you're safe against corruption as long as you note the right checksums down), for a truly clean and reproducible process.

1

See also:

https://stackoverflow.com/questions/1202347/simple-bash-script-to-install-mysql-need-some-help

1
  • this would have been better suited as a comment. I have flagged for moderator attention, and they should be able to make it into a comment. Feb 24, 2011 at 18:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .