BITS: Introduction to MySQL - Introduction and Installation

32
Introduction to MySQL Introduction Installation SQL Schema design Perl BITS/VIB Bioinformatics Training – Jun 24, 2011 – Luc Ducazu <[email protected]>

description

BITS: Introduction to relational databases and MySQL - Module 1: Introduction and Installation See http://www.bits.vib.be/index.php?option=com_content&view=article&id=17204047:green-basics-of-databases&catid=81:training-pages&Itemid=190

Transcript of BITS: Introduction to MySQL - Introduction and Installation

Page 1: BITS: Introduction to MySQL - Introduction and Installation

Introduction to MySQL

● Introduction● Installation● SQL● Schema design● Perl

BITS/VIB Bioinformatics Training – Jun 24, 2011 – Luc Ducazu <[email protected]>

Page 2: BITS: Introduction to MySQL - Introduction and Installation
Page 3: BITS: Introduction to MySQL - Introduction and Installation

Introduction to MySQL

● Introduction● Installation● SQL● Schema design● Perl

BITS/VIB Bioinformatics Training – Jun 24, 2011 – Luc Ducazu <[email protected]>

Page 4: BITS: Introduction to MySQL - Introduction and Installation

Databases

● A database is a collection of data– numbers– dates– text or labels– …

● A Database Management System– Data storage– Data retrieval– Data manipulation– Authentication & Authorization

Page 5: BITS: Introduction to MySQL - Introduction and Installation

Relational Databases

● Rigid structure● 2 dimensional tables: 

– columns (fields)– rows (records)

Page 6: BITS: Introduction to MySQL - Introduction and Installation

Relational Databases

● Model objects (entities) and their relationships● Eg a store sells products to customers

– Entities:● Customers

Attributes: name, address, telephone number...● Products 

Attributes: name, price...– Relationships:

● SaleAttributes: quantity, timestamp...

Page 7: BITS: Introduction to MySQL - Introduction and Installation

Relational Databases

● MySQL Workbench:– graphical representation of entities and relationships– generates SQL statements to create database & tables

Page 8: BITS: Introduction to MySQL - Introduction and Installation

Relational Database Management Systems (RDBMS)

● Enforce data intergrity:Honors constraints on columns

● Enforce referential integrity:Honors constraints on relations

● See also: the 12 rules of Edgar Coddhttp://en.wikipedia.org/wiki/Codd%27s_12_rules

Page 9: BITS: Introduction to MySQL - Introduction and Installation

RDBMS

● Commercial products:– Oracle– DB2 (IBM)– MS SQL Server (Microsoft)

● Open­source offerings:– MySQL (Oracle)

Forks:● MariaDB● Drizzle

– PostgreSQL– SQLite

Page 10: BITS: Introduction to MySQL - Introduction and Installation

NoSQL

● Key­value stores– Berkeley DB

● Document databases – unstructured data– CouchDB– MongoDB– Cassandra (FaceBook)

● See also: http://en.wikipedia.org/wiki/Nosql

Page 11: BITS: Introduction to MySQL - Introduction and Installation

Introduction to MySQL

● Introduction● Installation● SQL● Schema design● Perl

BITS/VIB Bioinformatics Training – Jun 24, 2011 – Luc Ducazu <[email protected]>

Page 12: BITS: Introduction to MySQL - Introduction and Installation

Installing MySQL on Linux

● For DEB based Linux distributions(Debian, Ubuntu, …)# apt­get install mysql­server

● For RPM based Linux distributions(RHEL, Fedora, CentOS, ...)# yum install mysql­server

Page 13: BITS: Introduction to MySQL - Introduction and Installation

Installing MySQL on Windows

● An installable (MSI) package is available on the MySQL site:http://www.mysql.com/– Follow the 'Downloads (GA)' link– Choose 'MySQL Community Server'– Select 'Microsoft Windows' as platform

Page 14: BITS: Introduction to MySQL - Introduction and Installation

Running MySQL

● To start / stop / restart the MySQL service:# service mysql start# service mysql stop# service mysql restart

● When starting MySQL for the first time, the system administrator is reminded that the MySQL setup is not yet secured

Page 15: BITS: Introduction to MySQL - Introduction and Installation

Running MySQL

● To check whether or not mysql is running correctly:

# service mysql statusmysql start/running, process 3394

# ps ­ef | grep mysqlmysql  3394  1  0 12:09 ?  00:00:00  /usr/sbin/mysqld

# netstat ­ltpn | grep mysqltcp 0 0  0.0.0.0:3306  0.0.0.0:*  LISTEN  3394/mysqld 

Page 16: BITS: Introduction to MySQL - Introduction and Installation

Exercises

● Install MySQL● Start the service● Check whether or not the service has been started

Page 17: BITS: Introduction to MySQL - Introduction and Installation

The MySQL monitor

● To connect or log on to a MySQL database service:$ mysql

● The MySQL monitor has many options, you can review them using:$ man mysqlor $ mysql ­­help

