MySQL for Database Administrators - ФОРСedu.fors.ru/toc/sun/D61762GC10_TOC.pdf · This is the...

20
MySQL for Database Administrators Student Guide - Volume I SQL-4502 Rev 2.2 D61762GC10 Edition 1.0 D62341

Transcript of MySQL for Database Administrators - ФОРСedu.fors.ru/toc/sun/D61762GC10_TOC.pdf · This is the...

Page 1: MySQL for Database Administrators - ФОРСedu.fors.ru/toc/sun/D61762GC10_TOC.pdf · This is the Student guide for the MySQL for Database Administrators training course. ... For

MySQL for Database Administrators

Student Guide - Volume I

SQL-4502 Rev 2.2

D61762GC10Edition 1.0D62341

Page 2: MySQL for Database Administrators - ФОРСedu.fors.ru/toc/sun/D61762GC10_TOC.pdf · This is the Student guide for the MySQL for Database Administrators training course. ... For

Copyright © 2009, 2010, Oracle and/or its affiliates. All rights reserved.

Disclaimer

This document contains proprietary information, is provided under a license agreement containing restrictions on use anddisclosure, and is protected by copyright and other intellectual property laws. You may copy and print this document solely foryour own use in an Oracle training course. The document may not be modified or altered in any way. Except as expresslypermitted in your license agreement or allowed by law, you may not use, share, download, upload, copy, print, display,perform, reproduce, publish, license, post, transmit, or distribute this document in whole or in part without the expressauthorization of Oracle.

The information contained in this document is subject to change without notice. If you find any problems in the document,please report them in writing to: Oracle University, 500 Oracle Parkway, Redwood Shores, California 94065 USA. Thisdocument is not warranted to be error-free.

Sun Microsystems, Inc. Disclaimer

This training manual may include references to materials, offerings, or products that were previously offered by SunMicrosystems, Inc. Certain materials, offerings, services, or products may no longer be offered or provided.Oracle and itsaffiliates cannot be held responsible for any such references should they appear in the text provided.

Restricted Rights Notice

If this documentation is delivered to the U.S. Government or anyone using the documentation on behalf of the U.S.Government, the following notice is applicable:

U.S. GOVERNMENT RIGHTSThe U.S. Government’s rights to use, modify, reproduce, release, perform, display, or disclose these training materials arerestricted by the terms of the applicable Oracle license agreement and/or the applicable U.S. Government contract.

Trademark Notice

Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respectiveowners.

AMD, Opteron, the AMD logo, and the AMD Opteron logo are trademarks or registered trademarks of Advanced MicroDevices. Intel and Intel Xeon are trademarks or registered trademarks of Intel Corporation. All SPARC trademarks are usedunder license and are trademarks or registered trademarks of SPARC International, Inc. UNIX is a registered trademarklicensed through X/Open Company, Ltd.

Page 3: MySQL for Database Administrators - ФОРСedu.fors.ru/toc/sun/D61762GC10_TOC.pdf · This is the Student guide for the MySQL for Database Administrators training course. ... For

MySQL for Database Administrators Preface

MySQL for Database Administrators Student Guide

This is the Student guide for the MySQL for Database Administrators training course. This guide was developed using the MySQL 5.0 and/or MySQL 5.1 Community Edition - Generally Available (GA) Releases.

iii

Page 4: MySQL for Database Administrators - ФОРСedu.fors.ru/toc/sun/D61762GC10_TOC.pdf · This is the Student guide for the MySQL for Database Administrators training course. ... For

MySQL for Database Administrators Preface

Conventions used in this training guide

The following typographical conventions are used throughout this training guide:

● Computer input and output is printed in this format: Computer input or output. This is also used for the names of executable programs and file locations.

● Keywords from the SQL language appear in this format: SQL KEYWORD. SQL keywords are not case sensitive and may be written in any letter case, but the training guide uses uppercase.

● Placeholders for user input inside appearing inside computer input appear in this format: <user input>

● For emphasis, the folowing style is used: Emphasis

● For extra emphasis, the following style is used: Extra Emphasis

When commands are shown that are meant to be executed from within a particular program, the prompt shown preceding the command indicates which command to use. For example, shell> indicates a command that you execute from your shell, and mysql> indicates a statement that you execute from the mysql client program:

shell> mysql -u root -h 127.0.0.1

mysql> SELECT * FROM world.City;

The “shell” is your command interpreter. On Linux, this is typically a program such as sh, csh, or bash. On Windows, the equivalent program is command.com or cmd.exe, typically run in a console window. When you enter a command or statement shown in an example, do not type the prompt shown in the example.

