Setting up your MySQL Database

Before we get started with some interesting things, first you’re going to want to set up your database.  For this tutorial we will be working with MySQL, but feel free to use whatever you’re most comfortable with.  If you already have your database set up, and you are only interested in Spring + Hibernate you can just skip this step completely.  My examples will be run on a mac, but if you have any questions on setting up in a different environment feel free to ask.

Installing MySQL

Head over to https://dev.mysql.com/downloads/mysql/ for the community version, you’ll probably see a few listed but pick the .dmg file.

pickme

Once that has finished downloading, you’ll be prompted through a general setup in which you’ll probably just want to click next until you see the “install” button.

Alright, so you’ve hit “install” and now you get a popup which provides you with your temporary root credentials.

You might not have any idea what that means, but that’s okay just remember to keep this information saved or your next google search might be something like “how do i change my mysql password?? grrr” because if you’re anything like me, you sure as hell won’t be going to the MySQL reference manual.

Creating Your First Database

Great, so you have now successfully installed MySQL and have saved your temporary credentials.  Open up terminal and we’ll get started on creating our first database.

screen-shot-2017-10-06-at-7-17-47-pm.pngType mysql -u root -p and when it prompts you to “Enter Password” type in the temporary one you were given earlier.  Once you’ve entered this password, you’ll likely be asked to change it.  Upon doing so, you should now be able to make use of the mysql command line tool where we can start entering in our SQL statements.  Your screen might look something like this…

Screen Shot 2017-10-06 at 7.21.57 PM


Potential Errors – Skip this chunk if you are able to successfully access the mysql command line tool.

'mysql' is not recognized as an internal or external command, operable program or batch file

If you are receiving the above error after attempting to access mysql via Terminal, there is an issue with your PATH variable.  To fix this, try running export PATH=${PATH}:/usr/local/mysql/bin in Terminal and then repeating the previous step.


Let’s create our first database.  I want to name my database “nintendo” for the purpose of this tutorial, so I’m going to perform CREATE DATABASE nintendo; 

You can name your database whatever you want.  After doing so, your terminal window might kind of look like this…

Screen Shot 2017-10-06 at 7.27.55 PM

This is good.  If we want to start using this database and adding tables to it through terminal, we can execute USE nintendo; which will mean that we have switched to use our newly created database and all of our SQL statements we would like to execute in this session will run against this database.  If we want to see the tables within our new database, we can type SHOW tables; which (at this point) will show an empty result as we have yet to create any.

Screen Shot 2017-10-06 at 7.41.16 PM

If you’ve made it this far, I hope you have been able to successfully install MySQL and create your first database.  For the purpose of this tutorial, we will not be creating any tables through terminal.  Rather, we will be setting up a Spring + Hibernate environment where I will show you how you can take an object-oriented domain written in Java and map it to a relational database.  Are you wondering what the hell this even means?  If so, I’ll walk you through it in the upcoming tutorials.

Have any questions, comments, suggestions?  Or maybe you’re just stumbling across a few issues trying to get your database set up (I know, it’s frustrating). Leave a comment and I’ll be happy to get back to you.