MySQL For Database Administrators Main Control Updated
MySQL For Database Administrators Main Control Updated
Administrators
Chapter 1:
Introduction
2
Introduction to MySQL
MySQL is a fast, easy-to-use RDBMS being used for many small and big
businesses.
MySQL is developed, marketed and supported by MySQL AB, which is a Swedish
company.
The world’s most popular open-source database
Over 15 million estimated active installations
The M of the LAMP stack
Used by 9 of the top 10 websites in the world
Embedded by over 3,000 ISVs and OEMs
The leading database in the cloud
Highly popular on social media (Facebook, Twitter, and so on)
3
Why MySQL Makes Sense for Oracle
Complete solutions
Best-of-breed at every level
On-premises and in the cloud
MySQL: web, mobile, and embedded
Oracle drives MySQL innovation.
4
MySQL Is Powering the World
Cloud
An impressive
customer and partner
community!
5
MySQL Database Server Editions
• MySQL Community Edition
– Open source, noncommercial
GPL • MySQL Cluster Community Edition
– Open source, noncommercial, cluster
6
MySQL Enterprise Edition
Highest levels of MySQL scalability, security, and up time
Oracle Premier
Lifetime Support
MySQL Enterprise Oracle Product
Security Certifications/Integration
s
MySQL Enterprise MySQL Enterprise
Audit Monitor/Query Analyzer
MySQL Enterprise
MySQL Workbench
High Availability
7
MySQL Connectors and APIs
Connectors APIs
ODBC C
JDBC PHP
NET Perl
Python Python
C Ruby
C++ Tc
l
8
MySQL Services
MySQL training
• Comprehensive set of MySQL training courses
MySQL certification
MySQL consulting
• Full range of consulting services from startup to optimization
9
Community Support
Mailing lists
Forums
Community articles
PlanetMySQL blogs
Twitter
Physical and virtual events
Developer Days
Tech Tours
Webinars
…and more
10
Oracle Premier Support for MySQL
Rely on the experts and get unique benefits:
Straight from the source
Largest team of MySQL experts
Backed by MySQL developers
Forward-compatible hot fixes
MySQL maintenance releases
MySQL support in 29 languages
24/7/365
Unlimited incidents
Knowledge base
MySQL consultative support
11
MySQL-Supported Operating Systems
Provides control and flexibility for users
Supports multiple commodity platforms, including:
Windows (x86, x86_64)
Linux (x86, x86_64, IA64)
Oracle Solaris (SPARC, x86_64, x86)
Mac OS X (x86, x86_64)
12
MySQL Websites
http://dev.mysql.com includes:
Developer Zone (articles, forums, PlanetMySQL, and more)
Downloads (GA and development release)
Documentation
http://www.mysql.com includes:
Downloads (GA)
Services
Consulting
Resources
White papers
Webinars
13
MySQL Curriculum Footprint
Database developers Database administrators
Intermediate
Advanced
14
MySQL Certification
The Oracle Certification Program validates various levels of MySQL expertise:
Introductory: Certified Associate
Oracle Certified Associate: MySQL
15
MySQL Online Documentation
MySQL Reference Manual
Topic guides
Expert guides
MySQL help tables
Example databases
Meta documentation
Community-contributed documentation
Printed books
Additional resources
16
Example Databases
MySQL provides several example databases:
world_innodb
world
employee
menagerie
sakila
Available for download from the MySQL Developer Zone website:
http://dev.mysql.com/doc/index-other.html
The world_innodb and sakila databases are used for demonstrations and
practices in this course.
17
Summary
In this lesson, you should have learned how to:
Describe the course goals
Explain the origin and status of the MySQL product
List the available MySQL products and professional services
List the currently supported operating systems
Describe how to access MySQL information and services on Oracle and MySQL
websites
List the available MySQL courses
Describe the Oracle Certification Program offerings for MySQL
Obtain MySQL online documentation and example databases
18
Chapter 2:
Architecture
19
Objectives
After completing this lesson, you should be able to:
Describe the MySQL client/server model
Understand communication protocols
Understand how the server supports storage engines
Explain the basics of how MySQL uses memory and disk space
20
MySQL Architecture
MySQL client/server model
Database
PHP
mysqld TCP/IP
MySQL Server Process
TCP/IP Socket
Linux
(localhost) TCP/IP
mysql PHP ODBC
Client Client
Process Process
Windows
21
Client Programs
Connect to the MySQL server to retrieve, modify, add, or remove data.
Use these client programs to perform the following actions:
mysql: Issue queries and view results.
mysqladmin: Administer the server.
mysqlcheck: Check the integrity of database tables.
mysqldump: Create logical backups.
mysqlimport: Import text data files.
mysqlshow: Show database, table, and column information.
mysqlslap: Emulate client load.
Use MySQL Workbench for database management.
22
Administrative and Utility Programs
Access data files directly without using a client to connect to the server.
Use non-client programs
innochecksum: Check an InnoDB tablespace file offline.
mysqlaccess: Check access privileges.
mysqldumpslow: Summarize the slow query log files.
mysqlbinlog: Display the binary log files.
Some of the programs have requirements that must be met prior to running:
Shut down the server.
Back up your current tables.
Review program requirements prior to implementation.
23
Chapter 3:
The MySQL Server
24
MySQL Server
Is the database server program called mysqld
Is not the same as a “host”
Is a single process and is multithreaded
Manages access to databases on disk and in memory
Supports simultaneous client connections
Supports multiple storage engines
Supports both transactional and nontransactional tables
Uses memory in the form of:
Caching
Buffering
25
Server Process
Connection layer
mysqld
SQL layer
Storage layer
26
Connection Layer
Connection layer
• Communication protocols
• Threads
• Authentication
mysqld
SQL layer
Storage layer
27
Communication Protocols
Protocol Types of Connections Supported Operating
Systems
TCP/IP Local, remote All
28
SQL Layer
Connection layer
SQL layer
mysqld
Storage layer
29
SQL Statement Processing
SQL layer Start
Optimize
No query
FOUND?
Execute
Yes
query
Update
cache
query
Finish
30
Storage Layer
Connection layer
SQL layer
mysqld
Storage layer
31
Storage Engine: Overview
Storage engines are server components that act as handlers for different table
types.
Storage engines are used to:
Store data
Retrieve data
Find data through an index
Two-tier processing
Upper tier includes SQL parser and optimizer.
Lower tier consists of a set of storage engines.
SQL tier is not dependent on the storage engine:
The engine does not affect SQL processing.
There are some exceptions.
32
Features Dependent on Storage Engine
Storage medium
Transactional capabilities
Locking
Backup and recovery
Optimization
Special features
Full-text search
Referential integrity
Spatial data handling
33
How MySQL Uses Disk Space
Disk Space
Data Directory
Server Log Files Program
Status Files Executable
Files
InnoDB Log Files
InnoDB System
Tablespace
Database Directory
(per database)
Data and Index Files
Program
Format Files (.frm) Log
Files
Triggers
34
How MySQL Uses Memory
Global
Allocated once
Shared by the server process and its threads
Session
Allocated for each thread
Dynamically allocated and deallocated
Used for handling query results
Buffer sizes per session
35
Memory Structures
Server allocates memory in three different categories:
• Query cache
Server/Shared • Thread cache
• Sort buffer
Connection/ • Read buffer
Session • Temporary table
36
MySQL Plugin Interface
Daemon plugins, run by the server
Plugin API allows loading and unloading of server components.
Supports dynamic loading, without restarting server
Supports full-text parser
Supports different authentication methods
Supports storage engines installed as plugins
Supports INFORMATION_SCHEMA plugins
Requires the PLUGINS table in the mysql database
MySQL supports both client and server plugins
37
Summary
In this lesson, you should have learned how to:
Describe the MySQL client/server model
Understand communication protocols
Understand how the server supports storage engines
Explain the basics of how MySQL uses memory and disk space
38
Practice 2-1 Overview: Quiz
In this quiz, you answer questions about the MySQL architecture.
39
Chapter 4:
Configure the MySQL Server
40
Objectives
41
MySQL Configuration Options
Precompiled
Options
Startup
Configuration
Command-Line Configuratio
Options n File
Options
42
Reasons to Use Option Files
43
Option File Groups
Option File
Client-Specific Server-Specific
• [mysql] • [mysqld]
• [mysqldump] • [mysqld_safe]
• ... • ...
44
Writing an Option File
A brief example of groups in an option file:
[client]
[mysql]
show-warnings
45
Option File Locations
MySQL server looks for files in standard locations.
Standard files are different for Linux and Windows:
In Linux, use the my.cnf file.
In Windows, use the my.ini file.
You can view option file lookup locations and groups with options:
shell> mysql --help
46
Startup Options in an Option File
Logging
System
Shared Memory
Variable
s
47
Sample Option Files
The MySQL installation provides a sample configuration file:
my-default.cnf
The install process copies this file to /etc/my.cnf.
If that file already exists, it copies it to /etc/my-new.cnf instead.
Specify new options in the file to replace, add, or ignore the standard
options.
Must be the first option on the command line
--defaults-file=<file_name>
--defaults-extra-file=<file_name>
--no-defaults
48
Displaying Options from Option Files
Display options per group from the command line.
Examples of [mysql] and [client] groups:
shell> my_print_defaults mysql client
--user=myusername
--password=secret
--host=localhost
--port=3306
--character-set-server=latin1
Or (for the same output):
mysql --print-defaults mysql client
49
Login Paths
To create a login path:
mysql_config_editor --login-path=<login-path> --user=<user> --password
-- host=<hostname>
To view a single login path in clear text:
mysql_config_editor print --login-path=<login-path>
To view all login paths in clear text:
mysql_config_editor print --all
To remove a login path:
mysql_config_editor remove --login-path=<login-path>
The default login path name is client.
Is read by all standard clients
50
Server System Variables
The MySQL server maintains many server system variables that indicate how
it is configured.
Each system variable has a default value.
You can set variables at server startup by doing either of the following:
Use options on the command line.
Use an option file.
You can refer to system variable values in expressions.
You can view the values that a server uses.
51
Dynamic System Variables
You can change most variables dynamically while the server is running.
– In this way, you can modify the operation of the server without having to
stop or restart it.
Variable categories:
Affects
Affects all sessions new Affects current session
session
52
Displaying Dynamic System Variables
List all available variables and their values:
SHOW [GLOBAL|SESSION] VARIABLES;
List specific variable values:
mysql> SHOW VARIABLES LIKE 'bulk%’;
53
Structured System Variables
A structured variable differs from a regular system variable in two ways:
It is a structure with components that specify closely related server parameters.
There can be several instances of a given type of structured variable. Each one has
a different name and refers to a different resource maintained by the server.
MySQL supports a structured variable to govern the operation of key caches.
54
Server Status Variables
Use the SHOW STATUS statement to assess the health of a system.
Choose from two categories (similar to dynamic variables):
55
SQL Modes
Configure many server operation characteristics by setting the SQL mode:
Specify how strict or forgiving MySQL is about accepting input data,
Set compatibility with other database systems.
Control query processing.
Enable or disable behavior relating to SQL conformance.
Override SQL “empty” default mode.
Empty mode does not enable restrictions or conformance behaviors.
The default SQL mode is NO_ENGINE_SUBSTITUTION.
The default configuration file adds STRICT_TRANS_TABLES.
56
Setting the SQL Mode
Set at startup from the command line:
shell> mysqld --sql-mode=<mode_value>
SET statement
Within mysql, after startup:
SET [SESSION|GLOBAL] sql_mode=<mode_value>
58
Log Files
MySQL has different log files to record server activity:
Error
General Query
Slow Query
Binary
Enterprise Audit
Log files:
Can use large amounts of disk space
Can be stored in files
Can be stored in tables
General and slow query logs only
Are written in text format
Except binary log
59
Log File Usage Matrix
File Name
Log File Options Programs
Table Name
60
Binary Logging
Log shipping system:
Master
Server
Restart
FLUSH Maximum
LOGS Log Size
SLAVE
Execution of Master
Binary Log Binary Log Files
61
Binary Logging Formats
Choose from two different types of logging:
Statement-Based Row-Based
62
List Binary Log Files
63
View Binary Log Contents
Cannot be viewed with normal text viewers:
Logs are stored in compact binary format.
64
Deleting Binary Logs
By default, old log files are not deleted.
Delete logs based on age:
SET GLOBAL expire_logs_days = 7;
…or…
PURGE BINARY LOGS BEFORE now() - INTERVAL 3 day;
Delete logs based on file name:
PURGE BINARY LOGS TO 'mysql-bin.000010';
65
Configuring Enterprise Audit
67
Summary
In this lesson, you should have learned how to:
Set up MySQL server configuration files
Explain the purpose of dynamic server variables
Review the server status variables that are available
Configure operational characteristics of the MySQL server by using the SQL
modes
Describe the available log files
Explain binary logging
Configure audit logging with MySQL Enterprise Audit
68
Chapter 5:
MySQL Clients
69
Objectives
After completing this lesson, you should be able to:
Describe the available clients for administrative tasks
Use MySQL administrative clients
Use the mysql command-line client
Use the mysqladmin command-line client for administrative tasks
Use the MySQL Workbench graphical client
Describe the available MySQL tools
List the available APIs (drivers and connectors)
70
Command-Line Client Programs
Prompt Description
71
Invoking Command-Line Clients
Invoke from a command line:
Linux or UNIX shell prompt (terminal window)
Windows console prompt
shell> <client> [options]
73
Quiz
All of the MySQL command-line clients display a list of available options when
the client command is executed with the _________ option.
A. -V
B. --help
C. --List
D. --options
74
Invoking mysql Client
Provide credentials on the command line:
shell> mysql -u<name> -p<password>
Execute a statement:
shell> mysql --login-path=<login-path> -e "<statement>“
76
mysql Client: Output Formats
Tabula Tabbed
r
Output
Interactive Batch Characters
--
-- batch
table
Other Formats
HTML XML
Format Format
--html --xml
77
mysql Client: MySQL Client Commands
List all MySQL client-level commands:
mysql> HELP
78
mysql Client: SQL Statements
79
mysql Client: Help on SQL Statements
View full SQL category list:
mysql> HELP CONTENTS
...
Account Management
Administration
Compound Statements
Data Definition
Data Manipulation
Data Types
...
Help on a specific SQL category or statement:
mysql> HELP Data Manipulation mysql> HELP JOIN
Abort statement
– Use the \c terminator 81
mysql> SELECT VERSION()\c
mysql>
mysql Client: Special Statement Terminators
Use multi-line statements:
Terminator is required at end.
Prompt changes from mysql> to ->.
mysql> SELECT VERSION(),
-> DATABASE();
End the session and exit the mysql client:
Use the \q terminator or QUIT or EXIT.
mysql> \q
Bye
82
mysql Client: Redefining the Prompt
Redefine the prompt:
mysql> PROMPT term 1> term 1>
83
mysql Client: Using Script Files
Process input files from within mysql:
If the files contain SQL statements, they are known as:
“Script file”
“Batch file”
84
mysqladmin Client
A command-line client for DBAs
Many capabilities
“Ping” the server.
Shut down the server.
Create and drop databases.
Display server and version information.
Display or reset server status variables.
Set passwords.
Reload grant tables.
Flush log files and caches.
Start and stop replication.
Display client information.
85
Invoking the mysqladmin Client
Execute from a command-line prompt:
shell> mysqladmin --login-path=<login-path> commands
shell> mysqladmin -u<name> -p<password> commands
Examples:
shell> mysqladmin --login-path=admin processlist shell> mysqladmin -uroot -
poracle status variables shell> mysqladmin --login-path=admin shutdown
86
Quiz
SQL statements can be issued within the mysqladmin client for creation and
manipulation of data by using statements such as CREATE DATABASE, SELECT,
and ALTER TABLE.
a. True
b. False
87
MySQL Tools
MySQL
MySQL
Workbench
Workbench
MySQL
MySQL Enterprise
Proxy Monitor
MySQL
MySQL
Server
Server
MySQL MySQL
Cluster Enterprise
Manager Backup
88
MySQL Enterprise Monitor
Web-based monitoring and advising system
Virtual DBA assistant
Runs completely within a corporate firewall
Principle features:
Enterprise Dashboard
— “At-a-glance” monitoring
Server/group management
MySQL and custom advisors and rules
Advisor rule scheduler
Customizable thresholds and alerts
Events and alert history
Replication Monitor
89
Query Analyzer
MySQL Enterprise Monitor: Dashboard
Refresh Setting/
Page Tabs Help
Support
Status
Server
Navigation
Heat Chart
Graphs
Critical
Events
90
MySQL Enterprise Monitor: Access
Commercial product
“Trial” version available
Prerecorded demos
http://www.mysql.com/products/enterprise/monitor.html
91
MySQL Workbench
GUI tool for managing the MySQL server
Three separate functional modules:
SQL Data
Development Modeling
Server
Administration
92
MySQL Workbench: GUI Window
93
MySQL Workbench: Access
Two versions of Workbench
Community Edition (OSS)
Standard Edition (SE)
OS platforms
Windows
Linux
Mac OS X
Installation
Requirements in MySQL Workbench Reference Manual
Download webpage
Quick-Start Tutorial
94
MySQL Proxy
Program that manages communication between your client and MySQL
servers:
Monitors
Analyzes
Transforms
95
MySQL Connectors
Application programming interfaces (APIs)
Also known as “database drivers”
Compatible with industry standards ODBC and JDBC
Available for Linux and Windows Officially MySQL-supported connectors:
Connector/ODBC
Connector/J
Connector/NET
Connector/Python
Connector/C and Connector/C++
OpenOffice
Multiple PHP extensions and APIs are supported.
96
Third-Party APIs
For several programming languages
Third-party and community supported
Many are based on the C client library libmysql.
Some are native implementations of the client protocol.
Some languages:
– C++
Eiffel
Perl
Python
Ruby
TCL
97
Quiz
The _ GUI tool can be used to create a database model.
a. MySQL Proxy
b. MySQL Enterprise Monitor
c. MySQL Workbench
d. MySQL Connectors
98
Summary
In this lesson, you should have learned how to:
Describe the available clients for administrative tasks
Use MySQL administrative clients
Use the mysql command-line client
Use the mysqladmin command-line client for administrative tasks
Use the MySQL Workbench graphical client
Describe the available MySQL tools
List the available APIs (drivers and connectors)
99
Chapter 6:
Overview of Data Types
100
Objectives
After completing this lesson, you should be able to:
Describe the major categories of data types
Explain the meaning of NULL
Describe column attributes
Explain character set usage with data types
Choose an appropriate data type
101
Data Types: Overview
Four major categories:
Numeric Character
Binary Temporal
103
Numeric Data Types
Factors to consider with numeric data types:
Range of values that the data type represents
Amount of space that column values require
Column precision and scale (floating-point and fixed-point)
104
Numeric Data Types
Class Type Description
105
Character String Data Types
A character string data type:
Represents a sequence of alphanumeric characters of a given character set
Stores text or binary data
Is implemented in nearly every programming language
Supports character sets and collation
Belongs to one of the following classes
Text: True, unstructured character string data types
Integer: Structured string types
106
Character String Data Types
Class Type Description
107
Character Set and Collation Support
Character set: A named set of symbols and encodings
A character string belongs to a specific character set.
Collation: A named collating sequence
Defines character sort order
Governs how individual characters and character strings can be compared to each
other
Column definitions for string data types can specify a character set or
collation of the column.
Attributes apply to CHAR, VARCHAR, TEXT, ENUM, and SET data types, as in the
following example:
108
Binary String Data Types
Sequence of bytes
Binary digits (bits) grouped in eights
Store binary values such as:
Compiled computer programs and applications
Image and sound files
Classes of character binary data types:
Binary: Binary strings of both fixed and variable length
BLOB: Variable-length unstructured collection of binary data
109
Binary String Data Types
Class Type Description
Binary BINARY Similar to the CHAR (fixed-length) type, but stores binary byte
strings instead of nonbinary character strings
110
Temporal Data Types
Store date, time, and year values (or a combination of these values)
111
Spatial Data Types
MySQL supports spatial data type extensions to enable the generation,
storage, and analysis of geographic features.
You can use spatial extensions with the InnoDB, MyISAM, NDB, and ARCHIVE
storage engine tables.
MyISAM supports both spatial and non-spatial indexes.
Other storage engines support non-spatial indexes.
112
Setting Data Types to NULL
Can allow missing values
Can be an empty query result
Can be assessed during database design
Cannot be used when a column must have a known value
Type Description
113
Creating Tables with Column Attributes
This determines how MySQL handles columns:
114
Column Attributes
Categories of column attributes:
Numeric: Apply to numeric data types (other than BIT)
String: Apply to the nonbinary string data types
General: Apply to any data type
115
Column Attributes
Data Type Attribute Description
Numeric UNSIGNED Causes negative values to be not
permitted
Integer- AUTO_INCREMENT Generates sequences of successive unique
only integer values
String CHARACTER SET Specifies the character set to use
116
Choosing Data Types
Consider which data types and character sets minimize storage and disk I/O.
Use fixed-length data types:
If all stored string values have the same length
Use variable-length data types:
If stored string values vary
For multibyte character sets
Use a multibyte character set that uses less space for frequently used
characters.
Use additional Unicode character sets outside the Basic Multilingual Plane
(BMP).
117
Summary
In this lesson, you should have learned how to:
Describe the major categories of data types
Explain the meaning of NULL
Describe column attributes
Explain character set usage with data types
Choose an appropriate data type
118
Practice 6-1 Overview: Quiz – MySQL
Data Types
In this quiz, you answer questions about MySQL data types
119
Practice 6-2 Overview: Setting a Data
Type
In this practice, you set and view one data type for a table column.
120
Chapter 7:
Obtaining Metadata
121
Objectives
After completing this lesson, you should be able to:
List the available metadata access methods
Recognize the structure of the INFORMATION_SCHEMA
database (schema)
Use the available commands to view metadata
Describe the differences between SHOW statements and the
INFORMATION_SCHEMA tables
Use the mysqlshow client program
Use the INFORMATION_SCHEMA tables to create shell commands and SQL
statements
122
Metadata Access Methods
View data that describes the structure of the database.
Query the INFORMATION_SCHEMA database tables.
They contain data about all objects managed by the MySQL database server.
Use SHOW statements.
MySQL-proprietary statements for obtaining database and table information
Use the DESCRIBE (or DESC) statement.
A shortcut to inspect table structure and column properties
Use the mysqlshow client.
A command-line interface to the SHOW syntax
123
INFORMATION_SCHEMA Database
Serves as a central repository for database metadata
Schema and schema objects
Server statistics (status variables, settings, connections)
Is kept in a table format allowing flexible access
Using arbitrary SELECT statements
Is a “virtual database”
Tables are not “real” tables (base tables) but are “system views.”
Tables are filled dynamically according to the privileges of the current user.
124
INFORMATION_SCHEMA Tables
List all tables in the INFORMATION_SCHEMA database
125
INFORMATION_SCHEMA Table Columns
List INFORMATION_SCHEMA database table columns:
126
Using SELECT with INFORMATION_SCHEMA
127
INFORMATION_SCHEMA Examples
128
Creating Shell Commands with
INFORMATION_SCHEMA Tables
Use the INFORMATION_SCHEMA tables to obtain information about creating shell
commands.
Use SELECT with CONCAT to create mysqldump scripts:
mysql> SELECT CONCAT("mysqldump -uroot -p ",
-> TABLE_SCHEMA," ", TABLE_NAME, " >> ",
-> TABLE_SCHEMA,".bak.sql")
-> FROM TABLES WHERE TABLE_NAME LIKE 'Country%';
– Results in the following shell commands:
shell> mysqldump -uroot -p world_innodb Country
>> world_innodb.bak.sql
shell> mysqldump -uroot -p world_innodb CountryLanguage >>
world_innodb.bak.sql
129
Place the result in an output file by adding:
...INTO OUTFILE '/Country_Dump.sh';
Creating Shell Commands with
INFORMATION_SCHEMA Tables
Use the INFORMATION_SCHEMA tables to obtain information about creating
shell commands.
132
SHOW Statement Examples
Some commonly used SHOW statements:
133
Additional SHOW Statement Examples
SHOW with LIKE and WHERE: mysql> SHOW DATABASES LIKE 'm%'; mysql>
SHOW COLUMNS FROM Country
WHERE `Default` IS NULL;
134
DESCRIBE Statement
Equivalent to SHOW COLUMNS
General syntax:
mysql> DESCRIBE <table_name>;
Shows INFORMATION_SCHEMA table information
135
mysqlshow Client
Information about structure of databases and tables
Similar to SHOW statements
General syntax:
shell> mysqlshow [options] [db_name [table_name [column_name]]]
Options can be standard connection parameters.
136
mysqlshow Examples
Show information for all databases, or for a specific database, table, and/or
column:
137
Summary
In this lesson, you should have learned how to:
List the available metadata access methods
Recognize the structure of the INFORMATION_SCHEMA
database (schema)
Use the available commands to view metadata
Describe the differences between SHOW statements and the
INFORMATION_SCHEMA tables
Use the mysqlshow client program
Use the INFORMATION_SCHEMA tables to create shell commands and SQL
statements
138
Practice 7-1 Overview: Obtaining Metadata by Using
INFORMATION_SCHEMA
139
Practice 7-2 Overview:
Obtaining Metadata by Using SHOW and DESCRIBE
In this practice, you use the SHOW and DESCRIBE statements to obtain
database metadata.
140
Practice 7-3 Overview: Obtaining Metadata
by Using mysqlshow
In this practice, you use the mysqlshow client to obtain database metadata.
141
Chapter 8:
Transaction & Locking
142
Objectives
143
Transactions
A collection of data manipulation execution steps that are treated as a single
unit of work
Use to group multiple statements
Use when multiple clients are accessing data from the same table concurrently
All or none of the steps succeed.
Execute if all steps are good
Cancel if steps have error or are incomplete
ACID compliant
144
Transaction Diagram
145
ACID
Atomic
All statements execute successfully or are canceled as a unit.
Consistent
A database that is in a consistent state when a transaction begins is left in a
consistent state by the transaction.
Isolated
One transaction does not affect another.
Durable
All changes made by transactions that complete successfully are recorded properly
in the database.
Changes are not lost. 146
Transaction SQL Control Statements
147
SQL Control Statements Flow: Example
148
AUTOCOMMIT Mode
Determines how and when new transactions are started
AUTOCOMMIT mode is enabled by default:
Implicitly commits each statement as a transaction
Set AUTOCOMMIT mode to 0 in an option file, or:
SET GLOBAL AUTOCOMMIT=0;
SET SESSION AUTOCOMMIT=0;
SET @@AUTOCOMMIT :=0;
When AUTOCOMMIT is disabled, transactions span multiple statements by default.
You can end a transaction with COMMIT or ROLLBACK.
Check the AUTOCOMMIT setting with SELECT:
SELECT @@AUTOCOMMIT;
149
Implicit Commit
An implicit commit terminates the current transaction.
SQL statements that implicitly commit:
START TRANSACTION
SET AUTOCOMMIT = 1
151
Transaction Isolation Problems
152
Isolation Levels
Four isolation levels:
READ UNCOMMITTED
Allows a transaction to see uncommitted changes made by other transactions
READ COMMITTED
Allows a transaction to see committed changes made by other transactions
REPEATABLE READ
Ensures consistent SELECT output for each transaction
Default level for InnoDB
SERIALIZABLE
Completely isolates the effects of a transaction from others
153
Isolation Level Problems
Isolation Non-Repeatable
Dirty Read Phantom Read
Level Read
Read
Uncommitted Possible Possible Possible
Read
Committed Not possible Possible Possible
* Not possible for InnoDB, which uses snapshots for Repeatable Read
154
Setting the Isolation Level
Set the level at server startup.
Use the --transaction-isolation option with the mysqld command.
Or set transaction-isolation in the configuration file:
[mysqld]
transaction-isolation = <isolation_level>
155
Global Isolation Level
Requires the SUPER privilege:
156
Transaction Example: Isolation
Session 1 Session 2
mysql> PROMPT s1>
s1> SET GLOBAL TRANSACTION
-> ISOLATION LEVEL READ COMMITTED;
s1> SELECT @@global.tx_isolation;
+-----------------------+
| @@global.tx_isolation |
+-----------------------+
| READ-COMMITTED |
+-----------------------+
157
Transaction Example: Isolation
Session 1 Session 2
s2> COMMIT;
158
Locking Concepts
MySQL uses a multithreaded architecture.
Problems arise with multiple client access to a table.
Client coordination is necessary.
Types of locks:
Shared lock
Exclusive lock
159
Explicit Row Locks
InnoDB supports two types of row locking:
LOCK IN SHARE MODE
Locks each row with a shared lock
SELECT * FROM Country WHERE Code='AUS' LOCK IN SHARE MODE\G
FOR UPDATE
Locks each row with an exclusive lock
SELECT counter_field INTO @@counter_field FROM child_codes FOR UPDATE;
UPDATE child_codes SET counter_field = @@counter_field + 1;
160
Deadlocks
Deadlocks occur when multiple transactions each require data that another
has already locked exclusively.
When a cyclical dependency occurs between two or more transactions
For example, T1 waits for a resource locked by T2, which waits for a resource locked by
T3, which waits for a resource locked by T1.
InnoDB detects and aborts (rollback) one of the transactions and allows the other
one to complete.
161
Transaction Example: Deadlock
Session 1 Session 2
s1> START TRANSACTION;
162
Implicit Locks
The MySQL server locks the table (or row) based on the commands issued and
the storage engines being used:
163
Summary
In this lesson, you should have learned how to:
Use transaction control statements to run multiple SQL statements
concurrently
Explain the ACID properties
Describe the transaction isolation levels
Use locking to protect transactions
164
Practice 8-1 Overview:
Quiz – Transactions and Locking
In this quiz, you answer questions about MySQL transactions and locking.
165
Practice 8-2 Overview:
Using Transaction Control Statements
166
Chapter 9:
User Management
167
Objectives
After completing this lesson, you should be able to:
Describe the user connection and query process
List requirements for user authentication
Create, modify, and drop user accounts
Configure authentication plugins
List requirements for user authorization
Describe the levels of access privileges for users
List the types of privileges
Grant, modify, and revoke user privileges
Use SHOW PROCESSLIST to display running threads
Disable server client access control
Set account resource limits
168
Importance of User Management
Managing users in MySQL gives you the ability to control what users can and
cannot do.
Create user accounts with different privileges that are appropriate to their
function.
Avoid using the root account.
Constrain compromised applications.
Protect against mistakes during routine maintenance.
Ensure data integrity by proper assignment of individual user privileges.
Permit authorized users to do their work.
Prevent unauthorized users from accessing data beyond their privileges.
169
User Account Verification
User Connection and Query Process
Connection Request
No Connection
Authentication
Denied
Yes
Query
No
Authorization Query Denied
Yes
Query
Execution Disconnect
170
View User Account Settings
Query the mysql database to view user identification info:
171
Native Authentication
During connection using the mysql_native_password plugin, MySQL uses the
following to authenticate an account:
Username
Password
Client host
When specifying host names, keep the proper perspective in mind:
Specify the server’s host name when connecting using a client.
Specify the client’s host name when adding a user to the server.
172
Creating a User Account
Provide a user and host for each user account.
For example, use the CREATE USER...IDENTIFIED BY statement to build an
account:
For a user named jim
To connect from localhost
Using the password Abc123
CREATE USER 'jim'@'localhost' IDENTIFIED BY 'Abc123';
Avoid possible security risks when creating accounts:
Do not create accounts without a password.
Do not create anonymous accounts.
Where possible, avoid wildcards when you specify account host names.
173
Host Name Patterns
Examples of permissible host name formats:
Host name: localhost
Qualified host name: 'hostname.example.com'
IP number: 192.168.9.78
IP address: 10.0.0.0/255.255.255.0
Pattern or wildcard: % or _
174
Confirming Passwords
Assign strong, unique passwords to all user accounts.
Avoid passwords that can be easily guessed.
Use the following SELECT statement to list any accounts without passwords:
SELECT Host, User FROM mysql.user WHERE Password = ‘’;
Expire passwords:
ALTER USER jim@localhost PASSWORD EXPIRE;
175
Manipulating User Accounts
Use the RENAME USER statement to rename a user account:
RENAME USER 'jim'@'localhost' TO 'james'@'localhost';
Changes the account name of an existing account
Changes either the username or host name parts of the account name, or both
176
Pluggable Authentication
MySQL supports a number of authentication mechanisms that are available
through pluggable authentication.
Plugins are built-in or available as external libraries.
Default server-side plugins are built-in, always available, and include:
mysql_native_password: This is the default mechanism, as described in the preceding
slides.
mysql_old_password: This plugin implements authentication as used before MySQL4.1.1.
sha256_password: This plugin enables SHA-256 hashing of passwords.
177
Client-Side Cleartext Authentication Plugin
The MySQL client library includes a built-in Cleartext Authentication plugin,
mysql_clear_password. The plugin is:
Used to send a plain text password to the server
The password is usually hashed.
Enabled by:
The LIBMYSQL_ENABLE_CLEARTEXT_PLUGIN environment variable
Specifying --enable-cleartext-plugin when running MySQL client applications such
as mysql and mysqladmin
The MYSQL_ENABLE_CLEARTEXT_PLUGIN option of the
mysql_options() C API function
178
Loadable Authentication Plugins
test_plugin_server: Implements native and old password authentication
This plugin uses the auth_test_plugin.so file.
auth_socket: Allows only MySQL users who are logged in via a UNIX socket
from a UNIX account with the same name
This plugin uses the auth_socket.so file.
authentication_pam: Allows you to log in using an external authentication
mechanism
This plugin uses the authentication_pam.so file.
To load one of these plugins, start the server with the plugin- load option set
to the plugin’s file name.
179
PAM Authentication Plugin
The PAM Authentication plugin is an Enterprise Edition plugin that
authenticates MySQL accounts against the operating system.
PAM defines services that configure authentication.
These are stored in /etc/pam.d.
The plugin authenticates against:
Operating system users and groups
External authentication such as LDAP
To create MySQL users that authenticate with PAM, do the following:
CREATE USER user@host
IDENTIFIED WITH authentication_pam AS 'pam_service, os_group=mysql_user'
180
Authorization
After a user authenticates, MySQL asks the following questions to verify
account privileges:
Who is the user?
What privileges does the user have?
Where do these privileges apply?
You must set up proper accounts and privileges for authorization to work.
181
Determining Appropriate User Privileges
Grant privileges at the correct level of access:
Global
Database
Table
Column
Stored routine
Grant privileges to users according to their access requirements:
Read-only users:
Global-, database-, or table-level privileges such as SELECT
Users who modify databases:
Global-, database-, or table-level privileges such as INSERT, UPDATE, DELETE, CREATE,
ALTER, and DROP
Administrative users:
Global-level privileges such as FILE, PROCESS, SHUTDOWN, and SUPER
182
Granting Administrative Privileges
The following global privileges apply to administrative users:
FILE: Allows users to instruct the MySQL server to read and write files in the server
host file system
PROCESS: Allows users to use the SHOW PROCESSLIST
statement to see all statements that clients are executing
SUPER: Allows users to kill other client connections or change the runtime
configuration of the server
ALL: Grants all privileges except the ability to grant privileges to other users
Grant administrative privileges sparingly, because they can be abused by
malicious or careless users.
183
GRANT Statement
The GRANT statement creates a new account or modifies an existing account.
GRANT syntax:
GRANT SELECT ON world_innodb.* TO 'kari'@'localhost' IDENTIFIED BY 'Abc123';
Clauses of the statement:
Privileges to be granted
Privilege level:
Global: *.*
Database: <db_name>.*
Table: <db_name>.<table_name>
Stored routine: <db_name>.<routine_name>
Account to which you are granting the privilege
An optional password
184
Display GRANT Privileges
Show general account privileges with the SHOW GRANTS
statement:
SHOW GRANTS;
SHOW GRANTS FOR CURRENT_USER();
Specify an account name:
mysql> SHOW GRANTS FOR
'kari'@'myhost.example.com';
185
User Privilege Restrictions
You cannot explicitly deny access to a specific user.
You cannot associate a password with a specific object such as a database,
table, or routine.
186
Grant Tables
MySQL server reads the grant tables from the mysql database into memory at
startup, and bases all access control decisions on those tables.
Tables correspond to privilege levels:
Privilege Level/
Contents and Privileges
Table
db Database-specific privileges
tables_priv Table-specific privileges
columns_priv Column-specific privileges
procs_priv Stored procedures and functions privileges
The tables indicate that the validity and privileges of each account and each
level are progressively more secure.
187
Use of Grant Tables
Each grant table has host and user columns to identify the accounts to which
its records apply.
During connection attempts, the server determines whether a client can connect.
After connection, the server determines access privileges for each statement.
The MySQL installation process creates the grant tables.
Grant tables use the MyISAM storage engine.
MyISAM is guaranteed to be available.
188
Effecting Privilege Changes
MySQL maintains in-memory copies of grant tables to avoid the overhead of
accessing on-disk tables.
Avoid modifying a user account directly in the grant tables.
If you modify the grant tables directly, reload the tables explicitly by issuing a
FLUSH PRIVILEGES statement.
Account modification statements such as GRANT, REVOKE, SET PASSWORD,
and RENAME USER apply changes to both the grant tables and the in-memory
table copies.
Changes to global privileges and passwords apply only to subsequent
connections of that account.
Changes to database-level privileges apply after the client’s next USE
db_name statement.
Changes to table and routine privileges apply immediately.
189
Revoking Account Privileges
Use the REVOKE statement to revoke specific SQL statement privileges:
REVOKE DELETE, INSERT, UPDATE ON world_innodb.* FROM 'Amon'@'localhost';
Revoke the privilege to grant privileges to other users:
REVOKE GRANT OPTION ON world_innodb.* FROM 'Jan'@'localhost';
Revoke all privileges, including granting privileges to others:
REVOKE ALL PRIVILEGES, GRANT OPTION
FROM 'Sasha'@'localhost';
Use the SHOW GRANTS statement before issuing REVOKE to determine which
privileges to revoke, and then again afterward to confirm the result.
190
SHOW PROCESSLIST
SHOW PROCESSLIST shows you which process threads are running.
SHOW PROCESSLIST produces the following columns:
Id: Connection identifier
User: MySQL user who issued the statement
Host: Host name of the client issuing the statement
db: Default database selected; otherwise NULL
Command: Type of command that the thread is executing
Time: Time (in seconds) that the thread has been in its current state
State: Action, event, or state indicating what the thread is doing
Info: Statement that the thread is executing; otherwise NULL
The PROCESS privilege permits you to see all threads.
191
Disabling Client Access Control
To tell the server not to read the grant tables and disable access control, use
the --skip-grant-tables option.
Every connection succeeds:
You can provide any username and any password, and you can connect from any host.
The option disables the privilege system entirely.
Connected users effectively have all privileges.
Prevent clients from connecting:
Use the --skip-networking option to prevent network access and allow access only on
local socket, named pipe, or shared memory.
Use the --socket option to start the server on a non- standard socket to prevent casual
access by local applications or users.
192
Setting Account Resource Limits
Limit the use of server resources by setting the global
MAX_USER_CONNECTIONS variable to a non-zero value.
This limits the number of simultaneous connections by any one account, but does
not limit what a client can do when connected.
Limit the following server resources for individual accounts:
MAX_QUERIES_PER_HOUR: The number of queries that an account can issue per
hour
MAX_UPDATES_PER_HOUR: The number of updates that an account can issue per
hour
MAX_CONNECTIONS_PER_HOUR: The number of times an account can connect to
the server per hour
MAX_USER_CONNECTIONS: The number of simultaneous connections allowed
193
Summary
In this lesson, you should have learned how to:
Describe the user connection and query process
List requirements for user authentication
Create, modify, and drop user accounts
Configure authentication plugins
List requirements for user authorization
Describe the levels of access privileges for users
List the types of privileges
Grant, modify, and revoke user privileges
Use SHOW PROCESSLIST to display running threads
Disable server client access control
Set account resource limits
194
Practice 11-1 Overview: Quiz – MySQL User Management
195
Practice 11-2 Overview: Creating, Verifying, and Dropping a User
196
Practice 11-3 Overview:
Setting Up a User for the world_innodb Database
In this practice, you create, drop, and verify a new user specifically for the
world_innodb database.
197
Practice 11-4 Overview:
Using the PAM Authentication Plugin
In this practice, you enable the PAM authentication plugin and log in to
MySQL using operating system credentials.
198
Practice 11-5 Overview: Additional Practice
In this practice, you use the information covered in this lesson to implement
user accounts and privileges.
199
Chapter 10:
Security
200
Objectives
After completing this lesson, you should be able to:
Recognize common security risks
Describe security risks that are specific to the MySQL installation
List security problems and counter-measures for networks, operating
systems, file systems, and users
Protect your data
Use SSL for secure MySQL server connections
Explain how SSH enables a secure remote connection to the MySQL server
Find additional information about common security issues
201
Security Risks
MySQL server security is at risk when multiple users access it concurrently,
particularly when such users connect over the Internet.
It is not only the MySQL server that is at risk. The entire server host can be
compromised
There are many types of security attacks:
Eavesdropping
Altering
Playback
Denial of service
MySQL uses security based on ACLs for all connections, queries, and other
operations.
ACLs also support SSL-encrypted connections between MySQL clients and
servers.
202
MySQL Installation Security Risks
The most common installation security risks:
Network
MySQL server permits clients to connect over the network and make requests.
Users can see client account information if privileges are not restricted.
Any accounts without passwords are vulnerable to attack.
Operating system
Extra user accounts on a server can be used to undermine the MySQL installation.
File system
Directories, database files, and log files on the server can be opened by users who
should not have access.
203
Network Security
Risk-prevention tasks:
Invest in a firewall.
Ensure access only to authorized clients.
Limit network interfaces used by the server.
Use a secure installation script:
mysql_secure_installation
Adhere to general privilege precautions.
Do not transmit plain (unencrypted) data over the Internet
204
Password Security
Risk-prevention tasks:
Secure initial MySQL accounts with strong passwords.
Do not store any plaintext passwords in your database.
The mysql database stores passwords in the user table.
It is better to store these passwords using one-way hashes.
Do not choose passwords from dictionaries.
205
Operating System Security
Related to the complexity of the configuration:
Minimize the number of non-MySQL-related tasks on the server host.
Additional usage introduces potential risk.
Login accounts are not necessary for MySQL-only machines.
There is a performance benefit to a system that is fully devoted to MySQL.
206
File System Security
MySQL installation (directories and files) risk-prevention tasks:
Change ownership and access permissions before starting the server.
Set multi-user system ownership to an account with administrative privileges.
Set MySQL-related directories and files and user and group table ownership to
mysql, including:
MySQL programs
Database directories and files
Log, status, and configuration files
Do not set passwords before protecting files. This can permit an unauthorized
user to replace the files.
Set up a dedicated account for MySQL administration.
207
Protect Your Data
There are many ways that users can corrupt data. You must take measures to
protect it from attacks such as SQL injection.
Do not trust any data entered by users of your applications.
Users can exploit application code by using characters with special meanings, such
as quotes or escape sequences.
Make sure that your application remains secure if a user enters something like
DROP DATABASE mysql;.
Protect numeric and string data values.
Otherwise, users can gain access to secure data and submit queries that can
destroy data or cause excessive server load.
Protect even your publicly available data.
Attacks can waste server resources.
Safeguard web forms, URL names, special characters, etc.
208
Using Secure Connections
MySQL uses unencrypted client-to-server connections by default.
Keeps standard configuration of MySQL as fast as possible
Unencrypted connections are not acceptable for moving data securely over a
network.
Network traffic is susceptible to monitoring and attack.
Data could be changed in transit between client and server.
Encryption algorithms can be used to resist most threats.
They make any kind of data unreadable.
This resists many kinds of attacks.
Some applications require the security that is provided by encrypted
connections, wherein the extra computation is warranted.
209
SSL Protocol
MySQL supports SSL (secure sockets layer) connections between MySQL clients
and the server.
SSL is a connection protocol that:
Uses different encryption algorithms to secure data over a public network
Detects any data change, loss, or replay
Incorporates algorithms that provide identity verification using the X509 standard
Applications requiring extra security (encryption) should use SSL.
Secure connections are based on the OpenSSL API.
210
Using SSL with the MySQL Server
SSL usage requirements:
The system must support yaSSL (which comes bundled with MySQL) or OpenSSL.
The MySQL version being used must be built with SSL support.
To get secure connections to work with MySQL and SSL, you must first do the
following:
Load OpenSSL (if you are not using a precompiled MySQL).
Configure MySQL with SSL support.
Make sure that the user table in the mysql database includes the SSL-related
columns (ssl_* and x509_*).
Check whether a server binary is compiled with SSL support, using the --ssl option.
Start the server with options permitting clients to use SSL.
211
Starting the MySQL Server with SSL
Enable the mysqld server clients to connect using SSL, with the following
options:
--ssl-ca: Identifies the Certificate Authority (CA) certificate to be used (required
for encryption)
--ssl-key: Identifies the server public key
--ssl-cert: Identifies the server private key
shell> mysqld --ssl-ca=ca-cert.pem \
--ssl-cert=server-cert.pem \
--ssl-key=server-key.pem
212
Requiring SSL-Encrypted Connections
Use the REQUIRE SSL option of the GRANT statement to permit only SSL-
encrypted connections for an account:
GRANT ALL PRIVILEGES ON test.*
TO 'root'@'localhost'
IDENTIFIED BY 'goodsecret' REQUIRE SSL;
Start the mysql client with the --ssl-ca option to acquire an encrypted
connection.
(Optional) You can also specify the --ssl-key and
--ssl-cert options for an X509 connection:
shell> mysql --ssl-ca=ca-cert.pem \
--ssl-cert=server-cert.pem \
--ssl-key=server-key.pem
213
Checking SSL Status
Check whether a running mysqld server supports SSL, using the value of the
have_ssl system variable:
mysql> SHOW VARIABLES LIKE 'have_ssl’;
Check whether the current server connection uses SSL, using the value of the
ssl_cipher status variable:
mysql> SHOW STATUS LIKE 'ssl_cipher';
214
Advantages and Disadvantages of Using SSL
Advantages Disadvantages
215
Secure Remote Connection to MySQL
MySQL supports SSH (secure shell) connection to a remote MySQL server. This
requires:
An SSH client on the client machine
Port forwarding through an SSH tunnel from the client to the server
A client application on the machine with the SSH client
When you complete the setup, you have a local port that hosts an SSH
connection to MySQL, encrypted using SSH.
216
MySQL Security FAQ
To find answers to many common security questions, see the FAQ section on
security in the MySQL Reference Manual:
http://dev.mysql.com/doc/refman/5.6/en/faqs-security.html
217
Summary
In this lesson, you should have learned how to:
Recognize common security risks
Describe security risks that are specific to the MySQL installation
List security problems and counter-measures for networks, operating systems, file
systems, and users
Protect your data
Use SSL for secure MySQL server connections
Explain how SSH enables a secure remote connection to the MySQL server
Find additional information about common security issues
218
Practice 12-1 Overview: Quiz – MySQL
Security
In this quiz, you answer questions about MySQL server security.
219
Practice 12-2 Overview: Determining the Status
of SSL Connectivity
In this practice, you evaluate whether the current system has SSL enabled for
the server and the client.
220
Practice 12-3 Overview: Enabling
MySQL for SSL Connections
In this practice, you set the appropriate variables to permit the MySQL server
and client to use an SSL connection.
221
Chapter 11:
Exporting and Importing Data
222
Objectives
After completing this lesson, you should be able to use SQL statements to do
the following:
Export data.
Import data.
223
Exporting and Importing Data
Types of export/import operations available:
SELECT...INTO OUTFILE
LOAD DATA INFILE
Uses for exporting data:
Copying databases from one server to another:
From within the same host
To another host
Testing a new MySQL release with real data
Transferring data from one RDBMS to another
224
Exporting Data by Using SELECT with INTO OUTFILE
225
Using Data File Format Specifiers
SELECT with INTO OUTFILE outputs TSV by default.
“Tab-separated values” files delimit columns with tab characters, and delimit records with
newline characters.
You can choose alternative delimiters:
FIELDS
TERMINATED BY 'string’
ENCLOSED BY 'char’
ESCAPED BY 'char'
LINES TERMINATED BY 'string’
227
Importing Data by Using LOAD DATA INFILE
LOAD DATA INFILE:
Reads rows values from a file into a table
Is the reverse operation of SELECT...INTO OUTFILE
Assumes the file is located on the server host in the database data directory
228
Skipping or Transforming Input Data
To ignore lines in the data file, use IGNORE n LINES:
mysql> LOAD DATA INFILE /tmp/City.txt’
-> INTO TABLE City IGNORE 2 LINES;
Query OK, 2231 rows affected (0.01 sec)
There are 2233 rows in the file, but only 2231 are loaded.
To ignore or transform column values:
Specify user variables (instead of a column name) in the statement’s column list.
(Optional) Transform columns by using a SET clause:
LOAD DATA INFILE /tmp/City.txt’
INTO TABLE City ( @skip, @Name,
CountryCode, @District, Population)
SET name=CONCAT(@Name,' ',@District);
The statement ignores the values of variables that are not used in a SET expression.
229
Duplicate Records
To control how LOAD DATA INFILE handles rows that contain a duplicate
primary or unique key:
Use the IGNORE keyword to discard rows with duplicate keys.
Use the REPLACE keyword to replace rows with the version from the file with the
same key.
This is similar to how you can control duplicates with the INSERT and REPLACE
statements:
IGNORE
ON DUPLICATE KEY UPDATE
230
Loading a File from the Server Host
When loading a file that is located on the server host, LOAD DATA INFILE
handles rows that contain duplicate unique keys as follows:
By default, an input record that causes a duplicate-key violation results in an
error; the rest of the data file is not loaded. Records processed up to that
point are loaded into the table.
If you provide the IGNORE keyword after the file name, new records that
cause duplicate-key violations are ignored, and the statement does not raise
an error. LOAD DATA INFILE processes the entire file, loads all records not
containing duplicate keys, and discards the rest.
If you provide the REPLACE keyword after the file name, new records that
cause duplicate-key violations replace any records already in the table that
contain the duplicated key values. LOAD DATA INFILE processes the entire file
and loads all its records into the table.
231
Loading a File from the Client Host
When you load a file from the client host, LOAD DATA INFILE ignores records
that contain duplicate keys by default.
That is, the default behavior is the same as if you specify the IGNORE option.
The reason for this is that the client/server protocol does not allow
interrupting the transfer of the data file from client host to server after the
transfer has started, so there is no convenient way to abort the operation in
the middle.
232
Summary
In this lesson, you should have learned how to use SQL statements to do the
following:
Export data.
Import data.
233
Practice 14-1 Overview: Exporting MySQL Data
234
Practice 14-2 Overview: Importing Data
In this practice, you use the LOAD DATA INFILE statement to import data.
235
Chapter 12:
Backup and Recovery
236
Objectives
After completing this lesson, you should be able to:
Describe backup basics
List the types of backups
List MySQL backup tools and utilities
Make binary and text backups
Explain the role of log and status files in backups
Perform data recovery
237
Backup Basics
Most important reasons to have backups:
Complete system recovery
Auditing capabilities
Typical DBA tasks
Backup types:
Hot
Cold
Warm
238
Backup Basics
Disk
Binary logs
Logical/textual backups
239
Backups with MySQL
Logical (textual representation: SQL statements)
Physical (binary copy of data files)
Snapshot-based
Replication-based
Incremental (binary log flushed)
240
Quiz
Which backup type takes place while the data is being read and modified with
little to no interruption to your ability to interface or manipulate data?
a. Cold backup
b. Hot backup
c. Warm backup
241
Logical (Textual) Backups
Logical backups:
Convert databases and tables to SQL statements
Are portable
Require that the MySQL server be running during the backup
Can back up both local and remote MySQL servers
Are generally slower than raw (binary) backups
The size of a logical backup file may exceed the size of the database being
backed up.
242
Physical (Raw or Binary) Backups
Physical backups:
Make an exact copy of the database files
You can use standard commands such as tar, cp, cpio, rsync, or xcopy.
Must be restored to the same database engine
Can be restored across machine architectures
Are faster than logical backups and recoveries
244
Replication-Based Backups
MySQL replication can be used for backups:
The master is used for the production applications.
A slave is used for backup purposes.
This eliminates the impact on production applications.
The backup of the slave is either logical or raw.
Higher cost: There must be another server and storage to store replica of the
database.
Based on asynchronous replication:
The slave may be delayed with respect to the master.
This is acceptable if the binary log is not purged before the slave has read it.
245
Quiz
Which backup type converts databases and tables to SQL statements?
a. Logical backup
b. Physical backup
c. Replication-based backup
d. Snapshot-based backup
246
Binary Logging and Incremental Backups
Control binary logging at the session level:
SET SQL_LOG_BIN = {0|1|ON|OFF}
Flush binary logs when taking logical or raw backups.
Synchronize binary log to backups.
Logical and raw backups are full backups.
All rows of all tables are backed up.
To perform an incremental backup, copy binary logs.
Binary logs can be used for fine-granularity restores.
You can identify transactions that caused damage and skip them during restore.
247
Backup Tools: Overview
SQL statements for logical backups
SQL statements combined with operating system commands for raw backup
Other raw backup tools for MySQL:
MySQL Enterprise Backup
mysqldump: Logical backups
Third-party tools
248
MySQL Enterprise Backup
Hot backup
InnoDB storage engine
Warm backup
Non-InnoDB storage engines
Incremental backups:
Back up data that changed since the previous backup
Are primarily intended for InnoDB tables, or non-InnoDB tables that are read-only
or rarely updated
Single-file backups:
Provide the ability to create a backup in a single-file format
Can be streamed or piped to another process
249
MySQL Enterprise Backup
Raw files that are backed up with mysqlbackup
InnoDB data
ibdata* files: Shared tablespace files
.ibd files: Per-table data files
ib_logfile* files: Log files
250
mysqlbackup
mysqlbackup is an easy-to-use tool for all backup and restore operations.
Basic usage:
mysqlbackup -u<user> -p<password>
--backup_dir=<backup-dir>
backup-and-apply-log
backup: Performs the initial phase of a backup
backup-and-apply-log: Includes the initial phase of the backup and the
second phase, which brings the InnoDB tables in the backup up to date,
including any changes made to the data while the backup was running
251
Restoring a Backup with mysqlbackup
Basic usage:
mysqlbackup --backup-dir=<backup-dir>
copy-back
<backup-dir>: Specifies where the backed-up files are stored
copy-back: Tells mysqlbackup to perform a restore operation
252
mysqlbackup Single-File Backups
Basic usage:
mysqlbackup -u<user> -p<password>
--backup-image=<image-file>
--backup_dir=<backup-dir> backup-to-image
Other scenarios
Standard output:
... --backup-dir=<backup-dir> --backup-image=- backup-to-image >
<image-file>
Convert an existing backup directory to a single file:
... --backup-dir=<backup-dir>
--backup-image=<image-file> backup-dir-to-image
253
Restoring mysqlbackup Single-File Backups
Extract a selection of files:
mysqlbackup -u<user> -p<password>
--backup-image=<image-file>
--backup_dir=<backup-dir> image-to-backup-dir
Other scenarios
- List contents:
... --backup-image=<image-file> list-image
Convert an existing backup directory to a single file:
... --backup-image=<image_file>
--src-entry=<file-to-extract>
--dst-entry=<file-to-extract> extract
254
Quiz
Which raw files are backed up with the mysqlbackup command?
a. ibdata* files
b. .ibd files
c. ib_logfile* files
d. All of the above
255
mysqlhotcopy
Perl script:
Backs up MyISAM and ARCHIVE tables
Uses FLUSH TABLES, LOCK TABLES, and cp or scp to make a database backup
Runs on the same machine where the database directories are located
UNIX only
Basic usage:
mysqlhotcopy -u<user> -p<password> <db_name> <backup-dir>
Options:
--flush-log
--record_log_pos
256
Raw InnoDB Backups
Backup procedure:
Stop the server for the duration of the copy operation.
Verify that the server shut down without error.
Make a copy of each component:
A.frm file for each InnoDB table
Tablespace files
System tablespace
Per-table tablespaces
InnoDB log files
my.cnf file
Restart the server.
257
Raw MyISAM and ARCHIVE Backups
Backup procedure:
While server is running, lock the table to be copied :
mysql> USE mysql
mysql> FLUSH TABLES users WITH READ LOCK;
Perform a file system copy.
Start a new binary log file:
FLUSH LOGS;
Release the lock after the file system copy:
UNLOCK TABLES;
258
LVM Snapshots
Perform raw backups using LVM snapshots when:
The host supports LVM
For example, Linux supports LVM2.
The file system containing the MySQL data directory is on a logical volume
Backup procedure:
Take a snapshot of the logical volume containing MySQL’s data directory.
Use FLUSH TABLES WITH READ LOCK if you are backing up non-InnoDB tables.
Perform a raw backup from the snapshot.
Remove the snapshot.
259
Raw Binary Portability
Binary databases can be copied from one MySQL server to another.
InnoDB
All tablespace and log files for the database can be copied directly. The database
directory name must be the same on the source and destination systems.
MyISAM, ARCHIVE
All files for an individual table can be directly copied.
Windows compatibility
The MySQL server internally stores lowercase database and table names on
Windows systems.
For case-sensitive file systems, use an option file statement:
lower_case_table_names=1
260
mysqldump
Dumps table contents to files:
All databases, specific databases, or specific tables
Allows back up local or remote servers
Independent of the storage engine
Written in text format
Portable
Excellent copy/move strategy
Good for small exports but not for full backup solution
Basic usage:
mysqldump --user=<user> --password=<password>
--opt db_name > backup.file
261
Consistency with mysqldump
Ensuring consistency:
--master-data option alone
Locks tables during backup
Records the binlog position in the backup file
--master-data and --single-transaction options used together
Tables are not locked; only InnoDB table consistency is guaranteed.
--lock-all-tables
Satisfies consistency by locking tables
--flush-logs
Starts a new binary log
262
mysqldump Output Format Options
Drop options:
--add-drop-database
--add-drop-table
Create options:
--no-create-db
--no-create-info
--no-data
--no-tablespaces
MySQL programming components:
--routines
--triggers
Top options in one option (--opt)
263
Restoring mysqldump Backups
Reload mysqldump backups with mysql:
mysql --login-path=<login-path>
<database> < backup_file.sql
Name the database if the backup file does not.
Copy from one database to another:
mysqldump -u<user> -p<password>
<orig-db> <table> |
mysql --login-path=<login-path> <copy-db>
mysqlimport
If mysqldump is invoked with the --tab option, it produces tab-delimited data
files:
SQL file containing CREATE TABLE statements
Text file containing table data
264
Quiz
mysqldump is good for small exports but not for a full backup solution.
a. True
b. False
265
Backing Up Log and Status Files
Binary log files
Option files used by the server (my.cnf and my.ini files)
Replication files
master.info
relay-log.info
Replication slave data files
SQL_LOAD-*
MySQL binaries and libraries
Strategies:
Static files: Backed up with normal system tools with the server running
Dynamic files: Backed up with normal system tools with the server stopped
266
Replication as an Aid to Backup
The master can continue to run.
The slave can be stopped to make a backup:
Stop the server.
STOP SLAVE SQL_THREAD
Flush tables.
Back up the slave’s databases:
Stopped servers can use system tools.
The slave thread is stopped, but the server that is still running can use any MySQL
tools.
Start the server:
Start server if it is stopped.
START SLAVE SQL_THREAD
267
Comparing Backup Methods
Method Hot/Warm/ Storage Logical/ Consistent Availability
Cold Engines Physical
MySQL Enterprise Backup Hot (InnoDB)/ warm All Physical Yes Commercially
(other) available
268
Backup Strategy
269
Processing Binary Log Contents
Determine which logs were written after a backup was made.
Convert the contents with mysqlbinlog:
mysqlbinlog bin.000050 bin.000051 bin.000052 | mysql
270
Quiz
Which command converts the contents of binary logs?
a. binconvert
b. mysql
c. mysqlbinlog
d. mysqldump
271
Summary
In this lesson, you should have learned how to:
Describe backup basics
List the types of backups
List MySQL backup tools and utilities
Make binary and text backups
Explain the role of log and status files in backups
Perform data recovery
272
Chapter 13:
Replication
273
Objectives
After completing this lesson, you should be able to:
Describe MySQL replication
Manage the MySQL binary log
Explain MySQL replication threads and logs
Set up a MySQL replication environment
Explain the role of replication in high availability and scalability
Design advanced replication topologies
Perform a controlled switchover
Configure replication with MySQL Utilities
Monitor MySQL replication
Troubleshoot MySQL replication
274
MySQL Replication
Replication is a feature of MySQL that allows servers
to copy changes from one instance to another.
The master records all data and structural changes to
the binary log.
The slave requests the binary log from the master and
applies its contents locally.
275
Replication Masters and Slaves
The master/slave relationship is one-to-many:
Each slave reads logs from one master.
A master can ship logs to many slaves.
A slave can act as the master to another slave.
276
Complex Topologies
More complex topologies are possible:
A bi-directional topology has two master servers,
each a slave of the other.
A circular topology has any number of servers.
Each is both a master and a slave of another master.
Changes on any master replicate to all masters.
Not every slave must be a master.
Note: MySQL replication does not perform
conflict resolution.
277
Replication Use Cases
Common uses for replication:
Horizontal scale-out: Spread the querying workload across multiple slaves.
Business intelligence and analytics: Run expensive reports and analytics on
a slave, letting the master focus on production applications.
Geographic data distribution: Serve local users with a local application, and
replicate business intelligence data to corporate servers.
278
Replication for High Availability
Replication allows various high-availability use cases.
Controlled switchover: Use a replica to act in place of a production server during
hardware or system upgrades.
Server Redundancy: Perform a failover to a replica server in case of a system
failure.
Online schema changes: Perform a rolling upgrade in an environment with several
servers to avoid an overall system outage.
Software upgrades: Replicate across different versions of MySQL during an
environment upgrade.
Slaves must run a later version than the master.
Queries issued during the upgrade must be supported by all versions used during the
upgrade process.
279
Configuring Replication
Configure a unique server-id for each server.
Configure each master:
Enable the binary log, and enable TCP/IP networking.
Create a new user with the REPLICATION SLAVE privilege.
Back up the master databases, and record the log coordinates if required.
Configure each slave
Restore the backup from the master.
Issue a CHANGE MASTER TO statement on each slave with the:
Network location of the master
Replication account username and password
Log coordinates from which to start replicating if required
Start replication with START SLAVE.
280
CHANGE MASTER TO
Issue the CHANGE MASTER TO… statement on the slave to configure
replication master connection details:
281
Failover with Log Coordinates
To find the new master and the correct log
coordinates for each slave, you must examine
the binary logs closely.
Find the most recent event applied to each
slave.
Select an up-to-date slave as a new master.
Identify the log coordinates on the new master
to match the latest applied event on each
other slave.
Issue the correct CHANGE MASTER TO… on each
slave.
In circular topologies, finding the source of
events in each binary log becomes very
difficult.
282
Global Transaction Identifiers (GTIDs)
Global Transaction Identifiers (GTIDs) uniquely identify each transaction in a
replicated network.
Each GTID is of the form <source-uuid>:<transaction-id>.
0ed18583-47fd-11e2-92f3-0019b944b7f7:338
A GTID set contains a range of GTIDs:
0ed18583-47fd-11e2-92f3-0019b944b7f7:1-338
Enable GTID mode with the following options:
gtid-mode=ON: Logs a unique GTID along with each transaction
enforce-gtid-consistency: Disallows events that cannot be logged in a transactionally safe
way
log-slave-updates: Records replicated events to the slave’s binary log
283
Replication with GTIDs
Use CHANGE MASTER TO... to enable GTID replication:
Tell the slave that transactions are identified by GTIDs:
CHANGE MASTER TO MASTER_AUTO_POSITION=1;
You do not need to provide log coordinates such as:
MASTER_LOG_FILE
MASTER_LOG_POS
You cannot provide MASTER_AUTO_POSITION and log coordinates in the same
CHANGE MASTER TO... statement.
284
Failover with GTIDs
When you use GTIDs, failover in a circular topology is trivial.
On the slave of the failed master, bypass it by issuing a single CHANGE MASTER TO
statement.
Each server ignores or applies transactions replicated from other servers in the
topology, depending on whether the transaction’s GTID has already been seen or
not.
Failover in a non-circular topology is similarly easy.
Temporarily configure the new master as a slave of an up-to- date slave until it has
caught up.
285
Replication Filtering Rules
Filters are server options that apply to a master or slave:
The master applies binlog-* filters when writing the binary log.
The slave applies replicate-* filters when reading the relay log.
Choose which events to replicate, based on:
Database:
replicate-do-db, binlog-do-db
replicate-ignore-db, binlog-ignore-db
Table:
replicate-do-table, replicate-wild-do-table
replicate-ignore-table, replicate-wild-ignore-table
286
MySQL Utilities
MySQL Utilities are command-line tools that provide a number of useful
features. They are:
Distributed with MySQL Workbench
Used for maintenance and administration of MySQL servers
Written in Python, and easily extensible by Python programmers using a supplied
library
Useful for configuring replication topologies and performing failover
287
MySQL Utilities for Replication
Several utilities are particularly useful for replication:
mysqldbcopy: Copies a database from a source server to a destination server
along with replication configuration
mysqldbcompare: Compares two databases to find differences and create a
script to synchronize them
mysqlrpladmin: Administers a replication topology
Failover to the best slave after a master failure
Switchover to promote a specified slave
Start, reset, or stop all slaves
mysqlfailover: Monitors a master continuously, and executes failover to the
best slave available
288
MySQL Utilities for Replication
mysqlrplcheck: Checks the prerequisites for replication between a master
and a slave, including:
Binary logging
Replication user with appropriate privileges
server_id conflicts
Various settings that can cause replication conflicts
mysqlreplicate: Starts replication between two servers, reporting warnings
for mismatches
mysqlrplshow: Displays a replication topology between a master and slave or
recursively for the whole topology
289
Asynchronous Replication
The slave requests the binary log and applies its contents.
The slave typically lags behind the master.
The master does not care when the slave applies the log.
The master continues operating without waiting for the slave
Synchronous
Asynchronous
290
Semisynchronous Replication
Semisynchronous replication:
Requires a plugin on the master and at least one slave
Blocks each master event until at least one slave receives it
Switches to asynchronous replication if a timeout occurs
Synchronous
The master blocks after committing Asynchronous
each transaction until at least
one slave commits it.
291
Quiz
To use Global Transaction Identifiers in replication, you must use a CHANGE
MASTER TO … statement to provide the binary log coordinates (file name and
position) of the most recent transaction on the master that the slave has not yet
applied.
a. True
b. False
292
Review of Binary Logging
The binary log:
Contains data and schema changes, and their time stamps
Statement-based or row-based logging
Is used for point-in-time recovery from backup, full recovery from backup, and
replication
Rotates when:
MySQL restarts
It reaches its maximum size as set by max_binlog_size
You issue a FLUSH LOGS statement
Can be inspected in various ways:
Metadata: SHOW BINARY LOGS, SHOW MASTER STATUS
Contents: mysqlbinlog
293
Replication Logs
Slave servers maintain information about replication events.
Relay log set:
Includes relay logs and relay log index file
Contains a copy of binary log events from the master
Slave status logs:
Contain information needed to perform replication
Master connection details and log coordinates
Relay log coordinates
Are stored in files or tables
master.info and relay-log.info files by default
slave_master_info and slave_relay_log_info tables in the mysql database
294
Crash-Safe Replication
Binary logging is crash-safe:
MySQL only logs complete events or transactions.
Use sync-binlog to improve safety.
By default, the value is 0, meaning the operating system writes to the file according to its
internal rules.
Set sync-binlog to 1 to force the operating system to write the file after every transaction, or set
it to any larger number to write after that number of transactions.
Store slave status logs in tables for crash-safe replication.
Options: master-info-repository and relay-log- info-repository
Possible values are FILE (the default) and TABLE.
TABLE is crash-safe.
295
Replication Threads
When a slave connects to a master:
The master creates a Binlog dump thread
Reads events from the binary log and sends them to the slave I/O thread
The slave creates at least two threads:
Slave I/O thread
Reads events from the master’s Binlog dump thread and writes them to the slave’s relay
log
Slave SQL thread
Applies relay log events on single-threaded slave
Distributes relay log events between worker threads on multithreaded slave
Slave worker threads
Apply relay log events on multithreaded slave
296
Controlling Slave Threads
Control slave threads:
START SLAVE;
STOP SLAVE;
Control threads individually:
START SLAVE IO_THREAD;
STOP SLAVE SQL_THREAD;
Start threads until a specified condition:
START SLAVE UNTIL SQL_AFTER_MTS_GAPS;
START SLAVE IO_THREAD
UNTIL SQL_AFTER_GTIDS =
0ed18583-47fd-11e2-92f3-0019b944b7f7:338;
297
Monitoring Replication
298
Replication Slave I/O Thread States
The most commonly seen I/O thread states are:
Connecting to master
Waiting for master to send event
Queueing master event to the relay log
Waiting to reconnect after a failed binlog dump request
299
Replication Slave I/O Thread States
Reconnecting after a failed binlog dump request
Waiting to reconnect after a failed master event read
Reconnecting after a failed master event read
Waiting for the slave SQL thread to free enough relay log space
300
Replication Slave SQL Thread States
The most commonly seen SQL thread states are:
Waiting for the next event in relay log
Reading event from the relay log
Making temp file
Slave has read all relay log; waiting for the slave I/O thread to update it
Waiting until MASTER_DELAY seconds after master executed event
The following state appears in worker threads:
Waiting for an event from Coordinator
301
Troubleshooting MySQL Replication
View the error log.
The error log can provide you with enough information to identify and correct
problems in replication.
Issue a SHOW MASTER STATUS statement on the master.
Logging is enabled if the position value is non-zero.
Verify that both the master and slave have a unique non- zero server ID value.
The master and the slave must have different server IDs.
Issue a SHOW SLAVE STATUS command on the slave.
Slave_IO_Running and Slave_SQL_Running display
Yes when the slave is functioning correctly.
Last_IO_Error and Last_SQL_Error show the most recent error messages from the IO
and SQL threads.
302
Troubleshooting MySQL Replication
Use mysqlrplcheck to ensure that servers meet the prerequisites for
replication.
Issue a SHOW PROCESSLIST command on the master and slave.
Review the state of the Binlog dump, I/O, and SQL threads.
For a slave that suddenly stops working, check the most recently replicated
statements.
The SQL thread stops if an operation fails due to a constraint problem or other
error.
The error log contains events that cause the SQL thread to stop.
Review known replication limitations.
http://dev.mysql.com/doc/refman/5.6/en/replication-features.html
Verify that the slave data has not been modified directly (outside of replication).
303
Summary
In this lesson, you should have learned how to:
Describe MySQL replication
Manage the MySQL binary log
Explain MySQL replication threads and logs
Set up a MySQL replication environment
Explain the role of replication in high availability and scalability
Design advanced replication topologies
Perform a controlled switchover
Configure replication with MySQL Utilities
Monitor MySQL replication
Troubleshoot MySQL replication
304
Practice 17-1 Overview: Quiz – Replication
In this quiz, you answer questions about replication
305
Practice 17-2 Overview: Configuring
Replication
In this practice, you:
Start four server instances of MySQL
Configure one server as a slave of another
Create some data on the master
See that the data replicates to the slave
306
Practice 17-3 Overview: Adding a New
Slave
In this practice, you
Provision a new server as a new slave of the existing slave
Change some data on the master
See that the data replicates to both slaves
307
Practice 17-4 Overview: Enabling GTID
and Configuring Circular Replication
In this practice, you:
Enable GTID on the three servers
Connect the master so that it becomes a slave of the second slave
Test the newly created circular topology by changing some data
308
Practice 17-5 Overview:
Using MySQL Utilities and Performing a Failover
309