Database, table, and column names must often be substituted into statements. To indicate that such substitution is necessary, this manual uses db_name, tbl_name, and col_name. For example, you might see a statement like this:

mysql> SELECT col_name FROM db_name.tbl_name;

This means that if you were to enter a similar statement, you would supply your own database, table, and column names for the placeholdes db_name, tbl_name, and col_name., perhaps like this:

mysql> SELECT author_name FROM biblio_db.author_list;

In syntax descriptions, square brackets ([ and ]) indicate optional words or clauses. For example, in the following statement, IF EXISTS is optional:

DROP TABLE [IF EXISTS] tbl_name;

When a syntax element consists of a number of alternatives, the alternatives are separated by vertical bars (pipe, |). When one member from a set of choices may be chosen, the alternatives are listed within square brackets ([ and ]):

TRIM([[BOTH | LEADING | TRAILING] [remstr] FROM] str)

When one member from a set of choices must be chosen, the alternatives are listed within braces ({ and }):

{DESCRIBE | DESC} tbl_name [col_name | wild]

iv

Page 5: MySQL for Database Administrators - ФОРСedu.fors.ru/toc/sun/D61762GC10_TOC.pdf · This is the Student guide for the MySQL for Database Administrators training course. ... For

MySQL for Database Administrators Preface

An ellipsis (...) indicates the omission of a section of a statement, typically to provide a shorter version of more complex syntax. For example, INSERT ... SELECT is shorthand for the form of INSERT statement that is followed by a SELECT statement.

An ellipsis can also indicate that the preceding syntax element of a statement may be repeated. In the following example, multiple reset_option values may be given, with each of those after the first preceded by commas:

RESET reset_option[, reset_option] ...

Commands for setting shell variables are shown using Bourne shell syntax. For example, the sequence to set the CC environment variable and run the configure command looks like this in Bourne shell syntax:

shell> CC=gcc ./configure

If you are using csh or tcsh, you must issue commands somewhat differently:

shell> setenv CC gcc

and

shell> ./configure

v

Page 6: MySQL for Database Administrators - ФОРСedu.fors.ru/toc/sun/D61762GC10_TOC.pdf · This is the Student guide for the MySQL for Database Administrators training course. ... For

MySQL for Database Administrators Preface

Supporting images used in this Training Guide

The following is a summary of the standard images used in this manual to support the instruction:

IMAGE NAME DESCRIPTION

Preparation This image is used to describe the steps required to be completed prior to performing a hands-on exercise.

Written exam This image is used to identify that the student is going to be tested upon the material previously presented in the instructional material

Inline Lab Throughout the course the instructor will conduct labs in line with the instruction, which are designed to help you to understand the “nuts and bolts” (inner-workings) of the topic.

Further Practice Lab

This image is used to convey to the student that there is a final exercise to complete prior to the completion of the chapter.

Student notes This image identifies an area on a page designated for students to write notes associated with the class.

Slide number box Indicates the number of an existing slide that corresponds to the text.

vi

123

Page 7: MySQL for Database Administrators - ФОРСedu.fors.ru/toc/sun/D61762GC10_TOC.pdf · This is the Student guide for the MySQL for Database Administrators training course. ... For

MySQL for Database Administrators Preface

Acknowledgments

Sun Microsystems would like to thank the many individuals that played a part in bringing this training material to the numerous students who will benefit from the knowledge and effort that each of these contributors put into the training. Even though there were a large number of contributions from many Sun Microsystems' employees, the following list of contributors played a vital role in developing this material and ensuring that its contents were accurate, timely and most of all presented in a way that would benefit those that are utilizing it for the benefit of improving their skills with MySQL.

● Max Mether, Course Development Manager

● Sarah Sproehnle, Lead Instructor, Subject Matter Expert

● Steven Jones, Subject Matter Expert

● Glynn Durham, Subject Matter Expert

● Jeffrey Gorton, Instructional Designer and Editor

vii

Page 8: MySQL for Database Administrators - ФОРСedu.fors.ru/toc/sun/D61762GC10_TOC.pdf · This is the Student guide for the MySQL for Database Administrators training course. ... For
Page 9: MySQL for Database Administrators - ФОРСedu.fors.ru/toc/sun/D61762GC10_TOC.pdf · This is the Student guide for the MySQL for Database Administrators training course. ... For

MySQL for Database Administrators Preface

Course Objectives

This instructor led, hands-on class will teach you how to properly install MySQL, create and execute backup strategies, secure users privileges, set resource limitations, and access controls. In addition, this course will prepare students for the MySQL Database Administrators certification exam.

● Describe the MySQL Architecture

● Install MySQL

● Upgrade to MySQL 5.1

● Perform the MySQL start and shutdown operations

