Download as pdf or txt
Download as pdf or txt
You are on page 1of 118

Welcome to MySQL

Haim Tzadok
Safe Usage
The material provided here is not free of
errors and should be treated as such.
If you find any error, please email me -
haim.tzadok@grigale.com and I will try my
best to correct any error.

Before using this material, always test and


test again, before implementing !
The technical material may be subject to
changes and should be treated as a
recommendation only.
Table Of Content

1. MySQL Introduction
2. MySQL Clients
3. MySQL Architecture
4. MySQL Basic Security
5. MySQL Configuration
6. MySQL Administration
7. MySQL Monitoring
8. MySQL Replication
9. Backup & Recovery
10. MySQL Advanced Security
11. MySQL Performance Monitoring &
Tuning
WELCOME
Introduction to MySQL
o MySQL Editions
o MySQL Components
o MySQL Installation Brief
MySQL Basic Security
o User Privileges
o User account management
MySQL Clients
o MySQL CLI
o MySQL Workbench
o MySQL Connectors
o MySQL Router
MySQL Architecture
o Internal Architecture
o Information Schema
o Storage Engines
MySQL Configuration
o Server configuration
o Client configuration
MySQL Administration
o OS Service
o Data directory
o Server logs
o Utilities
WELCOME
MySQL Monitoring
o Monitoring using Workbench
o Enterprise Monitoring
MySQL Replication
o Replication formats
o Replication methods
o Binlog Replication
o GTID replication
o Replication topologies
Backup & Recovery
o Logical backup
o Physical backup
Advanced Security
o MySQL Firewall
o MySQL Auditing
o Encryption
MySQL Performance Monitoring
MySQL Introduction
MySQL Introduction
“The wolrd’s most popular open source database”

Objectives

● Overview of MySQL History


● MySQL License and Availability
● MySQL Distributions
● MySQL Israeli Community Activities
● MySQL Editions
● MySQL Components
● MySQL Installation Brief – Windows/Linux
MySQL History

MySQL - started as an OpenSource project for RDBMS. (Initially called


msql)
My - is the name of the daughter of the founder - Monty Widenius
MySQL - Swedish company (MySQL AB)
2008 - it was bought by Sun Microsystems
2010 - Oracle acquired Sun Microsystems

“The wolrd’s most popular open source database”


MySQL Usage and Spread

● MySQL is the second world’s widely used RDBMS.


(Which is the first ?) dbengines ranking –
https://db-engines.com/en/ranking

● Ranking method: #of mentions in websites, frequency searches in


google, # of mentions in technical sites (stackoverflow, DBA stack
exchange), # of job offers, # of profiles in linkedin, # of tweets
MySQL Usage and Spread

● MySQL is the second world’s widely used RDBMS.


(Which is the first ?) dbengines ranking –
https://db-engines.com/en/ranking

● MySQL is known as M in LAMP.

● It is widely used in many Web Application frameworks such


as: Joomla, Drupal, WordPress etc…

● It is widely used by many large companies like: Google,


Facebook, Twitter, Flickr, YouTube,Wikipedia, Booking.com
etc..

● In Israel it is widely used by many companies.


Introduction to MySQL
The world's most popular open source database
MySQL License and Availability

● MySQL is Open Source project licensed under


Dual license - GPL/Commercial

● It is maintained by Oracle/MySQL

● MySQL is multi platform - runs on Linux, Windows,


Solaris, HP-UX, AIX, MAC OS/X and many more.
MySQL Distributions

● MySQL has other forks like:

○ MariaDB

○ Percona

○ Google patches

○ Facebook patches
MySQL Israeli Community activities

● MySQL enjoys large community and wide usage.


● MySQL Israeli User group - is an active Meetup
group –
http://meetup.com/MySQL-User-Group-Israel
● MySQL Acitivities and Conferences in Israel -
hosted by Oracle/MySQL
● Tech tour – March 2018
● Oracle week – November 2018 (2 MySQL seminars)
● MySQL Meetups – every 3~ Months.
● MySQL Workshops – every ½ year.
MySQL Editions

MySQL Product line:

○ MySQL Community edition


• Server, WorkBench, Connectors, utilities

○ MySQL Standard Edition


○ Community + 24/7 Support + Consultative support

○ MySQL Enterprise Edition


○ Standard + Enterprise tools like: MEB, MEM, Firewall, Audit,
Thread Pooling etc…

○ MySQL Cluster Carrier Grade Edition


○ Enterprise + NDB + Cluster
Introduction to MySQL
MySQL Components

• MySQL Server Backup (MEB)


