Mysql automated installation stuck - mysql

I have written a script to install some set of packages to a list of servers. When i execute the script mysql installation got stuck at "enter the root password" section. Is there anything i need to modify in my script ? Advice me.
Is there any way to pass the mysql root password through the script itself ?
Below is the code which i used
#!/usr/bin/env bash
read -p "Enter server name : " servername
echo "Installing package on $servername"
ssh "${servername}" sudo apt-get -y install apache2 mysql-server
Installation got stuck here
Even if I enter the password, it wouldn't proceed to the next step. I am very beginner for scripting. Let me know where to modify the script.

apt-get is a front-end to dpkg and debconf and is running in interactive mode by default, even -y won't change that.
mysql-server installation requires the root password to be interactively entered during installation.
To fully automatically install MySQL server on a Debian based Linux distro, you can enter non-interactive mode and preset the MySQL root password as follows.
In the shell where you want to run the process, execute:
export DEBIAN_FRONTEND="noninteractive"
Then
apt-get install -y debconf-utils
debconf-set-selections <<< "mysql-server mysql-server/root_password yournewpassword"
debconf-set-selections <<< "mysql-server mysql-server/root_password_again yournewpassword"
apt-get install -y mysql-server-5.6

#!/usr/bin/env bash
read -p "Enter server name : " servername
echo "Installing package on $servername"
ssh "${Host}" "echo 'mysql-server-5.7 mysql-server/root_password password your_password' | debconf-set-selections && \
echo 'mysql-server-5.7 mysql-server/root_password_again password your_password' | debconf-set-selections && \
apt-get update && \
apt-get -y install apache2 apache2-doc apache2-utils mysql-server"
The above code does the trick
Note : Backslash (\) are used for readability. They allow to continue the command on a next line.

Related

How to install mysql-server in debian:buster using script without being asked any configuration questions?

I am trying to install mysql-server in debian:buster using Dockerfile and shell script, when I run this command
apt install -y mysql-server and during the installation I need to enter the password and retype it as shown in the picture:
I did a little search and I found how to do it using here-string in this page Install MySQL on Ubuntu without a password prompt
so because I am using this command apt install -y mysql-server I did it like this:
debconf-set-selections <<< 'mysql-server mysql-server/root_password password your_password'
debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password your_password'
apt-get -y install mysql-server
but when I run them I still get the same thing and I need to enter the password.
Here is my Dockerfile:
From debian:buster
COPY ./run.sh /
CMD bash /run.sh
and this is run.sh:
apt update
apt install -y nginx gnupg wget lsb-release
wget https://dev.mysql.com/get/mysql-apt-config_0.8.13-1_all.deb
printf "1\n1\n4\n" | dpkg -i mysql-apt-config_0.8.13-1_all.deb //version selected is mysql-5.7
apt update
service nginx start
bash //just to stay in the bash of container and don't quit from it.
by the way I am trying to install mysql-server from inside the bash of the container then I'll write the commands in my script.
and there is something weird as you can see in the picture above it's written :
Configuring mysql-community-server
----------------------------------
even that I am installing with this command apt-get -y install mysql-server not this
apt-get -y install mysql-community-server
So do you know what am I missing here?
Use
shell> sudo debconf-set-selections <<< "mysql-community-server mysql-community-server/root-pass password mypassword"
shell> sudo debconf-set-selections <<< "mysql-community-server mysql-community-server/re-root-pass password mypassword"
shell> sudo DEBIAN_FRONTEND=noninteractive apt-get -y install mysql-server
Documentation

install mysql 5.7 purely from bash script on Ubuntu

