Vous êtes sur la page 1sur 16

MySQL for Database

Administrators

Student Guide - Volume I

D61762GC30
Edition 3.0
March 2013
D81028
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Author
Disclaimer
Jeremy Smyth
This document contains proprietary information and is protected by copyright and
Technical Contr ibutors other intellectual property laws. You may copy and print this document solely for your
own use in an Oracle training course. The document may not be modified or altered in
and Reviewer s any way. Except where your use constitutes "fair use" under copyright law, you may
not use, share, download, upload, copy, print, display, perform, reproduce, publish,
Mark Lewin
license, post, transmit, or distribute this document in whole or in part without the
Horst Hunger express authorization of Oracle.
Andrew Morgan The information contained in this document is subject to change without notice. If you
Todd Farmer find any problems in the document, please report them in writing to: Oracle University,
500 Oracle Parkway, Redwood Shores, California 94065 USA. This document is not
Jonathon Coombes warranted to be error-free.
Sneha Modi
Restricted Rights Notice
Bob Falasco
If this documentation is delivered to the United States Government or anyone using
Saikumar Vishvanatha the documentation on behalf of the United States Government, the following notice is
Kim Seong Loh applicable:

Mattias Jonsson U.S. GOVERNMENT RIGHTS


Chuck Bell The U.S. Government’s rights to use, modify, reproduce, release, perform, display, or
disclose these training materials are restricted by the terms of the applicable Oracle
Ingo Strüwing license agreement and/or the applicable U.S. Government contract.
Matt Lord
Trademark Notice
Bertrand Matthelié
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names
may be trademarks of their respective owners.
Editor s
Daniel Milne
Richard Wallis

Gr aphic Designer
Seema Bopaiah

Publisher s
Pavithran Adka
Srividya Rameshkumar
Contents

1 Introduction to MySQL
Course Goals 1-2
Course Lesson Map 1-5
Introductions 1-8
Classroom Environment 1-9
MySQL Is Everywhere 1-10
Why MySQL Makes Sense for Oracle 1-11
MySQL Is Powering the World 1-12
MySQL Database Server Editions 1-13
MySQL Enterprise Edition 1-14
MySQL Connectors and APIs 1-15
MySQL Services 1-16
Community Support 1-17
Oracle Premier Support for MySQL 1-18
MySQL-Supported Operating Systems 1-19
MySQL Websites 1-20
MySQL Curriculum Footprint 1-21
MySQL Certification 1-22
MySQL Online Documentation 1-23
Example Databases 1-24
Summary 1-25

2 Architecture
Objectives 2-2
MySQL Architecture 2-3
Client Programs 2-4
Administrative and Utility Programs 2-5
MySQL Server 2-6
Server Process 2-7
Connection Layer 2-8
Communication Protocols 2-9
SQL Layer 2-10
SQL Statement Processing 2-11
Storage Layer 2-12
Storage Engine: Overview 2-13

iii
Features Dependent on Storage Engine 2-14
How MySQL Uses Disk Space 2-15
How MySQL Uses Memory 2-17
Memory Structures 2-18
MySQL Plugin Interface 2-20
Summary 2-21
Practice 2-1 Overview: Quiz 2-22

3 System Administration
Objectives 3-2
MySQL Server Distributions 3-3
MySQL Binary Distributions 3-4
MySQL Source Distributions 3-5
MySQL RPM Installation Files for Linux 3-6
Linux MySQL RPM Installation Process 3-7
Linux MySQL Server Installation Directories 3-8
Starting the MySQL Server on Linux 3-9
Stopping the MySQL Server on Linux 3-10
Improving Installation Security 3-11
Windows MySQL Server Installation Directory 3-12
Running MySQL on Windows 3-13
Data Directory 3-14
MySQL Server Releases 3-15
Checking Upgraded Tables 3-16
Employing Multiple Servers 3-17
Multiple Server Options 3-18
Summary 3-19
Practice 3-1 Overview: Installing the MySQL Server 3-20
Practice 3-2 Overview: MySQL Data Directory 3-21
Practice 3-3 Overview: Starting and Stopping the MySQL Server 3-22

