Sunteți pe pagina 1din 20

MySQL for Database Administrators

Student Guide - Volume I


SQL-4502 Rev 2.2

D61762GC10
Edition 1.0
D62341

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 and
disclosure, and is protected by copyright and 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 any way. Except as expressly
permitted 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 express
authorization 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. This
document 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 Sun
Microsystems, Inc. Certain materials, offerings, services, or products may no longer be offered or provided.Oracle and its
affiliates 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 RIGHTS
The U.S. Governments rights to use, modify, reproduce, release, perform, display, or disclose these training materials are
restricted 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 respective
owners.
AMD, Opteron, the AMD logo, and the AMD Opteron logo are trademarks or registered trademarks of Advanced Micro
Devices. Intel and Intel Xeon are trademarks or registered trademarks of Intel Corporation. All SPARC trademarks are used
under license and are trademarks or registered trademarks of SPARC International, Inc. UNIX is a registered trademark
licensed through X/Open Company, Ltd.

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

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

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

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

123

vi

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.

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

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

MySQL for Database Administrators

Preface

Table of Contents
1 Introduction............................................................................................................................... 1-0
1.1 Learning Objectives........................................................................................................... 1-1
1.2 MySQL Overview..............................................................................................................1-2
1.2.1 Sun Acquisition.......................................................................................................... 1-2
1.2.2 MySQL Partners.........................................................................................................1-3
1.3 MySQL Products............................................................................................................... 1-4
1.3.1 MySQL Database Products........................................................................................ 1-4
1.3.2 MySQL GUI Tools..................................................................................................... 1-4
1.3.3 Other MySQL Tools................................................................................................... 1-5
1.3.4 MySQL Connectors....................................................................................................1-5
1.3.5 Solutions for Embedding MySQL............................................................................. 1-6
1.4 MySQL Services................................................................................................................ 1-7
1.4.1 MySQL Training........................................................................................................ 1-7
1.4.2 MySQL Certification..................................................................................................1-7
1.4.3 MySQL Consulting.................................................................................................... 1-7
1.4.4 MySQL Support......................................................................................................... 1-7
1.5 The MySQL Enterprise Subscription.................................................................................1-8
1.5.1 MySQL Enterprise Server.......................................................................................... 1-8
1.5.2 24x7 Production Support............................................................................................1-8
1.5.3 MySQL Enterprise Monitor....................................................................................... 1-9
1.5.4 Obtaining a MySQL Enterprise Subscription.......................................................... 1-10
1.6 Supported Operating Systems..........................................................................................1-11
1.7 MySQL Certification Program.........................................................................................1-12
1.8 Training Curriculum Paths...............................................................................................1-13
1.9 MySQL Website...............................................................................................................1-17
1.9.1 MySQL Community Web Page................................................................................1-18
1.9.2 MySQL Online Documentation............................................................................... 1-20
1.10 Installing MySQL.......................................................................................................... 1-23
1.11 Installing the 'world' database........................................................................................ 1-24
1.12 Chapter Summary.......................................................................................................... 1-25
2 MySQL Architecture................................................................................................................. 2-1
2.1 Learning Objectives........................................................................................................... 2-2
2.2 Client/Server Architecture................................................................................................. 2-3
2.2.1 MySQL Server .......................................................................................................... 2-3
2.2.2 Client Programs .........................................................................................................2-3
2.2.3 Communication Protocols..........................................................................................2-4
2.2.4 MySQL Non-Client Utilities ..................................................................................... 2-5
2.3 MySQL Architecture Overview......................................................................................... 2-6
2.3.1 Core Shared Subsystems............................................................................................ 2-6
2.3.2 Query Parsing, Optimization and Execution..............................................................2-8
2.3.3 Query Cache...............................................................................................................2-8
2.3.4 Storage Engine Implementation.................................................................................2-8
2.4 How MySQL Uses Disk Space........................................................................................2-10
2.4.1 Data Directory..........................................................................................................2-10
2.5 How MySQL Uses Memory............................................................................................ 2-13
2.5.1 Memory Allocation Categories................................................................................ 2-13
xiii