Page 18: BITS: Introduction to MySQL - Introduction and Installation

The MySQL monitor

● The most important options are:$ mysql [options] [database] ­u uname | ­­user=uname

default: UNIX account ­p [pwd]| ­­password[=pwd]

default: <none>if pwd not given, prompt for password

 ­h hname | ­­host=hnamedefault: localhost

 ­P prt | ­­port=prtdefault: 3306

Page 19: BITS: Introduction to MySQL - Introduction and Installation

The MySQL monitor

● Once connected to the database server, you can execute SQL statements:mysql> statement;

● Every SQL statement should end with a semi­colon (;)

Page 20: BITS: Introduction to MySQL - Introduction and Installation

Exercises

● Make sure you do these exercises as a normal UNIX user, and not as root.

● Connect to the database and execute the following SQL statements:mysql> select current_user;mysql> show databases;

● Connect to the databases as user root and execute the same statements.

● Do you understand the (security) implications?

Page 21: BITS: Introduction to MySQL - Introduction and Installation

Securing the server

● The process of securing the server is automated by running the script# mysql_secure_installationas root:– Changes the root password– Removes anonymous users– Disallows remote root logins– Removes the test database

Page 22: BITS: Introduction to MySQL - Introduction and Installation

Securing the server

● As an extra precaution, we will prevent any external access to the database server. This is done by putting the following line in the global config file (/etc/mysql/my.cnf) (*):[mysqld]bind­address = 127.0.0.1

● After restarting the MySQL service, verify with# netstat ­ltpn | grep mysqltcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 1228/mysqld

● (*) On standard MySQL installations, the global config file is /etc/my.cnf

Page 23: BITS: Introduction to MySQL - Introduction and Installation

Exercises

● Secure your MySQL installation● Repeat the last exercise:

– it is no longer possible to log in with your ordinary UNIX account ­ why ?

– you can still login as root, but with a twist...

Page 24: BITS: Introduction to MySQL - Introduction and Installation

Database Users

● In principle, database users and OS users are completely independent from each other:– If no user is specified when executing mysql, 

the OS user is taken– The database superadmin is called root@localhost

This user can do anything, including dropping databases

● It is not a good idea to always connect to the DB as root. Try to log in as a less privileged user as much as possible.

Page 25: BITS: Introduction to MySQL - Introduction and Installation

Database Users

● To create a database user, connect to the database server as root and issue the following statement:mysql> create user dbuser[@host]       [identified by 'passwd'];

● In this statement is:– dbuser: the user to be created– host: the hostname from which the user is going to 

connect ­ if not specified the user can connect from any host (%)

– passwd: the password needed to connect to the database server

Page 26: BITS: Introduction to MySQL - Introduction and Installation

Exercises

● Create a database user:– the database user has the same name as your UNIX 

account– the hostname is localhost– you are free to choose the password

● Try to connect as this user and execute the following SQL statements:mysql> select current_user;mysql> show databases;

Page 27: BITS: Introduction to MySQL - Introduction and Installation

The options file

● To avoid having to type your password every time you connect to the database service, you can create an options file:– the file name is .my.cnf– this file is located in your home directory– since it might contain a password, protect it from 

preying eyes: mode 600● The format of .my.cnf is similar to Windows 

ini­files: it contains key=value pairs in [sections]● In fact, the key=value pairs are provided as 

(invisible) command line parameters

Page 28: BITS: Introduction to MySQL - Introduction and Installation

The options file

● As an example, the password will be put in an options file.

● Looking at the command line parameters of mysql (and almost all client applications), the password can be provided as:$ mysql ­­password=pwd

● The options file contents could look like this:[client]password=pwd

Page 29: BITS: Introduction to MySQL - Introduction and Installation

Exercises

● Create an options file and put the password in● Make sure the options file is protected on the OS 

level● Try to connect to the database without specifying 

a password

Page 30: BITS: Introduction to MySQL - Introduction and Installation

Database User Privileges

● The created user has very limited privileges. To grant privileges prv on table tbl in database db, you need to execute the following statement:mysql> grant prv on db.tbl                   to user@host;

● Some convenient wild cards:– To grant all privileges, specify all as prv– To include all databases, specify * as db– To include all tables, specify * as tbl

● The given database and table names do not have to exist (yet)

Page 31: BITS: Introduction to MySQL - Introduction and Installation

Getting Help

● An extensive help system is available in the MySQL monitor:mysql> helpThis gives an overview of commands you can use to customize the output

● You can get help on any function or statement:mysql> help contentsThis shows you the broad topics of available help. You can drill down into any of these topics

Page 32: BITS: Introduction to MySQL - Introduction and Installation

Getting Help ­ Demo

● How to get help for creating users:mysql> helpmysql> help contentsmysql> help account managementmysql> help create user

● How to use less as a pager:$ export PAGER=/usr/bin/less$ mysqlmysql> pagerPAGER set to '/usr/bin/less'