• Workbench (GUI) • MySQL Enterprise
• MySQL Connectors Audit
• MySQL Router • MySQL Enterprise
• MySQL Utilities security (PAM
authentication plugin)
• MySQL Enterprise
Monitor (MEM) • MySQL Thread Pool
• MySQL Enterprise • MySQL Firewall
MySQL Installation Brief

Obtaining MySQL for Windows

● Available .msi or zip distributions


● Download the software from mysql.com
○ Requires Oracle SSO user in order to access
Oracle eDelivery site
○ Two kind of installers -
■ MySQL Web community - which
downloads mysql online
■ MySQL Community - Downloads the whole
installation for offline usage
Obtaining MySQL for Windows
● 4 Installation types:
○ Developer
■ installs Server, Client and excel plugin for MySQL.

○ Server Only
■ Only installs MySQL server

○ Client Only
■ Only installs Workbench MySQL Visual studio
plugin and MySQL Connectors

○ Full
■ installs everything

○ Custom
■ Allows to select what to install
Obtaining MySQL for Windows

● Pre-requisite -
○ In order to use workbench and other
MySQL tools it is better to download and
install - Microsoft Visual C++ 2013
redistributable/runtime package
Obtaining MySQL for Windows

Proceed to execute...
Obtaining MySQL for Linux

● Installation via packages


○ Download the packages as a single zip file from
Oracle eDelivery site
○ Open the zip file
○ Install the packages
○ Prerequisites should be met manually...

● Installation via Repositories -


○ Available CentOS based Linux repository
○ Available Debian flavors Linux repository
○ All prerequisites are handled automatically
Obtaining MySQL for Linux - DEMO

● Installation via packages


○ Download the packages as a single zip file from Oracle eDelivery
site, you may also download the commercial packages.
■ You should identify the right packeges for your Linux
distribution.
■ if you download commercial packages - remember that big
brother is watching...

○ Open the zip file - using the unzip command

○ Install the packages - using deb or rpm command

○ Prerequisites should be met manually…


■ Commonly missing packages are - proj, python etc...
■ To install all packages, you can also use:
yum localinstall *.rpm
Obtaining MySQL for Linux - DEMO

● Installation via Repositories -


○ Available CentOS based Linux repository -
Download the relevant package from the link:
○ http://dev.mysql.com/downloads/repo/yum/
○ Install the package Using:
yum localinstall mysql57-community-release-el6-n.noarch.rpm

○ Fetch MySQL from the repository using the


command:
yum install mysql-community-server
yum install mysql-workbench-community

○ Usually all prerequisites are handled


automatically, provided that you have the
necessary repositories
Obtaining MySQL for Linux - DEMO
Initalizing a DB on MySQL (5.6)
To initialize a database, just run the secure installation script
mysql_secure_installation

This script will ask about root password for the Database. It will remove
anonymous users and so on…

Initializing a DB on MySQL (5.7)


To initialize a fresh database of MySQL 5.7 - just start the
service.
The root password is generated automatically and can be
fetched using the command:
grep 'temporary password' /var/log/mysqld.log

To change the root password:


mysql -uroot -p
ALTER USER ’root’@’localhost’ identified by ’newpassword’
Obtaining MySQL for Linux - DEMO

Handling MySQL Service

● To start MySQL server:


○ service mysqld start (CentOS6)
○ systemctl start mysql (CentOS7)

● To stop MySQL server:


○ service mysqld stop (CentOS6)
○ systemctl stop mysql (CentOS7)

● To get status of MySQL server:


○ service mysqld status (CentOS6)
○ systemctl status mysql (CentOS7)
MySQL Basic Security
MySQL Basic Security

• MySQL Access Privileges

• MySQL User account Management


MySQL Access Privileges

• MySQL Access Privileges


• All the information is stored in MySQL DB.
• User is identified by a combination of - username and host
(userhost).

haim@mysql1 = haim@mysql2

• The wildcard ‘%’ for the host part – represents that a user
can connect from any host except from the localhost.
• A user is a global object, password is not associated with
db,table or routine
MySQL User account Management

Create Remove Status


USER create user haim@’%’ identified by DROP USER haim@’%’ ; select user, host from mysql.user;
‘pass123’;

Privilege GRANT ALL ON *.* TO haim@’%’; REVOKE INSERT ON *.* FROM show grants for user@’host’;
haim@’%’;

Resource CREATE USER haim@'localhost' ALTER USER haim@'localhost' desc mysql.user;