MySQL for Database Administrators

Preface

2.5.2 Memory Data Structures.......................................................................................... 2-13


2.6 The MySQL Plug-In Interface......................................................................................... 2-16
2.7 Chapter Summary............................................................................................................ 2-17
3 MySQL Server...........................................................................................................................3-1
3.1 Learning Objectives........................................................................................................... 3-2
3.2 Types of MySQL Distributions..........................................................................................3-3
3.2.1 MySQL Binary Distributions..................................................................................... 3-3
3.2.2 MySQL Source Distributions.....................................................................................3-4
3.2.3 Loading Time Zone Tables.........................................................................................3-5
3.3 Starting and Stopping MySQL on Windows..................................................................... 3-9
3.3.1 Server Installation on Windows................................................................................. 3-9
3.3.2 Running MySQL Server manually on MS Windows...............................................3-10
3.3.3 Running MySQL Server as a Windows Service...................................................... 3-11
3.4 Starting and Stopping MySQL on Linux......................................................................... 3-14
3.4.1 Server Installation on Linux.....................................................................................3-14
3.4.2 Choosing a Server Startup Method on Linux...........................................................3-16
3.4.3 Improve MySQL Installation Security..................................................................... 3-17
3.5 Upgrading MySQL.......................................................................................................... 3-18
3.5.1 The mysql_upgrade Program.................................................................................. 3-18
3.6 Using Multiple Servers.................................................................................................... 3-20
3.6.1 Server Start-up Options............................................................................................3-21
3.7 Chapter Summary............................................................................................................ 3-23
4 Configuring the MySQL Server.................................................................................................4-1
4.1 Learning Objectives........................................................................................................... 4-2
4.2 MySQL Configuration....................................................................................................... 4-3
4.2.1 Option Files................................................................................................................4-4
4.2.2 Option File Groups.....................................................................................................4-4
4.2.3 Option File Locations.................................................................................................4-5
4.2.4 Displaying Options from Option Files.......................................................................4-9
4.3 Dynamic Server Variables................................................................................................4-14
4.4 Server Status Variables.................................................................................................... 4-16
4.5 Server SQL Modes...........................................................................................................4-17
4.5.1 Setting the SQL Mode..............................................................................................4-17
4.5.2 SQL Mode Values.................................................................................................... 4-19
4.6 Log and Status Files.........................................................................................................4-20
4.6.1 Status Files............................................................................................................... 4-21
4.6.2 The Error Log...........................................................................................................4-21
4.6.3 The General Query Log........................................................................................... 4-22
4.6.4 The Slow Query Log................................................................................................ 4-23
4.6.5 The Binary Log........................................................................................................ 4-23
4.7 Binary Logging................................................................................................................ 4-24
4.7.1 Statement Based Binary Logging.............................................................................4-26
4.7.2 Row Based Binary Logging..................................................................................... 4-26
4.7.3 Row Based vs. Statement Based Binary Logging....................................................4-27
4.8 Chapter Summary............................................................................................................ 4-31
5 MySQL Clients.......................................................................................................................... 5-1
5.1 Learning Objectives........................................................................................................... 5-2
5.2 Overview of Administrative Clients.................................................................................. 5-3

xiv

MySQL for Database Administrators

Preface

5.3 Invoking MySQL Client Programs.................................................................................... 5-4


