Wednesday, October 6, 2021

Install mysql with docker in your local machine (ubuntu 20.04)

I had to use another laptop for some days and I didnt want to install packages that left garbage behind when moving away from this machine.

These are the two guides I followed for reference : 

 Preparation

In my case docker was already installed. docker can run from my user, so no need to append `sudo` to any `docker` command

Install mysql server via docker

The key here is to use the official mysql package from docker hub

# https://hub.docker.com/_/mysql
docker image ls
docker pull mysql:latest
docker image ls

# create a volume in local machine
docker volume create mysql-data
docker volume ls
docker volume inspect mysql-data

# map a local directory to host the config
mkdir -p /home/jesus/projects/docker/mysql-volume/conf.d
echo -e '[mysqld]\nmax_connections=100\nmysqlx= 0' > /home/jesus/projects/docker/mysql-volume/conf.d/my-custom.cnf

then create the container

docker run \
--detach \
--name=mysql_server \
--publish 3306:3306 \
--volume=/home/jesus/projects/docker/mysql-volume/conf.d:/etc/mysql/conf.d \
--volume=mysql-data:/var/lib/mysql \
--env MYSQL_ROOT_PASSWORD=root \
--env MYSQL_ALLOW_EMPTY_PASSWORD=yes \
mysql:latest

docker ps
docker logs mysql_server

change root password

# access to the container in interactive mode
docker exec -it mysql_server bash


#supply initial root password (to enter mysql inside the container)
mysql -uroot -proot

# SET PASSWORD FOR 'root' = 'something-super-secret';
SET PASSWORD FOR 'root' = '';
FLUSH PRIVILEGES;

# enable docker access from anywhere (probably the image has this already)
UPDATE mysql.user SET host = '%' WHERE user='root';
FLUSH PRIVILEGES;

Install mysql client

Install the client in your machine that accesses the mysql server running in the docker container. For other distributions adjust the method to install the mysql client (only the client is needed). The mysql site provides that information.

First, add the mysql repo. Go to https://dev.mysql.com/downloads/repo/apt/ and get the latest repository available. In my case (@2021-10-01). Adjust the .deb package accordingly.

# add the repo
wget https://dev.mysql.com/get/mysql-apt-config_0.8.19-1_all.deb
#Click OK with the default options or select which mysql you want
sudo dpkg -i mysql-apt-config_0.8.19-1_all.deb
rm mysql-apt-config_0.8.19-1_all.deb

# install the client from the repo
sudo apt-get update
sudo apt-get install mysql-client
# sudo apt-get install mysql-server

ensure it works

mysql -uroot -h127.0.0.1 -P3306
# or with password
# mysql -uroot -p -h127.0.0.1 -P3306

😄 Fin!

P.S. Useful commands

docker start mysql_server
docker stop mysql_server
docker restart mysql_server
docker ps
docker volume inspect mysql-data
docker container ls --all

mysql -uroot -p -h127.0.0.1 -P3306

# dangerous!
docker rm mysql_server
docker volume rm mysql-data



10 comments:

  1. Amazing blog....Thank you for the very good and meaningful blog. It is very helpful for all blog writers. In this article, you define how to start a blog you also defined what is the right way to choose the right way for the topic. if you want to write a new blog you have a great opportunity to write a blog on the new topic of Server hosting. You Should write on Malaysia VPS Hosting it offers an amazing hosting solution to users.

    ReplyDelete
  2. need to thank you for this very good read and I have it to check out new things from your post.
    USA Dedicated Server Thank you very much for sharing such a useful article and I will definitely save and revisit your site.

    ReplyDelete
  3. I appreciate the clear explanation of dedicated web server hosting services on your article. Well done on the dedicated hosting coverage. Furthermore, Dubai VPS Server Hosting provides the most accurate information on VPS Server Hosting. We should cherish your information!

    ReplyDelete
  4. Nice blog; thanks for sharing good information about real estate. This is very useful for the interesting buyer. India VPS Server

    ReplyDelete
  5. Excellent post this is very unique information for me. Your post is so valuable for every one. Thank you for your great post and for helping others become more aware. And you can buy Managed VPS Hosting from Sweden Server Hosting at very cheap price with best security features.

    ReplyDelete
  6. Really informative post, thanks for sharing. it is a high performance, higher security, reliability, security audits, and higher bandwidth or scalability. If you are searching for the fastest and cheap Ukraine Dedicated Server, you can ask us for more details and services.. Ukraine Dedicated Server

    ReplyDelete
  7. I've seen a number of blog comments and, let me know your blog was really wonderful for sharing this Dedicated Server Hosting Overview. If you are looking for the excellent Switzerland VPS Server availability you can request us for more details and services

    ReplyDelete
  8. I respect your blog's thorough data about Dedicated net server web hosting services. You cowl Dedicated web hosting well. Additionally, Switzerland Dedicated Server Hosting gives first-class statistics on VPS Server Hosting. Your records deserve to be valued!

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. We got a lot of information from your blog which is very useful for us. If you are searching for the fastest and cheapest Ukraine dedicated server, you can ask us for more details and services......Ukraine Dedicated Server

    ReplyDelete