4 Server Configuration
Objectives 4-2
MySQL Configuration Options 4-3
Reasons to Use Option Files 4-4
Option File Groups 4-5
Writing an Option File 4-6
Option File Locations 4-7
Startup Options in an Option File 4-8
Sample Option Files 4-9
Displaying Options from Option Files 4-10

iv
Obscuring Authentication Options 4-11
Login Paths 4-12
Server System Variables 4-13
Dynamic System Variables 4-14
Displaying Dynamic System Variables 4-15
Structured System Variables 4-16
Server Status Variables 4-17
SQL Modes 4-18
Setting the SQL Mode 4-19
Common SQL Modes 4-20
Log Files 4-21
Log File Usage Matrix 4-22
Binary Logging 4-24
Binary Logging Formats 4-25
List Binary Log Files 4-26
View Binary Log Contents 4-27
Deleting Binary Logs 4-29
Configuring Enterprise Audit 4-30
Audit Log File 4-31
Summary 4-32
Practice 4-1 Overview: Quiz 4-33
Practice 4-2 Overview: Editing and Creating a Configuration File 4-34
Practice 4-3 Overview: Additional Practice – Server Configuration 4-35

5 Clients and Tools


Objectives 5-2
Command-Line Client Programs 5-3
Invoking Command-Line Clients 5-4
Connection Parameter Options 5-5
Quiz 5-6
Invoking mysql Client 5-7
mysql Client: Safe Updates 5-8
mysql Client: Output Formats 5-9
mysql Client: MySQL Client Commands 5-10
mysql Client: SQL Statements 5-11
mysql Client: Help on SQL Statements 5-12
mysql Client: SQL Statement Terminators 5-13
mysql Client: Special Statement Terminators 5-14
mysql Client: Redefining the Prompt 5-15
mysql Client: Using Script Files 5-16
mysqladmin Client 5-17

v
Invoking the mysqladmin Client 5-18
Quiz 5-20
MySQL Tools 5-21
MySQL Enterprise Monitor 5-22
MySQL Enterprise Monitor: Dashboard 5-23
MySQL Enterprise Monitor: Access 5-24
MySQL Workbench 5-25
MySQL Workbench: GUI Window 5-26
MySQL Workbench: Access 5-27
MySQL Proxy 5-28
MySQL Connectors 5-29
Third-Party APIs 5-30
Quiz 5-31
Summary 5-32
Practice 5-1 Overview: Invoking the mysql Client 5-33
Practice 5-2 Overview: Invoking the mysqladmin Client 5-34
Practice 5-3 Overview: Viewing the MySQL Enterprise Monitor Demos 5-35
Practice 5-4 Overview: Performing System Administration Tasks with MySQL
Workbench 5-36

6 Data Types
Objectives 6-2
Data Types: Overview 6-3
Creating Tables with Data Types 6-4
Numeric Data Types 6-5
Character String Data Types 6-7
Character Set and Collation Support 6-9
Binary String Data Types 6-10
Temporal Data Types 6-12
Spatial Data Types 6-13
Setting Data Types to NULL 6-14
Creating Tables with Column Attributes 6-15
Column Attributes 6-16
Choosing Data Types 6-18
Summary 6-19
Practice 6-1 Overview: Quiz – MySQL Data Types 6-20
Practice 6-2 Overview: Setting a Data Type 6-21

7 Obtaining Metadata
Objectives 7-2
Metadata Access Methods 7-3

vi
INFORMATION_SCHEMA Database 7-4
INFORMATION_SCHEMA Tables 7-5
INFORMATION_SCHEMA Table Columns 7-7
Using SELECT with INFORMATION_SCHEMA 7-8
INFORMATION_SCHEMA Examples 7-9
Creating Shell Commands with INFORMATION_SCHEMA Tables 7-10
Creating SQL Statements with INFORMATION_SCHEMA Tables 7-11
MySQL-Supported SHOW Statements 7-12
SHOW Statement Examples 7-13
Additional SHOW Statement Examples 7-14
DESCRIBE Statement 7-15
mysqlshow Client 7-16
mysqlshow Examples 7-17
Summary 7-18
Practice 7-1 Overview: Obtaining Metadata by Using
INFORMATION_SCHEMA 7-19
Practice 7-2 Overview: Obtaining Metadata by Using SHOW and DESCRIBE 7-20
Practice 7-3 Overview: Obtaining Metadata by Using mysqlshow 7-21