● Configure MySQL server options at runtime

● Activate and manage numerous server logs

● Perform common DBA tasks utilizing MySQL programs

● Use INFORMATION_SCHEMA to access metadata

● Evaluate data types and character sets

● Understand data locking in MySQL

● Understand the multiple storage engines available in MySQL

● Maintain and Manage MySQL tables

● Perform backup and restore operations

● Manage user maintenance and security

● Maintain integrity of a MySQL installation

● Understand the DBA's role concerning stored routines

● Utilize triggers for administration tasks

● Understand views

● Compare multiple environment options

● Verifying the security of the server

● Understand table partitioning

● Set up and schedule events for maintenance

● Describe introductory performance tuning techniques

● Describe introductory high availability techniques

xi

5

6

Page 10: MySQL for Database Administrators - ФОРСedu.fors.ru/toc/sun/D61762GC10_TOC.pdf · This is the Student guide for the MySQL for Database Administrators training course. ... For
Page 11: MySQL for Database Administrators - ФОРСedu.fors.ru/toc/sun/D61762GC10_TOC.pdf · This is the Student guide for the MySQL for Database Administrators training course. ... For

MySQL for Database Administrators Preface

Table of Contents1 Introduction............................................................................................................................... 1-0

1.1 Learning Objectives...........................................................................................................1-11.2 MySQL Overview..............................................................................................................1-2

1.2.1 Sun Acquisition.......................................................................................................... 1-21.2.2 MySQL Partners.........................................................................................................1-3

1.3 MySQL Products............................................................................................................... 1-41.3.1 MySQL Database Products........................................................................................ 1-41.3.2 MySQL GUI Tools.....................................................................................................1-41.3.3 Other MySQL Tools...................................................................................................1-51.3.4 MySQL Connectors....................................................................................................1-51.3.5 Solutions for Embedding MySQL............................................................................. 1-6

1.4 MySQL Services................................................................................................................ 1-71.4.1 MySQL Training........................................................................................................ 1-71.4.2 MySQL Certification..................................................................................................1-71.4.3 MySQL Consulting.................................................................................................... 1-71.4.4 MySQL Support......................................................................................................... 1-7

1.5 The MySQL Enterprise Subscription.................................................................................1-81.5.1 MySQL Enterprise Server..........................................................................................1-81.5.2 24x7 Production Support............................................................................................1-81.5.3 MySQL Enterprise Monitor....................................................................................... 1-91.5.4 Obtaining a MySQL Enterprise Subscription.......................................................... 1-10

1.6 Supported Operating Systems..........................................................................................1-111.7 MySQL Certification Program.........................................................................................1-121.8 Training Curriculum Paths...............................................................................................1-131.9 MySQL Website...............................................................................................................1-17

1.9.1 MySQL Community Web Page................................................................................1-181.9.2 MySQL Online Documentation............................................................................... 1-20

1.10 Installing MySQL.......................................................................................................... 1-231.11 Installing the 'world' database........................................................................................ 1-241.12 Chapter Summary.......................................................................................................... 1-25

2 MySQL Architecture................................................................................................................. 2-12.1 Learning Objectives...........................................................................................................2-22.2 Client/Server Architecture................................................................................................. 2-3

2.2.1 MySQL Server .......................................................................................................... 2-32.2.2 Client Programs .........................................................................................................2-32.2.3 Communication Protocols..........................................................................................2-42.2.4 MySQL Non-Client Utilities .....................................................................................2-5

2.3 MySQL Architecture Overview......................................................................................... 2-62.3.1 Core Shared Subsystems............................................................................................ 2-62.3.2 Query Parsing, Optimization and Execution..............................................................2-82.3.3 Query Cache...............................................................................................................2-82.3.4 Storage Engine Implementation.................................................................................2-8

2.4 How MySQL Uses Disk Space........................................................................................2-102.4.1 Data Directory..........................................................................................................2-10

2.5 How MySQL Uses Memory............................................................................................ 2-132.5.1 Memory Allocation Categories................................................................................ 2-13

xiii

Page 12: MySQL for Database Administrators - ФОРСedu.fors.ru/toc/sun/D61762GC10_TOC.pdf · This is the Student guide for the MySQL for Database Administrators training course. ... For

MySQL for Database Administrators Preface

2.5.2 Memory Data Structures.......................................................................................... 2-132.6 The MySQL Plug-In Interface......................................................................................... 2-162.7 Chapter Summary............................................................................................................ 2-17

3 MySQL Server...........................................................................................................................3-13.1 Learning Objectives...........................................................................................................3-23.2 Types of MySQL Distributions..........................................................................................3-3

