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