IDENTIFIED BY ‘pass123' WITH WITH select user, host,
limits > MAX_QUERIES_PER_HOUR 20 MAX_CONNECTIONS_PER_HOUR MAX_QUERIES_PER_HOUR,
> MAX_UPDATES_PER_HOUR 10 0; MAX_CONNECTIONS_PER_HOUR
> MAX_CONNECTIONS_PER_HOUR 5 FROM mysql.user;
> MAX_USER_CONNECTIONS 2;

PASSWORD CREATE USER haim@'localhost' ALTER USER haim@'localhost'


IDENTIFIED BY ‘pass123' ; IDENTIFIED BY ‘pass123' ;
MySQL Clients
MySQL Clients
• SQL Review
• MySQL Client/Server Architecture
• Client Types
– CLI clients
• mysql client
• Mysqladmin client
– Application specific clients
• Connectors
• Router
– GUI Clients
• Workbench
SQL Review
SQL
SQL is a database computer language designed for the retrieval and management of
data in relational database. SQL stands for Structured Query Language.
DDL DML DCL
Data Definition Language Data manipulation language Data Control language
SHOW databases SELECT– select rows for viewing CREATE user
SHOW tables INSERT– insert data as rows ALTER user
CREATE database <db_name> UPDATE– update rows RENAME user
DROP database <db_name> DELETE– delete rows DROP user
CREATE table <table_name> TRUNCATE– delete all rows in table GRANT privileges
DROP table <table_name> REVOKE privileges
ALTER – change table columns SET PASSWORD
RENAME – rename table
SHOW CREATE – show information
on the DB or Table.

create table tab1 (ID int, FIRST char(20), LAST char(20));


Insert into db1.tab1 (ID,FIRST,LAST) VALUES (1,’Haim’,’Tzadok’);
SHOW [GLOBAL] VARIABLES LIKE ‘%pass%’;
SQL review
MySQL InnoDB
InnoDB is by default set to ‘autocommit’.

DDL DML
BEGIN/START TRANSACTION BEGIN/START TRANSACTION
CREATE T1 (C1 int); INSERT INTO T1(‘C1’) VALUES (10);
CREATE T2 (C1 int); INSERT INTO T1(‘C1’) VALUES (9);
Rollback; Rollback;
CREATE T3 (C1 int); SELECT * from T1;
SHOW TABLES; INSERT INTO T1(‘C1’) VALUES (7);
Select * from T1;

What is the output ? What is the output ?

create table tab1 (ID int, FIRST char(20), LAST char(20));


Insert into db1.tab1 (ID,FIRST,LAST) VALUES (1,’Haim’,’Tzadok’);
SHOW [GLOBAL] VARIABLES LIKE ‘%pass%’;
MySQL Client/Server Architecture

• All clients must negotiate for credentials via a


tcp/connection or via socket.
• Based on credited privileges, connected clients are then
able to query the server.
• A MySQL distribution provides several types of clients.
• MySQL also releases GUI clients (see Workbench)
Client Types

CLI Clients
• MySQL Command line clients
▪ mysql – a command line client
▪ mysqladmin – a command line client for management

GUI Clients
• Workbench
Application Clients
• MySQL Connectors
• MySQL Router
MySQL Client/Server Architecture

● All clients connect to MySQL using the MySQL Client


API.
● Most do not implement this API, but rather use
connectors or libraries to wrap it up.
● Not all connectors themselves implement the API.
Some rely on the libmysqlclient, for example.
MySQL Command Line Clients

● MySQL ships with many command line clients:


● mysql: a general, interactive/non-interactive client
● mysqladmin: a utility to perform administration tasks
● mysqldump: a client to export database structure
and data
● mysqlshow: a client to query metadata
● mysqlimport: an importing utility
● more in mysql utilities…
Command Line client options
● To connect with a MySQL command line client, one
must supply with credentials:
[root@olp1 ~]# mysql -u root -p
mysql: [Warning] Using a password on the command line interface can be insecure.
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 70
Server version: 5.7.13-enterprise-commercial-advanced-log MySQL Enterprise Server -
Advanced Edition (Commercial)
Copyright (c) 2000, 2016, 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>
mysql client - variables and values
bash:~$ mysql --help FALSE ssl-crlpath (No

… (at the end of output) no-beep default value)

Variables (--variable-name=value) FALSE ssl-verify-server-cert

and Boolean options {FALSE|TRUE} host (No FALSE

Value (after reading options) default value) table

--------------------------------- ---- html FALSE

------------------------------------ FALSE user root


xml safe-updates

debug-info FALSE FALSE

FALSE line-numbers TRUE i-am-a-dummy

database (No unbuffered FALSE

default value) FALSE connect-timeout 0

default-character-set auto column-names TRUE max-allowed-packet