8 Transactions and Locking


Objectives 8-2
Transactions 8-3
Transaction Diagram 8-4
ACID 8-5
Transaction SQL Control Statements 8-6
SQL Control Statements Flow: Example 8-7
AUTOCOMMIT Mode 8-8
Implicit Commit 8-9
Transactional Storage Engines 8-10
Transaction Isolation Problems 8-11
Isolation Levels 8-12
Isolation Level Problems 8-13
Setting the Isolation Level 8-14
Global Isolation Level 8-15
Transaction Example: Isolation 8-16
Locking Concepts 8-18
Explicit Row Locks 8-19
Deadlocks 8-20
Transaction Example: Deadlock 8-22
Implicit Locks 8-23
Summary 8-24

vii
Practice 8-1 Overview: Quiz – Transactions and Locking 8-25
Practice 8-2 Overview: Using Transaction Control Statements 8-26
Practice 8-3 Overview: Additional Practice with Transactions and Locking 8-27

9 Storage Engines
Objectives 9-2
Storage Engines and MySQL 9-3
Available Storage Engines 9-4
InnoDB Storage Engine 9-5
InnoDB as Default Storage Engine 9-6
InnoDB Features 9-7
Displaying the Storage Engine Setting 9-8
Setting the Storage Engine 9-9
Converting Existing Tables to InnoDB 9-10
InnoDB System Tablespace 9-11
Data Tablespaces 9-12
Tablespace Directory Structure 9-13
Shared Tablespace Configuration 9-14
Log Files and Buffers: Diagram 9-15
Configuring Buffer Pools for Performance 9-17
NoSQL and the memcached API 9-18
Configuring memcached 9-19
Key/Value Data 9-20
Using NoSQL with MySQL Databases 9-21
Referential Integrity 9-22
Multi-Versioning 9-23
Locking 9-24
Next-Key Locking 9-25
Consistent Non-Locking Reads 9-26
Reduce Deadlocks 9-27
Foreign Key Locking 9-28
MyISAM Storage Engine 9-29
MEMORY Storage Engine 9-30
ARCHIVE Storage Engine 9-31
BLACKHOLE Storage Engine 9-32
Summary 9-33
Practice 9-1 Overview: Quiz – InnoDB Storage Engine 9-34
Practice 9-2 Overview: Setting and Confirming InnoDB Settings 9-35

viii
10 Partitioning
Objectives 10-2
Partitioning 10-3
Partitioning Analogy 10-4
MySQL-Specific Partitioning 10-5
Partitioning Types 10-6
Partitioning Support 10-7
Improving Performance with Partitioning 10-8
Improving Performance with Partitioning: Pruning 10-9
Performance Challenges with Partitioning 10-10
Basic Partition Syntax 10-11
RANGE Partitioning 10-12
LIST Partitioning 10-13
HASH Partitioning 10-14
KEY Partitioning 10-15
Subpartitioning 10-16
Obtaining Partition Information 10-17
Obtaining Partition Information: SHOW CREATE TABLE 10-18
Obtaining Partition Information: SHOW TABLE STATUS 10-19
Obtaining Partition Information: INFORMATION_SCHEMA 10-20
Obtaining Partition Information: EXPLAIN PARTITIONS 10-21
Altering a Partition 10-22
Redefining the Partitioning Type 10-23
Exchanging Partitions 10-24
Dropping Partitions 10-25
Issues with Using DROP PARTITION 10-26
Removing Partitioning 10-27
Performance Effects of Altering a Partition 10-28
Quiz 10-29
Partitioning: Storage Engine Features 10-30
Partitioning and Locking 10-31
Partitioning Limitations 10-32
Partition Expression Limitations 10-33
Summary 10-34
Practice 10-1 Overview: Quiz – MySQL Partitioning 10-35
Practice 10-2 Overview: Creating and Modifying a Partitioned Table 10-36
Practice 10-3 Overview: Removing Partitions from a Table 10-37

11 User Management
Objectives 11-2
Importance of User Management 11-3

