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
BITS: Introduction to MySQL - Introduction and Installation
1. Introduction to MySQL
● Introduction
● Installation
● SQL
● Schema design
● Perl
BITS/VIB Bioinformatics Training – Jun 24, 2011 – Luc Ducazu <luc@daphnia.com>
2.
3. Introduction to MySQL
● Introduction
● Installation
● SQL
● Schema design
● Perl
BITS/VIB Bioinformatics Training – Jun 24, 2011 – Luc Ducazu <luc@daphnia.com>
4. 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
6. 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:
● Sale
Attributes: quantity, timestamp...
7. Relational Databases
● MySQL Workbench:
– graphical representation of entities and relationships
– generates SQL statements to create database & tables
8. 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 Codd
http://en.wikipedia.org/wiki/Codd%27s_12_rules
9. RDBMS
● Commercial products:
– Oracle
– DB2 (IBM)
– MS SQL Server (Microsoft)
● Opensource offerings:
– MySQL (Oracle)
Forks:
● MariaDB
● Drizzle
– PostgreSQL
– SQLite
10. NoSQL
● Keyvalue stores
– Berkeley DB
● Document databases – unstructured data
– CouchDB
– MongoDB
– Cassandra (FaceBook)
● See also:
http://en.wikipedia.org/wiki/Nosql
11. Introduction to MySQL
● Introduction
● Installation
● SQL
● Schema design
● Perl
BITS/VIB Bioinformatics Training – Jun 24, 2011 – Luc Ducazu <luc@daphnia.com>
12. Installing MySQL on Linux
● For DEB based Linux distributions
(Debian, Ubuntu, …)
# aptget install mysqlserver
● For RPM based Linux distributions
(RHEL, Fedora, CentOS, ...)
# yum install mysqlserver
13. 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
14. 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
15. Running MySQL
● To check whether or not mysql is running
correctly:
# service mysql status
mysql start/running, process 3394
# ps ef | grep mysql
mysql 3394 1 0 12:09 ? 00:00:00 /usr/sbin/mysqld
# netstat ltpn | grep mysql
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 3394/mysqld
16. Exercises
● Install MySQL
● Start the service
● Check whether or not the service has been started
17. 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 mysql
or
$ mysql help
18. 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=hname
default: localhost
P prt | port=prt
default: 3306
19. 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 (;)
20. 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?
21. Securing the server
● The process of securing the server is automated
by running the script
# mysql_secure_installation
as root:
– Changes the root password
– Removes anonymous users
– Disallows remote root logins
– Removes the test database
22. 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]
bindaddress = 127.0.0.1
● After restarting the MySQL service, verify with
# netstat ltpn | grep mysql
tcp 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
23. 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...
24. 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.
25. 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
26. 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;
27. 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
inifiles: it contains key=value pairs in [sections]
● In fact, the key=value pairs are provided as
(invisible) command line parameters
28. 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
29. 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
30. 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)
31. Getting Help
● An extensive help system is available in the
MySQL monitor:
mysql> help
This gives an overview of commands you can use
to customize the output
● You can get help on any function or statement:
mysql> help contents
This shows you the broad topics of available
help. You can drill down into any of these topics
32. Getting Help Demo
● How to get help for creating users:
mysql> help
mysql> help contents
mysql> help account management
mysql> help create user
● How to use less as a pager:
$ export PAGER=/usr/bin/less
$ mysql
mysql> pager
PAGER set to '/usr/bin/less'