views:

1059

answers:

6

I am writing a simple bash script to install MySQL on Ubuntu.

#!/bin/bash
apt-get update

# Install MySQL5 
aptitude -y install mysql-server mysql-client libmysqlclient15-dev

However MySQL prompts for a password and confirmation. How do I pass along a root password. Is there an echo I can use?

Thank you,

Cei

+2  A: 

look into using expect

It can be used to automate most interactive sessions, although I wouldn't use a root password

ennuikiller
+1  A: 

Expect is probably overkill. Look on one of the Debian or Ubuntu admin forums -- things like FAI et al have long used preseeding for debconf questions. You should be able to use that here too.

Lastly, you could probably also use apt-get itself or other frontends.

Dirk Eddelbuettel
+4  A: 

Thank you for the tip on expect. I couldn't find anything by searching Ubuntu admin forums so I went with expect. As you can see by the timestamp of this post, it took me 3 hours to get it working. Here is the code, I hope it can help someone:

#!/bin/bash
apt-get update
apt-get install expect

VAR=$(expect -c "
spawn apt-get -y install mysql-server
expect "New password for the MySQL \"root\" user:"
send "PasswordHere\r"
expect "Repeat password for the MySQL \"root\" user:"
send "PasswordHere\r"
expect eof
")

echo "$VAR"

apt-get -y install mysql-client libmysqlclient15-dev   

#For some reason important to restart - otherwise possible errors

/etc/init.d/mysql stop
/etc/init.d/mysql start
A: 

Use a Makefile:

$: cat Makefile

update:
    sudo apt-get update

install_mysql: update
    sudo aptitude -y install mysql-server mysql-client libmysqlclient15-dev

install_anythingelse:
    sudo aptitude -y ....

configure_mysql:
    mysql-admin .... (configure mysql)
    /etc/init.d/mysql stop
    /etc/init.d/mysql start

all: update install_mysql configure_mysql install_anythingelse

To install mysql, type 'make all', or just 'make install_mysql'. You wil be prompted to insert any password it needs.

dalloliogm
Isn't the point to get around the password prompt?
Sam Hoice
+1  A: 

Hi!

The easiest way to do this is to use the DEBIAN_FRONTEND environment variable and the aptitude -q and -y flags:

sudo DEBIAN_FRONTEND=noninteractive aptitude install -q -y mysql-server

Or more generically, assuming sudo password has been catered for some-how:

#!/bin/bash
INSTALLER_LOG=/var/log/my-installer.log

installnoninteractive(){
  sudo bash -c "DEBIAN_FRONTEND=noninteractive aptitude install -q -y $* >> $INSTALLER_LOG"
}

installnoninteractive mysql-server
cstar
+2  A: 

This is an excerpt from my setup script for new servers. You should be able to copy it word-for-word except for the password.

You'll need to run this using sudo if you're not already root.

#!/bin/bash
export DEBIAN_FRONTEND=noninteractive
apt-get -q -y install mysql-server
echo "Give mysql server time to start up before we try to set a password..."
sleep 5
mysql -uroot -e <<EOSQL "UPDATE mysql.user SET Password=PASSWORD('yourpasswordhere') WHERE User='root'; FLUSH PRIVILEGES;"
EOSQL
echo "Done setting mysql password."

Other answers have used the -y which makes apt-get always answer yes to questions. The -q hides some progress indicators so you can send the output to a log. You could also use -qq, which automatically gives you a -y. This is in the man page for apt-get.

The <<EOSQL is a bash heredoc syntax for readability.

I got the heredoc part of this solution from this guy: http://padwasabimasala.posterous.com/non-interactive-scripted-mysql-install-on-ubu

The thing to remember with the heredoc is that whitespace before the closing string breaks it. So don't indent that line. Here is a page about the heredoc syntax: http://tldp.org/LDP/abs/html/here-docs.html

Mnebuerquo