install mysql 5.7 purely from bash script on Ubuntu - mysql

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

Related

How to install mysql workbench in debian buster?

I tried the following commands :
sudo dpkg -i mysql-apt-config_0.3.5-1debian8_all.deb
sudo apt-get update
sudo apt-get install mysql-workbench-community
but on searching mysql-workbench-community
sudo apt-cache search workbench | grep mysql
returns nothing.
And
sudo apt-get install mysql-workbench-community
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package mysql-workbench-community
What to do ?
I did manage to install without issues MySQL Workbench 8.0.19 version from an Ubuntu package in Debian Buster, with the command:
sudo dpkg -i mysql-workbench-community_8.0.19-1ubuntu18.04_amd64.deb
First you must download the package from the mysql website:
Just select Ubuntu Linux operating system and 18.04 version (19.10 version had broken dependencies) .
Hope this helps.
MySQL Workbench mysql-workbench has been removed from Debian buster due to a release-critical bug.
https://tracker.debian.org/pkg/mysql-workbench
https://tracker.debian.org/news/943250/mysql-workbench-removed-from-testing/
https://bugs.debian.org/867943
https://bugs.mysql.com/bug.php?id=89898
It can also be installed from Snapcraft. I haven't used it that much, but apparently it works. First you need to enable snapd:
$ sudo apt update
$ sudo apt install snapd
$ sudo snap install core
Then install MySQL workbench (community edition):
$ sudo snap install mysql-workbench-community
If you want to delete it, simply:
$ sudo snap remove mysql-workbench-community
I was able to install the latest version of MySQL Workbench (as of this writing) on Debian 10 using this method:
sudo apt install ./mysql-workbench-community_8.0.23-1ubuntu20.10_amd64.deb
A couple dependencies were missing so I subsequently ran:
sudo apt --fix-broken install
Just started using it, but MySQL Workbench seems to be running fine. It's also worth mentioning the link below states MySQL Workbench community is not available on Debian, although I'm not sure why this is the case.
mysql-workbench-community - MySQL Workbench (not available for Debian
platforms)
https://dev.mysql.com/doc/mysql-apt-repo-quick-guide/en/#repo-qg-apt-available
Command line to install mysql on Debian 10:
$ sudo apt update
$ sudo apt upgrade
$ wget http://repo.mysql.com/mysql-apt-config_0.8.13-1_all.deb
$ sudo dpkg -i mysql-apt-config_0.8.13-1_all.deb
Remember to make safe passwords with lettler numbers and symbols
$ sudo apt update
$ sudo apt install mysql-server
$ sudo apt-get build-dep mysql-server
$ wget https://downloads.mysql.com/archives/get/file/mysql-workbench-community_8.0.16-1ubuntu18.04_amd64.deb
$ sudo dpkg -i mysql-workbench-community_8.0.16-1ubuntu18.04_amd64.deb
$ sudo apt install -f
$ sudo dpkg -i mysql-workbench-community_8.0.16-1ubuntu18.04_amd64.deb
have a nice coding!

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

Docker - How can I initiate mysql database with a .sql file running on a container?

It is my first time working with docker. I want to create a remote container with test environment for my java application. I also need mysql database. Everything should run on one container (it's a requirement, not my idea).
I need to have a db initiated with .sql file (let's say file.sql located in the same directory as Dockerfile)
Thank you very much for help :)
Here is part of my Dockerfile:
FROM ubuntu:16.04
#Install JRE
RUN apt-get update && apt-get install default-jre -y
#Install JDK
RUN apt-get update && apt-get install default-jdk -y
#Install Maven
RUN apt-get update && apt-get install maven -y
#Install wget
RUN apt-get update && apt-get install wget -y
#\&& rm -rf /var/lib/apt/lists/*
#Install unzip
RUN apt-get update && apt-get install unzip -y
#Install glassfish
RUN mkdir -p /opt && cd opt/
RUN wget http://download.java.net/glassfish/4.1.1/release/glassfish-4.1.1.zip
RUN unzip glassfish-4.1.1.zip
#Install mysql
ENV MYSQL_USER=mysql\ MYSQL_DATA_DIR=/var/lib/mysql \ MYSQL_RUN_DIR=/run/mysqld \ MYSQL_LOG_DIR=/var/log/mysql
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server \&& rm -rf ${MYSQL_DATA_DIR} \&& rm -rf /var/lib/apt/lists/*
First use a ADD to copy your .sql file in the image. Then add a RUN command to restore your DB using mysql commands.
Thank you all for answers!
I found a sollution if anyone will encouter the same problem it's here:
RUN echo "mysql-server mysql-server/root_password password password" | debconf-set-selections
RUN echo "mysql-server mysql-server/root_password_again password password" | debconf-set-selections
RUN apt-get update && apt-get -y install mysql-server && service mysql start
if mysql is still not starting check /etc/docker/daemon.json, make sure it looks like this:
{
"storage-driver":"overlay2"
}
if you don't have this file execute:
mkdir -p /etc/docker
echo '{"storage-driver":"overlay2"}' > /etc/docker/daemon.json
systemctl restart docker.service

Mysql automated installation stuck

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.

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.