Docker Installing mysql-5.7 server on Ubuntu 20.04 and connecting outside the docker
In previous versions of Ubuntu Installing mysql-server version 5.7 was as easy as executing,
apt install mysql-server # don't run this command, it will install mysql 5.8 ( which you dont need at this moment)
But in ubuntu 20.04 ( focal fossa) the default mysql version is version 8. Given that if you run the usual way it will install version 8. Not only that, if you need to install the older version process is very difficult ( might not be impossible). So the easiest solution is to use a docker.
First you need to install docker.
If you have older versions of docker remove them first.
sudo apt remove docker docker-engine docker.io containerd runc
Now update repository, and install the certificates and some dependencies.
sudo apt updatesudo apt install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common
Add repository key.
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Now set the stable repository
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
Now install the docker engine
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
To install a certain version you can list the available versions now
apt-cache madison docker-ce
Output will look like this,
docker-ce | 5:19.03.12~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:19.03.11~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:19.03.10~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:19.03.9~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
I’m selecting version ‘5:19.03.12~3–0~ubuntu-focal’ and install it as below.
Template is like this
sudo apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io
In my case,
sudo apt install docker-ce=5:19.03.12~3-0~ubuntu-focal docker-ce-cli=5:19.03.12~3-0~ubuntu-focal containerd.io
Now Start the docker daemon with followingsudo systemctl start docker
Then Enable docker to run on start of the OS
sudo systemctl enable docker
You can test docker by running the hello-world
sudo docker run hello-world
then see the created image by,
sudo docker images
Now remove this docker
sudo docker rmi hello-world
We need to give the user to run docker to avoid using sudo
Follow this,
sudo usermod -a -G docker $(whoami)//now check the user is addedgrep docker /etc/group
// output will be like
// docker:x:998:$(whoami)newgrp docker
Now, no need to use sudo for running docker related commands.
Now lets install mysql server 5.7. First we are pulling the docker image, then create a volume to store the mysql data.
docker pull mysql/mysql-server:5.7docker volume create mysql-volume
In following command set the passwd for root for your desired
docker run -d --restart unless-stopped --name=mysql -p3306:3306 -v mysql-volume:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=password -d mysql/mysql-server:5.7
Now check the created installation
docker psdocker exec -it mysql bash
Now create another user (other than root) to use from outside connections.
First Log into mysql with the password you gave to root early.
mysql -uroot -p// Hit enter and then enter the passwordcreate user 'admin'@'%' identified by ‘password’GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' WITH GRANT OPTION;
Now you can install db viewer community or any desired application to browse mysql from outside.
Keep in mind that docker we just installed is a centos version. So if you need to do any changes in the future google with centos. Most of requirements should be installed by yum inside the docker.
References
- https://docs.docker.com/engine/install/ubuntu/
- https://stackoverflow.com/questions/47854463/docker-got-permission-denied-while-trying-to-connect-to-the-docker-daemon-socke
- https://docs.docker.com/config/containers/start-containers-automatically/
- https://towardsdatascience.com/connect-to-mysql-running-in-docker-container-from-a-local-machine-6d996c574e55