3.2.1 MySQL Binary Distributions..................................................................................... 3-33.2.2 MySQL Source Distributions.....................................................................................3-43.2.3 Loading Time Zone Tables.........................................................................................3-5

3.3 Starting and Stopping MySQL on Windows..................................................................... 3-93.3.1 Server Installation on Windows................................................................................. 3-93.3.2 Running MySQL Server manually on MS Windows...............................................3-103.3.3 Running MySQL Server as a Windows Service...................................................... 3-11

3.4 Starting and Stopping MySQL on Linux......................................................................... 3-143.4.1 Server Installation on Linux.....................................................................................3-143.4.2 Choosing a Server Startup Method on Linux...........................................................3-163.4.3 Improve MySQL Installation Security.....................................................................3-17

3.5 Upgrading MySQL.......................................................................................................... 3-183.5.1 The mysql_upgrade Program.................................................................................. 3-18

3.6 Using Multiple Servers.................................................................................................... 3-203.6.1 Server Start-up Options............................................................................................3-21

3.7 Chapter Summary............................................................................................................ 3-234 Configuring the MySQL Server.................................................................................................4-1

4.1 Learning Objectives...........................................................................................................4-24.2 MySQL Configuration....................................................................................................... 4-3

4.2.1 Option Files................................................................................................................4-44.2.2 Option File Groups.....................................................................................................4-44.2.3 Option File Locations.................................................................................................4-54.2.4 Displaying Options from Option Files.......................................................................4-9

4.3 Dynamic Server Variables................................................................................................4-144.4 Server Status Variables.................................................................................................... 4-164.5 Server SQL Modes...........................................................................................................4-17

4.5.1 Setting the SQL Mode..............................................................................................4-174.5.2 SQL Mode Values.................................................................................................... 4-19

4.6 Log and Status Files.........................................................................................................4-204.6.1 Status Files............................................................................................................... 4-214.6.2 The Error Log...........................................................................................................4-214.6.3 The General Query Log........................................................................................... 4-224.6.4 The Slow Query Log................................................................................................4-234.6.5 The Binary Log........................................................................................................ 4-23

4.7 Binary Logging................................................................................................................ 4-244.7.1 Statement Based Binary Logging.............................................................................4-264.7.2 Row Based Binary Logging..................................................................................... 4-264.7.3 Row Based vs. Statement Based Binary Logging....................................................4-27

4.8 Chapter Summary............................................................................................................ 4-315 MySQL Clients..........................................................................................................................5-1

5.1 Learning Objectives...........................................................................................................5-25.2 Overview of Administrative Clients.................................................................................. 5-3

xiv

Page 13: MySQL for Database Administrators - ФОРСedu.fors.ru/toc/sun/D61762GC10_TOC.pdf · This is the Student guide for the MySQL for Database Administrators training course. ... For

MySQL for Database Administrators Preface

5.3 Invoking MySQL Client Programs.................................................................................... 5-45.3.1 General Command Option Syntax ............................................................................ 5-45.3.2 Connection Parameter Options ................................................................................. 5-5

5.4 Using the mysql client..................................................................................................... 5-105.4.1 Using mysql Interactively ....................................................................................... 5-105.4.2 Statement Terminators .............................................................................................5-115.4.3 The mysql Prompts ................................................................................................. 5-135.4.4 Using Editing Keys in mysql .................................................................................. 5-135.4.5 Using script files with mysql....................................................................................5-145.4.6 MySQL Output Formats ..........................................................................................5-155.4.7 Client Commands and SQL Statements .................................................................. 5-165.4.8 Using Server-Side Help ...........................................................................................5-175.4.9 Using the Safe Updates Option ............................................................................... 5-18

5.5 The mysqladmin Client....................................................................................................5-225.6 MySQL Connectors......................................................................................................... 5-23

5.6.1 Native (C).................................................................................................................5-235.6.2 MySQL Connector/ODBC ......................................................................................5-235.6.3 MySQL Connector/J ............................................................................................... 5-245.6.4 MySQL Connector/NET ......................................................................................... 5-245.6.5 PHP.......................................................................................................................... 5-24

5.7 Third-Party APIs.............................................................................................................. 5-255.8 Chapter Summary............................................................................................................ 5-26

6 Data Types................................................................................................................................. 6-16.1 Learning Objectives...........................................................................................................6-26.2 Data Type Overview.......................................................................................................... 6-3

6.2.1 The ABC’s of Data Types..........................................................................................6-36.2.2 Creating Tables with Data Types................................................................................6-3

6.3 Numeric Data Types.......................................................................................................... 6-46.3.1 Integer Types.............................................................................................................. 6-46.3.2 Floating-Point Types................................................................................................. 6-56.3.3 Fixed-Point Types...................................................................................................... 6-76.3.4 BIT Types................................................................................................................... 6-8

