Setting up a MySQL environment
In order to learn SQL, we need to choose a database. The most common choices are MySQL and Postgres. In this series we’ll use MySQL. In this post we’ll show you several different ways to setup a complete MySQL development environment.
MySQL for Windows, Mac and Linux using XAMPP
XAMPP is a great tool because it works on all three major systems: Mac, Windows, and Linux. It also sets up everything you need right out of the box. It includes Apache, MySQL, Perl, and PHP, so when you’re ready to use the database in your applications, everything’s all setup and ready to go.. at least for php.
Here’s a video on how to install XAMPP on Windows, Mac, and Linux using the original version. (They’ve recently started using a VM to install xampp, which is slightly different)
Setup and Install XAMPP on Mac with Xampp-VM
Virtual Machines are taking over the world! And it’s probably a good thing. They’re a little bit more complicated, but provide a lot of benefits. The big one being existing software on your computer has a much lower chance of breaking things in a virtual machine. Here’s how to use XAMPP’s Virtual Machine implementation of xampp.
MySQL using Docker
I’m currently a fan of Docker. It’s an advanced tool that can be difficult to learn, but once you understand how to use it, your life as a developer can become much easier. I assume you have Docker installed if you’re watching this video. Here’s a tutorial on how to setup a MySQL environment in Docker.
We’ll be using Adminer and MySQL instead of Xampp. (I highly recommend Adminer over Xampp when using Docker!!!)
Setting up a MySQL environment is super easy in Docker.
First, make sure you’ve got docker-compose setup so you can use the docker-compose command.
I got most of the code needed right on the Docker page for Mysql: Docker hub’s Mysql page.
The only thing we need to add is “persistence”. Every time you start and stop a docker container, it’s going to use a different “Volume” meaning your database information will not be re-used. You probably don’t want to start from scratch every time, so we need to use a volume.
Create a directory for your project, and create a docker-compose.yml file inside the project directory. Inside the docker-compose.yml file, paste the following code:
# Use root/example as user/password credentials version: '3.1' services: db: image: mysql restart: always volumes: - learnMysql:/var/lib/mysql environment: MYSQL_ROOT_PASSWORD: example adminer: image: adminer restart: always ports: - 8080:8080 volumes: learnMysql:
Now in a terminal, or whatever GUI docker may give you, run $ docker-compose up to start the project. When it’s finished loading you can visit localhost:8080 to visit Adminer, where you can handle all your database interactions.
The username for adminer is root
The password for adminer is example
When you’re done for the day, run $ docker-compose down to turn off the docker container.
Here’s a brief explanation of the above code.
version: ‘3.1’ – Version of docker-compose
services: – begins list of “services” in the docker project.
db: – service name is “db”
image: mysql – uses the mysql image. (all code to use mysql)
restart: always – restart if container is stopped.
volumes: – list of volumes
– learnMysql:/var/lib/mysql – mount “learnMysql” volume to container’s /var/lib/mysql so we can re-use our database information.
MYSQL_ROOT_PASSWORD: example – environment variable.
adminer: – service name is adminer
image: adminer – uses adminer image
restart: always – restarts if stopped.
– 8080:8080 – map port from local machine to adminer container. so we can access it on our machine.
learnMysql: – choose the name for our database volume.
Nice and simple right? That’s it! We’re done and ready to kick some Mysql butt!