delimiter ; sigint-ignore 16777216

enable-cleartext-plugin TRUE FALSE net-buffer-length

vertical port 0 16384

FALSE prompt select-limit 1000

force mysql> max-join-size

FALSE quick 1000000

named-commands FALSE secure-auth TRUE

FALSE raw show-warnings

ignore-spaces FALSE FALSE

FALSE reconnect TRUE

init-command (No socket

default value) /var/lib/mysql/mysql.sock

local-infile value)
The mysql client
SYNOPSIS
mysql [options] [db_name]
Usually reads /etc/my.cnf and $HOME/.my.cnf
General purpose options include:
--help : shows help and the current Variables options and Boolean values
-h / --host : server host name (default: localhost)
-P / --port : server TCP/IP port (default: 3306)
-S / --socket : UNIX socket file
-u / --user : MySQL login
-U / --safe-updates, --i-am-a-dummy : safe mode
-p / --password : password
--defaults-file : name of configuration file where options are listed.
Output format (Using ; or \G)
Execute queries - ending with ; or \G
A statement ending with ; will produce an output in table format.
mysqlmysql> explain select * from mysql.user;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
| 1 | SIMPLE | user | NULL | ALL | NULL | NULL | NULL | NULL | 4 | 100.00 | NULL |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)

A statement ending with \G will produce an output in list format.

mysql> explain select * from mysql.user\G


*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: user
partitions: NULL
type: ALL
….
filtered: 100.00
Extra: NULL
1 row in set, 1 warning (0.00 sec)
Different Output Format (XML)
Use --xml to get output in XML format

[root@olp1 ~]# mysql -uroot grigale --xml --execute="SELECT * FROM employees


LIMIT 2"
mysql: [Warning] Using a password on the command line interface can be
insecure.
<?xml version="1.0"?>

<resultset statement="SELECT * FROM employees LIMIT 2


" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<row>
<field name="ID">1</field>
<field name="FIRST">Haim</field>
<field name="LAST">Tzadok</field>
</row>
</resultset>
Operations on Databases

show databases – shows available databases.


use <db_name> - select the database you want to use.
create database <db_name> - creates an empty database.
drop database <db_name> - drops a database.
The mysql client
mysql> use grigale;
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_grigale |
+-------------------+
| employees |
+-------------------+
1 row in set (0.00 sec)

mysql>
Using pager

On UNIX/Linux Systems, use pager to manipulate the output of the


result set.

mysql> pager less …


PAGER set to 'less' (END)
mysql> show tables; mysql> pager grep country
+-----------------------------------+ PAGER set to 'grep country'
| Tables_in_opencart | mysql> show tables;
+-----------------------------------+ | oc_country |
| oc_address | 124 rows in set (0.01 sec)
| oc_affiliate |
| oc_affiliate_activity | mysql>
| oc_category_to_layout |
| oc_category_to_store |
| oc_country |


Pager command
Execute pager with no arguments to cancel current pager.
There are some nice tricks with pager.

mysql> pager md5sum;


PAGER set to 'md5sum'
mysql> select * from employees;
758839cc399f41c7f5ecf765f058acd9 -
1 row in set (0.00 sec)

mysql> pager
Default pager wasn't set, using stdout.
mysql>
Log session activities
Log all activity in external text file using tee, Disable with notee.

mysql> tee /tmp/output.txt


Logging to file '/tmp/output.txt'
mysql>
mysql> SELECT 123 FROM grigale.employees;
--------
| 123 |
--------
| 123 |
--------
1 row in set (0.00 sec)
mysql> notee;
Log session activities
To show the activities just use any printing command to view the file that was
saved.

root@mysql1:~# cat /tmp/output.txt


mysql>
mysql> SELECT 123 FROM grigale.employees;
-------
| 123 |
-------
| 123 |
--------
1 row in set (0.00 sec)
Execute SQL from external files
Use SOURCE to execute SQL statements from external files.

mysql> SOURCE /home/haim/test.sql;


Query OK, 1 row affected (0.00 sec)
Query OK, 1 row affected (0.00 sec)
Query OK, 1 row affected (0.00 sec)
...
Using non-interactive sql scripts
The mysql client accepts reads from standard input.
This allows pipelining or redirection:

[root@centv1]# cat /tmp/databases/world.sql | mysql -uroot