6.4 Character String Data Types........................................................................................... 6-156.4.1 Unstructured Character String Data Types..............................................................6-166.4.2 Text Type Comparison............................................................................................ 6-186.4.3 Structured Character String types............................................................................ 6-196.4.4 Character Set and Collation Support........................................................................6-20

6.5 Binary String Data Types................................................................................................6-286.5.1 Binary Types........................................................................................................... 6-286.5.2 Binary String Type Comparison...............................................................................6-29

6.6 Temporal Data Types....................................................................................................... 6-306.6.1 Temporal Data Types................................................................................................6-30

6.7 NULLs............................................................................................................................. 6-366.7.1 The Meaning of NULL............................................................................................ 6-366.7.2 When to Use NULL................................................................................................ 6-366.7.3 When NOT to Use NULL........................................................................................ 6-37

6.8 Column Attributes............................................................................................................6-386.8.1 Using the AUTO_INCREMENT Column Attribute................................................ 6-40

xv

Page 14: MySQL for Database Administrators - ФОРСedu.fors.ru/toc/sun/D61762GC10_TOC.pdf · This is the Student guide for the MySQL for Database Administrators training course. ... For

MySQL for Database Administrators Preface

6.8.2 Choosing Data Types for Character Columns..........................................................6-446.9 Chapter Summary............................................................................................................ 6-48

7 Obtaining Metadata................................................................................................................... 7-17.1 Learning Objectives...........................................................................................................7-27.2 Metadata Access Methods................................................................................................. 7-37.3 The INFORMATION_SCHEMA database........................................................................7-4

7.3.1 INFORMATION_SCHEMA Tables.......................................................................... 7-57.3.2 Displaying INFORMATION_SCHEMA Tables........................................................7-6

7.4 Using SHOW and DESCRIBE ....................................................................................... 7-137.4.1 SHOW Statements................................................................................................... 7-137.4.2 DESCRIBE Statements............................................................................................7-18

7.5 The mysqlshow Client Program.......................................................................................7-227.6 Administration Tasks with INFORMATION_SCHEMA................................................ 7-277.7 Chapter Summary............................................................................................................ 7-29

8 Storage Engines......................................................................................................................... 8-18.1 Learning Objectives...........................................................................................................8-28.2 SQL Parser and Storage Engine Tiers................................................................................8-3

8.2.1 Storage Engine Breakdown .......................................................................................8-38.3 Storage Engines and MySQL............................................................................................ 8-5

8.3.1 Available Storage Engines..........................................................................................8-58.3.2 The Most Common Storage Engines......................................................................... 8-58.3.3 View Available Storage Engines................................................................................ 8-68.3.4 Setting the Storage Engine......................................................................................... 8-68.3.5 Displaying Storage Engine Information.....................................................................8-7

8.4 The MyISAM Storage Engine......................................................................................... 8-108.4.1 MyISAM Row Storage Formats.............................................................................. 8-108.4.2 Compressing MyISAM Tables.................................................................................8-118.4.3 MyISAM Locking....................................................................................................8-138.4.4 MyISAM Pros and Cons.......................................................................................... 8-14

8.5 The InnoDB Storage Engine............................................................................................8-178.5.1 The InnoDB Tablespace and Logs........................................................................... 8-178.5.2 The InnoDB Plugin.................................................................................................. 8-198.5.3 The InnoDB Log files and Buffers...........................................................................8-268.5.4 InnoDB Locking.......................................................................................................8-278.5.5 InnoDB Pros and Cons.............................................................................................8-30

8.6 The MEMORY Storage Engine....................................................................................... 8-318.6.1 MEMORY Indexing Options................................................................................... 8-318.6.2 MEMORY Best Practices.........................................................................................8-328.6.3 MEMORY Pros and Cons........................................................................................ 8-32

8.7 Storage Engine Summary................................................................................................ 8-348.8 Other Storage Engines..................................................................................................... 8-35

8.8.1 The EXAMPLE Storage Engine.............................................................................. 8-358.8.2 The ARCHIVE Storage Engine............................................................................... 8-358.8.3 The CSV Storage Engine......................................................................................... 8-368.8.4 The BLACKHOLE Storage Engine.........................................................................8-378.8.5 The NDB Storage Engine (MySQL Cluster)........................................................... 8-398.8.6 Falcon ......................................................................................................................8-40

8.9 Choosing Appropriate Storage Engines...........................................................................8-41

xvi

Page 15: MySQL for Database Administrators - ФОРСedu.fors.ru/toc/sun/D61762GC10_TOC.pdf · This is the Student guide for the MySQL for Database Administrators training course. ... For