5.3.1 General Command Option Syntax ............................................................................ 5-4
5.3.2 Connection Parameter Options ................................................................................. 5-5
5.4 Using the mysql client..................................................................................................... 5-10
5.4.1 Using mysql Interactively ....................................................................................... 5-10
5.4.2 Statement Terminators .............................................................................................5-11
5.4.3 The mysql Prompts ................................................................................................. 5-13
5.4.4 Using Editing Keys in mysql .................................................................................. 5-13
5.4.5 Using script files with mysql....................................................................................5-14
5.4.6 MySQL Output Formats ..........................................................................................5-15
5.4.7 Client Commands and SQL Statements .................................................................. 5-16
5.4.8 Using Server-Side Help ...........................................................................................5-17
5.4.9 Using the Safe Updates Option ............................................................................... 5-18
5.5 The mysqladmin Client....................................................................................................5-22
5.6 MySQL Connectors......................................................................................................... 5-23
5.6.1 Native (C).................................................................................................................5-23
5.6.2 MySQL Connector/ODBC ...................................................................................... 5-23
5.6.3 MySQL Connector/J ............................................................................................... 5-24
5.6.4 MySQL Connector/NET ......................................................................................... 5-24
5.6.5 PHP.......................................................................................................................... 5-24
5.7 Third-Party APIs.............................................................................................................. 5-25
5.8 Chapter Summary............................................................................................................ 5-26
6 Data Types................................................................................................................................. 6-1
6.1 Learning Objectives........................................................................................................... 6-2
6.2 Data Type Overview.......................................................................................................... 6-3
6.2.1 The ABCs of Data Types..........................................................................................6-3
6.2.2 Creating Tables with Data Types................................................................................6-3
6.3 Numeric Data Types.......................................................................................................... 6-4
6.3.1 Integer Types.............................................................................................................. 6-4
6.3.2 Floating-Point Types................................................................................................. 6-5
6.3.3 Fixed-Point Types...................................................................................................... 6-7
6.3.4 BIT Types................................................................................................................... 6-8
6.4 Character String Data Types........................................................................................... 6-15
6.4.1 Unstructured Character String Data Types..............................................................6-16
6.4.2 Text Type Comparison............................................................................................ 6-18
6.4.3 Structured Character String types............................................................................ 6-19
6.4.4 Character Set and Collation Support........................................................................6-20
6.5 Binary String Data Types................................................................................................6-28
6.5.1 Binary Types........................................................................................................... 6-28
6.5.2 Binary String Type Comparison...............................................................................6-29
6.6 Temporal Data Types....................................................................................................... 6-30
6.6.1 Temporal Data Types................................................................................................6-30
6.7 NULLs............................................................................................................................. 6-36
6.7.1 The Meaning of NULL............................................................................................ 6-36
6.7.2 When to Use NULL................................................................................................ 6-36
6.7.3 When NOT to Use NULL........................................................................................ 6-37
6.8 Column Attributes............................................................................................................6-38
6.8.1 Using the AUTO_INCREMENT Column Attribute................................................ 6-40

xv

MySQL for Database Administrators

Preface

6.8.2 Choosing Data Types for Character Columns..........................................................6-44