world
[root@centv1]# cat /tmp/databases/world.sql \
| sed -e s/ENGINE=MyISAM/ENGINE=InnoDB/g \
| mysql -uroot world
bash# mysql -uroot world < /tmp/databases/world.sql
Using non-interactive mysql
command
Execute a query without getting into interactive shell,
using –e “” /--execute=“”.
This can be further combined with pipelining
[root@olp1 ~]# mysql -u root -p grigale -e "
select count(*) from employees;"
mysql: [Warning] Using a password on the command line interface can
be insecure.
Enter password:
+----------+
| count(*) |
+----------+
| 1 |
+----------+
[root@olp1 ~]#
The mysqladmin client
• Allows for administrative tasks.
• Provide the processlist argument to show pending connections.

[root@olp1 ~]# mysqladmin -uroot -p processlist


Enter password:
+----+------+-----------+----+---------+------+----------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+----+---------+------+----------+------------------+
| 76 | root | localhost | | Query | 0 | starting | show processlist |
+----+------+-----------+----+---------+------+----------+------------------+
[root@olp1 ~]#
The mysqladmin client
• Add -c, -i to make for successive invocations.
• -c : count -i: interval

[root@olp1 ~]# mysqladmin -uroot -p processlist -c 2 -i 1


Enter password:
+----+------+-----------+----+---------+------+----------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+----+---------+------+----------+------------------+
| 79 | root | localhost | | Query | 0 | starting | show processlist |
+----+------+-----------+----+---------+------+----------+------------------+

+----+------+-----------+----+---------+------+----------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+----+---------+------+----------+------------------+
| 79 | root | localhost | | Query | 0 | starting | show processlist |
+----+------+-----------+----+---------+------+----------+------------------+
[root@olp1 ~]#
The mysqladmin client
• Use the status argument to print general purpose status:
• -c : count -i: interval

[root@olp1 ~]# mysqladmin -uroot -p status -c 2 -i 1


Enter password:
Uptime: 1215337 Threads: 1 Questions: 9390 Slow queries: 0 Opens: 589
Flush tables: 1 Open tables: 567 Queries per second avg: 0.007
Uptime: 1215338 Threads: 1 Questions: 9391 Slow queries: 0 Opens: 589
Flush tables: 1 Open tables: 567 Queries per second avg: 0.007
[root@olp1 ~]#
The mysqladmin client
• Use the kill argument forcibly kill a connection

[root@olp1 ~]# mysqladmin -uroot -p processlist


+----+------+-----------+----+---------+------+------------+-------------------------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+----+---------+------+------------+-------------------------------------------------+
| 82 | root | localhost | | Query | 34 | User sleep | select 1 from grigale.employees where
sleep(10000) |
| 84 | root | localhost | | Query | 0 | starting | show processlist |
+----+------+-----------+----+---------+------+------------+-------------------------------------------------+
[root@olp1 ~]# mysqladmin -uroot -p kill 82
[root@olp1 ~]# mysqladmin -uroot -p processlist
+----+------+-----------+----+---------+------+----------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+----+---------+------+----------+------------------+
| 86 | root | localhost | | Query | 0 | starting | show processlist |
+----+------+-----------+----+---------+------+----------+------------------+
[root@olp1 ~]#
The mysqladmin client
Other commands:
• extended-status: dump the result of SHOW
GLOBAL STATUS
• shutdown: shut down the server
• password: change an account's password
• flush-logs: flush + rotate logs
• more...
Connectors

Oracle/MySQL supply with MySQL connectors


These are client libraries, implemented in popular
programming languages:
• Connector/J, JDBC driver for Java
• ADO.NET for the .NET framework
• Connector/ODBC
• Connector/C++
• More...
Work is evolving and new connectors are occasionaly added.
Connectors

3rd parties also provide MySQL connectors:


● libdbd-mysql-perl
● libdbd-mysql-ruby
● Python MySQLdb
● More...
The common connectors are open source, released
under varying licenses.

If using Enterprise Edition:


– It is strongly recommended to use the commercial connectors.
Connectors

The MySQL connectors usually implement the MySQL


Client API, and 3rd party solutions usually rely on
client libraries (e.g. libmysqlclient)
The average user will use the ordinary connector, and
will not dwell into the Client API.
Consult you favorite connector's API to learn how to
connect and query MySQL.
MySQL Router

• MySQL Router is a lightweight middleware that provides


transparent routing between your applications to your MySQL
servers.
• MySQL Router is normally installed in each application server.
• Two scheduling based on mode option are available
▪ Read-write: uses a list with First available routing method.
▪ Read-only: uses round-robin routing method.
▪ Default configuration file:
/etc/mysqlrouter/mysqlrouter.ini
MySQL Router

• Read-only configuration: • Read-Write configuration:


