How to Set Up MySQL Database with Docker (2024)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys in this video I'm going to show you how you can install and use MySQL server with Docker so for this video I will be using my Mac operating system but if you have Windows operating system or Linux operating system the commands will be same which I'm going to show you in this video once you have Docker installed on your operating system which you are using so I will start with the install ination process of Docker on my Mac operating system and I will put the link of the videos about how to install Docker on Windows or your Linux operating system so whatever operating system you have just click the link of that video and install Docker using those instruction so let's start with the installation of Docker so first of all open your favorite browser and search for Docker and the first l which will appear here will be from doer. so we are going to click on this link and straight away we are on this doer. website now in order to install Docker click on get started now on this page you can see get started with Docker and you will see some options to install Docker first one is for Docker for Mac for apple chips that means if you have apple chips like M1 M2 M3 then you can just click on this button if you have mac with Intel chips then you can click on this option which says um download for Mac Intel chips I have a Macbook which supports apple chips so I'm going to just click on this uh download for mac Apple chip button which is going to start the download for this DMG file so just wait for this download to finish and once this DMG file is downloaded loed I'm going to click on this DMG file and let me minimize my browser so the installation of Docker is really simple you just need to drag this dcker symbol into your applications folder if you're using your MacBooks trackpad then just press command key and then just drag and drop this icon into your applications folder which is going to start the copying process you can see copying Docker to Applications just wait it for a few seconds and once this is copied we can close this window eject this Docker from our Macbook so I can just right click on this Docker icon and then click on eject Docker now once Docker is copied into your applications folder you can go to your Launchpad section and here search for Docker and you will be able to see the docker icon here so I'm going to click on this Docker icon which which is going to start Docker on your Mac operating system at this point you just need to wait for a few seconds and you will see this kind of window which appears so here you can see uh Docker subscription service agreement and you will also see that Docker is starting on your Mac operating system and at the top you can see uh this kind of Docker icon right so I'm going to click on accept here and now I can see this uh window which says complete the installation of Docker desktop the configuration below can be changed later in the settings for now I'm going to choose uh this option which says use recommended settings which requires the password I'm going to just click on finish here and then I'm going to provide my Mac books password which I use to log to my Mac operating system and once that's done this window appears you can also see some uh notifications here which I'm going to close so on this next window Docker will ask you to sign in with your Docker account the creation of Docker account is really simple just click on sign up here and then it's going to redirect you to this kind of web page where you can provide your email username and password and then click on the sign up button which is going to sign you up into your Docker account now this kind of uh dockerhub account is useful when you want to push some images which are some custom images uh into your dockerhub account and then you also need to pull those images so I already have the docker account so I'm going to click on sign in here and then I'm going to sign in with my Docker account and once I'm uh signed in with my Docker account I'm going to click allow here which is going to sign me in in my Docker desktop and here you can see this welcome survey I don't want to uh give any feedback so I'm going to click on skip server here and now I can see my Docker desktop so let me just close everything here and this is how this Docker desktop looks like on the top you can see the docker uh icon and you can also see that Docker desktop is running from here you can restart the docker quit Docker desktop also you can pause the docker right you can also see the images once you have some local images or if you have pushed some images to your Docker Hub then you can also see those images here also you can see the volumes and other information about your Docker containers once that's done I'm going to minimize is this and then I'm going to open the terminals so just go to your Spotlight and then just search for terminal and open the terminal and here we can just write Docker space version here and then press enter and it's going to give you some information about your Docker version you can also give this command which is Docker info and then press enter and it's going to give you the information about your docker engine once those two commands uh works in your terminal you can also use Docker in your terminal so I'm going to uh clear the terminal and let's say I want to pull some image into my local machine so what you can do is you can search for dockerhub in your favorite uh browser and you can open this website which is hub. do.com so on this do Hub website we are going to search for MySQL and then I'm going to press enter and it's going to search for all the MySQL related images in the docker Hub website now as you can see here the number one result here is the official Docker image and it has been downloaded around 1 billion times right so it's the most popular and it's also the off offal image of MySQL server so we are going to click on this first result here and here you can see more documentation about how you can use this image with Docker on your operating system which you are using so when you scroll down a little here you will see a section called how to use this image and you will see some Docker run commands here so I'm I'm going to use this first command so let me copy this command and then you can open any uh text editor of your choice I'm going to open Visual Studio code here and paste this command here so basically what this command does is it runs the docker container with the name Su MySQL so this will be the name of your Docker container so hyphen hyphen name uh here provides the docker container name and using hyphen e you can provide some environment variables to your container image so here in this uh command we are providing the MySQL root password which will be used as the password when we want to log our MySQL server and by default this password is my hyphen secret hyphen PW and then using hyphen D you can provide the image with the image tag so in this case you can even remove this colon tag from here because if you give the name of the image then it will always patch the latest image for MySQL we can also give the port on which we want to run MySQL so here after the environment variable flag I'm going to provide one new flag which will be called hyphen p and using hyphen P we can provide the mapping of the port uh so we want to run let's say MySQL on 3306 Port we will map this 3306 Port which will be running inside our container to the Local Host so we will be able to connect to our uh MySQL server using 3306 on our local computer so I'm going to just write 33 06 here and provide the space here so this will be our final command and I'm going to provide this command in the description of this video also so that you can copy this command directly and paste it into your terminal so I'm going to copy this command and then you can open the terminal so let me open the terminal here and once the terminal is open make sure your docker desktop is running and then I'm going to just paste this command here and then I'm going to just press enter here which is going to fetch the latest image of doer you can see unable to find the image that's why it will fetch the MySQL colon latest which means that it's going to fetch the latest image of MySQL and then you can see it has already pulled the image and then it has started our container now to check whether our container is running or not you can give this command which is Docker space PS and then press enter and it's going to show you the container which is running so you can see here the name of the image is MySQL the name of the container is some MySQL here and the port we are forwarding here is 3306 on our Local Host right so our container is running successfully here you can also check this on the docker desktop tool which we have downloaded so I'm going to open the docker desktop and you can see the container name is some MySQL and the image name is uh MySQL and we have started this image 2 minutes ago and the port mapping here is 3306 now what we are going to do is we are going to use a tool called MySQL workbench which is a tool by MySQL itself and it's an official tool which is a graphical user interface tool which will allow you to connect to your MySQL server if you don't know how to install MySQL workbench on your Mac operating system or any other system I will also put the link of the videos which I have created to install MySQL workbench on your operating system so let me open the MySQL workbench from here so I'm going to search for my SQL workbench and I can see this result here so I have already downloaded this MySQL workbench once you download and install MySQL workbench it's going to look something like this where you can create some connections so I'm going to click on this plus uh icon here to create a new connection and then I can provide the name to my connection so I'm going to give the name as Local Host for example you can also uh write Local Host Docker for example then the host name here is uh 127.0.0.1 which is essentially Local Host because we are running our container on Local Host itself so if you don't provide any IP address here this command is going to run the MySQL on the local host and we are already forwarding our Port of the container to the uh 3306 port on our machine right so you can see 3306 uh is the default Port which we have already provided in the command itself now the username here is root so if you don't provide the username using these environment variables the default username will be the root user right so we can leave the username as root here the password Here will be the password which we have provided so you can see in this command we have provided this password so you can copy this uh password from here and then go to your workbench and then provide your password and then click on okay and once you have provided that password I can click on the test connection and then I can see this window which says successfully made the MySQL connection so our connection is successful so I'm going to click on okay here and then click on okay so our connection is made to our MySQL server and then I can just click on this connection then click on continue anyway and here I can see I'm connected to my MySQL server and here I can provide some queries here so for example I can write uh select now and provide these parenthesis and then I can uh run this command for example and when I run this command it's going to give me the current time for example so everything is working fine so we have a running MySQL server using the docker and we are able to connect to the MySQL server using our mycle workbench graphical user interface tool now in some cases you don't want to uh write this command again and again on the terminal because this is a very uh lengthy command you might not want to write uh this command again and again so there is a utility called Docker compose using Docker compose you can create a file which will be a Docker compose file and in that file you can provide all these flags and save that file on your computer and then use that file again and again to run your Docker container so for now I'm going to stop the docker container so whatever result you see using the docker PS command you can use that uh container ID to stop your container so either I can write uh Docker stop and my container name is sum MySQL so Su hyphen my SQL and then press enter which is going to stop this container and you can see it's going to give me the result and when I give the docker PS command once again I can see there is no container running right now you can verify this in this uh Docker desktop uh section we have this image but this container is no longer running okay so now let me show you how you can create the Docker compose file with the same flags and you can save that file and how you can run that file so I have created this Docker compost file I'm going to put the link to this Docker compost file so you can directly copy the content of this Docker compos file so what you need to do is you just need to create this uh file with the name Docker hyphen compos do yml and then paste the content of this uh Docker compose which uh I'm going to provide you in your Docker compose file so here basically what I'm doing is I'm just uh pulling the image with the tag 8.0 you can also remove this tag to fetch the latest image then the docker container name will be MySQL hyphen local we are also mapping the port 3306 to our local machine and then we are providing some environment variables here so the root password will be root password you can replace this password the mySQL database name will be example DB and the MySQL username will be example user all these environment variables you can change it according to your requirements and the password we are going to use is example password Here I have also provided this volumes section here and this will allow you to persist your MySQL data so once you exit from your MySQL container then also you will be able to fetch the data which you have already saved into your mySQL database in the next container run so as I said I will provide this uh content in the description of this video so you can uh just uh click the link and then copy the content and paste it in your Docker compost file also and then you can open the terminal I'm going to open the integrated terminal in my visual studio code you can also use the terminal in your Mac operating system or any other system so just go to the directory in which your Docker compos file is there and then give this command which is Docker hyphen compose and space up space hyphen D which is going to start your container you can see it has created the container and started the container this time we have given the name MySQL hyphen local to our container right so when we go to the desktop uh utility you can see this container is running we have uh started this container 22 seconds ago you can see uh we have ran a container which is under Docker and and you can see the container name is MySQL hyphen local and we are forwarding this port again which is 3306 so let's go to our MySQL workbench once again and here I'm going to close uh this connection from here and let's create a new connection for our new uh container image so I'm going to click on this plus icon once again this time I'm going to name my container as docker compose Local Host here and then the host name will be uh same which is 127.0.0.1 Port is same which is 3306 this time the username is different right because in my Docker compose do yaml file the username which I've provided is example hyphen user so I'm going to use this username now instead of username rout here I'm going to provide example hyphen user and the password Here will be the password which I have given here which is example hyphen password so I'm going to provide this uh password here and then click on okay and then I can test the connection from here and connection is once again successful so I'm going to click on okay here which creates this new connection so I'm going to open this connection here and now the connection is open to my new container right here you can see I have this uh schema which is called example hyphen DB I have also created using this command so the database name is example hyphen DB right also see that the MySQL root password is root hyphen password so if you have used the root username then you could have used the root password Here Also right so now also everything is running fine now to stop this container which you ran using the docker compose file you can once again go to the terminal where you have started the container using Docker compose command this time instead of giving Docker compose space uh up hyphen D just write Docker compose Down Here Without hyphen D and then press enter and it's going to stop your container and you can see the container is stopped and we have also removed the network from here so this is how you can stop your container using the docker compose command so this is how you can use Docker to run MySQL server on your uh operating system it can be Mac OS windows or Linux operating system I hope you have enjoyed this video and I will see you in the next video
Info
Channel: ProgrammingKnowledge
Views: 1,544
Rating: undefined out of 5
Keywords:
Id: igc2zsOKPJs
Channel Id: undefined
Length: 22min 54sec (1374 seconds)
Published: Thu Jun 27 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.