6.9 Chapter Summary............................................................................................................ 6-48
7 Obtaining Metadata................................................................................................................... 7-1
7.1 Learning Objectives........................................................................................................... 7-2
7.2 Metadata Access Methods................................................................................................. 7-3
7.3 The INFORMATION_SCHEMA database........................................................................7-4
7.3.1 INFORMATION_SCHEMA Tables.......................................................................... 7-5
7.3.2 Displaying INFORMATION_SCHEMA Tables........................................................7-6
7.4 Using SHOW and DESCRIBE ....................................................................................... 7-13
7.4.1 SHOW Statements................................................................................................... 7-13
7.4.2 DESCRIBE Statements............................................................................................7-18
7.5 The mysqlshow Client Program.......................................................................................7-22
7.6 Administration Tasks with INFORMATION_SCHEMA................................................ 7-27
7.7 Chapter Summary............................................................................................................ 7-29
8 Storage Engines......................................................................................................................... 8-1
8.1 Learning Objectives........................................................................................................... 8-2
8.2 SQL Parser and Storage Engine Tiers................................................................................8-3
8.2.1 Storage Engine Breakdown .......................................................................................8-3
8.3 Storage Engines and MySQL............................................................................................ 8-5
8.3.1 Available Storage Engines..........................................................................................8-5
8.3.2 The Most Common Storage Engines......................................................................... 8-5
8.3.3 View Available Storage Engines................................................................................ 8-6
8.3.4 Setting the Storage Engine......................................................................................... 8-6
8.3.5 Displaying Storage Engine Information.....................................................................8-7
8.4 The MyISAM Storage Engine......................................................................................... 8-10
8.4.1 MyISAM Row Storage Formats.............................................................................. 8-10
8.4.2 Compressing MyISAM Tables................................................................................. 8-11
8.4.3 MyISAM Locking....................................................................................................8-13
8.4.4 MyISAM Pros and Cons.......................................................................................... 8-14
8.5 The InnoDB Storage Engine............................................................................................ 8-17
8.5.1 The InnoDB Tablespace and Logs........................................................................... 8-17
8.5.2 The InnoDB Plugin.................................................................................................. 8-19
8.5.3 The InnoDB Log files and Buffers........................................................................... 8-26
8.5.4 InnoDB Locking.......................................................................................................8-27
8.5.5 InnoDB Pros and Cons.............................................................................................8-30
8.6 The MEMORY Storage Engine....................................................................................... 8-31
8.6.1 MEMORY Indexing Options................................................................................... 8-31
8.6.2 MEMORY Best Practices.........................................................................................8-32
8.6.3 MEMORY Pros and Cons........................................................................................ 8-32
8.7 Storage Engine Summary................................................................................................ 8-34
8.8 Other Storage Engines..................................................................................................... 8-35
8.8.1 The EXAMPLE Storage Engine.............................................................................. 8-35
8.8.2 The ARCHIVE Storage Engine............................................................................... 8-35
8.8.3 The CSV Storage Engine......................................................................................... 8-36
8.8.4 The BLACKHOLE Storage Engine.........................................................................8-37
8.8.5 The NDB Storage Engine (MySQL Cluster)........................................................... 8-39
8.8.6 Falcon ......................................................................................................................8-40
8.9 Choosing Appropriate Storage Engines........................................................................... 8-41

xvi

MySQL for Database Administrators

Preface

8.10 Using Multiple Storage Engines.................................................................................... 8-42


8.11 Storage engine comparison chart................................................................................... 8-43
8.12 Chapter Summary.......................................................................................................... 8-46
9 Partitioning................................................................................................................................ 9-1
9.1 Learning Objectives........................................................................................................... 9-2
9.2 Overview of Partitioning................................................................................................... 9-3
9.2.1 MySQL Specific Partitioning .................................................................................... 9-4
9.3 Reasons for Using Partitioning.......................................................................................... 9-5
9.4 Creating a Partitioned Table...............................................................................................9-7
9.4.1 Basic Partition Syntax................................................................................................ 9-7
9.4.2 RANGE Partitioning.................................................................................................. 9-7
9.4.3 LIST Partitioning....................................................................................................... 9-8
9.4.4 HASH Partitioning..................................................................................................... 9-9
9.4.5 KEY Partitioning......................................................................................................9-10
9.4.6 Subpartitioning.........................................................................................................9-10
9.5 Obtaining Partition Information.......................................................................................9-12
9.5.1 SHOW CREATE TABLE........................................................................................ 9-12
9.5.2 SHOW TABLE STATUS......................................................................................... 9-12
9.5.3 INFORMATION_SCHEMA Metadata....................................................................9-13
9.5.4 EXPLAIN PARTITIONS......................................................................................... 9-14
9.6 Modifying Partitions........................................................................................................ 9-15
9.6.1 Altering a Partition................................................................................................... 9-15
9.6.2 Using the COALESCE PARTITION statement.......................................................9-17
9.6.3 Redefine Partitioning Type.......................................................................................9-17
9.7 Removing Partitions........................................................................................................ 9-18
9.7.1 Dropping Partitions.................................................................................................. 9-18
9.7.2 Removing all partitions............................................................................................ 9-19
9.8 Partition Modification Performance Effects.................................................................... 9-20
9.9 Partition Pruning.............................................................................................................. 9-21
9.10 Storage Engine Partitioning Information....................................................................... 9-22
9.10.1 Partitioning with MyISAM.................................................................................... 9-23
9.10.2 Partitioning with InnoDB.......................................................................................9-24
9.11 Partitioning and Locking ...............................................................................................9-24
9.12 Partitioning Limitations................................................................................................. 9-25
9.13 Chapter Summary.......................................................................................................... 9-28
10 Transactions And Locking..................................................................................................... 10-1
10.1 Learning Objectives....................................................................................................... 10-2
10.2 What is a Transaction?................................................................................................... 10-3
10.2.1 ACID...................................................................................................................... 10-4
10.3 Transaction Control Statements..................................................................................... 10-5
10.3.1 The autocommit mode............................................................................................10-5
10.3.2 Statements causing an Implicit COMMIT............................................................. 10-7
10.3.3 Finding a Storage Engine that supports transactions............................................. 10-7
10.4 Isolation Levels............................................................................................................10-13
10.4.1 Consistency issues................................................................................................10-13
10.4.2 Four Levels...........................................................................................................10-14
10.5 Locking........................................................................................................................ 10-19
10.5.1 Locking Concepts.................................................................................................10-19