ix
User Account Verification 11-4
View User Account Settings 11-5
Native Authentication 11-7
Creating a User Account 11-8
Host Name Patterns 11-9
Setting the Account Password 11-11
Confirming Passwords 11-12
Manipulating User Accounts 11-13
Pluggable Authentication 11-14
Client-Side Cleartext Authentication Plugin 11-15
Loadable Authentication Plugins 11-16
PAM Authentication Plugin 11-17
Authorization 11-19
Determining Appropriate User Privileges 11-20
Granting Administrative Privileges 11-21
GRANT Statement 11-22
Display GRANT Privileges 11-23
User Privilege Restrictions 11-24
Grant Tables 11-25
Use of Grant Tables 11-26
Effecting Privilege Changes 11-27
Revoking Account Privileges 11-28
SHOW PROCESSLIST 11-29
Disabling Client Access Control 11-30
Setting Account Resource Limits 11-31
Summary 11-32
Practice 11-1 Overview: Quiz – MySQL User Management 11-33
Practice 11-2 Overview: Creating, Verifying, and Dropping a User 11-34
Practice 11-3 Overview: Setting Up a User for the world_innodb Database 11-35
Practice 11-4 Overview: Using the PAM Authentication Plugin 11-36
Practice 11-5 Overview: Additional Practice 11-37

12 Security
Objectives 12-2
Security Risks 12-3
MySQL Installation Security Risks 12-4
Network Security 12-5
Password Security 12-6
Operating System Security 12-7
File System Security 12-8
Protect Your Data 12-9

x
Using Secure Connections 12-10
SSL Protocol 12-11
Using SSL with the MySQL Server 12-12
Starting the MySQL Server with SSL 12-13
Requiring SSL-Encrypted Connections 12-14
Checking SSL Status 12-15
Advantages and Disadvantages of Using SSL 12-16
Secure Remote Connection to MySQL 12-17
MySQL Security FAQ 12-18
Summary 12-19
Practice 12-1 Overview: Quiz – MySQL Security 12-20
Practice 12-2 Overview: Determining the Status of SSL Connectivity 12-21
Practice 12-3 Overview: Enabling MySQL for SSL Connections 12-22

13 Table Maintenance
Objectives 13-2
Implementation of Table Maintenance 13-3
SQL for Table Maintenance Operations 13-4
ANALYZE TABLE Statement 13-5
CHECK TABLE Statement 13-7
CHECKSUM TABLE Statement 13-9
OPTIMIZE TABLE Statement 13-11
REPAIR TABLE Statement 13-13
mysqlcheck Client Program 13-15
myisamchk Utility 13-17
Options for mysqlcheck and myisamchk 13-18
InnoDB Table Maintenance 13-20
MyISAM Table Maintenance 13-21
MEMORY Table Maintenance 13-23
ARCHIVE Table Maintenance 13-24
Summary 13-25
Practice 13-1 Overview: Quiz – Table Maintenance 13-26
Practice 13-2 Overview: Using Table Maintenance SQL Statements 13-27
Practice 13-3 Overview: Using Table Maintenance Utilities 13-28

14 Exporting and Importing Data


Objectives 14-2
Exporting and Importing Data 14-3
Exporting Data by Using SELECT with INTO OUTFILE 14-4
Using Data File Format Specifiers 14-5
Escape Sequences 14-6

xi
Importing Data by Using LOAD DATA INFILE 14-7
Skipping or Transforming Input Data 14-8
Duplicate Records 14-9
Summary 14-11
Practice 14-1 Overview: Exporting MySQL Data 14-12
Practice 14-2 Overview: Importing Data 14-13

15 Programming Inside MySQL


Objectives 15-2
Stored Routines 15-3
Uses of Stored Routines 15-4
Stored Routines: Issues 15-5
Executing Stored Routines 15-6
Stored Procedure: Example 15-7
Stored Function: Example 15-8
Examine Stored Routines 15-10
Stored Routines and Execution Security 15-12
Quiz 15-13
Triggers 15-14
Create Triggers 15-15
Trigger Events 15-16
Trigger Error Handling 15-17
Examining Triggers 15-18
Dropping Triggers 15-19
Restrictions on Triggers 15-20
Trigger Privileges 15-21
Quiz 15-22
Events 15-23
Schedule 15-25
Event Scheduler and Privileges 15-27
Event Execution Privileges 15-28
Examining Events 15-29
Dropping Events 15-30
Quiz 15-31
SIGNAL and RESIGNAL 15-32
Summary 15-33
Practice 15-1 Overview: Creating Stored Routines 15-34
Practice 15-2 Overview: Reviewing Stored Routines 15-35
Practice 15-3 Overview: Creating a Trigger 15-36
Practice 15-4 Overview: Creating and Testing an Event 15-37

