You are on page 1of 222

MariaDB for DBAs

MariaDB Training
Introduction
MariaDB for DBAs

Introduction Client Utilities Storage Engines Partitioning Monitoring & Troubleshooting MariaDB Replication
Architecture Configuration Table Schema User & Server Security Back-Ups & Restoration High Availability
Installation
 Resource Usage
 Views 
 Log Files
 Customizing
 Optimization
Introducing MariaDB Ab
Founders from MySQL the
Company and Community
Funded by Founders, Employees,
and Venture Capital
Over 100 Employees
Several former MySQL Employees
and Community Members in
over 14 Countries

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 3
Personal Introductions
Instructor
Name and Background
Participants
Name and Company
MariaDB Experience
How You Use MariaDB
Needs Related to Course Topics

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 4
Class Schedule & Personal Concerns
Starting and Ending Times
Planned Breaks
On- Site
Location of Rest Rooms
Smoking Areas
Snacks and Drinks
LVC Classes
Chat with Everyone

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 5
Course Outline
Overview & Server Core Admin. Tasks
Architecture User & Server Security
Installation Logs Files
Client Utilities Monitoring &
Configuration Troubleshooting
Resource Usage Back-Ups & Restoration
Schema Choices Advanced Topics
Storage Engines Customizing
Table Schema MariaDB Replication
Views High Availability
Partitioning
 Optimization

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 6
Architecture
MariaDB for DBAs

Introduction Client Utilities Storage Engines Partitioning Monitoring & Troubleshooting MariaDB Replication
Architecture Configuration Table Schema User & Server Security Back-Ups & Restoration High Availability
Installation
 Resource Usage
 Views 
 Log Files
 Customizing
 Optimization
MariaDB Terrain

SQL Tier — Clients


Storage Engine
DBA
socket Tier
file
ssh & tcp/ip mysql Server Tier
MyISAM

mysqld Handler Interface


socket file InnoDB
Memory
cron mysqladmin

application
(e.g., php)
Archive

web tcp/ip
user

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 8
Client Connections
TCP/IP Connections available on All Platforms
--skip-networking (disables)

Socket Files available on Unix Platforms - fastest choice


Named Pipe and Shared Memory available on Windows
--enable-named-pipe, --shared-memory

MariaDB Connections require little resources and easy to open.


Most use External Connection Pools (not needed usually)
Set Global Client Connection Limit (max_connections=n)

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 9
Client Libraries and the Like
Various Libraries for connecting to MariaDB — Most
are Wrappers for MariaDB C API, which uses
MariaDB Network Protocol
PHP API, Perl DBI, Python, Ruby
MariaDB Connector for Java
Native C/C++ driver
MariaDB ODBC Drive
Embedded MariaDB (libmysqld)

Information on MariaDB Connectors: https://mariadb.com/products/connectors-plugins

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 10
MariaDB Query Process

Clients

Thread Cache
Storage
Aria
Engines
Query Cache
InnoDB
Memory
SQL Parser

Optimizer
Archive

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 11
Thread Cache
Thread is Assigned to Each Thread Cache
Connection
Threads may be Reused from the Query Cache

Thread Cache or Created


SQL Parser

Optimizer

Storage
Engines

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 12
Thread Pool
Main Thread Listens for Client Connections Thread Pool

User Authentication based on Host, User, and


Password Query Cache

Client Buffers for Session Variables and Network


SQL Parser
Communications

Optimizer
thread_handling = pool-of-threads
thread_pool_size = 3
Excerpt from /etc/my.cnf.d/server.cnf Storage
Engines

Documentation on Thread Pool: https://mariadb.com/kb/en/mariadb/thread-pool-in-mariadb/#using-thread-pool-scheduler

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 13
Query Cache
Stores SELECT Query Result Sets Thread Cache
No Longer
Recommended
Useful for High Read, Low Write
Servers Query Cache

Cache Purged when Related Data


SQL Parser
Changed
SHOW VARIABLES LIKE Optimizer
'query_cache_type';

Possible to Stipulate in a Query Storage


Engines
SELECT SQL_CACHE * FROM ...
SELECT SQL_NO_CACHE * FROM ...

Documentation on Query Cache: https://mariadb.com/kb/en/mariadb/query-cache/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 14
SQL Parser
Converts SQL Text to Binary Format Thread Cache

Parses SQL into Tokens (i.e., keyword, table, field, value)


Applies Grammar Rules to check Validity (Lexical Query Cache

Scanner, Grammar Rules Module)


SQL Parser
Construct a Parse Tree to be passed to Optimizer

Optimizer

Storage
Engines

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 15
Optimizer
The Optimizer Reads the Parse Tree and Determines Thread Cache
an Execution Plan
Query Cache
Locate any Related Indexes
Compare Efficiency of Index Access to Table Scan SQL Parser

Determine JOIN order of Table


Optimizer
Eliminate Unnecessary Tables and WHERE clauses
Find Indexes that can handle GROUP BY and ORDER Storage

BY Engines

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 16
Installation
MariaDB for DBAs

Introduction Client Utilities Storage Engines Partitioning Monitoring & Troubleshooting MariaDB Replication
Architecture Configuration Table Schema User & Server Security Back-Ups & Restoration High Availability
Installation
 Resource Usage
 Views 
 Log Files
 Customizing
 Optimization
Installation Methods & Packages
Installation Tools (MariaDB, Oracle, Percona)
Built and Tested by Experts
Installer (e.g., yum)
Use Up-to-Date Versions
Distribution Packages (rpm, deb, pkg, dmg)
Easy and Quick to Install
Need to Configure Manually
May be Old Versions
Build from Source — Useful for Custom Patches
Required for Non-Standard Platforms

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 18
Installing with Binary Files
Package Managers
rpm -Ivh
yum install
dpkg -i
apt-get install
Configuration File (datadir, etc.)
Execute manually mysql_install_db
InnoDB Log File Sizes in my.cnf
Service Scripts (i.e., /etc/init.d/mysql start|stop|restart)

Download MariaDB: https://downloads.mariadb.org/mariadb/


Repository Configuration Tool: https://downloads.mariadb.org/mariadb/repositories/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 19
Basics Steps to Install Tar Files
Download and Extract Tar Zip File
Configuration File (/etc/my.cnf or c:\my.ini)

Add System User, mysql


Data Directory (owner, mysql)
Install the System Tables (scripts/mysql_install_db)
Install the mysql.server script (e.g., /etc/init.d)
MariaDB binaries in $PATH
Start Server
Run mysql_secure_installation

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 20
Installing with Source Files
System Build Environment
Install a Compiler (e.g., gcc), Libraries, Headers, etc.
Build Scripts for various Platforms (see ./build/)
Unix — Normal Configure and make Process (./
configure --help)

Windows — get Visual C++ 6.0 or Higher


Configure System to use Same Process as Binary

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 21
Upgrade Plan
Consideration
Review Change Logs (major and minor version upgrades)
Major Upgrade is Safest — with Backup and Reload
MySQL 5.1 to 5.5, 5.6; MySQL 5.5 to MariaDB 10.1;
MySQL 5.5 to MariaDB 5.5
Minor Upgrade (mysql_upgrade)
Required Downtime
Preparation
Back-Up Data
Update Applications and Libraries
Use a Test Server
Use Replication — Upgrade Slave, Promote It, then Upgrade Master

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 22
Client Utilities
MariaDB for DBAs

Introduction Client Utilities Storage Engines Partitioning Monitoring & Troubleshooting MariaDB Replication
Architecture Configuration Table Schema User & Server Security Back-Ups & Restoration High Availability
Installation
 Resource Usage
 Views 
 Log Files
 Customizing
 Optimization
mysql Client — Command Line Tool (CLI)
Shell or ssh
Execute Manually any SQL statement
Display mysqld settings and status counters
Use as Interactive Session, or Pipe SQL via Shell

shell> mysql --user root -p

mysql> SHOW DATABASES;

mysql -p -u user_name --execute "SHOW DATABASES"

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 24
mysqladmin — Command Line Tool (CLI)
Shell or ssh
Manage User Accounts, Passwords, Permissions
Display mysqld settings and status counters
View and Kill Active Connections
Ping or Shutdown mysqld
Create and Drop Databases

mysqladmin processlist
mysqladmin -u root -p create employees
mysqladmin extended-status

Documentation on mysqladmin: https://mariadb.com/kb/en/mariadb/mysqladmin/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 25
Other Client Utilities
mysqldump
mysql_upgrade
my_print_defaults

mysqlslap
sysbench
mysqlcheck
Percona Toolkit

Documentation on mysqldump: https://mariadb.com/kb/en/mariadb/mysqldump/


Documentation on mysql_upgrade: https://mariadb.com/kb/en/mariadb/mysql_upgrade/
Documentation on my_print_defaults: https://mariadb.com/kb/en/mariadb/my_print_defaults/
Documentation on mysqlslap: https://mariadb.com/kb/en/mariadb/mysqlslap/
Documentation on mysqlslap: https://mariadb.com/kb/en/mariadb/mysqlcheck/
Link to Percona Toolkit: https://www.percona.com/software/database-tools/percona-toolkit

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 26
Other Command-Line Tools
mysql.server mysqldumpslow
mysqld_safe myisamchk *
mysql_fix_privilege_tables pt-query-digest
mysql_install_db innochecksum
mysql_tz_to_sql
myisampack *
mysqlbinlog
* Caution: These MyISAM Tools Directly Access the Files

Documentation on mysql.server: https://mariadb.com/kb/en/mariadb/mysqlserver/


Documentation on mysqld_safe: https://mariadb.com/kb/en/mariadb/mysqld_safe/
Documentation on mysql_install_db: https://mariadb.com/kb/en/mariadb/mysql_install_db/
Documentation on mysqldumpslow: https://mariadb.com/kb/en/mariadb/mysqldumpslow/
Documentation on pt-query-digest: https://www.percona.com/doc/percona-toolkit/2.2/pt-query-digest.html

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 27
HeidiSQL
GUI Application (MS Windows)
Connect via SSL or SSH tunnel

Schema and Query Construction


Optimization and Profiling
User Account Management
Task and Backup Scheduling
Data Migration and Cleaning
Schema Sync and Diff Tools
Notifications

Site for HeidiSQL: http://www.heidisql.com/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 28
SQLyog
Graphical Interface for Executing
Queries
Schema Changes
Make Back-Ups
Bulk Import Data

Site for SQLyog: https://www.webyog.com/product/sqlyog

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 29
Configuration
MariaDB for DBAs

Introduction Client Utilities Storage Engines Partitioning Monitoring & Troubleshooting MariaDB Replication
Architecture Configuration Table Schema User & Server Security Back-Ups & Restoration High Availability
Installation
 Resource Usage
 Views 
 Log Files
 Customizing
 Optimization
