BITS: Introduction to MySQL - Introduction and Installation
description
Transcript of 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]>
Introduction to MySQL
● Introduction● Installation● SQL● Schema design● Perl
BITS/VIB Bioinformatics Training – Jun 24, 2011 – Luc Ducazu <[email protected]>
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
Relational Databases
● Rigid structure● 2 dimensional tables:
– columns (fields)– rows (records)
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...
Relational Databases
● MySQL Workbench:– graphical representation of entities and relationships– generates SQL statements to create database & tables
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
RDBMS
● Commercial products:– Oracle– DB2 (IBM)– MS SQL Server (Microsoft)
● Opensource offerings:– MySQL (Oracle)
Forks:● MariaDB● Drizzle
– PostgreSQL– SQLite
NoSQL
● Keyvalue stores– Berkeley DB
● Document databases – unstructured data– CouchDB– MongoDB– Cassandra (FaceBook)
● See also: http://en.wikipedia.org/wiki/Nosql
Introduction to MySQL
● Introduction● Installation● SQL● Schema design● Perl
BITS/VIB Bioinformatics Training – Jun 24, 2011 – Luc Ducazu <[email protected]>
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
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
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
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
Exercises
● Install MySQL● Start the service● Check whether or not the service has been started
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
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
The MySQL monitor
● Once connected to the database server, you can execute SQL statements:mysql> statement;
● Every SQL statement should end with a semicolon (;)
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?
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
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 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
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...
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.
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
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;
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
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
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
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)
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
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'