[routing: ro_prod] [routing: rw_prod]
bind_port=7001 bind_port=7002
Destinations = slave1, slave2, slave3 Destinations = master1, master2, master3
mode=read-only mode=read-write
Workbench
Workbench is the default GUI client that comes with MySQL. Available
on Windows/Linux.
Main Features –
• Rich and Sophisticated connection mechanism use SSH to tunnel
and encrypt connection, or use direct connection to DB. (DB can be
local or remote machine)
• SSL Encryption available for direct connection
• Data design & Modeling
• Execute queries and analyze queries
• Status and online monitoring
• Extensive performance monitoring reports
• Full DB Administration
Workbench
Workbench Connection administration

• - Add a connection
- Edit one of the available Connections
Workbench
Workbench Connection details
– by tapping the right upper edge of an existing connection.
Workbench
Main Navigation

Navigator
Use navigator
to navigate
Between
Available
Administration
options
Workbench
Management -> Server status – Shows server Status and online monitoring
Workbench –
Database design and modeling

Workbench Data design and modeling

- Add a new data model file


- Open an existing data model file.
- Create EER diagram from an existing database or script
Workbench – Data Modeling
Database –> Reverse
Engineering
You can create a EER diagram
of your existing database.

Database –> Forward


engineering
You can implement a EER
diagram to a new database.
Workbench – Preferences
Edit – Preferences

You can change the


Behavior of workbench to
fit your needs.

For example –
Show Metadata and
Internal schemas - will Reveal
internal databases Such as mysql,
performance_schema, sys and more…
Combine Managenent Tools and
Schema Tree - will combine/Separate
the management tools From the
Schema tree management.
MySQL Architecture
MySQL Architecture

• MySQL Architecture
• MySQL Information Schema
• MySQL Storage Engines
MySQL Architecture
MySQL Architecture
MySQL Information Schema

• MySQL Information Schema


– Holds database metadata
• Schema design and architecture
• Running processes
• Internal counters
• Replication status
• Storage engine specific information
• Grants and privileges
– A collection of read only views/tables
– Aggregate data from various MySQL components and the
user’s data.
– Allows for SQL access to metadata
– Holds no data – all the information is gathered at runtime.
MySQL Storage Engines

Sotrage Engine History


● Originally MySQL provided ISAM, then MyISAM engines.
● Berkeley DB (BDB) was the first 3rd party storage engine to work
with MySQL.
● InnoDB was later introduced.
● The storage engine API has since evolved.
● MySQL continued developing in house storage engines.
● There is now a large number of MySQL storage engines.
MySQL Storage Engines

What is a storage Engine?


● Every table belongs to a specific storage engine.
● The storage engine is a module responsible for storing table's data and
keys.
● It is responsible for allocating disk storage, memory cache, storing and
fetching of data, providing with information.
● It is similar in concept to file systems: while they basically provide the
same functionality, implementation is different; plus different features can
be added.
MySQL Storage Engines

What is Storage Engine ?


● There is a fine line between MySQL's roles and the SE's roles.
For example:
● Parsing of the query is strictly MySQL's role.
● Building the B-Tree for the index is strictly the SE's role.
● Evaluation the execution plan is MySQL's role, but based on
recommendations from the SE.
MySQL Storage Engines

Storage engine responsibilities


● Data storage
● Index implementations
● Transactional support
● Caching
● Compression
● Constraints
● Additional features (e.g. accept special syntax)
MySQL Storage Engines

Major players
In-house (Oracle/MySQL) storage engines are:
● MyISAM (Non transactional SE, not ACID compliant)
● MERGE
● InnoDB
● MEMORY (Saves all the data in the RAM)
● Blackhole (don’t save data in db files, used for replication shipping)
● CSV (Save the data as .csv files, don’t allow indexes)
● ARCHIVE (Compress the data and archive it)
● Federated
● NDB (as part of MySQL Cluster)
Newer, emerging and gaining popularity:
● XtraDB (InnoDB plugin fork by Percona)
● MariaDB (by Monty Program AB)
Today MySQL recommend using InnoDB for all kind of uses !
MySQL Architecture
MySQL Storage Engines
Which engine do we use ?
Use SHOW commands.

mysql> show create table employees;