MySQL for Database Administrators Preface

8.10 Using Multiple Storage Engines.................................................................................... 8-428.11 Storage engine comparison chart................................................................................... 8-438.12 Chapter Summary.......................................................................................................... 8-46

9 Partitioning................................................................................................................................ 9-19.1 Learning Objectives...........................................................................................................9-29.2 Overview of Partitioning................................................................................................... 9-3

9.2.1 MySQL Specific Partitioning .................................................................................... 9-49.3 Reasons for Using Partitioning.......................................................................................... 9-59.4 Creating a Partitioned Table...............................................................................................9-7

9.4.1 Basic Partition Syntax................................................................................................ 9-79.4.2 RANGE Partitioning.................................................................................................. 9-79.4.3 LIST Partitioning....................................................................................................... 9-89.4.4 HASH Partitioning..................................................................................................... 9-99.4.5 KEY Partitioning......................................................................................................9-109.4.6 Subpartitioning.........................................................................................................9-10

9.5 Obtaining Partition Information.......................................................................................9-129.5.1 SHOW CREATE TABLE........................................................................................ 9-129.5.2 SHOW TABLE STATUS......................................................................................... 9-129.5.3 INFORMATION_SCHEMA Metadata....................................................................9-139.5.4 EXPLAIN PARTITIONS.........................................................................................9-14

9.6 Modifying Partitions........................................................................................................ 9-159.6.1 Altering a Partition................................................................................................... 9-159.6.2 Using the COALESCE PARTITION statement.......................................................9-179.6.3 Redefine Partitioning Type.......................................................................................9-17

9.7 Removing Partitions........................................................................................................ 9-189.7.1 Dropping Partitions.................................................................................................. 9-189.7.2 Removing all partitions............................................................................................ 9-19

9.8 Partition Modification Performance Effects.................................................................... 9-209.9 Partition Pruning.............................................................................................................. 9-219.10 Storage Engine Partitioning Information.......................................................................9-22

9.10.1 Partitioning with MyISAM.................................................................................... 9-239.10.2 Partitioning with InnoDB.......................................................................................9-24

9.11 Partitioning and Locking ...............................................................................................9-249.12 Partitioning Limitations................................................................................................. 9-259.13 Chapter Summary.......................................................................................................... 9-28

10 Transactions And Locking..................................................................................................... 10-110.1 Learning Objectives.......................................................................................................10-210.2 What is a Transaction?................................................................................................... 10-3

10.2.1 ACID...................................................................................................................... 10-410.3 Transaction Control Statements..................................................................................... 10-5

10.3.1 The autocommit mode............................................................................................10-510.3.2 Statements causing an Implicit COMMIT............................................................. 10-710.3.3 Finding a Storage Engine that supports transactions............................................. 10-7

10.4 Isolation Levels............................................................................................................10-1310.4.1 Consistency issues................................................................................................10-1310.4.2 Four Levels...........................................................................................................10-14

10.5 Locking........................................................................................................................ 10-1910.5.1 Locking Concepts.................................................................................................10-19

xvii

Page 16: MySQL for Database Administrators - ФОРСedu.fors.ru/toc/sun/D61762GC10_TOC.pdf · This is the Student guide for the MySQL for Database Administrators training course. ... For

MySQL for Database Administrators Preface

10.5.2 Locking Reads......................................................................................................10-1910.5.3 Implicit Locks...................................................................................................... 10-23

10.6 Chapter Summary........................................................................................................ 10-2611 Security and User Management............................................................................................. 11-1

11.1 Learning Objectives....................................................................................................... 11-211.2 Security Risks.................................................................................................................11-311.3 Security Measures.......................................................................................................... 11-4

11.3.1 Network Security....................................................................................................11-411.3.2 Operating System Security..................................................................................... 11-511.3.3 Filesystem Security................................................................................................ 11-511.3.4 MySQL Server User Security.................................................................................11-6

11.4 Privileges........................................................................................................................11-711.4.1 Types of Privileges MySQL Supports.................................................................... 11-711.4.2 The Grant Tables.................................................................................................... 11-911.4.3 Modifying Grant Tables........................................................................................11-1111.4.4 The GRANT Statement........................................................................................ 11-1111.4.5 The SHOW GRANTS Statement......................................................................... 11-12

11.5 User Accounts.............................................................................................................. 11-1311.5.1 Creating and Dropping User Accounts.................................................................11-1411.5.2 Specifying Account Names.................................................................................. 11-1511.5.3 Setting Account Password.................................................................................... 11-16