xvii

MySQL for Database Administrators

Preface

10.5.2 Locking Reads......................................................................................................10-19


10.5.3 Implicit Locks...................................................................................................... 10-23
10.6 Chapter Summary........................................................................................................ 10-26
11 Security and User Management............................................................................................. 11-1
11.1 Learning Objectives....................................................................................................... 11-2
11.2 Security Risks.................................................................................................................11-3
11.3 Security Measures.......................................................................................................... 11-4
11.3.1 Network Security....................................................................................................11-4
11.3.2 Operating System Security..................................................................................... 11-5
11.3.3 Filesystem Security................................................................................................ 11-5
11.3.4 MySQL Server User Security.................................................................................11-6
11.4 Privileges........................................................................................................................11-7
11.4.1 Types of Privileges MySQL Supports.................................................................... 11-7
11.4.2 The Grant Tables.................................................................................................... 11-9
11.4.3 Modifying Grant Tables........................................................................................11-11
11.4.4 The GRANT Statement........................................................................................ 11-11
11.4.5 The SHOW GRANTS Statement......................................................................... 11-12
11.5 User Accounts.............................................................................................................. 11-13
11.5.1 Creating and Dropping User Accounts.................................................................11-14
11.5.2 Specifying Account Names.................................................................................. 11-15
11.5.3 Setting Account Password.................................................................................... 11-16
11.6 User Account Maintenance.......................................................................................... 11-19
11.6.1 Revoking Privileges............................................................................................. 11-19
11.6.2 When Privilege Changes Take Effect................................................................... 11-21
11.6.3 Specifying Resource Limits................................................................................. 11-21
11.6.4 Precautions for Granting Privileges..................................................................... 11-25
11.7 Disabling Client Access Control.................................................................................. 11-26
11.8 Using Secure Connections........................................................................................... 11-27
11.8.1 Using SSL.............................................................................................................11-27
11.9 Chapter Summary.........................................................................................................11-33
12 Table Maintenance.................................................................................................................12-1
12.1 Learning Objectives....................................................................................................... 12-2
12.2 Table Maintenance......................................................................................................... 12-3
12.2.1 SQL Statements for Table Maintenance Operations ............................................. 12-3
12.2.2 The CHECK TABLE statement............................................................................. 12-4
12.2.3 The CHECKSUM TABLE statement.....................................................................12-4
12.2.4 The REPAIR TABLES statement...........................................................................12-5
12.2.5 The ANALYZE TABLE statement........................................................................ 12-5
12.2.6 The OPTIMIZE TABLE statement........................................................................ 12-6
12.3 Client and Utility Programs for Table Maintenance.................................................... 12-10
12.3.1 The mysqlcheck Client Program.......................................................................... 12-10
12.3.2 The myisamchk Utility......................................................................................... 12-11
12.3.3 Options for mysqlchk and myisamchk.................................................................12-12
12.4 Table Maintenance per Storage Engine....................................................................... 12-16
12.4.1 InnoDB Table Maintenance................................................................................. 12-17
12.4.2 MEMORY Table Maintenance.............................................................................12-17
12.4.3 MyISAM Table Maintenance...............................................................................12-18
12.4.4 CSV Table Maintenance.......................................................................................12-19