I want a bash script that installs a MySQL 5.7 instance without needing any manual input.
I was following the tutorial on Digital Ocean and it says for 5.7 you have to run the following commands and then put commands into a prompt (screenshot below).
wget http://dev.mysql.com/get/mysql-apt-config_0.6.0-1_all.deb
sudo dpkg -i mysql-apt-config_0.6.0-1_all.deb
How can I automate the installation if it requires me to use a prompt? Should I try to simulate keystrokes? Or am I going about it the wrong way?
This answer is a combination and alteration of Bimmy's and khrm's answers.
STEP 1:
You have to set debconf values which will automatically fill in the values prompted for by the installation.
export DEBIAN_FRONTEND="noninteractive"
sudo debconf-set-selections <<< "mysql-server mysql-server/root_password password rootpw"
sudo debconf-set-selections <<< "mysql-server mysql-server/root_password_again password rootpw"
To get the values you need, just run installation normally, a good tutorial of it is here
STEP 2:
Update the information needed for APT by adding the 5.7 repository and updating `apt-get
sudo apt-key adv --keyserver pgp.mit.edu --recv-keys 5072E1F5
cat <<- EOF > /etc/apt/sources.list.d/mysql.list
deb http://repo.mysql.com/apt/ubuntu/ trusty mysql-5.7
EOF
sudo apt-get update
STEP 3:
Install MySQL. You can run my mysql_secure_installation but then that will ask you for more prompts. mysql_secure_installation is just a script so if you want to you can just run the parts of that script which are relevant to you.
I just ran
sudo apt-get install -y mysql-server-5.7 on its own.
You are proceeding in the wrong way. You don't need that package. That package only setup mysql repo.
You need to manually setup the mysql repository if you don't want prompt. Assuming you are using trusty (Ubuntu 14.04):
sudo apt-key adv --keyserver pgp.mit.edu --recv-keys 5072E1F5
cat <<- EOF > /etc/apt/sources.list.d/mysql.list
deb http://repo.mysql.com/apt/ubuntu/ trusty mysql-5.7
EOF
sudo apt-get update
sudo apt-get install -y mysql-server-5.7
mysql_secure_installation
If you want other stuffs like workbench-6.2, etc. You need to include it like this in file /etc/apt/sources.list.d/mysql.list after the first entry:-
deb http://repo.mysql.com/apt/ubuntu/ trusty workbench-6.2
I think this link may be useful for you. The video shows the whole process using a previous version (5.6).
To sum up, you should:
Export a variable called DEBIAN_FRONTEND with the value "noninteractive".
Use debconf-set-selections in order to set a root password (for your MySQL Server).
Install mysql-server-5.7 package.
Run a secure installation afterwards.
export DEBIAN_FRONTEND="noninteractive"
sudo debconf-set-selections <<< "mysql-server mysql-server/root_password password rootpw"
sudo debconf-set-selections <<< "mysql-server mysql-server/root_password_again password rootpw"
sudo apt-get install -y mysql-server-5.7
mysql_secure_installation
NOTE: You can install debconf-utils typing the following command: sudo apt-get install -y debconf-utils

Installing Percona/MySQL unattended on Ubuntu

I can install MYSQL on Ubuntu without prompts with the code below:
dbpass="mydbpassword"
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
The part with the debconf-set-selections I got somewhere online (could be here can't remember) and it has worked ok for me thus far. I'm not that much of an expert to understand how it works but it does.
However, I want to do the same thing for Percona. I've setup the apt package manager to deal with using apt-get for percona. So now my code is the following:
dbpass="dbpass" && export dbpass
export DEBIAN_FRONTEND=noninteractive
echo percona-server-server-5.5 percona-server-server-5.5/root_password password $dbpass | debconf-set-selections
echo percona-server-server-5.5 percona-server-server-5.5/root_password_again password $dbpass | debconf-set-selections
apt-get -y install percona-server-server-5.5
However, Percona installs but without a password as defined. I know I'm missing something in the debconf bit.
I'd appreciate some guidance here.
Thanks in advance.
I actually found that the answer here install mysql on ubuntu without password prompt that suggested
export DEBIAN_FRONTEND=noninteractive
apt-get -q -y install mysql-server
Worked and left me with a root user with no password, which is what I wanted.
The second part of the debconf-prefix should not contain the version number:
echo percona-server-server-5.5 percona-server-server/root_password password $dbpass | sudo debconf-set-selections
echo percona-server-server-5.5 percona-server-server/root_password_again password $dbpass | sudo debconf-set-selections
For 5.6:
echo percona-server-server-5.6 percona-server-server/root_password password $dbpass | sudo debconf-set-selections
echo percona-server-server-5.6 percona-server-server/root_password_again password $dbpass | sudo debconf-set-selections
If you understand what is going on underneath the hood it makes it easier to debug and figure out why this isn't working.
When you install a debian package often times you get questions about licenses, passwords, locations, etc. All of those values are stored in debconf. If you are wanting to do an unattended installation you can preload those answers into debconf so you aren't prompted for those questions, since they are already answered.
The challenge comes when understanding how to properly answer those questions. To do this you first need to install the debconf-utils
apt install debconf-utils
next you need to manually install your package.
In my case I am installing the percona-xtradb-cluster-57 package.
wget https://repo.percona.com/apt/percona-release_0.1-4.$(lsb_release -sc)_all.deb
sudo dpkg -i percona-release_0.1-4.$(lsb_release -sc)_all.deb
sudo apt-get update -y
sudo apt-get install -y percona-xtradb-cluster-57
After this has been installed you can get the selections that have been set by using the deb-get-selections tool.
debconf-get-selections | grep percona
In the response you will see the selections that were set. In this case
percona-xtradb-cluster-server-5.7 percona-xtradb-cluster-server-5.7/root-pass password
percona-xtradb-cluster-server-5.7 percona-xtradb-cluster-server-5.7/re-root-pass password
percona-xtradb-cluster-server-5.7 percona-xtradb-cluster-server-5.7/remove-data-dir boolean false
percona-xtradb-cluster-server-5.7 percona-xtradb-cluster-server-5.7/root-pass-mismatch error
percona-xtradb-cluster-server-5.7 percona-xtradb-cluster-server-5.7/data-dir note
You can now copy the values that you want to set. In my case I want automatically set the root password.
In your automated installation script you can now use the debconf-set-selections tool to automate setting the values for the root password question and the confirm root password question.
echo "percona-xtradb-cluster-server-5.7 percona-xtradb-cluster-server-5.7/root-pass password my_temp_password" | debconf-set-selections
echo "percona-xtradb-cluster-server-5.7 percona-xtradb-cluster-server-5.7/re-root-pass password my_temp_password" | debconf-set-selections
Happy Automating!
Think I figured it out
echo "percona-server-server-5.5 mysql-server/root_password password mypassword" | debconf-set-selections
echo "percona-server-server-5.5 mysql-server/root_password_again password mypassword" | debconf-set-selections
Don't use export DEBIAN_FRONTEND=noninteractive. If the debconf entries are correct, then you won't be prompted anyway. If they are incorrect and you use noninteractive then the prompt will continue with a blank password.
Since Percona 'hooks into' MySQL check that it installed correctly using
service mysql status
and you will know it is percona if you see something like
mysql.service - LSB: Start and stop the mysql (Percona Server) daemon
Then finally check the password was set correctly
mysql -u user -pmypassword
EDIT: That said, for a newer version of percona, F21's answer worked for me. You can check the entries in /var/cache/debconf/passwords.dat
you can always do normal installation and then script:
killing mysql server
starting it with skip-grant-tables
adjusting passwords
killing the temporarily started mysql without authentication
starting regular mysql

Scripting LAMP to be installed with predefinied root password

I would like to write the following Bash Script:
When installing LAMP like this, it would ask me a root password for the MySQL installation.
I would like to bash script it.
sudo apt-get install lamp-server^
Can I do it like this to pass the variable as a parameter so it would not ask me about it during installation?
rootPassword="MyRootPassword";
sudo apt-get install lamp-server^ -y $rootPassword
In order to answer debconf questions non-interactively, you will need the debconf-utils package. The debconf-set-selections command can be used to manually insert things into the debconf database that will normally be asked interactively. This can also be done at OS install time using FAI or preseeding.
export DEBIAN_FRONTEND=noninteractive
debconf-set-selections < 'mysql-server-5.1 mysql-server/root_password select PASSWORD'
debconf-set-selections < 'mysql-server-5.1 mysql-server/root_password_again select PASSWORD'
You will likely need to change the name of the mysql-server package name to the version used by your distribution. Note that the package "mysql" is a virtual package which points to the real package (which includes the version number).

How can I pass a password from a bash script to aptitude for installing mysql?

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?
This is so much easier ..
install mysql on ubuntu without password prompt
sudo debconf-set-selections <<< 'mysql-server-5.1 mysql-server/root_password password your_password'
sudo debconf-set-selections <<< 'mysql-server-5.1 mysql-server/root_password_again password your_password'
sudo apt-get -y install mysql-server
If your shell doesn't support here-strings (zsh, ksh93 and bash support them), use:
echo ... | sudo debconf-set-selections
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
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
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
This script was successful when launched as superuser:
#!/bin/bash
export temp_mysql_password="**********"
export DEBIAN_FRONTEND=noninteractive
debconf-set-selections <<< "mysql-server mysql-server/root_password password $temp_mysql_password"
debconf-set-selections <<< "mysql-server mysql-server/root_password_again password $temp_mysql_password"
debconf-set-selections <<< "mysql-server-5.6 mysql-server/root_password password $temp_mysql_password"
debconf-set-selections <<< "mysql-server-5.6 mysql-server/root_password_again password $temp_mysql_password"
apt-get update
apt-get -y upgrade
apt-get -y install mysql-server
look into using expect
It can be used to automate most interactive sessions, although I wouldn't use a root password
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.