Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

6 Answers

up vote 25 down vote accepted

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
share|improve this answer
    
Yes, it works, but after installed I can't login into mysql(mysql -u root, it displays permission denied). –  Samnang Oct 4 '10 at 9:20
3  
How about mysql -u root -p –  PhilT Apr 16 '12 at 20:47
add comment

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.

share|improve this answer
add comment

In addition to Puppet as mentioned, there's also Chef. Opscode provides a cookbook for configuring MySQL, with a user tunable server root password which is by default randomly generated. The recipe is already set up to use a preseed package installation. There's also a cookbook for setting up a mysql database server for a Rails application, which can be modified, of course.

Disclosure: I work for Opscode.

share|improve this answer
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

See also:

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

share|improve this answer
    
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. –  Ben Pilbrow Feb 24 '11 at 18:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.