xviii

MySQL for Database Administrators

Preface

12.4.5 Archive Table Maintenance..................................................................................12-19


12.5 Chapter Summary........................................................................................................ 12-22
13 Exporting and Importing Data...............................................................................................13-1
13.1 Learning Objectives....................................................................................................... 13-2
13.2 Exporting and Importing Data....................................................................................... 13-3
13.2.1 Export Data Using SELECT with INTO OUTFILE.............................................. 13-3
13.2.2 Importing Data Using LOAD DATA INFILE........................................................13-9
13.3 Import Data with SQL Scripts..................................................................................... 13-13
13.4 Chapter Summary........................................................................................................ 13-15
14 Programming with MySQL................................................................................................... 14-1
14.1 Learning Objectives....................................................................................................... 14-2
14.2 What is a Stored Routine?..............................................................................................14-3
14.3 Execute Stored Routines................................................................................................ 14-5
14.4 Examine Stored Routines...............................................................................................14-6
14.5 Stored Routines and Execution Security....................................................................... 14-7
14.6 What are Triggers?.........................................................................................................14-8
14.7 Creating Triggers........................................................................................................... 14-9
14.7.1 Triggers Events.......................................................................................................14-9
14.7.2 Trigger Error Handling.........................................................................................14-12
14.8 Delete Triggers.............................................................................................................14-13
14.9 Restrictions on Triggers............................................................................................... 14-13
14.10 Trigger Privileges.......................................................................................................14-13
14.11 Events.........................................................................................................................14-18
14.11.1 Schedule............................................................................................................. 14-20
14.11.2 Event Scheduler and MySQL Privileges............................................................14-21
14.12 DBA's Using MySQL's Programming Capabilities...................................................14-27
14.12.1 Using stored routines and triggers for security purposes................................... 14-27
14.12.2 Using Stored Routines to Enhance Performance............................................... 14-28
14.13 Chapter Summary...................................................................................................... 14-33
15 Views..................................................................................................................................... 15-1
15.1 Learning Objectives....................................................................................................... 15-2
15.2 What Are Views?........................................................................................................... 15-3
15.3 Creating Views...............................................................................................................15-5
15.3.1 The CREATE VIEW Statement............................................................................. 15-5
15.4 Updatable Views............................................................................................................ 15-8
15.4.1 Insertable Views................................................................................................... 15-10
15.4.2 SQL SECURITY and DEFINER Characteristics................................................ 15-13
15.4.3 WITH CHECK OPTION..................................................................................... 15-14
15.5 Managing Views.......................................................................................................... 15-16
15.5.1 Checking Views................................................................................................... 15-16
15.5.2 Altering Views......................................................................................................15-17
15.5.3 Dropping Views................................................................................................... 15-17
15.6 Obtaining View Metadata............................................................................................ 15-19
15.6.1 Using the information_schema............................................................................ 15-19
15.6.2 SHOW Statements............................................................................................... 15-20
15.7 Chapter Summary........................................................................................................ 15-23
16 Backup and Recovery............................................................................................................16-1
16.1 Learning Objectives....................................................................................................... 16-2

xix

