How do I write a script to install MySQL server on Ubuntu?
sudo apt-get install mysql
will install, but it will also ask for a password to be entered in the console.
How do I do this in a non-interactive way? That is, write a script that can provide the password?
#!/bin/bash
sudo apt-get install mysql # To install MySQL server
# How to write script for assigning password to MySQL root user
# End
This should do the trick
export DEBIAN_FRONTEND=noninteractive
sudo -E apt-get -q -y install mysql-server
Of course, it leaves you with a blank root password - so you'll want to run something like
mysqladmin -u root password mysecretpasswordgoeshere
Afterwards to add a password to the account.
Use:
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server
sudo mysql -h127.0.0.1 -P3306 -uroot -e"UPDATE mysql.user SET password = PASSWORD('yourpassword') WHERE user = 'root'"
Another way to make it work:
echo "mysql-server-5.5 mysql-server/root_password password root" | debconf-set-selections
echo "mysql-server-5.5 mysql-server/root_password_again password root" | debconf-set-selections
apt-get -y install mysql-server-5.5
Note that this simply sets the password to "root". I could not get it to set a blank password using simple quotes ''
, but this solution was sufficient for me.
Based on a solution here.
Source: Stackoverflow.com