11.6 User Account Maintenance.......................................................................................... 11-1911.6.1 Revoking Privileges............................................................................................. 11-1911.6.2 When Privilege Changes Take Effect................................................................... 11-2111.6.3 Specifying Resource Limits................................................................................. 11-2111.6.4 Precautions for Granting Privileges..................................................................... 11-25

11.7 Disabling Client Access Control.................................................................................. 11-2611.8 Using Secure Connections........................................................................................... 11-27

11.8.1 Using SSL.............................................................................................................11-2711.9 Chapter Summary.........................................................................................................11-33

12 Table Maintenance.................................................................................................................12-112.1 Learning Objectives.......................................................................................................12-212.2 Table Maintenance......................................................................................................... 12-3

12.2.1 SQL Statements for Table Maintenance Operations ............................................. 12-312.2.2 The CHECK TABLE statement............................................................................. 12-412.2.3 The CHECKSUM TABLE statement.....................................................................12-412.2.4 The REPAIR TABLES statement...........................................................................12-512.2.5 The ANALYZE TABLE statement........................................................................ 12-512.2.6 The OPTIMIZE TABLE statement........................................................................ 12-6

12.3 Client and Utility Programs for Table Maintenance.................................................... 12-1012.3.1 The mysqlcheck Client Program.......................................................................... 12-1012.3.2 The myisamchk Utility.........................................................................................12-1112.3.3 Options for mysqlchk and myisamchk.................................................................12-12

12.4 Table Maintenance per Storage Engine....................................................................... 12-1612.4.1 InnoDB Table Maintenance................................................................................. 12-1712.4.2 MEMORY Table Maintenance.............................................................................12-1712.4.3 MyISAM Table Maintenance...............................................................................12-1812.4.4 CSV Table Maintenance.......................................................................................12-19

xviii

Page 17: MySQL for Database Administrators - ФОРСedu.fors.ru/toc/sun/D61762GC10_TOC.pdf · This is the Student guide for the MySQL for Database Administrators training course. ... For

MySQL for Database Administrators Preface

12.4.5 Archive Table Maintenance..................................................................................12-1912.5 Chapter Summary........................................................................................................ 12-22

13 Exporting and Importing Data...............................................................................................13-113.1 Learning Objectives.......................................................................................................13-213.2 Exporting and Importing Data....................................................................................... 13-3

13.2.1 Export Data Using SELECT with INTO OUTFILE..............................................13-313.2.2 Importing Data Using LOAD DATA INFILE........................................................13-9

13.3 Import Data with SQL Scripts..................................................................................... 13-1313.4 Chapter Summary........................................................................................................ 13-15

14 Programming with MySQL................................................................................................... 14-114.1 Learning Objectives.......................................................................................................14-214.2 What is a Stored Routine?..............................................................................................14-314.3 Execute Stored Routines................................................................................................14-514.4 Examine Stored Routines...............................................................................................14-614.5 Stored Routines and Execution Security....................................................................... 14-714.6 What are Triggers?.........................................................................................................14-814.7 Creating Triggers........................................................................................................... 14-9

14.7.1 Triggers Events.......................................................................................................14-914.7.2 Trigger Error Handling.........................................................................................14-12

14.8 Delete Triggers.............................................................................................................14-1314.9 Restrictions on Triggers............................................................................................... 14-1314.10 Trigger Privileges.......................................................................................................14-1314.11 Events.........................................................................................................................14-18

14.11.1 Schedule............................................................................................................. 14-2014.11.2 Event Scheduler and MySQL Privileges............................................................14-21

14.12 DBA's Using MySQL's Programming Capabilities...................................................14-2714.12.1 Using stored routines and triggers for security purposes................................... 14-2714.12.2 Using Stored Routines to Enhance Performance............................................... 14-28

14.13 Chapter Summary...................................................................................................... 14-3315 Views..................................................................................................................................... 15-1

15.1 Learning Objectives.......................................................................................................15-215.2 What Are Views?........................................................................................................... 15-315.3 Creating Views...............................................................................................................15-5

15.3.1 The CREATE VIEW Statement............................................................................. 15-515.4 Updatable Views............................................................................................................ 15-8

15.4.1 Insertable Views................................................................................................... 15-1015.4.2 SQL SECURITY and DEFINER Characteristics................................................ 15-1315.4.3 WITH CHECK OPTION..................................................................................... 15-14

15.5 Managing Views.......................................................................................................... 15-1615.5.1 Checking Views................................................................................................... 15-1615.5.2 Altering Views......................................................................................................15-1715.5.3 Dropping Views................................................................................................... 15-17

15.6 Obtaining View Metadata............................................................................................ 15-1915.6.1 Using the information_schema............................................................................ 15-1915.6.2 SHOW Statements............................................................................................... 15-20