MySQL for Database Administrators

Preface

16.2 Backup Basics................................................................................................................16-3


16.2.1 Backup Types......................................................................................................... 16-4
16.2.2 Backup Strategy..................................................................................................... 16-5
16.3 Backups with MySQL................................................................................................... 16-7
16.3.1 Logical (Textual) Backups..................................................................................... 16-7
16.3.2 Raw (Binary) Backups........................................................................................... 16-8
16.3.3 Snapshot-Based Backup.........................................................................................16-8
16.3.4 Replication-Based Backup..................................................................................... 16-8
16.3.5 Binary Logging and Incremental Backup.............................................................. 16-9
16.4 Backup Tools Overview...............................................................................................16-10
16.4.1 Tool Impact on Database and Application Availability........................................ 16-11
16.5 Making Raw Backups.................................................................................................. 16-12
16.5.1 Making Raw MyISAM Backups..........................................................................16-12
16.5.2 Making Raw InnoDB Backups............................................................................ 16-13
16.5.3 Other Raw Backup Tools..................................................................................... 16-14
16.6 Making Logical (Text) Backups.................................................................................. 16-17
16.6.1 Making Logical Backups with mysqldump......................................................... 16-17
16.6.2 Backing Up Stored Routines................................................................................16-20
16.7 Backing Up Log and Status Files................................................................................ 16-22
16.8 Replication as an Aid to Backup..................................................................................16-23
16.9 Backup Methods Compared.........................................................................................16-24
16.10 Data Recovery............................................................................................................16-25
16.10.1 Data Importing Methods Used for Recovery..................................................... 16-25
16.11 Processing Binary Log Contests................................................................................ 16-28
16.12 Chapter Summary...................................................................................................... 16-30
17 Introduction to
Performance Tuning................................................................................................................... 17-1
17.1 Learning Objectives....................................................................................................... 17-2
17.2 Using EXPLAIN to Analyze Queries............................................................................ 17-3
17.2.1 How EXPLAIN Works...........................................................................................17-3
17.2.2 Nested Joins........................................................................................................... 17-5
17.2.3 Analyzing a Nested Join.........................................................................................17-6
17.3 General Table Optimizations......................................................................................... 17-8
17.4 Setting and Interpreting MySQL Server Variables...................................................... 17-11
17.4.1 Setting Server Variables....................................................................................... 17-11
17.4.2 Accessing Server Variable Information................................................................17-12
17.4.3 Accessing Status Variable Information................................................................ 17-14
17.4.4 Obtaining Metadata for Server Variables............................................................. 17-17
17.5 Chapter Summary........................................................................................................ 17-19
18 Introduction to MySQL
High Availability.........................................................................................................................18-1
18.1 Learning Objectives....................................................................................................... 18-2
18.2 MySQL Replication....................................................................................................... 18-3
18.2.1 MySQL Replication Threads and Files.................................................................. 18-4
18.2.2 Setting up MySQL Replication.............................................................................. 18-5
18.2.3 Backing Up a Slave................................................................................................18-9
18.2.4 Monitoring MySQL Replication.......................................................................... 18-10
18.2.5 Slave I/O Thread States........................................................................................ 18-11

xx

MySQL for Database Administrators

Preface

18.2.6 Slave SQL Thread States......................................................................................18-12


18.2.7 Replication and the Binary Log........................................................................... 18-13
18.3 Chapter Summary........................................................................................................ 18-27
19 Conclusion............................................................................................................................. 19-1
19.1 Learning Objectives....................................................................................................... 19-2
19.2 Training and Certification Website............................................................................... 19-3
19.3 Course Evaluation..........................................................................................................19-5
19.4 Thank you!..................................................................................................................... 19-5
19.5 Q&A Session..................................................................................................................19-6

Appendix A
Appendix B
Appendix C
Appendix D

MySQL Lab Preparation....................................................................................... A-1


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

xxi

S-ar putea să vă placă și