How to Create a MySQL Server using Docker Compose

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to this video my name is David Dawn uh in this video we're going to be creating a MySQL um server using Docker compose rather than just Docker itself now why would you want to use Docker compose rather than Docker well I think the example and the demonstration will will talk for itself so with that said uh let's get started okay um just a point to note try and stay away from them with yaml files uh they don't tend to like it on Mac so we're going to use Visual Studio code instead okay with that said let's um start by creating a Docker compose yaml and uh let's just move this in and the first thing that we want to create uh put in here is then the version now the version um it can be a little bit funny with regards to which uh platform you're using or be operating system such as Windows Mac or um Linux um so it depends on which different version you've got installed but I always find version three is just enough um to to get you by um without worrying about the rest okay if you want some specific new things then you just use another version but threes are okay okay the the next thing that we want to declare is services and we also want some uh volumes in this example and we want to use networks too so let's start at the bottom why do we want a network well we'll just write it out first and then we'll explain it uh this example is only going to use one service but is it's more than likely that you're going to have multiple services with inside of Docker compos file if you just got one service you might as well just use Docker run by itself there's not really much need for um Docker compos file however when you have like lots of microservices and so on then it's kind of vital to put them on their own network and Docker actually recommends you to do this they don't want you to use the the normal host uh Network they want you to to use this your own network itself now there's some other uh configuration points that you can do here but this is all you need to do right now now because we're MySQL we want to be able to persist the data so if the container gets destroyed or whatever we want to know that this data we' inside the database and all of the tables and so on the configurations all all stay there so we're going to use a volume for this and we're going to call the volume the my S volume to keep it nice and simple now if this doesn't exist uh compose will create this upon uh executing this one Docker compos yo file um but you'll see in a minute okay next thing is we're going to have our actual uh database service and we're going to call it uh MySQL now it's Al also going to be the host name on that Network too now I like just out of best practices to say container name to and yes I'm going to give it the same name but like I said I just like it out best practices to say I'm always going to use the container name as the host name rather than uh the service name here the next thing is we want to Define which image we're going to use now we don't need a custom image here because there is already one provided to us by Docker and uh I don't like using the latest version even though 8.1 is the latest version today I like to be more specific because I've got control over uh which version is going to get installed etc etc um so the next thing we want to do is want to be able to map the the volume uh the MySQL volume to uh the myoll container itself to make sure that we get all of the data with inside of that one container to be stored on your host machine so we say the MySQL volume and then inside a um a MySQL server the the the mysq data is normally stored unless you've defined it else normally stored with inv v lib MySQL so upon starting all of the contents of this one directory will be mapped to I mean saved to basically this one uh volume and now um Linux will save it to to one place Mac will save it to another place and so on I'm sure windows will do it another way as well something some crazy way um but yeah if you wanted any other configuration files um for example if we had like a my comp file that a custom one that we wanted that might be mapped to uh this here to to the uh whoops uh to this here right and if you had your own um like say uh let's just call it client. com and that was then going to go to I'll say root and then my uh com you could add these here as well but we're not going to use them in in this one example now we've done that we want to um we want to be able to access this uh one container from externally as well so we need to Define which Port is going to be open on this one machine and which Port is going to be open or Exposed on the container itself now I like just to be a little bit different let's use 3307 rather than the traditional 3306 on this one machine it keeps the hackers and the penetration testers working if they've just got to use a different uh port instead and we're just going to say 3306 so we're going to use the default Port that's inside the container and we're going to map it to 3307 um now that we've done that we want to put it on a network even though no one else is talking to this one my Square server it's always worthwhile putting on a network as I told before and uh the network was called their myql uh Network to okay uh now we've done that we don't have any any other services but if you did have another services I don't say like PHP or whatever uh then you might you might say that this PHP uh depends upon uh this one this one service but but we don't and this naturally to be able to talk with each other would want to be on the same the same network too and I don't know let's just say that you had an engine X here as well and you'd want to put you'd want to put these on the same the same network also so they can all so they can all communicate with each other but this example is not about that it's just about getting the my SC up and running okay with all that one uh talking let's jump back over to the terminal and let's just run Docker minus uh compose now most new versions of Docker you can just say uh Docker compose as well without the minus and we're going to say up we're not going to give it any- D for the time being because I want to see the standard output in standard out so we can buget and let's do it and we can see that it exited already well this is because we're not we haven't added the required um environment variables which is super interesting so we're going to have then uh it says here you need to specify one of the following as an environment variable so let's uh do what it says and come back over to here and um let's just pop it at the top and we can say environment here and uh the root password is going to be 1 2 3 4 5 6 why would we want to do anything think uh anything else than that and uh we can do this too so um let's now restart this once again and we should now get an up and running server and it will be going through the normal standard initialization steps and it seems to be seems to me that everything is working perfectly fine y we've got a socket the port and so on so let's just come and have a look at uh is it up and running what up and running 17 seconds 20 seconds now and we can see that it's running on Port 3306 and we've got it mapped from 3307 on our host machine to it so let's now go ahead and uh execute uh using interactive shell and let's just say MySQL and then a user and then a password put in our secure password and we're now there so we can say uh show database and because we didn't Define any new databases right we didn't Define any new users so we can say let just this select user host from MyQ user and semicon and as you can see we didn't get any new users um so it's it's something that you might want to do if you need any other any other users with that said let's just go ahead and do it all right so we're going to say the new user is going to be David and uh the user password is going to be 1 2 3 4 5 6 and the database that we want it to create is going to be davidor DB now this is where the volume comes into hat right or volume comes into place sorry is that it's already up and running so this database is not going to get created so if we if we run uh Docker compose once again and we get out of this and now we come back into uh say here it my whoops MyQ and MyQ user uh David MP and is not running so let's just say dock PS and it exited oh very interesting okay but that's because let's just let's just run in again shall we and see where exit anymore I think that's just because there were two machines up and running and I killed one of them okay we're now now we're up so let's just run that that come on again and we're loging as David see access denied for David at Local Host because that was the volume is already there so let's now just do uh rout once again and uh yes and now we're in so now we can say select user and host from my from my S uh user and you can see the David user is not there and uh that's because the the volume is there right so if we now we now come to this and we uh stop this one server and now rather than stop it we can say down I mean it's already down already and we can say down and minus V and this will delete all volumes and as you can see here now volume MySQL MySQL volume removed so if we run up again again now and we'll do- D just so we put it in the background the new volume gets created and if we uh do this once again we should be able to access it now as David and we do so we can say show databases as well and we get David uncore uh DB so this is basically how you do it in uh Docker or in Docker yeah um now at the moment we're kind of accessing it um through docket executes or ex EC um but we could just say uh MySQL here and then use this and use a p and we we can't connect through through to this right because we've got to say the port that we we opened up which was 3307 and the host we're going to say is then on the Loop pack address and you can see now we can access it right so we know that um we know that we we're accessing it uh correctly if we say exit and we run this just without the the the large- P and we do the password 1 2 4 5 and you can see we can't connect to it through 3306 is because it's running it's running through Port 3307 on our host machine and the container um Port of 3306 is open um just like we showed uh earlier okay uh with that said this is basically a a real introduction to how to install my squ server uh using dock compos there's a whole bunch more things that you could do but this should get you up and running and uh yeah yeah this is definitely get you up and running um yeah but if you're just going to be running a MySQL server or Maria DB or whatever it is on like a digital Ocean droplet or something I wouldn't bother with with Docker whatsoever I would just get my S up and running by itself because it's just another overhead it's another thing that you've got to have run on the server and uh but if you've got lots and lots of microservices on the same server itself then yes I would use it um but anyway that's another topic for another day um thanks very much uh for watching let's just get this a bit smaller thanks very much for watching I hope you enjoyed this video if you do have any questions please feel free to put them uh in the comments below if you like it dislike it do whatever but if you don't like it let me know why not and if you do like it please also tell me what you did like and if you have any other ideas or anything that I I should do with regards to my squ and so on or any other ideas in general um feel free to um contact me okay that's it thanks very much for watching uh there'll be a video on the screen right now that I either I recommend to you or YouTube recommends to ring recommends to you um thanks then chaow goodbye
Info
Channel: Davids Videos
Views: 2,375
Rating: undefined out of 5
Keywords: #davidsvideos, mysql server, mysql, docker, docker-compose, docker compose up, docker pull mysql, how to create a mysql server using docker, running a mysql service using docker, docker compose, mysql server containerized, how to install mysql server on mac
Id: BQk_CQfmnJg
Channel Id: undefined
Length: 13min 28sec (808 seconds)
Published: Tue Oct 10 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.