Home Tutorials Training Consulting Products Books Company Donate Contact us

New Online Training

Quick links


This tutorial explains how to install MySQL and how to create and query a database. It includes important SQL statements which you need to use to create, update and read from database tables.

1. Using the MySQL database

MySQL is a popular relational open source database. Applications can use this database to persist data.

This tutorial covers the installation of MySQL and the usage of the database.

To work in MySQL databases you use SQL. SQL is a query language for relational databases.

2. Installation of MySQL

2.1. Ubuntu

Under Ubuntu you can install MySQL with the following command:

sudo apt-get install mysql-server

The installation procedure allow you to set the root password.

Resetting the database password under Ubuntu

See Ubuntu password reset to learn how to set and reset your password under Ubuntu.

2.2. Windows

Under Windows download the community edition from http://dev.mysql.com/downloads/. For windows you have installer program available which guides you through the installation process.

3. Connecting to MySQL

MySQL provides the mysql command line interface for administrative work. To connect to the MySQL database server use the following command:

# connect as root to MySQL server
mysql -u root -p

3.1. Initial setup of the dabase

To create a database and a user with access rights, use the following commands.

CREATE DATABASE vogella_blog; CREATE USER 'myblog'@'localhost' IDENTIFIED BY 'my_password'; GRANT ALL PRIVILEGES ON * . * TO 'myblog'@'localhost'; GRANT ALL ON vogella_blog.* TO 'myblog'@'localhost' IDENTIFIED BY 'my_password' WITH GRANT OPTION; FLUSH PRIVILEGES;

Once you have created a database on this server you can also connect directory to the database with the following command.

mysql db_name -u user -pmypassword

The default port of the database server is 3306. For example, the Java JDBC connection string to connect to the iotcloud database would be jdbc:mysql://localhost:3306/iotcloud.

4. Further SQL commands

5. Import and export

The mysql command line interface allows you to export your database and import existing backups. The mysqldump command provides the option to create backup of your database, e.g. export your database.

To export the database use the following command.

# creates a dump of the database vogella_blog
mysqldump vogella_blog > vogella_blog.sql -p

To import an existing SQL dump file use the following command.

# vogella_blog.sql is your exported SQL file
# use option -p to enter your password
mysql vogella_blog < /var/www/vhosts/vogella.com/vogella_blog.sql -p

6. About this website

7.1. vogella GmbH training and consulting support


The vogella company provides comprehensive training and education services from experts in the areas of Eclipse RCP, Android, Git, Java, Gradle and Spring. We offer both public and inhouse training. Whichever course you decide to take, you are guaranteed to experience what many before you refer to as “The best IT class I have ever attended”.

The vogella company offers expert consulting services, development support and coaching. Our customers range from Fortune 100 corporations to individual developers.

Copyright © 2012-2017 vogella GmbH. Free use of the software examples is granted under the terms of the EPL License. This tutorial is published under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Germany license.

See Licence.