xii
16 MySQL Backup and Recovery
Objectives 16-2
Backup Basics 16-3
Backups with MySQL 16-7
Quiz 16-8
Logical (Textual) Backups 16-9
Physical (Raw or Binary) Backups 16-11
Snapshot-Based Backups 16-12
Replication-Based Backups 16-13
Quiz 16-14
Binary Logging and Incremental Backups 16-15
Backup Tools: Overview 16-16
MySQL Enterprise Backup 16-18
mysqlbackup 16-20
Restoring a Backup with mysqlbackup 16-21
mysqlbackup Single-File Backups 16-22
Restoring mysqlbackup Single-File Backups 16-23
Quiz 16-24
mysqlhotcopy 16-25
Raw InnoDB Backups 16-26
Raw MyISAM and ARCHIVE Backups 16-27
LVM Snapshots 16-28
Raw Binary Portability 16-30
mysqldump 16-31
Consistency with mysqldump 16-32
mysqldump Output Format Options 16-33
Restoring mysqldump Backups 16-34
Quiz 16-36
Backing Up Log and Status Files 16-37
Replication as an Aid to Backup 16-38
Comparing Backup Methods 16-39
Backup Strategy 16-40
Processing Binary Log Contents 16-41
Quiz 16-43
Summary 16-44
Practice 16-1 Overview: Quiz – Introduction to Backups 16-45
Practice 16-2 Overview: MySQL Enterprise Backup 16-46
Practice 16-3 Overview: mysqldump 16-47
Practice 16-4 Overview: Backup and Recovery Using LVM Snapshots and the Binary
Log 16-48

xiii
17 Replication
Objectives 17-2
MySQL Replication 17-3
Replication Masters and Slaves 17-4
Complex Topologies 17-5
Replication Use Cases 17-7
Replication for High Availability 17-8
Configuring Replication 17-9
CHANGE MASTER TO 17-10
Failover with Log Coordinates 17-11
Global Transaction Identifiers (GTIDs) 17-12
Replication with GTIDs 17-14
Failover with GTIDs 17-15
Replication Filtering Rules 17-16
MySQL Utilities 17-17
MySQL Utilities for Replication 17-18
Asynchronous Replication 17-20
Semisynchronous Replication 17-21
Quiz 17-23
Review of Binary Logging 17-24
Replication Logs 17-25
Crash-Safe Replication 17-26
Replication Threads 17-27
Controlling Slave Threads 17-29
Monitoring Replication 17-30
Replication Slave I/O Thread States 17-32
Replication Slave SQL Thread States 17-34
Troubleshooting MySQL Replication 17-35
Summary 17-38
Practice 17-1 Overview: Quiz – Replication 17-39
Practice 17-2 Overview: Configuring Replication 17-40
Practice 17-3 Overview: Adding a New Slave 17-41
Practice 17-4 Overview: Enabling GTID and Configuring Circular Replication 17-42
Practice 17-5 Overview: Using MySQL Utilities and Performing a Failover 17-43

18 Introduction to Performance Tuning


Objectives 18-2
Factors That Affect Performance 18-3
Monitoring 18-4
Performance Schema 18-5
Instruments, Instances, Events, and Consumers 18-6

xiv
General Database Optimizations 18-8
PROCEDURE ANALYSE 18-10
EXPLAIN 18-11
EXPLAIN Formats 18-13
Examining Server Status 18-14
Top Status Variables 18-15
Tuning System Variables 18-17
Top Server System Variables 18-19
Preparing for Tuning 18-21
Tuning in Practice 18-22
Tuning Example: Sort Buffer Size 18-23
Summary 18-24
Practice 18-1 Overview: Quiz – Introduction to Performance Tuning 18-25
Practice 18-2 Overview: EXPLAIN 18-26
Practice 18-3 Overview: PROCEDURE ANALYSE 18-27

19 Conclusion
Course Goals 19-2
MySQL Curriculum Footprint 19-5
MySQL Resources 19-6
We Need Your Evaluation 19-7
Thank You 19-8
Q&A Session 19-9

xv