Configuration File Locations
Linux Systems Windows
/etc/my.cnf c:\Windows\my.ini
/etc/my.cnf.d/*.cnf c:\my.ini
--defaults-extra-file %PROGRAMDATA%\MySQL\MySQL Server
~/.my.cnf (clients) x.x\my.cnf
%WINDIR%\my.cnf
INSTALLDIR\my.cnf
--defaults-extra-file

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 31
Server Defaults
Finding Defaults: mysqld --print-defaults

mysqld would have been started with the


following arguments:
--datadir=/data/mysql --socket=/var/lib/mysql/
mysql.sock --user=mysql --symbolic-links=0

Configuration File (my.cnf): [mysqld]


datadir=/data/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

!includedir /etc/my.cnf.d

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 32
Global Variables
Global Variables are System Wide
SHOW GLOBAL VARIABLES;

Can be Changed with SET Statement


Set in Configuration File at Start

SET GLOBAL
tmp_table_size = 32*1024*1024;
Requires SUPER privilege for mysql.plugin table

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 33
Global Status
SHOW STATUS provides Counters and
Thresholds of Internal Statistics

SHOW STATUS LIKE


'%innodb_buffer_pool%';

Check Status Variables to Decide on


Adjustments to Global Variables

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 34
Session Variables
Session Variables are Values for Current Connection
Global Variable Values is Default for Session

SHOW GLOBAL VARIABLES LIKE SHOW SESSION VARIABLES LIKE


'%character_set_client%'; '%character_set_client%';

+----------------------+--------+ +----------------------+-------+
| Variable_name | Value | | Variable_name | Value |
+----------------------+--------+ +----------------------+-------+
| character_set_client | latin1 | | character_set_client | greek |
+----------------------+--------+ +----------------------+-------+

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 35
Resource Usage
MariaDB for DBAs

Introduction Client Utilities Storage Engines Partitioning Monitoring & Troubleshooting MariaDB Replication
Architecture Configuration Table Schema User & Server Security Back-Ups & Restoration High Availability
Installation
 Resource Usage
 Views 
 Log Files
 Customizing
 Optimization
Global Memory
Global Memory Buffers are Allocated once when mysqld Starts
table_open_cache
table_definition_cache
query_cache_size
thread_cache

Permissions Tables
Global Memory Buffers are also Allocated for Storage Engines
MyISAM: key_buffer_size
InnoDB: innodb_buffer_pool_size,
innodb_additional_mem_pool_size,
innodb_log_buffer_size

Storage Engines may Allocate internally Other Memory

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 37
Session Memory
Buffers Allocated as required for each Client Connection
Some Allocated more than once for Joins and Sorting
Released when Query is Finished or Client Session Closed
Persistent Connections need to be Reset
Client Session Memory Variables:
thread_stack read_rnd_buffer_size
read_buffer_size max_heap_table_size
join_buffer_size max_allowed_packet
bulk_insert_buffer_size sort_buffer_size
binlog_cache_size tmp_table_size
net_buffer_length myisam_sort_buffer_size

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 38
Estimating Memory Usage
Worst Case, Maximum Memory Usage (sum(session_buffers) * max_connections)

Scenario: + sum(global_buffers) = total memory usage

Chances of All Clients Allocating All Possible Basic Formula for Estimating Memory Usage

Session Buffers at once is usually Small


Normal Practice is to Commit Over Memory
Requirements
Out-of-Memory (OOM) may occur if mysqld
is near System Memory Limits
Add Memory or Use a Crash-Safe Storage
Engine
Use SWAP space, but Avoid letting MySQL
Swap for Normal Load

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 39
Temporary Disk Space
MariaDB Uses Temporary Disk Space
Large Implicit Temporary Tables for Resolving Queries
Some Sort Operations (ORDER BY, GROUP BY)
tmpdir defaults to system temporary location
Some Unix platforms mount /tmp as tmpfs
slave_load_tmpdir for Replicating LOAD DATA INFILE
Defaults to Location of tmpdir — Never use tmpfs
Slave Exports from binlog to here, then LOAD DATA
INFILE

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 40
Disk Speed
Disk Speed is important when a Data Set
won’t Fit in Memory
Disk Reads are Alleviated by Caching
Some Storage Engines Implement their own
Caches
Some Rely on OS Disk Caching Mechanism

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 41
Disk Redundancy — Preparing for Disk Failure
RAID, SAN or other Redundant, Distributed Storage
Solutions
Good Disk Failure Protection
MariaDB Replication Slave
Because Asynchronous, will lose some data (see
sync_binlog)

DRBD
Slower Writes

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 42
File Handles
Logs and Replication Files need to be Open
Client Connections each need a File (socket =
file_handle)

Storage Engines may have Specific Needs:


MyISAM uses a file per table.frm, and multiple for
table.MYI and table.MYD
InnoDB needs only a few for Global Table-Space and
Logs (unless using innodb_file_per_table)
Implicit Disk-Based Temporary Tables are Files
SELECT INTO OUTFILE uses a file

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 43
Disk Data Directory

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 44
Disk Data Directory

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 45
Thread Cache
Use the SHOW STATUS for Thread SHOW GLOBAL STATUS LIKE 'threads%';
+-------------------+-------+
Cache information: | Variable_name | Value |
+-------------------+-------+
| Threads_cached | 0 |
| Threads_connected | 3 |
| Threads_created | 604 |
| Threads_running | 1 |
+-------------------+-------+

Use the SHOW VARIABLES for Thread SHOW VARIABLES LIKE


'thread_cache%';
Cache Variables: +-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| thread_cache_size | 0 |
+-------------------+-------+

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 46
Server CPU
Since mysqld is multi-threaded, it can use many CPU
cores
A Client Connection is a single thread — a Query uses only One
Core

Query Concurrency is very Schema and Traffic


Dependent

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 47
Storage Engines
MariaDB for DBAs

Introduction Client Utilities Storage Engines Partitioning Monitoring & Troubleshooting MariaDB Replication
Architecture Configuration Table Schema User & Server Security Back-Ups & Restoration High Availability
Installation
 Resource Usage
 Views 
 Log Files
 Customizing
 Optimization
Storage Engine Basics
Multiple Storage Engines Allowed SHOW ENGINES;

They Determine Storage Medium (disk,


memory, etc.)

Control Atomicity Consistency


Isolation Durability (ACID)
Lock at certain Levels (table, page, row)
Some offer Special Features (Foreign Keys, CREATE TABLE table1 (col1 INT)
GIS, ColumnStore) ENGINE = AriaDB;

Some Provide Optimization ALTER TABLE table1


ENGINE = MyISAM;
Storage Engine is Set for Table:

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 49
Pluggable Storage Engines
There are Storage Engines with
[mysqld]
different Features and Purposes plugin_dir=/usr/lib/mysql/plugin/
plugin_dir=/usr/lib64/mysql/plugin/
Multiple Engines on a Server and
Excerpt from my.cnf or my.ini configuration file
within a Query are Permitted

Engines can be Installed and INSTALL PLUGIN BLACKHOLE


Removed SONAME 'ha_blackhole.so';

UNINSTALL PLUGIN BLACKHOLE;

Requires INSERT privilege for mysql.plugin table

Documentation on INSTALL PLUGIN: https://mariadb.com/kb/en/mariadb/install-plugin/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 50
Advantages & Disadvantages of InnoDB
Advantages Disadvantages

Fully Data & Indexes Supports Foreign Slower than


Transactional, Cached by mysqld Keys and Multi- MyISAM, but
ACID Compliant in Buffer Pool Version Higher
Concurrency Concurrency
Control

Row-Level Supports Four Reliable Crash


Locking for High- Isolation Levels Recovery
Concurrency Isolation Levels

Read Uncommitted
Read Committed
Repeatable Read
(no fantom rows)

Serializable

Documentation for InnoDB & XtraDB: https://mariadb.com/kb/en/mariadb/xtradb-and-innodb/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 51
InnoDB Architecture
Memory

Additional 
 Disk
Memory Pool

1 INSERT, UPDATE, Data Files * * *


DELETE

3 CHECKPOINT
Redo Log (File 1/2)
Buffer Pool *

Redo Log (File 2/2)


Log Buffer * *

2 COMMIT * Cached Data & Indexes, Stored in Pages


** Transaction Data Writes
*** Data, Index, and Undo Log Files

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 52
InnoDB Clustered Index
PAGE (16KB) PAGE (16KB) PAGE (16KB)

root

B-Tree
node node

leaf leaf leaf leaf

HEADER PRIMARY KEY ROW DATA

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 53
InnoDB Secondary Index
PAGE (16KB) PAGE (16KB) PAGE (16KB)

root

B-Tree
node node

leaf leaf leaf leaf

HEADER VALUES OF INDEXED COLUMNS PRIMARY KEY

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 54
Pros & Cons of MyISAM
Advantages Disadvantages

Poor Crash
Not Recovery
High Read,
Many Column Transactional
Low Write (Relies on OS to
Data Types Flush Data to
Traffic Disk)

Concurrent Table-Level Data Caching


Writes Locking Relies on OS
Disk Cache

Indexes are Cached by mysqld in key_buffer


Don't Disable — System Tables use MyISAM Storage Engine (InnoDB in Future)
Documentation for InnoDB & XtraDB: https://mariadb.com/kb/en/mariadb/xtradb-and-innodb/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 55
Aria
Replacement for MyISAM
Buffers Pages before Writing Rows
Index & Pages Cached (aria_pagecache_buffer_size)
Crash Safe — Statement Commit & Rollback (notes a log file)
Used for Temporary Tables
Variable aria_used_for_temp_tables should be ON

Documentation for Aria: https://mariadb.com/kb/en/mariadb/aria-storage-engine/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 56
Memory
Temporary, but Fast
Table Structure on Disk
Data and Indexes in RAM and the Like
Uses Table-Level Locking

Data won’t survive mysqld Restart — Schema Remains


Allows Only Fixed-Length Records — Variable-Length Columns Padded to Full Width
Some Data Types are Not Supported (e.g., VARCHAR(255), TEXT, BLOB)
Uses a Hash Index Type — Only Equality Comparisons (e.g., No LIKE Operator)

Documentation for Memory: https://mariadb.com/kb/en/mariadb/memory-storage-engine/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 57
Blackhole — /dev/null
Useful for Development Servers for Testing only Query
Execution
Can be Used for Replication since Queries are written to
Binary Log
Master can Replicate Data without Storing Locally
Relay Slave can use Filters Rules
Use on Slave to Prevent Storing Locally Replicated Data
Useful for Solving Performance Problems
Remove Temporarily and Instantly Excessive Log Table
Activity
Benchmark to determine Non-Storage-Engine Bottlenecks
Documentation for Blackhole: https://mariadb.com/kb/en/mariadb/blackhole/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 58
Archive
Useful for Storing Large Amounts of Archived Data or
Logging Tables with Frequent Writes and Infrequent reads
Minimal Memory Footprint
All Data is Compressed on Disk with zlib
Supports only INSERT and SELECT statements
Doesn’t support DELETE, UPDATE or REPLACE
Indexes Are Not Stored
No Data Caching, Except the OS Disk Cache
Non-Transactional

Documentation for Archive: https://mariadb.com/kb/en/mariadb/archive/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 59
Spider
Partitions Large Tables across Multiple Servers
Transparent — No Changes Necessary to Client Applications
Allows for more Parallelism, Scale-out.
Parallel Partition Scanning — Make MariaDB Faster than Other
RDBMS
ACID Compliant & Transactional
Not Yet Production Ready
Schema Creation & Changes Cumbersome

Documentation on Spider: https://mariadb.com/kb/en/mariadb/spider/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 60
ColumnStore (Columnar)
Massively Parallel, Distributed Data Engine
Powerful Analytics
Data Warehouse
Fast Import (ETL)
cpimport

Columnar Architecture
No Indexes
Each Column is Stored Separately
WHERE Clauses Parsed Differently

Documentation on ColumnStore: https://mariadb.com/kb/en/mariadb/mariadb-columnstore/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 61
Connect
Maps Diverse Data to Tables
Flat Files including CSV
Tables in External ODBC Databases
JOIN Mapped Data to Database Tables

Generated tables (PIVOT, etc.)


Plug-in API for Customized Mapping

Documentation on Connect: https://mariadb.com/kb/en/mariadb/introduction-to-the-connect-engine/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 62
Other Storage Engines
EXAMPLE — Example Storage Engine
CSV — Comma-Separated Value Storage Engine
FEDERATED — Federated Storage Engine

Documentation on EXAMPLE: https://mariadb.com/kb/en/mariadb/example-storage-engine/


Documentation on CSV: https://mariadb.com/kb/en/mariadb/csv-overview/
Documentation on FEDERATED: https://mariadb.com/kb/en/mariadb/federated-storage-engine/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 63
Table Schema & Maintenance
MariaDB for DBAs

Introduction Client Utilities Storage Engines Partitioning Monitoring & Troubleshooting MariaDB Replication
Architecture Configuration Table Schema User & Server Security Back-Ups & Restoration High Availability
Installation
 Resource Usage
 Views 
 Log Files
 Customizing
 Optimization
Creating Databases & Tables
Creating Databases is Simple CREATE DATABASE db1;

Creating Tables can be Simple or CREATE TABLE table1


(col1 INT AUTO_INCREMENT KEY,
Complex col2 INT DEFAULT '100',
col3 VARCHAR(255) CHARACTER SET 'greek',
List Column Names, Data Types,
col4 DATETIME)
Default Values, Character Sets ENGINE = Aria;
and Collation, etc.
Define Indexes and Specify
Storage Engine

Documentation for CREATE DATABASE: https://mariadb.com/kb/en/mariadb/create-database/


Documentation for CREATE TABLE: https://mariadb.com/kb/en/mariadb/create-table/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 65
Altering Tables
ALTER TABLE is used to change a Table's Schema
ADD COLUMN to Add a Column
DROP COLUMN to Drop a Column — Deletes Data
CHANGE COLUMN and MODIFY COLUMN to Alter a Column

ALTER TABLE table1


ADD COLUMN col5 CHAR(8),
DROP COLUMN col3,
CHANGE COLUMN col4 col4 DATE;

Basic Syntax Example for ALTER TABLE Statement

Documentation for ALTER TABLE: https://mariadb.com/kb/en/mariadb/alter-table/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 66
InnoDB & MyISAM Row Formats
Static
Fixed-Length Data Types
Less Data File Fragmentation
Heaviest Disk Space Usage INT, CHAR, etc.

Dynamic
Fixed and Variable Length Data Types
More Data File Fragmentation
More Efficient Disk Space Usage VARCHAR, TEXT, etc.

Compressed
Decompressed as Needed
Minimal Disk Space Usage
Read-Only

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 67
Setting SQL Mode
Determines the SQL Server SET sql_mode='TRADITIONAL';

Environment SHOW VARIABLES LIKE 'sql_mode' \G


Supported SQL Syntax
Variable_name: sql_mode
Type of Data Validation Value: STRICT_TRANS_TABLES,
STRICT_ALL_TABLES,
SQL Mode is Initially Empty NO_ZERO_IN_DATE,
Use SET Statement to set NO_ZERO_DATE,
ERROR_FOR_DIVISION_BY_ZERO,
SQL Mode TRADITIONAL,
NO_AUTO_CREATE_USER,
NO_ENGINE_SUBSTITUTION

List of SQL Modes: https://mariadb.com/kb/en/mariadb/sql_mode/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 68
Column Attributes
Default Values can be Specified in the CREATE TABLE
Statement
CREATE TABLE people
(name VARCHAR(20) DEFAULT 'unknown');

Columns can be NULL, unless defined NOT NULL


NULL means "No Value", "Not Applicable", or
"Unknown"
Use NULL when Value is Not an Empty String or 0
NOT NULL Reduces Storage in some Engines

Documentation on Default Column Values: https://mariadb.com/kb/en/mariadb/data-types/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 69
Data Types for Integers
Use UNSIGNED when Appropriate TINYINT
INT(n) Specifies Display Precision, Not Storage SMALLINT
MEDIUMINT
Precision
INTEGER,INT
Size and Precision is Storage Engine Dependent BIGINT

Define Handling of Out-of-Range Values with


sql_mode
Default Mode: Values are Truncated Silently
Strict Mode: Errors are Generated

Documentation on Integer Data Types: https://mariadb.com/kb/en/mariadb/data-types/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 70
AUTO_INCREMENT
Use LAST_INSERT_ID()to get Value Generated for Client
Connection
SERIAL is a synonym for "BIGINT UNSIGNED NOT NULL
AUTO_INCREMENT UNIQUE"

MyISAM the Counter can be Set Back Manually or If Counter


Value Wraps
InnoDB prepares AUTO_INCREMENT counters when mysqld starts

SELECT MAX(auto_inc_field)
FROM table;

Documentation on AUTO_INCREMENT: https://mariadb.com/kb/en/mariadb/auto_increment/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 71
Floating and Fixed Point
FLOAT and DOUBLE are Approximate Types - uses 4 and 8 FLOAT
bytes IEEE storage format DOUBLE
DECIMAL(m,d) - maximum total number of digits, number of decimal DECIMAL
points NUMERIC
An Exact Value type, up to 65 digits precision, 4 bytes REAL
storage for each multiple of nine digits
NUMERIC is a synonym for DECIMAL

REAL is a synonym for DOUBLE - Unless in REAL_AS_FLOAT


SQL mode

Documentation on FLOAT Data Type: https://mariadb.com/kb/en/mariadb/float/


Documentation on DECIMAL Data Type: https://mariadb.com/kb/en/mariadb/decimal/
Documentation on DOUBLE Data Type: https://mariadb.com/kb/en/mariadb/double/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 72
String Data Types
All String Data Types have a Character Set CHAR
CHAR(n) — number of characters, not bytes, wide VARCHAR
TINYTEXT
VARCHAR(n)— Changes to CHAR in Implicit Temporary Tables and
TEXT
mysqld internal buffers
MEDIUMTEXT
TEXT —Not Supported by the MEMORY Storage Engine; Implicit LONGTEXT
Temporary Tables may Convert to MyISAM

Documentation on String Data Types: https://mariadb.com/kb/en/mariadb/string-data-types/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 73
Character Set & Collation
Character Set may be Global or for Schema, Table or
Column
Multi-Byte Character Sets Increase Disk Storage and
Working Memory Requirements (e.g, UTF-8 Requires 3 or
4 bytes per Character)

Collations affect String Comparison (Character Order)


Collations can be Changed for Query:

SELECT * FROM table1


ORDER BY col1 COLLATE
latin1_german2_ci;

Documentation on Character Sets and Collation: https://mariadb.com/kb/en/mariadb/data-types-character-sets-and-collations/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 74
Binary Data Types
BINARY and VARBINARY are Case-Sensitive Versions of BINARY
CHAR and VARCHAR VARBINARY
TINYBLOB
No Character Set and Collation for Binary Types -
BLOB
ordered by bytes
MEDIUMBLOB
Blobs are Used often to Store Files in a Database LONGBLOB
Files on Disk are often Faster
Blobs are Included in Transactions, Replication, and
Backups
Blobs Inflate mysqld memory usage

Documentation on BINARY Data Type: https://mariadb.com/kb/en/mariadb/binary/


Documentation on VARBINARY Data Type: https://mariadb.com/kb/en/varbinary/
Documentation on BLOB Data Type: https://mariadb.com/kb/en/blob/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 75
Date & Time Data Types
DATE — from 1000-01-01 to 9999-12-31 (YYYY-MM-DD) DATE
TIME — from -838:59:59 to 838:59:59 TIME
DATETIME
DATETIME — Same Range with Time (YYYY-MM-DD HH:mm:ss)
TIMESTAMP
TIMESTAMP — Unix timestamp, in seconds from YEAR

1970-01-01
Many Apps Store UNIX_TIMESTAMP() values in unsigned integer SELECT CURTIME(4);
field +---------------+
| CURTIME(4) |
YEAR — Accepts YY or YYYY +---------------+
| 05:33:09.1061 |
+---------------+

Documentation on Date and Time Data Types: https://mariadb.com/kb/en/mariadb/date-and-time-data-types/


Documentation on Microseconds in MariaDB: https://mariadb.com/kb/en/mariadb/microseconds-in-mariadb/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 76
Manipulating Date & Time
Functions for Date and Time Manipulation
ADDDATE() DAYOFWEEK() MONTH() TIME() WEEK()
ADDTIME() DAYOFYEAR() MONTHNAME() TIME_FORMAT() WEEKDAY()
CONVERT_TZ() EXTRACT() NOW() TIME_TO_SEC() WEEKOFYEAR()
CURDATE() FROM_DAYS() PERIOD_ADD() TIMEDIFF() YEAR()
CURTIME() FROM_UNIXTIME() PERIOD_DIFF() TIMESTAMP() YEARWEEK()
DATE() GET_FORMAT() QUARTER() TIMESTAMPADD()
DATE_ADD() HOUR() SEC_TO_TIME() TIMESTAMPDIFF()
DATE_FORMAT() LAST_DAY() SECOND() TO_DAYS()
DATE_SUB() MAKEDATE() STR_TO_DATE() UNIX_TIMESTAMP()
DATEDIFF() MAKETIME() SUBDATE() UTC_DATE()
DAYNAME() MICROSECOND() SUBTIME() UTC_TIME()
DAYOFMONTH() MINUTE() SYSDATE() UTC_TIMESTAMP()

Documentation on Date and Time Functions: https://mariadb.com/kb/en/mariadb/date-and-time-functions/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 77
Listing Acceptable Values
ENUM is an Enumerated List of CREATE TABLE colors
(primary_colors
String Values - uses a 2-byte ENUM('red', 'yellow', 'blue'));
integer index Simple Example for an ENUM Column

SET is a Specified List of String Values — CREATE TABLE colors


Can Hold Multiple Specified Values (primary_colors
SET('red','yellow','blue'));

INSERT INTO colors


VALUES('red'), ('red,blue');
Simple Example for a SET Column
Documentation on ENUM: https://mariadb.com/kb/en/mariadb/enum/
Documentation on SET: https://mariadb.com/kb/en/mariadb/set-data-type/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 78
Dynamic Columns
Stores Key/Value Pairs within a CREATE TABLE clothes
BLOB Column with (item_name VARCHAR(32) PRIMARY KEY,
item_attributes BLOB);
COLUMN_CREATE()
INSERT INTO clothes
Used to Store many Attributes VALUES ('MariaDB T-shirt',
which are Unknown in Advance COLUMN_CREATE('color', 'blue', 'size', 'XL'));

Manipulated with Special


Dynamic Column Functions: SELECT item_name,
COLUMN_GET(item_attributes, 'color' AS CHAR)
COLUMN_GET() COLUMN_LIST() AS color
FROM clothes;
COLUMN_ADD() COLUMN_DELETE()
+-----------------+-------+
| item_name | color |
+-----------------+-------+
| MariaDB T-shirt | blue |
Documentation on Dynamic Columns: https://mariadb.com/kb/en/dynamic-columns/ +-----------------+-------+
Tutorial on Dynamic Columns: http://radar.oreilly.com/2015/04/dynamic-columns-in-mariadb.html

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 79
Virtual Columns
Two Virtual Column Types: CREATE TABLE sales
(sales_id INT AUTO_INCREMENT KEY,
PERSISTENT (stored) qty INT,
VIRTUAL (generated only) price DECIMAL(6,2),
total DECIMAL(6,2) AS (qty * price) VIRTUAL);
All Data Types Supported
Use PERSISTENT for Indexes - Cannot
be Primary Key
Used with InnoDB, Aria,
MyISAM, CONNECT

Documentation on Virtual Columns: https://mariadb.com/kb/en/virtual-columns/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 80
Table Maintenance SQL Statements
ANALYZE TABLE — Check and Store Index Cardinal REPAIR TABLE — Fix a Corrupted Table
Values
Arguments:
Run Periodically QUICK, EXTENDED, USE_FRM
OPTIMIZE TABLE — Defragment MyISAM Data CHECKSUM TABLE
Files and Sort Indexes Arguments:
Maps Simply to a Full Table Rebuild QUICK, EXTENDED
for InnoDB
CHECK TABLE — Check Table for Errors
Arguments:
QUICK, FAST, CHANGED,
MEDIUM, EXTENDED

Documentation on CHECK TABLE: https://mariadb.com/kb/en/mariadb/sql-commands-check-table/
Documentation on CHECKSUM TABLE: https://mariadb.com/kb/en/checksum-table/
Documentation on REPAIR TABLE: https://mariadb.com/kb/en/repair-table/
Documentation on ANALYZE TABLE: https://mariadb.com/kb/en/analyze-table/
Documentation on OPTIMIZE TABLE: https://mariadb.com/kb/en/optimize-table/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 81
Views
MariaDB for DBAs

Introduction Client Utilities Storage Engines Partitioning Monitoring & Troubleshooting MariaDB Replication
Architecture Configuration Table Schema User & Server Security Back-Ups & Restoration High Availability
Installation
 Resource Usage
 Views 
 Log Files
 Customizing
 Optimization
Purpose of Views
Adapt and Standardize Table Schema for Applications
or Across Application Upgrades
Restricting Visible Table Data to Specific Users and
Applications
Useful to Simplify, Split, or Factor Complex Reporting
Queries

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 83
View Example & Caveats
An SQL Statement Represented CREATE VIEW emp_names AS

as a Table SELECT emp_id, name_first, name_last


FROM employees;

Views are Not Materialized SELECT * FROM view_example LIMIT 5;


The SELECT Query always Re- +--------+------------+-----------+
Executes | emp_no | name_first | name_last |
+--------+------------+-----------+
| 10021 | Ramzi | Erde |
| 10087 | Xinglin | Eugenio |
| 10122 | Ohad | Esposito |
| 10131 | Magdalena | Eldridge |
| 10145 | Akemi | Esposito |
+--------+------------+-----------+

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 84
Creating and Changing Views
CREATE VIEW Statement Creates a
View based on a SELECT statement

CREATE VIEW view_loans AS


SELECT id, loan_amount,
(loan_months / 12) AS loan_years
FROM loan_table;

Documentation on CREATE VIEW: https://mariadb.com/kb/en/create-view/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 85
Creating and Changing Views
ALTER VIEW Statement Deletes View
and Creates New One
ALTER VIEW view_loans AS
SELECT ...;

DROP VIEW Statement Deletes a View


DROP VIEW view_loans;

Documentation on ALTER VIEW: https://mariadb.com/kb/en/mariadb/alter-view/


Documentation on DROP VIEW: https://mariadb.com/kb/en/mariadb/drop-view/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 86
Showing Views
Use SHOW TABLES to List Views
SHOW FULL TABLES
WHERE Table_type = 'VIEW';

Use SHOW CREATE to Inspect Schema


of a View
SHOW CREATE VIEW view_loans \G

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 87
View Security
Views may be Writable — INSERT, UPDATE and DELETE CREATE VIEW view_loans
Affect Underlying Table ...
SQL SECURITY = INVOKER;
Conditions of Non-Writable View
ALGORITHM Set to TEMPTABLE
UNION, HAVING, some Joins, and Sub-Queries
Data Aggregation (AVG, GROUP BY, DISTINCT)
Derived Fields — Field using a Function or Calculation
Updatable Views
WITH CHECK OPTION Constrains Writes to Rows
in View
Set Security Based on View Creator (DEFINER) or
User (INVOKER)
training@mariadb.com MariaDB for DBA's
Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 88
Setting the Algorithm
View Algorithm Controls Query Execution Method
CREATE VIEW view_loans
ALGORITHM = MERGE AS SELECT ...

TEMPTABLE
View Data put in Temporary Table, then Query Executed
Possibly Slower since No Index for Temporary Table
MERGE
Query and Original View SELECT are Merged
Possibly Faster since Underlying Table Index Used
UNDEFINED
The Optimizer choose what’s Best (Default)
Documentation on View Algorithms: https://mariadb.com/kb/en/mariadb/view-algorithms/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 89
Partitioning
MariaDB for DBAs

Introduction Client Utilities Storage Engines Partitioning Monitoring & Troubleshooting MariaDB Replication
Architecture Configuration Table Schema User & Server Security Back-Ups & Restoration High Availability
Installation
 Resource Usage
 Views 
 Log Files
 Customizing
 Optimization
Value of Partitions
For Tables Larger than Memory
Easier Maintenance with a Single Partitioned Table *

Partition Instead of Entire Table table1.frm table1.par

(Less Data, Fewer Locks, Faster Operations)


table1#P#p0.MYD table1#P#p2.MYD

Partition Pruning — Better table1#P#p0.MYI table1#P#p2.MYI


Performance for Queries
Involving only Specific table1#P#p1.MYD table1#P#p3.MYD

Partitions table1#P#p1.MYI table1#P#p3.MYI

Bulk Management of Data — Archiving * Files Generated for a MyISAM Table with Four Partitions

& Fast Elimination of Data

Documentation for Partitions: https://mariadb.com/kb/en/mariadb/create-table/#partitions

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 91
Factors in Partitioning
Custom Partitioning Function is defined for a Table to
return a Partitioning Key
Key Determines which Partitions Rows are Stored
Key must be Integer or NULL
Stored Procedures, Stored Functions, UDFs, Plug-ins, User
Variables & Sub-Queries Not Allowed in Function
A Column may be Used Directly as Key without a Function
Maximum of 1024 Partitions per Table
Foreign Keys are Disabled in an InnoDB Table
FULLTEXT and Spatial Data Types are Disabled in MyISAM

CONNECT Storage Engine Doesn't Support Partitioning

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 92
RANGE Partitions
Partition by a Range of Column
Values
Distribution of Rows
employees table
CREATE TABLE employees (
id INT, name VARCHAR(20),
(100, 'Bob Smith', 4),
dept_id INT NOT NULL) (102, 'Wendy Jones', 6), Partition p0
ENGINE = MyISAM (105, 'Sarah Jane', 4),
(109, 'Bill Johnson', 7)
PARTITION BY RANGE(dept_id) (
PARTITION p0 VALUES LESS THAN (10),
PARTITION p1 VALUES LESS THAN (20),
PARTITION p2 VALUES LESS THAN MAXVALUE); (101, 'Tim Schmidt', 16),
(103, 'John Wells', 12), Partition p1
(107, 'Cindy James', 18)

MAXVALUE indicates the Maximum


Value Possible (104, 'Lena Denison', 25),
(106, 'Tom Smith', 20), Partition p2
(108, 'Elaine Wilson', 30),
(110, 'Susan Hill, 120)
Documentation for Partitions: https://mariadb.com/kb/en/mariadb/create-table/#partitions

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 93
RANGE Columns
Partition by Column Tuples within a Range
Tuples are like Sets of Values for Columns
Distribution of Rows
employees table
CREATE TABLE employees (
id INT AUTO_INCREMENT KEY,
name VARCHAR(20), dept_id INT) (40, 'Bob Smith', 4),
(85, 'Wendy Joans', 25), Partition p0
ENGINE = MyISAM (99, 'Sarah Jane', 30),
PARTITION BY RANGE COLUMNS(id, dept_id) ( (120, 'Bill Johnson', 8)

PARTITION p0 VALUES LESS THAN (100,10),


PARTITION p1 VALUES LESS THAN (200,20),
PARTITION p2 VALUES LESS THAN (MAXVALUE,MAXVALUE)); (125, 'Tim Schmidt', 16),
(100, 'John Wells', 15), Partition p1
(150, 'Cindy James', 40)

Integer, Date, and String Data Types Allowed


for Partition Keys (210, 'Lena Denison', 25),
(200, 'Tom Smith', 50) Partition p2
TEXT, BLOB, DECIMAL, and FLOAT Not Allowed

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 94
LIST Partitions
Partition by a List of Specific Column
Values
Distribution of Rows
CREATE TABLE clients ( clients table
client_id INT,
name VARCHAR(20), active INT NOT NULL) (110, 'Harold Wilson', 1), Partition
(116, 'Leslie Haynes', 1), active_clients
ENGINE = MyISAM (120, 'Mike Brown', 1),
PARTITION BY LIST(active) ( (126, 'Tim Simms', 1)

PARTITION active_clients VALUES IN (1),


PARTITION inactive_clients VALUES IN (0));
(115, 'Jim Berns', 0), Partition
(120, 'Ed Gibson', 0),
(122, 'Sally King', 0) inactive_clients

Values Not Listed are Rejected


No MAXVALUE (106, 'Betty Mills', -1),
(125, 'Joe Cohen', 2)
Error

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 95
LIST COLUMNS Partitions
Partition by List of Specific String,
Integer or Date Values
One or More Columns Allowed Distribution of Rows
employees table

CREATE TABLE employees (


(110, 'Harold Wilson', 'Toykyo'), Partition
id INT AUTO_INCREMENT KEY, (116, 'Leslie Haynes', 'Tokyo'), employees_east
(120, 'Mike Brown', 'Sydney'),
name VARCHAR(20), (126, 'Tim Simms', 'Sydney')
office VARCHAR(20) NOT NULL)
ENGINE = MyISAM
PARTITION BY LIST COLUMNS(office) (115, 'Jim Berns', 'London'), Partition
(PARTITION employees_west (120, 'Ed Gibson', 'New York'),
(122, 'Sally King', 'New York) employees_west
VALUES IN ('London', 'New York'),
PARTITION employees_east
VALUES IN ('Tokyo', 'Sydney'));
(106, 'Betty Mills', 'Londonn'), Error
(125, 'Joe Cohen', 'Chicago')

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 96
HASH Partitions
Partition Determined by a Hash from a
Given Expression
Distribution of Rows
CREATE TABLE employees ( employees table
id INT AUTO_INCREMENT KEY, (100, 'Bob Smith', '2014-05-15'),
(101, 'Tim Schmidt', '2014-05-20'),
name VARCHAR(20), dept_id INT, (102, 'Wendy Jones', 2014-06-01')
join_date DATE) Partition p0
ENGINE = MyISAM
(103, 'John Wells', '2014-09-15'),
PARTITION BY HASH(id) PARTITIONS 4; (104, 'Lena Denison', '2014-12-01'),
(105, 'Sarah Jane', '2015-01-15') Partition p1

Partitions contain Same Number of Rows


(106, 'Tom Smith', '2015-02-14'),
(107, 'Cindy James', '2015-05-01'),
Use COALESCE to Reduce Number of (108, 'Elaine Wilson', '2015-09-20') Partition p2

Partitions
(109, 'Bill Johnson', '2016-02-24'),
(110, 'Susan Hill, '2016-03-15'), Partition p3
ALTER TABLE employees (111, 'Rob Kane, '2016-08-03')
COALESCE PARTITION 3;

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 97
KEY Partitions
Partition with a Hash, Using an
Expression Generated Automatically

CREATE TABLE employees (


id INT AUTO_INCREMENT KEY,
name VARCHAR(20), dept_id INT,
join_date DATE)
ENGINE = MyISAM
PARTITION BY KEY(dept_id, join_date)
PARTITIONS 4;

Primary Key or Specified Columns


Used for Partition Key

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 98
Composite or Sub-Partitioning
Used to further Divide Partitions
patients table
Each Partition Contains same Number of
Sub-Partitions elderly#p0 elderly#p2

elderly#p1 elderly#p3
Partitions and Sub-Partitions may use
different Methods middle_age#p0 middle_age#p2

CREATE TABLE patients ( middle_age#p1 middle_age#p3


id INT,
name VARCHAR(20), birth_date DATE) ENGINE = MyISAM young#p0 young#p1
PARTITION BY RANGE( YEAR(birth_date) )
young#p1 young#p3
SUBPARTITION BY HASH( QUARTER(birth_date) )
SUBPARTITIONS 4 (
PARTITION elderly VALUES LESS THAN (1950),
PARTITION middle_age VALUES LESS THAN (1975),
PARTITION young VALUES LESS THAN MAXVALUE );

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide 99
Partition Specific Queries
Partition may be Specified in SQL Statements

SELECT COUNT(*)
FROM employees
PARTITION(employees_west);

Supports Queries and Data Manipulation Statements


SELECT, DELETE, INSERT, REPLACE, UPDATE,
LOAD DATA, and LOAD XML

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide100
Partition Maintenance
Use Normal Full Table Maintenance Statements
(ANALYZE, CHECK, OPTIMIZE, and REPAIR TABLE)

REPAIR TABLE clients;


+--------------+--------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+--------------+--------+----------+----------+
| test.clients | repair | status | OK |
+--------------+--------+----------+----------+

Use ALTER TABLE to Perform Specific ALTER TABLE patients


Partition Maintenance (ANALYZE, CHECK, REBUILD PARTITION elderly;
Hard Disk Space is Reclaimed
REBUILD, REPAIR, and OPTIMIZE)

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide101
Partition Changes
Adding Partitions ALTER TABLE clients
ADD PARTITION
Provide Full Definition of New (PARTITION suspended_clients
Partition VALUES IN (-1) );

Dropping Partitions ALTER TABLE clients


DROP PARTITION inactive_clients;
Data Contained is Dropped
Partition is Lost

Removing Partitioning ALTER TABLE clients


REMOVE PARTITIONING;
Data is Not Lost

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide102
Partition Changes
Alterations are Allowed on RANGE, ALTER TABLE table1
REORGANIZE PARTITION partition_a, partition_b
LIST Partitions INTO(PARTITION partition_ab
VALUES LESS THAN (1000));
Merging Partitions

Renaming a Partition ALTER TABLE employees_test

Use REORGANIZE PARTITION with REORGANIZE PARTITION middle_age


INTO(PARTITION older VALUES LESS THAN (1975));
New Name, but Same Parameters

ALTER TABLE table1


REORGANIZE PARTITION p0 INTO
Splitting Partitions (PARTITION n0 VALUES LESS THAN (1000),
PARTITION n1 VALUES LESS THAN (2000));

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide103
Exchanging Partitions with Tables
Move a Partition or Sub-Partition to an Unpartitioned
Table

ALTER TABLE clients


EXCHANGE PARTITION inactive_clients
WITH TABLE clients_archive;

Both Partition and Table need Same Structure


No Foreign Keys in Table, Nor Other Referencing
Tables
Data is Moved; Partition is Preserved ALTER TABLE clients
DROP PARTITION
Use DROP PARTITION to Remove Partition inactive_clients;

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide104
Storage Engine Considerations
Storage Engine can be Specified for a Partitioned
Table
ENGINE Clause precedes PARTITION Clause
Partitions in Table must use Same Storage Engine
MERGE, CSV and FEDERATED Don't Support
Partitioning
DATA DIRECTORY and INDEX DIRECTORY Clauses Don't
Effect Partitioned InnoDB Tables

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide105
User & Server Security
MariaDB for DBAs

Introduction Client Utilities Storage Engines Partitioning Monitoring & Troubleshooting MariaDB Replication
Architecture Configuration Table Schema User & Server Security Back-Ups & Restoration High Availability
Installation
 Resource Usage
 Views 
 Log Files
 Customizing
 Optimization
Security — a Multi-Layered Challenge

Clients
Server

Data
mysqld Storage
Port 3306

Other
Processes

Network Considerations Server


Private Network Considerations
Apps Wifi Access Data Location
SSL for Data In Motion Non-Root Users
DNS Controlled Passwords
Other Processes

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide107
MariaDB User Accounts
Authentication Based on User, Host, and Password
Empty String is User Wildcard
Percent Sign (%) is a Host Wildcard
Host is an IP or Host Name — DNS, /etc/hosts
localhost is used by Local Socket on Linux Systems

CREATE USER 'ted'@localhost


IDENTIFIED BY 'rover123';

Privileges are Based on User and Host Combined

Documentation for CREATE USER: https://mariadb.com/kb/en/mariadb/create-user/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide108
Granting Privileges — Databases and Tables
Users given Permission with GRANT Statement
Privilege Levels — Global, Database, Table, Column, or Routine
ON *.* — Access to All Databases and Tables
ON database_name.* — Access to Database Named GRANT SELECT, DELETE,
UPDATE(col2,col3)
ON database_name.table_name — Access to Table Named
ON demo.tester
privilege(column) ON TABLE db_name.table_name — TO 'bob'@'localhost';
Column by Privilege
ON PROCEDURE database_name.routine_name — Access to
Routine

Documentation for GRANT: https://mariadb.com/kb/it/grant/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide109
More User SQL Statements
User Names and Hosts can be Changed
RENAME USER 'robert'@'127.0.0.1'
TO 'bob'@'localhost';

Users Passwords can be Changed


SET PASSWORD FOR 'bob'@'localhost'
= PASSWORD('fido456');

Users can be Deleted


DROP USER 'bob'@'localhost';

Documentation for RENAME USER: https://mariadb.com/kb/en/rename-user/


Documentation for SET PASSWORD: https://mariadb.com/kb/en/set-password/
Documentation for DROP USER: https://mariadb.com/kb/en/drop-user/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide110
Permissions Tables
Permissions Checked when Client Connects and
Query Received
mysql Database contains Permissions or Grants Tables
— Restrict Access

Grants Tables Loaded into Memory at Start


Possible to INSERT, UPDATE, and DELETE Manually Tables
in mysql
FLUSH PRIVILEGES Required to Reload Manual Changes

mysqldump will Copy mysql Database by Default

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide111
Privileges Available (SHOW PRIVILEGES)
Basic Privileges Developer Privileges Special Privileges Administrator Privileges
USAGE CREATE CREATE TEMPORARY ALL [PRIVILEGES]
TABLES
SELECT ALTER SUPER
CREATE VIEW
INSERT INDEX CREATE USER
SHOW VIEW
UPDATE DROP GRANT OPTION
LOCK TABLES
DELETE PROCESS
Customizing Privileges
SHOW DATABASES Replication Privileges FILE
CREATE ROUTINE
REPLICATION CLIENT RELOAD
ALTER ROUTINE
REPLICATION SLAVE
 SHUTDOWN
EXECUTE
EVENT
TRIGGER

Documentation on User Privileges: https://mariadb.com/kb/en/mariadb/grant/#privilege-levels

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide112
Checking User Privileges
Check Regularly user Table in mysql Database for User
Accounts
Check for Obsolete Accounts
Check for Blank Passwords
SELECT User, Host, Password
FROM mysql.user;

Use the SHOW GRANTS statement for User-Host Account

SHOW GRANTS FOR 'bob'@'localhost';


SHOW GRANTS FOR 'bob'@'home';

Documentation for SHOW GRANTS: https://mariadb.com/kb/en/show-grants/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide113
Revoking User Privileges
Use REVOKE Statement to Revoke Privileges — Need GRANT
OPTION

List Privileges to Revoke or ALL PRIVILEGES


REVOKE Doesn’t Remove a User-Host Account — Use DROP
USER

REVOKE DELETE,UPDATE
ON db1.table1
FROM 'bob'@'localhost';

REVOKE ALL PRIVILEGES ON db1.table1


Check Results with SHOW GRANTS FROM 'bob'@'localhost';
after Revoking
GRANT SELECT ON
database_name.table_name
TO 'bob'@'localhost';
Documentation for REVOKE: https://mariadb.com/kb/en/revoke/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide114
Limiting Users
User-Host Accounts may be Limited by Resources

GRANT SELECT ON db_name.table_name


TO 'bob'@'home_bob'
WITH MAX_QUERIES_PER_HOUR 20
MAX_CONNECTIONS_PER_HOUR 10
MAX_USER_CONNECTIONS 2
MAX_UPDATES_PER_HOUR 5;

Usage Related to Limits are kept in mysql Database


Counters Reset when Server Starts or FLUSH FLUSH USER_RESOURCES;
PRIVILEGES Executed
User Counters may be Reset Specifically — Not
MAX_USER_CONNECTIONS

Documentation for User Account Resource Limits: http://dev.mysql.com/doc/refman/5.6/en/user-resources.html

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide115
Pluggable Authentication Module
Uses Plug-in to Authenticate Client Connections
Allows Credentials from Other Sources — Linux Passwords,
LDAP, Kerberos, Active Directory

[mysqld]
plugin-load=authentication_pam.so
Excerpt from my.cnf or my.ini configuration file

Specify PAM when Creating Users


CREATE USER user
IDENTIFIED WITH authentication_pam
AS 'mysql, root=developer,
users=data_entry';

Documentation on PAM: https://mariadb.com/kb/en/mariadb/pam-authentication-plugin/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide116
Proxy Users
PROXY Permission give a User-Account Permission of
Another Account
GRANT PROXY REVOKE PROXY
ON 'manager'@'localhost' ON 'manager'@'localhost'
TO 'bob'@'localhost'; FROM 'bob'@'localhost';

MariaDB Perceives User as Proxied User


SELECT USER(), CURRENT_USER();
+---------------+-------------------+
| USER() | CURRENT_USER() |
+---------------+-------------------+
| bob@localhost | manager@localhost |
+---------------+-------------------+

Documentation on Proxy Users: http://dev.mysql.com/doc/refman/5.5/en/proxy-users.html

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide117
User Roles
User Role is a Set of Privileges available for a Group
An Alternative to Assigning Privileges Individually to
Each User Account
Useful for Assigning Privileges to Many Users or to
External Users

Documentation on User Roles: https://mariadb.com/kb/en/roles-overview/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide118
Creating a User Role
Use CREATE ROLE to Create Group Privileges
Use GRANT to Grant Privileges to Role

CREATE ROLE 'sales_staff';

GRANT SELECT, INSERT, UPDATE


ON sales.* TO 'sales_staff';
No Host is given for a Role

Documentation on CREATE ROLE: https://mariadb.com/kb/en/mariadb/create-role/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide119
Listing & Showing Privileges of Roles
Query information_schema to SELECT * FROM
information_schema.APPLICABLE_ROLES \G
Get List of Roles Created
********* 1. row ********
GRANTEE: root@localhost

Use SHOW GRANTS to Check ROLE_NAME: admin_remote


IS_GRANTABLE: YES
Privileges for Role IS_DEFAULT: NO

There is No SHOW ROLES Statement

SHOW GRANTS FOR 'admin_remote';

+------------------------------------------------------+
| Grants for admin_remote |
+------------------------------------------------------+
| GRANT USAGE ON *.* TO 'admin_remote' |
| GRANT INSERT, UPDATE ON `admin`.* TO 'admin_remote' |
+------------------------------------------------------+

No Host is Given for a Role

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide120
Granting a Role to a User
Use GRANT to Designate Users for Role
GRANT 'sales_staff' TO
'bob'@localhost;
Executed while Logged in as an Administrator Use WITH ADMIN OPTION so
Grantee may Grant Role

Check mysql.roles_mapping for Roles Granted GRANT 'admin_remote'


TO 'bob'@localhost
SELECT * FROM mysql.roles_mapping;
WITH ADMIN OPTION;
+-----------+-------+--------------+--------------+
| Host | User | Role | Admin_option |
+-----------+-------+--------------+--------------+
| localhost | bob | admin_remote | N |
| localhost | root | admin_remote | Y |
+-----------+-------+--------------+--------------+
Executed while Logged in as an Administrator

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide121
Assuming and Relinquishing a Role
User Assumes a Role for Session with
SET ROLE SELECT CURRENT_ROLE();
+----------------+
SET ROLE | CURRENT_ROLE() |
sales_staff; +----------------+
Executed while Logged in as bob@localhost | sales_staff |
+----------------+
Executed while Logged in as user bob@localhost

User Relinquishes a Role by Setting it


to None — Or by Ending Session
SET ROLE NONE; User can Set Default Role to NONE or
Executed while Logged in as bob@localhost
Preferred Role
SET DEFAULT ROLE
Documentation on CURRENT_ROLE( ): https://mariadb.com/kb/en/mariadb/current_role/ sales_staff;
Documentation on SET ROLE: https://mariadb.com/kb/en/mariadb/set-role/ Executed while Logged in as user bob@localhost

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide122
Revoking a Role
Check mysql.roles_mapping
for a List of Roles SELECT * FROM mysql.roles_mapping;

Granted +-----------+---------+--------------+--------------+
| Host | User | Role | Admin_option |
+-----------+---------+--------------+--------------+
| localhost | ted | admin_remote | N |

Use REVOKE to take Role | localhost | bob | admin_remote | Y |


| localhost | root | admin_remote | Y |

Option from User +-----------+---------+--------------+--------------+

Executed while Logged in as an Administrator

REVOKE 'admin_remote'
FROM 'bob'@localhost;
Executed while Logged in as an Administrator

Documentation on Revoking Roles: https://mariadb.com/kb/en/mariadb/revoke/#roles


Documentation on DROP ROLE: https://mariadb.com/kb/en/mariadb/drop-role/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide123
Dropping a Role
Check mysql.user for List of Roles
Use DROP ROLE to take Eliminate a Role

DROP ROLE
'sales_staff';
Executed while Logged in as an Administrator

SELECT User
FROM mysql.user
WHERE is_role='Y';
Executed while Logged in as an Administrator

Documentation on Revoking Roles: https://mariadb.com/kb/en/mariadb/revoke/#roles


Documentation on DROP ROLE: https://mariadb.com/kb/en/mariadb/drop-role/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide124
Log Files
MariaDB for DBAs

Introduction Client Utilities Storage Engines Partitioning Monitoring & Troubleshooting MariaDB Replication
Architecture Configuration Table Schema User & Server Security Back-Ups & Restoration High Availability
Installation
 Resource Usage
 Views 
 Log Files
 Customizing
 Optimization
MariaDB Logs
Error Log Error Log

General Query Log General Query Log

mysqld SQL Error Log


SQL Query Log Slow Query Log

Slow Query Log Binary Log

Binary Log

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide126
Error Log
Contain Startup, Shutdown, Error Error Log
Messages General Query Log

Unix uses stderr, Sent to host_name.err in mysqld SQL Query Log

datadir Slow Query Log

Binary Log
Some Unix systems Redirect to System Log
Windows uses host_name.err in datadir,
or System Event Log
[mysqld]
log-error = /path/file_name
log-warnings = 1

Excerpt from my.cnf or my.ini configuration file

Set log-warnings to 2 for Verbose


Documentation on Error Log: https://mariadb.com/kb/en/mariadb/error-log/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide127
General Query Log
All Queries Received from All Clients Error Log

Order Received — Not Execution General Query Log

mysqld
Potential Problems with Log SQL Error Log

Slow Query Log


Possible Disk I/O bottleneck on High-
Binary Log
Traffic Servers
Log can Quickly become Huge
Contains Queries in Plain Text — a
Security Risk [mysqld]
general-log
general_log_file='/path/host.log'
Excerpt from my.cnf or my.ini configuration file

Documentation on General Query Log: https://mariadb.com/kb/en/mariadb/general-query-log/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide128
SQL Error Log
Logs SQL Errors Error Log

General Query Log


Error Messages mysqld SQL Error Log
SQL Statement Slow Query Log

Part of MariaDB Audit Plugin Binary Log

Used for Detecting SQL Injections


Logs User, Host, & Time
INSTALL PLUGIN sql_error_log
SONAME 'sql_errlog';

Requires INSERT Privilege on mysql.plugin Table

Documentation on SQL Query Log: https://mariadb.com/kb/en/mariadb/sql_error_log-plugin/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide129
Slow Query Log
Queries which take more seconds Error Log
to execute than long_query_time General Query Log
mysqld
Contains Queries in Plain Text SQL Error Log

Slow Query Log


Use mysqldumpslow to Read Binary Log

Log
[mysqld]
slow_query_log = ON
slow_query_log_file = /path/file
long_query_time = 0.5
log_queries_not_using_indexes
log_slow_admin_statements
Relevant Entries from my.cnf or my.ini configuration file

Documentation on Slow Query Log: https://mariadb.com/kb/en/slow-query-log/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide130
Binary Log
Writes in the Order Executed Error Log

Useful for Recovering Lost Data General Query Log

mysqld SQL Error Log


Used for Replication Slow Query Log

Move from to Separate Disk for Binary Log

Security
Set Expiration or Purge
Occasionally
[mysqld]
log-bin
expire_logs_days = 7
Excerpt from my.cnf or my.ini configuration file
Documentation on Binary Log: https://mariadb.com/kb/en/binary-log/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide131
Managing Log Files
Rotated Regularly the Log Files
Move manually the Existing Log Files
and then Flush Logs
FLUSH LOGS; mysqladmin flush-logs
An Alternative Method from the Command-Line

Back-Ups
Include Logs in Back-ups
Synchronize Binary Logs with Back-ups
Error Log
Stop mysqld, Move the Log, then Restart mysqld

Documentation on FLUSH: https://mariadb.com/kb/en/mariadb/flush/


Documentation on mysqladmin: https://mariadb.com/kb/en/mariadb/mysqladmin/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide132
Managing Binary Logs
Inspect Current Binary Logs SHOW BINARY LOGS;

FLUSH LOGS triggers a Binary Log


Rotation
Binary Log Files use Incrementally PURGE BINARY LOGS TO
'mariadb-bin.000010';
Numbered Extensions
Purges All Binary Logs Before One Named
Binary Log Files can be Purged
explicitly PURGE BINARY LOGS BEFORE
'2016-06-01 10:00:00';
Binary Logs can be set to Expire in Purges All Binary Logs Before One which
Configuration File (expire_logs_days) Contains Entries for Date Given

Allow Time for all Slaves to Read the


Binary Log Files
Documentation on PURGE BINARY LOGS: https://mariadb.com/kb/en/mariadb/sql-commands-purge-logs/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide133
Audit Plugin Log
Includes Table Event Logging (Triggers, Stored Procedure Calls)
Optional Field Substitution of Placeholders in Query
Log to Improve Security
Filtering Audit Logs by Role & User Accounts
Records Privilege Changes
Password Change Logging

Documentation on Audit Plugin: https://mariadb.com/kb/en/mariadb/about-the-mariadb-audit-plugin/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide134
Monitoring & Troubleshooting
MariaDB for DBAs

Introduction Client Utilities Storage Engines Partitioning Monitoring & Troubleshooting MariaDB Replication
Architecture Configuration Table Schema User & Server Security Back-Ups & Restoration High Availability
Installation
 Resource Usage
 Views 
 Log Files
 Customizing
 Optimization
Command-Line Monitoring Tools
mytop mytop -u user -p password
A Clone of top for Monitoring
MariaDB Threads
Works any Shell, SSH, or Shell
Script

innotop
Tool for Monitoring MariaDB
Threads
Includes InnoDB Specific
Information
Site for mytop: http://jeremy.zawodny.com/mysql/mytop/
Site for innotop: https://code.google.com/p/innotop/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide136
MonYog
User-Friendly, Cross Platform,
GUI
No Server-Side Agent Required
Manages Easily Multiple Servers
Track and Alert many Metrics
Monitor Error Log and Security
Vulnerabilities
Replication Lag and Excessive
Resource Usage
Slow Queries and Deadlocks
Detailed History and Trend
Analysis
Site for MonYog: https://www.webyog.com/product/monyog

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide137
Automated Monitoring Utilities
Automated Monitoring can poll MariaDB and Send
Alerts
CA-Unicenter Munin
Cacti Nagios
Ganglia NewRelic
HP Openview OpenNMS
Hyperic PMM
IBM Tivoli Zabbix

Some are Generic Network Monitors with Plug-ins for


MariaDB

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide138
Custom Monitoring Solutions
Custom Monitoring Solution or Method is sometimes
Required
Check if mysqld is active with mysqladmin ping or C
function, mysql_ping()
Look for Performance Hot Spots with SHOW GLOBAL
STATUS
Parse the mysqld Error Log for “Error” or “Warning” of
other Keywords
Use a Scheduler (e.g., cron) to run Monitor Tools — Email
Results or Create SNMP Alerts

Kristian Koehntopp (http://vvv.koehntopp.de/rrd/)

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide139
Error Log
Error Log is Stored by Default in Data Directory
(datadir)

Error Log Contains Information about Errors from


Queries
Shows Warnings - Depends on --log-warnings
Shows mysqld Stops, Starts, and Error Events
Crashes will Appear as Abrupt Stop without Shutdown
Messages
Check System Log for Information on Shutdown or
Crash

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide140
MariaDB Status
Check Global Status SHOW GLOBAL STATUS;

Unusually Large Values - Note Typical Results


Check Processes SHOW PROCESSLIST;

Long Running Queries - Lock & Load Problems


Intermittent or Short Term Problems - Try Multiple Snapshots
or Polling

Check Status of Storage Engines Used SHOW ENGINE INNODB STATUS \G

Useful for InnoDB, though Returns Complex Results

Documentation on SHOW PROCESSLIST: https://mariadb.com/kb/en/mariadb/show-processlist/


Documentation on SHOW STATUS: https://mariadb.com/kb/en/show-status/
Documentation on SHOW ENGINE INNODB STATUS: https://mariadb.com/kb/en/show-engine-innodb-status/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide141
Server Status
See if Reached Limits of CPU, Disk, Memory, or
Network Usage
Track System Statistics to Identify Trends or Patterns -
Use a Monitoring Program

Watch for Unpredictable and Reoccurring Problems


Check What is Running on Server — Check cron
Check if Other Servers Affected, if more than One

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide142
Application Status
Check if New Applications Installed Recently
Code Changes or Updates Applied Recently — If it
Handles Deadlocks
Check Logs for Applications
MariaDB Connection or Disconnection Problems
Unexpected Error Codes from MariaDB
Check if Application Traffic and Dataset within
Expected Bounds

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide143
MariaDB Support
24 x 7 On-Line, Live Assistance
General Consulting
Performance Tuning
Code Review
Login Support
Bug and Hot fixes
On-Site Health Check

Web Page for MariaDB Support: http://www.mariadb.com/products/mysql-support

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide144
Back-Ups & Restoration
MariaDB for DBAs

Introduction Client Utilities Storage Engines Partitioning Monitoring & Troubleshooting MariaDB Replication
Architecture Configuration Table Schema User & Server Security Back-Ups & Restoration High Availability
Installation
 Resource Usage
 Views 
 Log Files
 Customizing
 Optimization
Backup Principles
High Availability Can’t Prevent User Errors
Copy Data, Logs, and Configuration Files
Daily Snapshots (mysqldump, InnoDB Hot Backup, LVM) and
Real-Time Backups (Replication)
Store Backups in Multiple Locations - Onsite for fast access;
Offsite for security
Synchronize Binary Logs with Backup Files
Back-Up Methods
Physical Back-Up
Logical Back-Up
Binary Back-Up
Test Backup and Recovery

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide146
Making Physical Back-Ups
Produces a Binary Copy of Data
Faster than Dumping to SQL Text File
Recovered Only to the Same Storage Engine
Not Useful for Migrations
Corrupted Files are Undetected
Multiple Options
Copy Manually Data Directory (Stopping mysqld is Necessary)
LVM on Linux for Volume Snapshot
(Data Complete and Consistent, but still Trigger InnoDB Recovery after
Restore)
InnoDB Hot Backup tool, or XtraBackup

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide147
Logical Backups
Produce Text Files with SQL statements to Replay to
Rebuild Database
Allows Backup of Databases or Specific Tables — Schema
or only Data
An SQL dump is independent of Storage Engine, and
can be Restored to a different Storage Engine, or
Used for Migration
Process can be Slow and Requires Locks
Use a Local Drive, not across Network
Multiple Options (mysqldump, "SELECT INTO OUTFILE", etc.)

Documentation on mysqldump: https://mariadb.com/kb/en/mariadb/mysqldump/


Documentation on SELECT…INTO OUTFILE: https://mariadb.com/kb/en/mariadb/select-into-outfile/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide148
Advantages & Factors of mysqldump
Exports Schema and Data (SQL text file, tab-delimited, or XML)
Good for Backups & Transferring Data between Servers
Requires Locks for Consistency —May Affect Traffic
Dump all Databases, or Specific Databases or Tables
Optimize for Importing (extended INSERT, temporarily disable
indexes, and ignore foreign key checks)

mysqldump -u backup_user -p --all-databases > backup.sql


mysqldump -u backup_user -p db1 table1 > table.sql
mysqldump -u backup_user -p -h production | mysql -u dev_user -h development

Documentation on mysqldump: https://mariadb.com/kb/en/mariadb/mysqldump/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide149
Create a Back-Up User
Create a Special User for Making Back-ups with
mysqldump

CREATE USER 'admin_backup'@localhost


IDENTIFIED BY 'rover123';

GRANT SELECT, RELOAD, LOCK TABLES,


REPLICATION CLIENT, SHOW VIEW, EVENT, TRIGGER
ON *.* TO 'admin_backup'@'localhost';

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide150
Recovery with Binary Logs
Restore Databases from Dump file
Use the Binary Logs to Execute
Remaining SQL Statements

SHOW BINARY LOGS;


SHOW MASTER STATUS;

Use mysqlbinlog to Convert Binary Log and Pipe to


mysql Client

mysqlbinlog binlog.000005 binlog.000006 | mysql -u admin_user -p

Documentation on mysqlbinlog: https://mariadb.com/kb/en/mariadb/mysqlbinlog/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide151
Point-in-Time Recovery
Give a Stop Time to mysqlbinlog to Recover Until
Then (e.g., Noon)
mysqlbinlog --stop-datetime='2017-02-10 11:59:59' \
/var/log/mysql/bin.000006 | mysql -u admin_user -p

Give a Start Time to mysqlbinlog to Recover From


Then
mysqlbinlog --start-datetime='2013-11-03 12:01:00' \
/var/log/mysql/bin.000006 | mysql -u admin_user -p

Documentation on Point-in-Time Recovery: http://dev.mysql.com/doc/refman/5.5/en/point-in-time-recovery.html

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide152
Point-in-Time Recovery — Event Positions
Determine Event Position Numbers (log_pos)

mysqlbinlog --start-datetime='2013-11-03 11:55:00' \


--stop-datetime='2013-11-03 12:05:00' \
/var/log/mysql/bin.000001 > /tmp/mysql_restore.sql

Run mysqlbinlog on Binary Log until Stop Position


Execute Binary Log again from a Start Position
mysqlbinlog --stop-position=720 /var/log/mysql/bin.0000006 \
| mysql -u admin_user -p

mysqlbinlog --start-position=828 /var/log/mysql/bin.000006 \


| mysql -u admin_user -p

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide153
Replication as a Back-up Method
Use MariaDB Replication to copy Data to Slave (close to
real time, but technically asynchronous)

Recovery can be Fast and Simple


Switch Traffic to Slave, Restore Master, Switch Traffic
back to Master
Alternatively, Replay Binary Logs with mysqlbinlog tool
Allows other Methods to be executed on Slave without
increasing Load on Master

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide154
XtraBackup (Percona)
Fast Back-Up Process
Transactions Processed during Back-Up Process
Low Drain on System Resources
Back-Up Files Verified Automatically
Faster Restore Process

Documentation on XtraBackup: http://www.percona.com/doc/percona-xtrabackup


Installing XtraBackup: http://www.percona.com/doc/percona-xtrabackup/2.1/installation.html

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide155
Develop a Back-Up Plan
Take Inventory of Databases
Write a Back-Up Schedule
Write a Verification Schedule
Check and Test Back-Ups
Practice Restoring

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide156
Take Inventory
Assessment of company Database
List of Databases & Tables Tables Rows Changing Active Sensitive

Number of Rows clients 1,159,913 ✓ ✓ ✓


clients_addresses 1,159,913 ✓ ✓ ✓
Frequency of Changes clients_email 1,159,913 ✓ ✓ ✓
clients_telephone 1,159,913 ✓ ✓ ✓
How Active employees 5,042 ✓ ✓

Sensitivity of Data employees_email 5,042 ✓


employees_salaries 1,628 ✓ ✓
employees_telephones 5,042 ✓
org_departments 18
org_divisions 5
org_warehouses 5
ref_job_titles 240
ref_name_titles 9
ref_states 56

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide157
Back-Up Schedule
Which Databases & Tables
Labels and File Naming Patterns
Frequency — Days & Times
Location — Security & Off-Site

Back-Up Tables Frequency Schedule Secure Off-Site

company — full back-up all tables weekly Sunday, ✓ ✓


(company-yyyy-mmm-dd.sql) 1:00 p.m.

company — client tables clients, daily every day, ✓


(company-clients-yyyy-mmm-dd.sql) clients_addresses, 1:00 a.m.
client_telephones

company — employee tables employees, daily every day, ✓


(company-employees-yyyy-mmm-dd.sql) employees_email, 1:15 a.m.
employees_salaries,
employees_telephones

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide158
Verification Schedule
Regularly Verify Back-Ups
Practice Restoring
Databases, Tables, Rows — Frequency
Retention Periods
Restoration Tests
Back-Up Verify Tables Rows Retention

company — full back-up weekly monthly two months


company — client tables weekly semi-monthly semi-monthly one month
company — employee tables weekly semi-monthly semi-monthly one month
Back-ups will be verified on a regular basis. For testing and practicing purposes, databases, tables, and rows will be
restored regularly in a test environment.

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide159
Customizing
MariaDB for DBAs

Introduction Client Utilities Storage Engines Partitioning Monitoring & Troubleshooting MariaDB Replication
Architecture Configuration Table Schema User & Server Security Back-Ups & Restoration High Availability
Installation
 Resource Usage
 Views 
 Log Files
 Customizing
 Optimization
Method and Value of Stored Routines
Procedures and Functions are Supported
Database Level Functions — Isolating Certain Functionality
Databases Used as Namespaces (e.g., CALL
db_name.my_proc())
Routines are Dropped with Database
Library of Common Functions to make Complex Logic
Accessible

Documentation on CREATE PROCEDURE: https://mariadb.com/kb/en/create-procedure/


Documentation on CREATE FUNCTION: https://mariadb.com/kb/v/create-function/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide161
Procedures vs. Functions
Stored Procedures
Called Directly (e.g., CALL show_user())
Can Replace SQL statements, Encapsulate Complex Logic
Can Recurse (see, max_sp_recursion_depth and thread_stack)
Returns One or More Results Sets
User Defined Functions
Called within other SQL Statements (e.g., SELECT DELTA_PCT(n, n))
Performs Smaller Data Manipulation, Calculations, or Conversion
Cannot Recurse
Returns a Single Scalar Value

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide162
User Defined Function Example
Identify Need: CREATE FUNCTION DELTA_PCT
(old_nbr INT, new_nbr INT)
No Function for RETURNS DECIMAL(6,2)
Percentage of Change NO SQL
COMMENT 'Fills need for percentage change function.'
Determine Any Input and RETURN (new_nbr - old_nbr) / old_nbr;

Output SELECT DELTA_PCT(100, 125);


Develop SQL Statements

GRANT EXECUTE ON my.*


TO 'tim'@localhost;

Stored Procures: https://mariadb.com/kb/v/stored-procedure-overview/


User Defined Functions: https://mariadb.com/kb/v/user-defined-functions/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide163
Stored Procedure Example
Identify Need: DELIMITER //
CREATE PROCEDURE SHOW_USERS()
No SHOW USERS Statement CONTAINS SQL
SQL SECURITY DEFINER
Determine Any Input and Output COMMENT 'Fills need for SHOW USERS statement.'
BEGIN
Develop SQL Statements SELECT User AS 'user',
SUBSTRING(Host, 1, 16) AS 'host',
IF(Password != '', '','no') AS 'no pwd',
IF(is_role = 'Y', 'yes', '') AS 'role',
default_role AS 'default role'
FROM mysql.user ORDER BY User, Host;
END //
DELIMITER ;

CALL my.show_users;
Stored Procures: https://mariadb.com/kb/v/stored-procedure-overview/
User Defined Functions: https://mariadb.com/kb/v/user-defined-functions/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide164
Block and Scope
Block of SQL Statements Entered CREATE PROCEDURE procedure1()
BEGIN
Between BEGIN and END SET @variable1 = CURDATE();
UPDATE table1 SET col1 =
@variable1;
Variable Scope can be Local or END

Outside the Block


Local Variables Declared
SET for Non-Local Variables (i.e.,
Between BEGIN and END external, user, system)
BEGIN
BEGIN
DECLARE variable1 INT DEFAULT
100 SET max_connections = 10;
... ...
END END

Documentation on BEGIN…END: https://mariadb.com/kb/v/begin-end/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide165
Flow Control — Conditions & Switches
Conditional Branching Switches or Case Statements
IF condition DECLARE value1 INT
THEN statements
CASE value1
ELSEIF condition WHEN 10 THEN statements
THEN statements WHEN 20 THEN statements
ELSE statements ELSE statements

END IF END CASE

Documentation on IF statement: https://mariadb.com/kb/v/if-statement/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide166
Flow Control — Loops
Simple LOOP Syntax Example
CREATE PROCEDURE procedure1
BEGIN
LOOP
statements
END LOOP
END //

Documentation on LOOP: https://mariadb.com/kb/v/loop/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide167
Flow Control — Loop Methods
REPEAT & WHILE Syntax— Label Optional ITERATE & LEAVE — Label Required
CREATE PROCEDURE procedure2 CREATE PROCEDURE procedure3
label1: WHILE condition DECLARE c1 INT;
DO statements label3: LOOP
END WHILE label1 SET c1 = c1 +1;
IF c1 < 5
label2: REPEAT statements ITERATE label3;
UNTIL condition END IF;
END REPEAT label2 LEAVE label3;
END LOOP label3
END // END //

Documentation on REPEAT: https://mariadb.com/kb/v/repeat-loop/


Documentation on WHILE: https://mariadb.com/kb/v/while/
Documentation on ITERATE: https://mariadb.com/kb/v/iterate/
Documentation on LEAVE: https://mariadb.com/kb/v/leave/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide168
Error Handling
DECLARE is Used also for Conditions

DECLARE condition_name
CONDITION FOR condition_value

DECLARE can Declare Handlers

DECLARE handler_type
HANDLER FOR condition_value
statement

SIGNAL for Complete Error Handling


SIGNAL condition_value
SET signal_information

Documentation on DECLARE HANDLER: https://mariadb.com/kb/v/declare-handler/


Documentation on SIGNAL: https://mariadb.com/kb/v/signal/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide169
Cursors
DECLARE Used to Define a Cursor BEGIN
<variable_declarations>
OPEN, FETCH, CLOSE, then Operate DECLARE cursor1 CURSOR FOR
the Cursor SELECT col1,col2 FROM table1;
<handler_declarations>
Cursors are also Supported in
OPEN cursor1;
Triggers and Events read_loop: LOOP
FETCH cursor1 INTO a, b;
...
END LOOP;
CLOSE cursor1;
END //

Documentation on DECLARE CURSOR: https://mariadb.com/kb/v/declare-cursor/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide170
Triggers
Executes Automatically SQL Statements — Not for Views or
Temporary Tables

Useful for Checking, Filtering, Adjusting Data before


Writing
Maintaining Related Data (History Tables, Materialized Views)
Mimics Foreign Keys and Constraints for All Storage
Engines
Executes Once per Row, Not Once per SQL Statement
Separate Triggers Allowed for Before and After SQL
Statements
Stored in .TRG Files in the Database Folder
Documentation on Triggers: https://mariadb.com/kb/v/trigger-overview/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide171
Trigger Example
Timing of Trigger: BEFORE or AFTER CREATE TABLE admin_log.co_salary_chg
(change_id INT AUTO_INCREMENT KEY,
Type of SQL Statement to Start user_info VARCHAR(141),

Trigger: INSERT, UPDATE or DELETE salary_id INT, amount_before DECIMAL(9,2),


amount_after DECIMAL(9,2), chg_date DATETIME);
May want to surpress warnings (e.g., binary_format = 'mixed')

Action to Trigger: an SQL Statement


CREATE TRIGGER salary_change
of Multiple Statements with BEGIN… BEFORE UPDATE ON company.employees_salaries
END FOR EACH ROW
INSERT INTO admin_log.co_salary_chg
Can't Create Triggers on mysql (user_info, salary_id,
Database amount_before, amount_after, chg_date)
VALUES(USER(), OLD.salary_id,
OLD.amount, NEW.amount, NOW());

Documentation on CREATE TRIGGER: https://mariadb.com/kb/en/create-trigger/


Documentation on DROP TRIGGER: https://mariadb.com/kb/v/drop-trigger/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide172
Event Scheduler
Executes Tasks based on Predefined Schedule
Alternative to cron or similar Utilities — Portable with
Database

Enable and Activate the Event Scheduler


event_scheduler = ON
Entry in Configuration File (e.g., my.cnf)

Documentation on Event Scheduler: https://mariadb.com/kb/v/events/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide173
Creating & Monitoring Events
Schedule One-Time or Reoccurring Events
Specify Optionally Start and End Dates and Times

CREATE EVENT cleanup_log


ON SCHEDULE EVERY 1 HOUR
DO DELETE FROM admin_log.user_logins
WHERE login_time < DATE_SUB(NOW(), INTERVAL 1 HOUR);

Check and Monitor Regularly Events


SHOW EVENTS;
SHOW SCHEDULER STATUS;

Documentation on CREATE EVENT: https://mariadb.com/kb/v/create-event/


Documentation on SHOW EVENTS: https://mariadb.com/kb/v/show-events/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide174
MariaDB Replication
MariaDB for DBAs

Introduction Client Utilities Storage Engines Partitioning Monitoring & Troubleshooting MariaDB Replication
Architecture Configuration Table Schema User & Server Security Back-Ups & Restoration High Availability
Installation
 Resource Usage
 Views 
 Log Files
 Customizing
 Optimization
Purpose of MariaDB Replication
Load Balancing (Scaling SELECT Queries)
Move Slow, Heavy Queries to Slave
Take Slave Off-Line to Make Back-ups
Multiple Data Centers Need Fast Reads
Gain Redundancy (High Availability)
Fail Over - Promote Quickly a Slave to Master
Fail Over Isn’t Automatic — Requires External
Monitoring
Minimal Downtime for Upgrades or Schema Changes
Apply Changes to a Slave
Promote Slave to Master and Redirect Traffic
Apply Changes to Master and Switch

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide176
Replication Terrain
Slave 1

SQL Thread
Master Data
mysqld Storage
IO Thread

Data
mysqld Storage
IO Thread
Relay
Log
INSERT
UPDATE
DELETE Binary
Log
Dump Thread
Slave 2/Master 2

Data Slave 2A
Client Threads mysqld Storage
IO Thread
Slave 2B

CREATE Binary
ALTER Relay Log
DROP Log
Slave 2C

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide177
MariaDB Replication Factors
One Master, Multiple Slaves
No true Multi-Master Solution, but Circular Replication
Close to Real Time, but Asynchronous
Semi-Synchronous Replication Mode
Crash-Safe Slaves with Transactional Storage Engines
A Slave may also be a Master
Set log_slave_updates in Configuration File
Apply Optionally Replication Filtering Rules or Storage
Engine Changes on Intermediate Slaves

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide178
Replication — Threads
Master binlog Dump Thread
Pushes binlog Events to Slave
Visible in SHOW PROCESSLIST as "Binlog Dump"
Slave IO Thread — Visible in SHOW SLAVE STATUS

Requests and receives binlog events from the Master


Writes them to the local relay log
Slave SQL Thread — Visible in SHOW SLAVE STATUS

Reads the Relay Log and Executes Queries on Local Data


Checks the Query Result Codes Match those Recorded by Master
Slave Multiple Execution Threads
Multi-Threaded Slave separates events based on Database Names
Updates are Applied in Parallel, Not Sequence

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide179
Parallel Replication
Replication Process on Slaves
Events Received from Master by IO Thread and Queued
in Relay Log
Each Relay Log Entry is Retrieved by the SQL Thread
Each Transaction is Applied to the Slave
On Non-Parallel Systems, Application Performed
Sequentially by SQL Thread
On Parallel Systems, Application Performed in Pool of
Separate Replication Worker Threads

Documentation on Parallel Replication: https://mariadb.com/kb/en/mariadb/documentation/replication/standard-replication/parallel-replication/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide180
Topologies
Master to Slave
Simplest Solution and Used Most Widely
Allows Off-Loading of SELECT Traffic to Slave
Master1 to MasterN ... to Master1 (circular)
Servers Replicate in a Circle, with binlog Events
Traversing the ring until Originating Server
Does Not Alleviate Heavy Write Load
Needs Careful setup of server-id and
auto_increment_offset, auto_increment_increment
settings
Master to Slave to Slaves
Can build Complex Trees
Useful for Replication Rules or Storage Engine Changes

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide181
Master Configuration
Enable Binary Log — Choose Binary Log Format
Set server-id in Configuration File to Unique Value
Create Replication User Account on Master

GRANT REPLICATION SLAVE ON *.*


TO 'maria_replicator'@'52.34.19.24'
IDENTIFIED BY 'rover123';

Make a Consistent Snapshot of Data on Master


mysqldump -p -u admin_backup --master-data --flush-logs \
--all-databases > full-dump.sql

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide182
Slave Configuration
Set server-id in Configuration File to Unique Value
Add read-only in Configuration File to Prevent Writes
Set Optionally Replication Rules — Covered Later in Class
Restart MariaDB
CHANGE MASTER TO

Load Data from Master MASTER_HOST='35.161.145.71',


MASTER_PORT=3306,
mysql -p -u root < full-dump.sql MASTER_USER='maria_replicator',
MASTER_PASSWORD='rover123';
Execute START SLAVE on Slave

scp -i ./.ssh/mariadb.pem ec2-user@35.161.145.71:/home/ec2-user/full-dump.sql .

Documentation on Slave Options: https://mariadb.com/kb/en/mariadb/replication-and-binary-log-server-system-variables/


Documentation on CHANGE MASTER TO: https://mariadb.com/kb/en/mariadb/change-master-to/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide183
Monitoring Replication
Check Regularly Status on Master —
Includes binlog number and position

SHOW MASTER STATUS;

Check More Often Status of Replication SHOW SLAVE STATUS \G

on Slave
Slave_IO_State:
Waiting for master to send
event
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Last_Errno: 0
Last_Error:
Seconds_Behind_Master: 300
Documentation on SHOW MASTER STATUS: https://mariadb.com/kb/en/show-master-status/
Documentation on SHOW SLAVE STATUS: https://mariadb.com/kb/en/mariadb/show-slave-status/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide184
Replication Files
Binary Log Files (Master)
Master Records Write-Queries to File
Rotated when Flushed or Periodically to New Log File — File
Name Pattern (.000001)

Relay Log File (Slave)


Record of Master binlog Events
Rotated when Flushed or Periodically — File Name Pattern (.000001)
Replication Configuration Recorded in master.info (Slave)
Name of Relay Log File Recorded in relay-log.info (Slave)

Documentation mysqlbinlog: https://mariadb.com/kb/en/mariadb/using-mysqlbinlog/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide185
Slave Configuration Files
master.info relay-log.info

52.89.128.176 196803
maria_replicator mariadb-bin.000040
rover123 196513
3306 30217
60 9
0 ...
...

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide186
Replication File Maintenance & Back-Ups
Replication Files Updated & Purged Automatically —
Don’t Edit or Move Manually

Include Replication Files when Making Binary Back-


ups
Use --raw option with mysqlbinlog to Back-up Binary
Log

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide187
Binary Log Format
Statement Based (SBR) — Original Queries are Replicated
Least Data Sent over Wire and Tested for Years
Non-Deterministic Statements Executed on Slave — Slave Load is Increased vs. RBR
Row Based (RBR) - Table Rows are Replicated
Only Non-Deterministic Statements Executed on Master — Slave Load is Reduced vs. SBR
More Data sent over Wire — Not Supported by All Engines
Mixed (default) - Smart Switching between SBR and RBR
[mysqld]
binlog-format=MIXED

Checksum (--binlog-checksum) in Binary and Relay Logs to detect Errors


Includes Errors in Memory, Disk, Network and Database
Can be Implemented for each Slave
Documentation on Binary Log Format: https://mariadb.com/kb/en/mariadb/binary-log-formats/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide188
Slave Filtering Rules — Database Level
Exclude Specific Databases (e.g., mysql)
CHANGE REPLICATION FILTER SET GLOBAL replicate_ignore_db = 'mysql';
REPLICATE_IGNORE_DB = (mysql);

Include Specific Databases


CHANGE REPLICATION FILTER SET GLOBAL replicate_ignore_db = '';
REPLICATE_IGNORE_DB = (), SET GLOBAL
REPLICATE_DO_DB = (sales,inventory); replicate_do_db = 'sales,inventory';

Excluding can Cause Problems with Joins

Documentation on Slave Options: https://mariadb.com/kb/en/mariadb/replication-and-binary-log-server-system-variables/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide189
Slave Filtering Rules — Table Level
Ignore Specific Tables CHANGE REPLICATION FILTER
REPLICATE_IGNORE_TABLE =
(employees.salary);

Include Specific Tables CHANGE REPLICATION FILTER


REPLICATE_IGNORE_DB = (employees),
REPLICATE_DO_TABLE = (employees.names,
employees.contacts);

Wildcards for Multiple Tables CHANGE REPLICATION FILTER


REPLICATE_IGNORE_DB = (sales),
REPLICATE_DO_TABLE = (sales.europe_%),
REPLICATE_IGNORE_TABLE =
(sales.europe_uk_%);

Documentation on Slave Options: https://mariadb.com/kb/en/mariadb/replication-and-binary-log-server-system-variables/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide190
MariaDB Replication - Asynchronous
Master Doesn’t Wait for Slaves
IO Thread may be Slow to Receive binlog Packets
Network Congestion or Disconnects
SQL Thread may be Slow in Processing Relay Log
Events
Load on Slave or Network Problems

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide191
Semi-Synchronous
Implemented with an optional Plug-In INSTALL PLUGIN rpl_semi_sync_master
SONAME 'semisync_master.so';
A COMMIT on Master can Wait for a Slave to
Acknowledge it’s Received Transaction INSTALL PLUGIN rpl_semi_sync_slave
SONAME 'semisync_slave.so';
Master Waits for Slave to Write
Transaction to Relay Log, Not to
Execute Transaction — Slave SQL Thread
may still Lag

One Slave Response needed for Master to


Continue (Semi-Synchronous, Not Synchronous)
Can Affect Significantly Performance of
Master
Documentation on Semi-Synchronous Replication: https://mariadb.com/kb/en/mariadb/semisynchronous-replication/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide192
Lagging Slave
When Slave SQL Thread is Slow or Disabled due to
Errors, Slave is said to Lag behind Master
Slave SQL Thread must Execute Serially Queries that
were Executed in Parallel on the Master
Slave Multiple Execution Threads
Multi-Threaded Slave separates events based on
Database Names
Updates are Applied in Parallel, Not Sequence
Time-Delayed Replication - Set
Provides a Buffer to Stop Replication of Mistakes

CHANGE MASTER TO MASTER_DELAY = seconds;

Documentation on Time-Delayed Replication: http://dev.mysql.com/doc/refman/5.6/en/replication-delayed.html

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide193
Troubleshooting Problems with Replication
Check Slave Error Log for Errors affecting Replication
Look for Disconnects from Network Problems
Binary or Relay Log Event Corruption will cause Slave
SQL Thread to Stop
Different Query Error Codes on Slave indicate it’s Not
Synchronized with Master
Tools like Maatkit can help with Replication
Troubleshooting and Recovery
May Need to Rebuild Slave from a fresh Snapshot
(Back-up of Master or Another Slave)

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide194
High Availability
MariaDB for DBAs

Introduction Client Utilities Storage Engines Partitioning Monitoring & Troubleshooting MariaDB Replication
Architecture Configuration Table Schema User & Server Security Back-Ups & Restoration High Availability
Installation
 Resource Usage
 Views 
 Log Files
 Customizing
 Optimization
High Availability Goals and Concepts
Never Lose Data because of a Crash — Users Should
Never Notice

Remain Operational Despite Unforeseen


Problems
System Redundancy of Software and Hardware
Write Data to Multiple Devices and Locations
(e.g., RAID, Replication, Galera, DRBD)
A Second Database Server Designated as a Hot
Spare

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide196
High Availability Goals — Reality Check
Availability = Up-Time / (Up-Time + Down-Time)
Availability Down-Time
90% 1 Nine 36.5 days per year
99% 2 Nines 3.65 days per year
99.9% 3 Nines 8.76 hours per year
99.99% 4 Nines 52 minutes per year
99.999% 5 Nines 5 minutes per year
99.9999% 6 Nines 31 seconds per year

Availability = Mean Time Before Failure / (Mean Time Before Failure + Mean Time To Recovery)

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide197
MariaDB Replication
Close to Real Time — Asynchronous or Semi-
Synchronous
Mono-Threaded on Slaves
Semi-Synchronous Replication Mode
A Slave may also be a Master
Set log_slave_updates in Configuration File
Master High Availability Manager
Monitors the Master
Automates Master Fail-Over and Slave Promotion
Integrates with Pacemaker & Heartbeat

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide198
MariaDB MaxScale
Application-to-Database Dynamic Data Client Client Client

Routing Platform
Insulates Client Applications from
Complexities of Backend Database
Cluster MaxScale

Improves Database Availability,


Security and Scalability MariaDB
 MariaDB
Slave Master
Database-to-Database Dynamic Data
MariaDB

Routing Platform Slave

Simplifies Interoperability across


Databases
Tutorials on MaxScale: https://github.com/mariadb-corporation/MaxScale/tree/develop/Documentation/Tutorials
Documentation on MaxScale: https://github.com/mariadb-corporation/MaxScale/blob/develop/Documentation/Getting-Started/MariaDB-MaxScale-Installation-Guide.md

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide199
MariaDB Replication Manager
High Availability Solution
Monitor and Administer MariaDB Replication and
MariaDB Enterprise Clusters
On-Demand Slave to Master Promotion (i.e.,
Switchover)
Electing a New Master on Failure Detection — MaxScale
will Route Queries to New Master

Download MariaDB Replication Manager: https://github.com/mariadb-corporation/replication-managerl

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide200
MariaDB Galera Cluster
Virtually Synchronous Replication
Uses Only InnoDB Storage Engine
True Multi-Master Solution
Conflict Detection & Resolution upon Commit
Automatic Provisioning
Nodes Isolated Easily for Maintenance

Documentation for Galera: http://galeracluster.com/documentation-webpages/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide201
Distributed Replicated Block Device
DRBD is a Linux Kernel Module,
Providing Synchronous
Replication of a Block Device
between Two Servers — Hot Spare
Server

If Primary Server Fails, Secondary


Server is Used — Immediately and
Seamlessly

DRBD Disk Writes over Network


slows MariaDB

DRBD User Guide: http://www.drbd.org/en/doc/users-guide-84/p-introl

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide202
Shared-Disk Architecture
Active – Passive replication
Failover requires MariaDB Crash
Recovery — Often File System Crash Recovery
Non-Transactional — Only MyISAM Storage
Engine

Combined with Pacemaker/Heartbeat


for Auto Failover
Virtual IP often used to Fail Over
In theory the SAN is a SPOF

More information on Shared Storage: https://mariadb.com/sites/default/files/SharedStorage.pdfl

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide203
Optimization
MariaDB for DBAs

Introduction Client Utilities Storage Engines Partitioning Monitoring & Troubleshooting MariaDB Replication
Architecture Configuration Table Schema User & Server Security Back-Ups & Restoration High Availability
Installation
 Resource Usage
 Views 
 Log Files
 Customizing
 Optimization
Optimization Concepts & Considerations
Performance Optimizing relates to
Resolving Bottlenecks to Handle
More Traffic or Data at a Time
Efficiency Optimizing has to do with
the Best Use of Hardware,
Bandwidth, and Budget
Consider Carefully Schema and Indexes
Don’t Spend too much Time on
Optimizing without Data — Need Real
Data and Traffic to Optimize Effectively

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide205
Query Tuning
Don’t Tune Queries on Production Server
Tune Queries on a Test Server
Use a Complete Copy of Data
Use Indexes to Improve Read Performance

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide206
Query Analysis
Use the Slow Log to find Problem Queries (-- EXPLAIN SELECT * FROM employees
log-slow-queries) WHERE MONTH(birth_date) = 8 \G
Use mysqldumpslow Utility for Manageable id: 1
select_type: SIMPLE
Reports
table: employees
Use EXPLAIN to see how MariaDB Executes a type: ALL
Troublesome Query and if Indexes are Used possible_keys: NULL
key: NULL
Use EXPLAIN EXTENDED and SHOW key_len: NULL
WARNINGS to see how MariaDB Rearranges ref: NULL
a Query before Execution rows: 299587
Extra: Using where

EXPLAIN EXTENDED SELECT …;


SHOW WARNINGS;
Documentation on EXPLAIN: https://mariadb.com/kb/en/mariadb/explain/
Documentation on EXPLAIN EXTENDED: https://mariadb.com/kb/en/mariadb/explain/#explain-extended
Documentation on mysqldumpslow: https://mariadb.com/kb/en/mariadb/mysqldumpslow/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide207
Fields from EXPLAIN
Field Description
id Query identifier
select_type Type of SELECT performed

table The table in use (each table in a JOIN will use one row)

*type The table access strategy

possible_keys Any available indexes that could resolve the query

key Index MariaDB chose from possible_keys


key_len The portion of the chosen index to be used (width in bytes)
ref Columns from the index, or a constant, used
rows Number of rows the query is expected to touch
Extra Extra information relating to the access strategy
* See Next Slide for List of Types

Documentation on EXPLAIN: https://mariadb.com/kb/en/mariadb/explain/


Documentation on EXPLAIN EXTENDED: https://mariadb.com/kb/en/mariadb/explain/#explain-extended

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide208
Scan or Index Method Types
Field Description
const A constant value can be read once and cached
eq_ref One index access per outer query row

ref Multiple index accesses per outer query row

index_merge Multiple indexes used, merging into a single result set

range Multiple index accesses to return all rows within a range

index Full index scan (every index entry is read sequentially)


all Full table scan (every record is read sequentially)

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide209
Choosing Data Types
Appropriate Type (e.g., INT for SELECT col1 FROM table1
numbers not VARCHAR) WHERE col1 = 'test'
PROCEDURE ANALYSE() \G
Accurate Type (e.g., DECIMAL for
currency, not DOUBLE) Field_name: db1.table1.col1
Min_value: 7450554
Smallest Useful Type (e.g., CHAR for Max_value: 8592336
known text length, not VARCHAR) Min_length: 7
Max_length: 7
Use NOT NULL, if practical
Empties_or_zeros: 0
Analyze Columns with PROCEDURE Nulls: 0
ANALYSE() Avg_value_or_avg_length: 7955635.7647
Std: 342328.4478
Optimal_fieldtype: MEDIUMINT(7) UNSIGNED NOT NULL

Documentation for PROCEDURE ANALYSE( ): https://mariadb.com/kb/en/mariadb/procedure-analyse/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide210
Optimize Tables SELECT first_name, last_name
FROM employees PROCEDURE ANALYSE() \G
Appropriate Data Types - Smaller is Better *************************** 1. row *********************
Field_name: employees.employees.first_name
Min_value: Aamer
Shorten Variable Length Columns (i.e., VARCHAR) Max_value: Zvonko
— May Expand to Full Width in Memory Min_length: 3
Max_length: 14
Use PROCEDURE ANALYSE() for Data Type Empties_or_zeros: 0
Recommendations Nulls: 0
Avg_value_or_avg_length: 6.2157
Std: NULL
DESCRIBE employees; Optimal_fieldtype: VARCHAR(14) NOT NULL
+------------+---------------+------+ *************************** 2. row *********************
Field_name: employees.employees.last_name
| Field | Type | Null |
Min_value: Aamodt
+------------+---------------+------+
Max_value: Zykh
| emp_no | int(11) | NO | Min_length: 4
| first_name | varchar(255) | YES | Max_length: 16
| last_name | varchar(255) | YES | Empties_or_zeros: 0
Nulls: 0
+------------+---------------+------+
Avg_value_or_avg_length: 7.1539
Std: NULL
Optimal_fieldtype: VARCHAR(16) NOT NULL

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide211
Indexes for Performance Optimization
MariaDB can Resolve Queries Faster with Indexes
Faster Retrieval of Matching Rows, Faster Sorting of
Result Sets
Without Indexes, MariaDB does Full Table Scan
Indexes can be for a Column or Multiple Columns (i.e.,
Composites)

Index for Speed, but Avoid Indexing Excessively or


Arbitrarily
Remove Unused or Redundant Indexes

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide212
Table Indexes
Use ALTER TABLE or CREATE INDEX ALTER TABLE employees
ADD INDEX emp_names
to add an Index (name_last, name_first);

CREATE INDEX client_names ON clients


(name_last(10), name_first(5));
Two Methods for Creating an Index for an Existing Table

Use SHOW CREATE TABLE to see a Table's SHOW CREATE TABLE clients \G
*************************** 1. row ***********************
Indexes Table: clients
Create Table: CREATE TABLE 'clients' (
'client_id' int(11) NOT NULL AUTO_INCREMENT,
'name_first' varchar(255) DEFAULT NULL,
'name_last' varchar(255) DEFAULT NULL,
PRIMARY KEY ('client_id'),
KEY 'client_names'
('name_last'(10),'name_first'(5)))
ENGINE=InnoDB DEFAULT CHARSET=latin1
Documentation on CREATE INDEX: https://mariadb.com/kb/en/mariadb/create-index/

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide213
Creating Indexes
Indexes are Created with CREATE TABLE
and ALTER TABLE
Create an Index for a new Table with CREATE TABLE people
CREATE TABLE (id INT, name CHAR(20),
age TINYINT UNSIGNED,
KEY aged_name(age, name));
Add an Index to a Table with ALTER
TABLE or CREATE INDEX ALTER TABLE employees
ADD INDEX full_name
Keep Indexes as Small as Practical (name_last, name_first);

For Indexing Strings, Use Prefix CREATE INDEX short_name


ON employees
(name_last(8),
name_first(5));

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide214
Better JOIN Construction
Foreign Keys used with Joins should be Indexed
Index Columns in ON and USING Clauses
Keep GROUP BY and ORDER BY columns in one Table for better
Indexing

SELECT last_name, city, country_name


FROM clients
JOIN country_names USING(country_id)
WHERE clients.country_id IN('de','fr','it','uk')
ORDER BY country_name, city, last_name;

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide215
Optimize Tables
Minimize Table Size on Disk and in Memory
Use Partitioning
Spread Large Tables across Multiple Servers if Application
Permit
Archive Table Data when Possible and Appropriate
Index Appropriately
Remove Duplicate or Unused Indexes
Don’t Index a Column if First Field in Another Index

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide216
Buffer Pool
Use the SHOW STATUS statement to see
SHOW STATUS LIKE '%innodb_buffer_pool%';
+-----------------------------------------+-----------+
| Variable_name | Value |
Variables related to Buffer Pool +-----------------------------------------+-----------+
| Innodb_buffer_pool_pages_data | 8032 |
| Innodb_buffer_pool_bytes_data | 131596288 |
SHOW VARIABLES LIKE '%innodb_buffer_pool%'; | Innodb_buffer_pool_pages_dirty | 0 |
+---------------------------------------+-----------+ | Innodb_buffer_pool_bytes_dirty | 0 |
| Variable_name | Value | | Innodb_buffer_pool_pages_flushed | 850451 |
+---------------------------------------+-----------+ | Innodb_buffer_pool_pages_LRU_flushed | 119 |
| innodb_buffer_pool_instances | 1 | | Innodb_buffer_pool_pages_free | 1 |
| innodb_buffer_pool_populate | OFF | | Innodb_buffer_pool_pages_made_not_young | 0 |

| innodb_buffer_pool_restore_at_startup | 0 | | Innodb_buffer_pool_pages_made_young | 207678 |


| Innodb_buffer_pool_pages_misc | 158 |
| innodb_buffer_pool_shm_checksum | ON |
| Innodb_buffer_pool_pages_old | 2944 |
| innodb_buffer_pool_shm_key | 0 |
| Innodb_buffer_pool_pages_total | 8191 |
| innodb_buffer_pool_size | 134217728 |
| Innodb_buffer_pool_read_ahead_rnd | 0 |
+---------------------------------------+-----------+
| Innodb_buffer_pool_read_ahead | 0 |
| Innodb_buffer_pool_read_ahead_evicted | 0 |
| Innodb_buffer_pool_read_requests | 215938795 |
| Innodb_buffer_pool_reads | 37986 |
| Innodb_buffer_pool_wait_free | 0 |
| Innodb_buffer_pool_write_requests | 4817642 |
+-----------------------------------------+-----------+

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide217
Optimize Hardware
Benchmark Systems using Real Data and Real Traffic
Search for Performance Limits Related to Equipment
Upgrade, Fine Tune, or Buy More Hardware
Buy Fast Disks for Large Databases
More RAM for Faster Query Processing - Enough Memory
for Most or All Results Sets
More CPU Cores Helps High Traffic Sites
Ideally, Run Only MariaDB on Server (e.g., Web, Email Service
on Separate Server)

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide218
Conclusion
MariaDB for DBAs

Introduction Client Utilities Storage Engines Partitioning Monitoring & Troubleshooting MariaDB Replication
Architecture Configuration Table Schema User & Server Security Back-Ups & Restoration High Availability
Installation
 Resource Usage
 Views 
 Log Files
 Customizing
 Optimization
On-Line Resources
Documentation
Knowledge Base
Open-Source Community
Post Questions on Forums
Ask Questions on IRC — FreeNode

MariaDB Documentation and Knowledge Base: http://mariadb.com/kb

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide220
Other Training Courses
Public In-Person Courses
Private In-Person Courses
Live Virtual Training Courses
Self-Paced On-Line Courses

Training Courses: https://mariadb.learnupon.com/store


Upcoming Courses: https://mariadb.learnupon.com/store/sessions

training@mariadb.com MariaDB for DBA's


Copyright MariaDB Ab. Commercial in Confidence
Version 3.3, Slide221
Thanks for Participating
MariaDB for DBAs

You might also like