15.7 Chapter Summary........................................................................................................ 15-2316 Backup and Recovery............................................................................................................16-1

16.1 Learning Objectives.......................................................................................................16-2

xix

Page 18: MySQL for Database Administrators - ФОРСedu.fors.ru/toc/sun/D61762GC10_TOC.pdf · This is the Student guide for the MySQL for Database Administrators training course. ... For

MySQL for Database Administrators Preface

16.2 Backup Basics................................................................................................................16-316.2.1 Backup Types......................................................................................................... 16-416.2.2 Backup Strategy..................................................................................................... 16-5

16.3 Backups with MySQL................................................................................................... 16-716.3.1 Logical (Textual) Backups..................................................................................... 16-716.3.2 Raw (Binary) Backups........................................................................................... 16-816.3.3 Snapshot-Based Backup.........................................................................................16-816.3.4 Replication-Based Backup..................................................................................... 16-816.3.5 Binary Logging and Incremental Backup.............................................................. 16-9

16.4 Backup Tools Overview...............................................................................................16-1016.4.1 Tool Impact on Database and Application Availability........................................16-11

16.5 Making Raw Backups..................................................................................................16-1216.5.1 Making Raw MyISAM Backups..........................................................................16-1216.5.2 Making Raw InnoDB Backups............................................................................ 16-1316.5.3 Other Raw Backup Tools..................................................................................... 16-14

16.6 Making Logical (Text) Backups.................................................................................. 16-1716.6.1 Making Logical Backups with mysqldump......................................................... 16-1716.6.2 Backing Up Stored Routines................................................................................16-20

16.7 Backing Up Log and Status Files................................................................................ 16-2216.8 Replication as an Aid to Backup..................................................................................16-2316.9 Backup Methods Compared.........................................................................................16-2416.10 Data Recovery............................................................................................................16-25

16.10.1 Data Importing Methods Used for Recovery..................................................... 16-2516.11 Processing Binary Log Contests................................................................................ 16-2816.12 Chapter Summary...................................................................................................... 16-30

17 Introduction to Performance Tuning................................................................................................................... 17-1

17.1 Learning Objectives.......................................................................................................17-217.2 Using EXPLAIN to Analyze Queries............................................................................ 17-3

17.2.1 How EXPLAIN Works...........................................................................................17-317.2.2 Nested Joins........................................................................................................... 17-517.2.3 Analyzing a Nested Join.........................................................................................17-6

17.3 General Table Optimizations......................................................................................... 17-817.4 Setting and Interpreting MySQL Server Variables...................................................... 17-11

17.4.1 Setting Server Variables....................................................................................... 17-1117.4.2 Accessing Server Variable Information................................................................17-1217.4.3 Accessing Status Variable Information................................................................ 17-1417.4.4 Obtaining Metadata for Server Variables............................................................. 17-17

17.5 Chapter Summary........................................................................................................ 17-1918 Introduction to MySQL High Availability.........................................................................................................................18-1

18.1 Learning Objectives.......................................................................................................18-218.2 MySQL Replication....................................................................................................... 18-3

18.2.1 MySQL Replication Threads and Files.................................................................. 18-418.2.2 Setting up MySQL Replication.............................................................................. 18-518.2.3 Backing Up a Slave................................................................................................18-918.2.4 Monitoring MySQL Replication.......................................................................... 18-1018.2.5 Slave I/O Thread States........................................................................................18-11

xx

Page 19: MySQL for Database Administrators - ФОРСedu.fors.ru/toc/sun/D61762GC10_TOC.pdf · This is the Student guide for the MySQL for Database Administrators training course. ... For

MySQL for Database Administrators Preface

18.2.6 Slave SQL Thread States......................................................................................18-1218.2.7 Replication and the Binary Log........................................................................... 18-13

18.3 Chapter Summary........................................................................................................ 18-2719 Conclusion............................................................................................................................. 19-1

19.1 Learning Objectives.......................................................................................................19-219.2 Training and Certification Website............................................................................... 19-319.3 Course Evaluation..........................................................................................................19-519.4 Thank you!..................................................................................................................... 19-519.5 Q&A Session..................................................................................................................19-6

Appendix A MySQL Lab Preparation....................................................................................... A-1Appendix B Further Practice and Quiz Solutions......................................................................B-1Appendix C Explain Output Columns....................................................................................... C-1Appendix D MySQL Enterprise Monitor.................................................................................. D-1

xxi

Page 20: MySQL for Database Administrators - ФОРСedu.fors.ru/toc/sun/D61762GC10_TOC.pdf · This is the Student guide for the MySQL for Database Administrators training course. ... For