2

I'm trying to install MySQL on Ubuntu Natty from a shell script. However, I keep running into one major issue: when I try to define the password outside of the shell script.

Below is the code to my shell script (which I have saved in /etc/init.d/install_mysql:

export DEBIAN_FRONTEND=noninteractive
echo mysql-server-5.1 mysql-server/root_password password $dbpass | debconf-set-selections
echo mysql-server-5.1 mysql-server/root_password_again password $dbpass | debconf-set-selections
apt-get -y install mysql-server

So what I enter in the terminal is:

dbpass="mysqlpass"
chmod +x /etc/init.d/install_mysql
/etc/init.d/install_mysql

MySQL installs, but it installs without a password, so I can just do something like mysql -uroot to access mysql (which I don't want).

The funny thing is if I put the password in the shell script as regular text, it works ok. So if I my install script is as follows, everything works (i.e. I must specify a password to access mysql):

export DEBIAN_FRONTEND=noninteractive
echo mysql-server-5.1 mysql-server/root_password password mysqlpass | debconf-set-selections
echo mysql-server-5.1 mysql-server/root_password_again password mysqlpass | debconf-set-selections
apt-get -y install mysql-server

Is there a way I can use a shell script variable to define my password in the shell script, instead of entering the password literally?!

Thanks in advance.

EDIT:

I just edited the variable declaration $dbpass="mysqlpass" to dbpass="mysqlpass". It was a typo.

the paul
  • 8,972
  • 1
  • 36
  • 53
ObiHill
  • 11,448
  • 20
  • 86
  • 135

2 Answers2

1
$dbpass="mysqlpass"

Yeah, this is several types of wrong. Variable names don't start with $, and variables won't be passed to the environment of subprocesses unless exported.

dbpass="mysqlpass"
export dbpass

Note that environment variables are not considered a secure mechanism for sharing data; you may want to retool your script to read the password from stdin instead, which would be a bit more secure.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • It was a typo, my apologies. I'm trying the suggestion you mentioned about using export, I'll let you know. Thanks. – ObiHill Nov 15 '11 at 15:34
  • Thanks a lot. It turns out `export dbpass` was just what I was looking for. It works like a charm now. Cheers. – ObiHill Nov 15 '11 at 15:57
0
$dbpass="mysqlpass"

should be

dbpass="mysqlpass"

As you've written it, $dbpass will be evaluated, and isn't defined, so comes out as an empty variable, boiling down to

="mysqlpass"

well, since it was a typo, then here's the answer:

MySQL looks for the envvar MYSQL_PWD: http://dev.mysql.com/doc/refman/5.0/en/environment-variables.html

Marc B
  • 356,200
  • 43
  • 426
  • 500