UPDATED ON JUNE 2018!


MySQL is the most popular Open Source SQL database management system and it’s developed by Oracle. Databases are a structured collection of data, such as a contact list or shopping list. MySQL server is designed to handle large amounts of data and allows you to add, access and store them in your hard disk.

Installation

As usual, you can install it using the apt package manager. This time let’s add another package that we will need to get php and MySQL communicate with each other.

ronin@Komei:/$ sudo apt-get install mysql-server php7.0-mysql

The packages are quite large, around 100Mb. Once accepted, you will be asked to create a password for the administrative root user. Write down what you insert, you will need it for pretty much every web oriented application.

mysql

The installation will continue creating configuration files and installing dependencies. Now, run the shell script to initialize the MySQL data directory and create the system tables. Run the script with

ronin@Komei:/$ sudo mysqld --initialize

In the reference manual of MySQL 5.5 they recommend to improve the security of the installation by setting the root’s password, by removing the anonymous user account, the test database and privileges that permit anyone to access databases with names that start with test_. Just run the script with

ronin@Komei:/$ sudo mysql_secure_installation

The script will ask you a few simple questions.

Enter current password for root (enter for none): Enter the one you previously set
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

You already have a root password set, so you can safely answer 'n'.

Change the root password? [Y/n] n (we did it already)
 ... skipping.

Remove anonymous users? [Y/n] Y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] Y
 ... Success!

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y
 - Dropping test database...
ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
 ... Failed!  Not critical, keep moving...
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
 ... Success!

Reload privilege tables now? [Y/n] Y
 ... Success!

At this point, your database system has been set up. Let’s test it! You can access to MySQL with:

ronin@Komei:/$ mysql -u root -p **********

“Nooo another prompt!!” – “Yes, that’s a quick way to access and check or modify tables. We will install the well-known graphical interface, phpMyAdmin later”

If you just type

ronin@Komei:/$ mysql
mysql ERROR 1045 (28000): Access denied for user 'you'@'localhost' (using password: NO)
ronin@Komei:/$ mysql -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

The only way not to show your passoword while writing is

ronin@Komei:/$ mysql -u root -p
Enter password: **********
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 50
Server version: 5.5.38-0ubuntu0.14.04.1 (Ubuntu)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

Basic commands

Let’s first take a look around. Use the ‘show databases;‘ command to display all databases. The ‘\u‘ command to change or access db.

mysql> show databases;

| Database          
+------------------------------+
| information_schema 
| mysql              
| performance_schema 
+------------------------------+
3 rows in set (0.00 sec)

mysql> \u information_schema
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

mysql> show tables;
+---------------------------------------+
| Tables_in_information_schema          |
+---------------------------------------+
| CHARACTER_SETS                        |
| COLLATIONS                            |
| COLLATION_CHARACTER_SET_APPLICABILITY |
| COLUMNS                               |
| COLUMN_PRIVILEGES                     |
| ENGINES                               |
| EVENTS                                |
| FILES                                 |
| GLOBAL_STATUS                         |
| GLOBAL_VARIABLES                      |
| KEY_COLUMN_USAGE                      |
| PARAMETERS                            |
| PARTITIONS                            |
| PLUGINS                               |
| PROCESSLIST                           |
| PROFILING                             |
| REFERENTIAL_CONSTRAINTS               |
| ROUTINES                              |
| SCHEMATA                              |
| SCHEMA_PRIVILEGES                     |
| SESSION_STATUS                        |
| SESSION_VARIABLES                     |
| STATISTICS                            |
| TABLES                                |
| TABLESPACES                           |
| TABLE_CONSTRAINTS                     |
| TABLE_PRIVILEGES                      |
| TRIGGERS                              |
| USER_PRIVILEGES                       |
| VIEWS                                 |
| INNODB_BUFFER_PAGE                    |
| INNODB_TRX                            |
| INNODB_BUFFER_POOL_STATS              |
| INNODB_LOCK_WAITS                     |
| INNODB_CMPMEM                         |
| INNODB_CMP                            |
| INNODB_LOCKS                          |
| INNODB_CMPMEM_RESET                   |
| INNODB_CMP_RESET                      |
| INNODB_BUFFER_PAGE_LRU                |
+---------------------------------------+
40 rows in set (0.00 sec)

Ok, we explored the information_schema database and displayed its tables.

Next, create two databases: rovers and humanoids.

mysql> create database rovers;
Query OK, 1 row affected (0.00 sec)
mysql> create database humanoids;
Query OK, 1 row affected (0.00 sec)

Check if the databases have been created successfully with the ‘show databases;’ command.

Create users and grant permissions

If you are used to access with your root account, for security’s sake, it’s better to change your habits. Create two users, usera with password ‘pssa’ and userb with ‘pssb’.

mysql> CREATE USER 'usera'@'localhost' IDENTIFIED BY 'pssa';
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE USER 'userb'@'localhost' IDENTIFIED BY 'pssb';
Query OK, 0 rows affected (0.00 sec)

If you exit and access with your new users, it will be impossible to access any db. You need more power. We will give privileges to usera to access the rovers db and userb to access the other one.

mysql> grant all privileges on rovers.* TO 'usera'@'localhost';
Query OK, 0 rows affected (0.00 sec)

mysql> grant all privileges on humanoid.* TO 'userb'@'localhost';
Query OK, 0 rows affected (0.00 sec)

The syntax for command is:

GRANT [type of permission] ON [database name].[table name] TO ‘[username]’@’localhost’;

Reload the privileges and you’re done.

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

Test your changes

Exit Mysql, log in with ‘usera‘ and try to use the ‘humanoid‘ database and then ‘rovers‘ db

mysql> exit
Bye
ronin@Komei:/$ mysql -uusera -ppssa
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 53
Server version: 5.5.38-0ubuntu0.14.04.1 (Ubuntu)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> \u humanoid
ERROR 1044 (42000): Access denied for user 'usera'@'localhost' to database 'humanoid'
mysql> \u rovers
Database changed

Get rid of the terminal and install phpMyAdmin

PhpMyAdmin provides a web interface to interact with MySQL. Let’s install it.

ronin@Komei:/$ sudo apt-get install phpmyadmin

In order to configure your installation correctly

  • Select Apache2 for server selection. Hit space to select the option, then tab to select Ok and finally Enter.
  • Press Yes to use dbconfig-common
  • Enter your administrative MySQL password
  • Choose and confirm a password for phpMyAdmin program

You can reconfigure phpMyAdmin by running

ronin@Komei:/$  sudo dpkg-reconfigure phpmyadmin

Once PhpMyAdmin has been correctly installed and configured you can now access control panel in your browser. Just go to http://localhost/phpmyadmin

Finally login with your administrative MySQL credential and use phpMyAdmin with your graphical interface.

Reference