Getting started with MySQL and MySQL Workbench

This post is going to take you through –  how to install and query  MYSQL and MySQL Workbench on your local box.

Let’s get started

Let’s begin our installation part.

Install MySQL

Install MySQL via command prompt.

For Mac:

$ brew install mysql

For Ubuntu :

$ sudo apt-get install mysql-server

After installing MySQL, we can access it through below command

$ mysql -u root

Provide MySQL details (username and password) to connect to local mysql. The default password is blank.

 

Note : 

Sometimes you might get this error while running MySQL –

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

This might be because MySql isn’t running. To fix this start MYSQL by this command.

$ brew services start mysql

Install MySQL Workbench

We can download MySQL Workbench from the official site  here.

Download and install the appropriate version based on your operating system.

 

That’s all for installation. Let’s move to querying data.

 

Querying Data

Let’s start the fun part. Here we will query data via 3 different techniques :

  • Directly via console
  • Via MySQL workbench
  • Via Python script

Directly via console

Here you can directly start your query via terminal. Following are some sample query to start with.

$ mysql -u root

 

 

Via MySQL Workbench

Here you can use MySQL Workbench to play with data.

 

Configure MySQL workbench 

 

 

Provide MySQL details (username and password) to connect to local mysql. The default password is blank.

 

 

Click on test connection to verify the connection is successful.

 

 

Now, let’s connect to database.

 

 

 

 

 

Query Data

Let’s begin with simple query.

 

 

 

 

 

Via Python Script

Install MySQL connector : 

Python needs MySQL connector to be able to talk to MySQL database. Use below command to install MySQL connector. Note:

I am using pip3 for python 3, replace pip3 with pip for python 2.x version.

 

$ pip3 install mysql-connector-python

 

Query data :

Let’s query the data via Python. Here we are using Jupyter notebook  to query via Python, but you can use any Python IDE of your choice.

All the code can be found here.

 

 

 

That’s all for this post. Hope it’s helpful.

Cheers !!!

Leave a Reply

Your email address will not be published. Required fields are marked *