+-----------+----------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------+
| Table | Create Table
|
+-----------+----------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------+
| employees | CREATE TABLE `employees` (
`id` int(11) DEFAULT NULL,
`first` char(30) DEFAULT NULL,
`last` char(30) DEFAULT NULL,
KEY `i1` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-----------+----------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
MySQL Architecture
MySQL Storage Engines
InnoDB
Each database has a directory inside the data dir.
*.frm – contains table structure.
*.ibd – contains data and indexes.

show engine innodb status – Shows the status of innodb engine.


Lsn- current log sequence number of the redo logs.

Redo-logs – a mechanism of innodb.


Bin-log – a mechanism in mysql not related to innodb, used for
replication and point-in-time recovery.
MySQL Configuration
MySQL Configuration
AGENDA

• MySQL Server Configuration (/etc/my.cnf mysqld section,


dynamic/static variables – allow/don’t allow changes at runtime)

• MySQL Client Configuration (/etc/my.cnf client section)


• MySQL Variables (show variables like ‘%pass%’, global/session
specific variables, read-only/read-write parameters)

• MySQL Server Status (show status)


MySQL Server Configuration
• Essential parameters:
– port, socket, datadir,

• General parameters:
– init-connect – statement that will be executed upon start of connection
– sql_mode – a set of modes that mysql will comply to when running SQL statements.
– read_only (0=OFF, 1=ON)
– wait_timeout (in seconds)
– InnoDB parameters
– innodb_buffer_pool_size – the amount of memory innodb will use for data & index caches.
– Innodb_log_file_size – size of transaction log size.
– Innodb_file_per_table – required for replication.
– Innodb_lock_wait_timeoiut – seconds to wait before rolling back a locking transaction.

– Workbench -> MANAGEMENT -> Status and System Variables


– Status variables – read only variables
– System variables – configurable variables.
MySQL Variables
• MySQL Variables
– Global parameters
– Session specific parameters.

• MySQL Variables are parameters that comes from my.cnf or as dynamic


parameters.

• Some of them are read only some of them are writable.

• Full list of 5.7 parameters:


http://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html

show variables like ‘%string%’;


show global variables like ‘%string%’;
select @@global.read_only ;
set global max_connections = 1000; - set max connections variable globally
SET sql_log_bin=0; - disable bin logging in the current session
MySQL Server Status

• MySQL Server Status

Metadata
show databases | show tables | show plugins |
show triggers | show indexes| show grants
show create table/procedure/trigger |
MySQL Server Status

status
show status|show master status| show slave
status | show open tables |show engine innodb
status | show [full] processlist |show global
status

• show status like ‘%conn%’;


MySQL Administration
MySQL Administration
AGENDA
• MySQL Service
• MySQL Data Directory
• MySQL Server Logs
• MySQL Multiple Instances
• MySQL Utilities
MySQL Service

Handling MySQL Service

● To start MySQL server:


○ service mysqld start (CentOS 6)
○ systemctl start mysql (CentOS 7)

● To stop MySQL server:


○ service mysqld stop (CentOS 6)
○ systemctl stop mysql (CentOS 7)

● To get status of MySQL server:


○ service mysqld status (CentOS 6)
○ systemctl status mysql (CentOS 7)
MySQL Data Directory

• /etc/my.cnf – default configuration file.


• /var/lib/mysql – Default data dir.
• /var/log/mysqld.log – default log file
• /var/lib/mysql/mysql.sock – default socket
file
• /var/lib/mysql/mysqld.pid – pid file
• /usr/lib64/mysql/plugin – plugins directory
MySQL Server logs
• Bin log - saves events that changes the database
(DDL/DML) including their times, but does not save
show/select events.
– Normally needed for replication between the current master
and it’s slaves.
– When doing backup all changes are stored in the binlog till the
backup is finished and then re-run from the binlog to take
affect.
– Good for point in time recovery.
– Available formats: MIXED (recommended), ROW, STATEMENT
• General log – saves any event that occurred in the
database. (usually enabled when needed more debugging).
• Error log – stores error events, usually under
/var/log/mysqld.log
• Slow log – stores slow query logs (normally more than 10
seconds - in real time), normally does not log slow
administrative or replication statements.
MySQL Utilities
• MySQL Utilities is a package with set of administrative scripts
written in python mainly handling administration, replication,
general usage and more.

• Bin log operation –


• DB operations
• General operations
• HA operations
• Server operations
• Specialized operations
MySQL Monitoring
MySQL Monitoring

• MySQL Monitoring Tools


• Monitoring MySQL using WorkBench
• Monitoring MySQL Server using CLI
• MySQL Enterprise Monitor – DEMO
MySQL Replication
MySQL Replication

• MySQL Replication formats

• MySQL Replication methods

• MySQL Replication topologies


MySQL Replication
• MySQL Replication formats
– SBR – Statement Based Replication
– RBR – Raw Based Replication
– Mixed – Utilize both formats
• MySQL Replication methods
– Replication using Binlog
– Replication using GTID’s
• MySQL Replication topologies
– Master/Slave Topology
– Chained Topology
– Group Replication Topology
MySQL Replication
• MySQL Replication formats
– SBR – Statement based Replication, replication
ships the statements between replicated nodes.

– RBR – Raw based replication, replication ships the


outcome of the execution of statements between
replicated nodes.

– Mixed – Utilize both formats.


MySQL Replication
• MySQL Replication Methods
– Replication using Binlog – Slave fetches transactions
from the Master using binlog position mechanism.

(Each transaction is anonymous transaction)

– Replication using GTID’s – Slave fetches transactions


from the Master using Global Transaction ID’s

(Each transaction has an ID)


MySQL Replication
• MySQL Replication topologies
– Master/Slave(s) S
M
S

– Chained (Master/Intermediate/Slave)
I S
M

M M
– Group Replication
M M
MySQL Replication
MySQL Replication formats

CAP Theorem – Consistency, Availability, Partition Tolerance


Consistency

CP – There is a risk of data CA – Network problem


Becoming unavailable Might stop system.
Ex: MongoDB, Hbase, Memcache Ex: RDBMS Systems
(Oracle, MySQL, …)

Partition PA – Clients may read Availability


Tolerance inconsistent Data
Ex: Cassandra, Riak, CouchDB

We can only have two out of the three.


MySQL Backup & Recovery
MySQL Backup and Recovery

• MySQL Backup Methods


– MySQL logical Backup & Restore

– MySQL physical Backup & Restore


MySQL Backup and Restore
MySQL Backup Methods

• Logical Backup – using mysqldump


– Storage engine independent
– Architecture independent
– Textual backup, easy to manipulate
– Corruption is easily detected.
– Usually very slow…
MySQL Backup and Recovery
MySQL Backup Methods

• Physical(Raw) Backup – using MEB (MySQL


Enterprise Backup)
– Consist of raw copies of directories and files using
snapshot like technology.
– Suitable for large, important databases.
– Usually very fast…
MySQL
Advanced Security
MySQL Advanced Security

• MySQL Firewall
• MySQL Auditing
• MySQL encryption
MySQL Firewall
• MySQL Firewall consists of several plugins and tables in
information_schema and mysql db.
• MySQL Firewall is enabled globally but it will not work till it
is implemented on users.
• Firewall mode on users –
 OFF – Firewall is not implemented on the user.
 RECORDING – Generates white list for the specific user which is
being recorded.
 DETECTING - detect and log any transaction which is not in the
white list.
 PROTECTING - enables the firewall to work in enforcing mode.
(any statement that is not in the white list will be denied).
MySQL Auditing
MySQL Auditing
MySQL Enterprise Audit provides an easy to use, policy-based
auditing solution that helps organizations implement stronger
security controls and satisfy regulatory compliance.

As more sensitive data is collected, stored and used online,


database auditing becomes an essential component of any
security strategy. To guard against the misuse of information,
popular compliance regulations including HIPAA, Sarbanes-
Oxley, and the PCI Data Security Standard require
organizations to track access to information.
MySQL Encryption

Encryption vs TDE
Enabling MySQL Encryption
MySQL Encryption

Encryption vs TDE
Encryption – Data itself is encrypted
No indexes can be enabled on
encrypted column. Data is decrypted
using a private key.
TDE – Transparent Data encryption is
encryption in the file-system layer.
MySQL Performance
Monitoring
MySQL Performance Monitoring

• Buffer Pool, Redo logs and Binlog


• Performance Schema
• SYS Schema
• Monitoring Tools
Buffer Pool, Redo Logs and Binlog
• The Buffer pool is mainly a cache for incoming read/write operations. Every new transaction
is first written to the redo log
• After a while the redo logs are flushed to the data files.
• Binlog is a different mechanism to store logs mainly for replication and point-in-time
recovery.
• If the server is crashed the recovery is reading the redo logs and flush things to disk.

REDO
Bufferpool LOGS
Tab1
Tab2
Tab3

Datadir
datafiles
Performance schema

• A storage engine for collecting performance.


• Collection of sensors that inform about the
time an access has occurred.
• Collection of tables that stores all the timers.
• Usually not easily readable.
SYS Schema
• A storage engine that helps to interpret the
performance schema.
• Collection of views that summarize
performance schema data
• Stored procedures that enables performance
schema operations such as configuration
changes and diagnostic reports.
MySQL Performance
Monitoring and Tuning
Monitoring tools
• Workbench
– Performance -> Performance Reports -> High Cost
SQL Statements
• Mysqlslap – load emulation client
• HeidiSQL – SQL GUI
• MonYog – 3rd party monitoring tool

You might also like