Top Banner
MySQL Administration and Development Student Workbook
90

MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Mar 12, 2021

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration

and Development

Student Workbook

Page 2: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Page ii Rev 1.1.1 © 2008 ITCourseware, LLC

MySQL Administration and Development

MySQL Administration and Development

John McAlister and Dave Shaw

Published by ITCourseware, LLC., 7245 South Havana Street, Suite 100, Centennial, CO 80112

Contributing Authors: Danielle Hopkins

Editor: Danielle North

Special thanks to: Many instructors whose ideas and careful review have contributed to the quality of this

workbook and the many students who have offered comments, suggestions, criticisms, and insights.

Copyright © 2008 by ITCourseware, LLC. All rights reserved. No part of this book may be reproduced

or utilized in any form or by any means, electronic or mechanical, including photo-copying, recording, or by

an information storage retrieval system, without permission in writing from the publisher. Inquiries should be

addressed to ITCourseware, LLC., 7245 South Havana Street, Suite 100, Centennial, Colorado, 80112.

(303) 302-5280.

All brand names, product names, trademarks, and registered trademarks are the property of their respective

owners.

Page 3: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

© 2008 ITCourseware, LLC Rev 1.1.1 Page iii

MySQL Administration and Development

Contents

Chapter 1 - Course Introduction ............................................................................................................. 9

Course Objectives .......................................................................................................................... 10

Course Overview ........................................................................................................................... 12

Using the Workbook ...................................................................................................................... 13

Suggested References ..................................................................................................................... 14

Chapter 2 - Introduction to Database Concepts and MySQL ................................................................ 17

Features of a Relational Database ................................................................................................... 18

Where Does SQL Fit In? ................................................................................................................ 20

Database Access ............................................................................................................................ 22

Why MySQL? ............................................................................................................................... 24

The History of MySQL ................................................................................................................... 26

Chapter 3 - Installation, Configuration, and Upgrading ........................................................................... 29

MySQL Software ........................................................................................................................... 30

MySQL Software Features ............................................................................................................. 32

Preparing to Install MySQL ............................................................................................................ 34

Available Client Software ................................................................................................................ 36

After the Download ........................................................................................................................ 38

Configuring the Server .................................................................................................................... 40

Starting the Server .......................................................................................................................... 42

The Initial User Accounts ................................................................................................................ 44

Verifying Server Operation .............................................................................................................. 46

Upgrading ...................................................................................................................................... 48

Copying a Database Between Architectures .................................................................................... 50

Environment Variables ..................................................................................................................... 52

Labs ............................................................................................................................................... 54

Chapter 4 - Database Design ................................................................................................................ 57

Developing the Design of a Database .............................................................................................. 58

Database Entities ............................................................................................................................ 60

The Primary Key ............................................................................................................................ 62

Foreign Key Relationships .............................................................................................................. 64

Data Models and Normalization ...................................................................................................... 66

Page 4: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Page iv Rev 1.1.1 © 2008 ITCourseware, LLC

MySQL Administration and Development

Second Normal Form (2NF) .......................................................................................................... 68

Third Normal Form (3NF) and Beyond .......................................................................................... 70

Translating a Data Model into a Database Design ............................................................................ 72

Labs ............................................................................................................................................... 74

Chapter 5 - The mysql Command-Line Tool ......................................................................................... 77

Running the mysql Client ................................................................................................................. 78

Customizing the mysql Prompt ........................................................................................................ 80

mysql Commands ........................................................................................................................... 82

Using the Help Command ............................................................................................................... 84

Some Useful mysql Options ............................................................................................................ 86

Working with a Database ................................................................................................................ 88

Examining Table Definitions ............................................................................................................. 90

Other SHOW Options .................................................................................................................... 92

Labs ............................................................................................................................................... 94

Chapter 6 - DDL — Data Definition Language ...................................................................................... 97

DDL & DML Overview .................................................................................................................. 98

Building Table Definitions .............................................................................................................. 100

Identifiers ..................................................................................................................................... 102

Column Definitions ........................................................................................................................ 104

Numeric Datatypes ....................................................................................................................... 106

ENUM and SET Types ................................................................................................................ 110

Date and Time Datatypes .............................................................................................................. 112

AUTO_INCREMENT ................................................................................................................. 114

UNIQUE Constraints ................................................................................................................... 116

Primary Keys ............................................................................................................................... 118

Modifying Tables .......................................................................................................................... 120

Foreign Keys ................................................................................................................................ 122

Renaming and Dropping Tables ..................................................................................................... 124

Labs ............................................................................................................................................. 126

Chapter 7 - DML — Data Manipulation Language .............................................................................. 129

DDL & DML Overview ................................................................................................................ 130

Data Values: Numbers .................................................................................................................. 132

Data Values: Strings ...................................................................................................................... 134

Working with NULL Values .......................................................................................................... 136

Bulk Loading of Data .................................................................................................................... 138

Bulk Data Format ......................................................................................................................... 140

Page 5: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

© 2008 ITCourseware, LLC Rev 1.1.1 Page v

MySQL Administration and Development

Working with Special Values in Bulk Data ..................................................................................... 142

Adding New Table Rows with INSERT ........................................................................................ 144

Copying Rows .............................................................................................................................. 146

UPDATE...................................................................................................................................... 148

REPLACE ................................................................................................................................... 150

Removing Table Rows .................................................................................................................. 152

Transactions ................................................................................................................................. 154

InnoDB: Using Transactional Processing ........................................................................................ 156

Locking Tables ............................................................................................................................. 158

Labs ............................................................................................................................................. 160

Chapter 8 - Queries — The SELECT Statement ................................................................................. 163

SELECT Syntax Summary ............................................................................................................ 164

Choosing Data Sources and Destinations for SELECT .................................................................. 166

Presentation of Table Data with SELECT ...................................................................................... 168

Being Selective About Which Rows are Displayed ......................................................................... 170

User-Defined Variables ................................................................................................................. 172

Expressions and Functions ............................................................................................................ 174

Control Flow Operators and Functions ......................................................................................... 176

Function Names ........................................................................................................................... 178

Comparison Operators and Functions ........................................................................................... 180

String Functions ............................................................................................................................ 182

Numeric Operators and Functions ................................................................................................. 184

Date and Time Functions ............................................................................................................... 186

Forcing Data Interpretation ........................................................................................................... 188

Miscellaneous Functions ............................................................................................................... 190

Labs ............................................................................................................................................. 192

Chapter 9 - Building a Result Set from Several Sources ....................................................................... 195

UNION ....................................................................................................................................... 196

Combining Data from Two Tables ................................................................................................. 198

Using WHERE to Choose Matching Rows .................................................................................... 200

INNER JOIN .............................................................................................................................. 202

OUTER JOINs ............................................................................................................................ 204

Multiple Tables, Fields, Joins, and Ordering ................................................................................... 206

SELECT * and USING Columns ................................................................................................. 208

Labs ............................................................................................................................................. 210

Page 6: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Page vi Rev 1.1.1 © 2008 ITCourseware, LLC

MySQL Administration and Development

Chapter 10 - Advanced SQL Techniques ............................................................................................ 213

MySQL Pattern Matching ............................................................................................................. 214

Multipliers, Anchors, and Grouping ............................................................................................... 216

GROUP BY ................................................................................................................................. 218

Aggregates ................................................................................................................................... 220

Subqueries ................................................................................................................................... 222

Subquery Comparisons and Quantifiers ......................................................................................... 224

Other Subqueries .......................................................................................................................... 226

Subquery Alternatives and Restrictions .......................................................................................... 228

InnoDB Multi-Table Updates and Deletes ..................................................................................... 230

Building a VIEW .......................................................................................................................... 232

Updatable VIEWs ........................................................................................................................ 234

Labs ............................................................................................................................................. 236

Chapter 11 - MySQL Storage Engines ................................................................................................ 239

Storage Engine Overview .............................................................................................................. 240

Other Storage Engine Types .......................................................................................................... 242

The Basics of Commonly Used Storage Engines ............................................................................ 244

MyISAM Limits and Features ....................................................................................................... 246

MyISAM Data File Format .......................................................................................................... 248

InnoDB and Hardware Limitations ................................................................................................ 250

InnoDB Shared Tablespace Configuration ..................................................................................... 252

InnoDB Per-Table Tablespaces ..................................................................................................... 254

InnoDB Data Management ........................................................................................................... 256

MEMORY and FEDERATED ...................................................................................................... 258

MERGE and ARCHIVE ............................................................................................................... 260

Labs ............................................................................................................................................. 262

Chapter 12 - Utilities .......................................................................................................................... 265

Client Overview ............................................................................................................................ 266

Specifying Options for Command-Line Clients .............................................................................. 268

Client Option Files ........................................................................................................................ 270

Checking Tables with myisamchk and mysqlchk ............................................................................ 272

Using myisamchk and mysqlcheck for Repairs ............................................................................... 274

mysqlshow and mysqlimport ......................................................................................................... 276

Using mysqldump ......................................................................................................................... 278

The Query Browser ...................................................................................................................... 280

MySQL Query Browser: Deeper .................................................................................................. 282

Page 7: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

© 2008 ITCourseware, LLC Rev 1.1.1 Page vii

MySQL Administration and Development

MySQL Administrator: Basic Operations ....................................................................................... 284

MySQL Administrator: Monitoring the Server and User Administration .......................................... 286

Third Party Tools .......................................................................................................................... 288

Labs ............................................................................................................................................. 290

Chapter 13 - Administering a Database and Users ............................................................................... 293

The Server-Side Programs ............................................................................................................ 294

Starting the MySQL Server ........................................................................................................... 296

Using SET for Server Options ....................................................................................................... 298

Table Management ....................................................................................................................... 300

Server Log Files ........................................................................................................................... 302

mysqladmin .................................................................................................................................. 304

Backup and Restore ..................................................................................................................... 306

Miscellaneous Functions ............................................................................................................... 308

User Account Management ........................................................................................................... 310

Understanding User Privileges ....................................................................................................... 312

User Account Rights Management ................................................................................................. 314

User Account Privileges ................................................................................................................ 316

Managing Access to the Database Environment ............................................................................. 318

Labs ............................................................................................................................................. 320

Chapter 14 - Database Programmability .............................................................................................. 323

Stored Routines: Basic Concepts .................................................................................................. 324

Routine Creation and Use ............................................................................................................. 326

Flow Control Statements .............................................................................................................. 328

Writing Blocks of Code ................................................................................................................ 330

Triggers ........................................................................................................................................ 332

Stored Routines, Triggers, and the Binary Log ............................................................................... 334

Table HANDLERs ....................................................................................................................... 336

Prepared Statements ..................................................................................................................... 338

Labs ............................................................................................................................................. 340

Chapter 15 - Optimization and Performance Tuning ............................................................................. 343

Hardware Limitations .................................................................................................................... 344

Optimizing the MySQL Server's Interaction with the External World .............................................. 346

Adjusting the MySQL Server Configuration ................................................................................... 348

Optimizing Your Database ............................................................................................................. 350

Optimizing Queries ....................................................................................................................... 352

Page 8: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Page viii Rev 1.1.1 © 2008 ITCourseware, LLC

MySQL Administration and Development

The Use of Indexes to Support Queries ......................................................................................... 354

Thinking about JOIN Queries ....................................................................................................... 356

Query Sorts, Indexes, and Short-Circuiting ................................................................................... 358

INSERT, UPDATE, DELETE, and Table Locks ........................................................................... 360

Some General Optimizations ......................................................................................................... 362

Optimizations Specific to MyISAM............................................................................................... 364

Optimizations Specific to InnoDB.................................................................................................. 366

Labs ............................................................................................................................................. 368

Chapter 16 - MySQL Programming Interfaces .................................................................................... 371

Database Application Architectures ............................................................................................... 372

Connecting MySQL to ODBC...................................................................................................... 374

Connecting MySQL to MS/Office and MS/Access ....................................................................... 376

Connecting to MySQL from Perl ................................................................................................... 378

Programming Perl to MySQL ....................................................................................................... 380

Connecting to MySQL from PHP ................................................................................................. 382

Programming PHP to MySQL ...................................................................................................... 384

Solutions ............................................................................................................................................ 387

Index .................................................................................................................................................. 417

Page 9: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Course Introduction

© 2008 ITCourseware, LLC Rev 1.1.1 Page 9

Chapter 1

Chapter 1 - Course Introduction

Page 10: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 10 Rev 1.1.1 © 2008 ITCourseware, LLC

� Describe MySQL's benefits and features.

� Configure the MySQL server.

� Perform database design and normalization.

� Work with the command-line mysql tool.

� Set up and manage data and data schemas in MySQL.

� Use SELECT to retrieve data from a database.

� Combine data from multiple sources.

� Utilize more complex SQL functionality.

� Understand the MySQL storage engine types and their applicability.

� Use some of the more common MySQL standalone tools.

� Manage database resources and users.

� Write and call stored procedures.

� Optimize database storage for performance.

� Connect MySQL to external programming interfaces.

Course Objectives

Page 11: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Course Introduction

© 2008 ITCourseware, LLC Rev 1.1.1 Page 11

Chapter 1

Page 12: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 12 Rev 1.1.1 © 2008 ITCourseware, LLC

� Audience: Application and web developers, or system administrators.

� Prerequisites: Prior experience installing software and programming in any

language, such as HTML, is recommended but not required.

� Classroom Environment:

� A workstation per student.

Course Overview

Page 13: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Course Introduction

© 2008 ITCourseware, LLC Rev 1.1.1 Page 13

Chapter 1

Using the Workbook

Chapter 2 Servlet Basics

© 2002 ITCourseware, LLC Rev 2.0.0 Page 17

Add an init() method to your Today servlet that initializes a bornOn date, then print the bornOn date

along with the current date:

Today.java

...

public class Today extends GenericServlet {

private Date bornOn;

public void service(ServletRequest request,

ServletResponse response) throws ServletException, IOException

{

...

// Write the document

out.println("This servlet was born on " + bornOn.toString());

out.println("It is now " + today.toString());

}

public void init() {

bornOn = new Date();

}

}

Hands On:

The init() method is

called when the servlet is

loaded into the container.

This workbook design is based on a page-pair, consisting of a Topic page and a Support page. When you

lay the workbook open flat, the Topic page is on the left and the Support page is on the right. The Topic

page contains the points to be discussed in class. The Support page has code examples, diagrams, screen

shots and additional information. Hands On sections provide opportunities for practical application of key

concepts. Try It and Investigate sections help direct individual discovery.

In addition, there is an index for quick lookup. Printed lab solutions are in the back of the book as well as

online if you need a little help.

Java Servlets

Page 16 Rev 2.0.0 © 2002 ITCourseware, LLC

� The servlet container controls the life cycle of the servlet.

� When the first request is received, the container loads the servlet class

and calls the init() method.

� For every request, the container uses a separate thread to call

the service() method.

� When the servlet is unloaded, the container calls the destroy()

method.

� As with Java’s finalize() method, don’t count on this being

called.

� Override one of the init() methods for one-time initializations, instead of

using a constructor.

� The simplest form takes no parameters.

public void init() {...}

� If you need to know container-specific configuration information, use

the other version.

public void init(ServletConfig config) {...

� Whenever you use the ServletConfig approach, always call the

superclass method, which performs additional initializations.

super.init(config);

The Servlet Life Cycle

The Topic page provides

the main topics for

classroom discussion.

The Support page has

additional information,

examples, and suggestions.

Code examples are in a

fixed font and shaded. The

online file name is listed

above the shaded area.

Screen shots show

examples of what you

should see in class.

Topics are organized into

first (�), second (�), and

third (�) level points.

Pages are numbered

sequentially throughout

the book, making lookup

easy.

Callout boxes point out

important parts of the

example code.

Page 14: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 14 Rev 1.1.1 © 2008 ITCourseware, LLC

DuBois, Paul. 2007. MySQL Cookbook, Second Edition. O'Reilly Media Inc., Sebastopol, CA.

ISBN 0-596-52708-X.

Schwartz, Baron et al. 2008. High Performance MySQL, Second Edition. O'Reilly Media Inc.,

Sebastopol, CA. ISBN 0-596-10171-6.

Tahaghoghi, Seyed M.M. (Saied) and Hugh Williams. 2006. Learning MySQL. O’Reilly Media Inc.,

Sebastopol, CA. ISBN 0-596-00864-3.

http://dev.mysql.com/doc/refman/5.0/en/index.html

Suggested References

Page 15: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Course Introduction

© 2008 ITCourseware, LLC Rev 1.1.1 Page 15

Chapter 1

Page 16: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 16 Rev 1.1.1 © 2008 ITCourseware, LLC

Page 17: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 2 Introduction to Database Concepts and MySQL

© 2008 ITCourseware, LLC Rev 1.1.1 Page 17

Chapter 2 - Introduction to Database

Concepts and MySQL

Objectives

� Describe the structure of a relational

database.

� Describe the uses of the SQL

programming language.

� Explain the different ways that users can

access a database.

� Describe MySQL's benefits and features.

� Describe the history of MySQL and

MySQL AB.

Page 18: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 18 Rev 1.1.1 © 2008ITCourseware, LLC

Features of a Relational Database

� A relational database organizes data into tables, each table being much like a

spreadsheet.

� Each table in a database normally describes a specific group of information.

� Each table is given a specific name.

� A table is organized in columns, each with a specific name and each capable of

storing a specific datatype.

� Each column, also called a field, represents an attribute of the table.

� A row is a distinct set of values, one value for each column

� Each row, also called a record, represents a single entity in the real world.

� A column value might be empty, or null, for a particular row.

� A user accesses a subset of the data, the result set of a query, based on existing

tables.

� The logical data structure of a database is determined by the relationships

between the tables.

Page 19: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 2 Introduction to Database Concepts and MySQL

© 2008 ITCourseware, LLC Rev 1.1.1 Page 19

Consider the following as a table design for sports teams:

Team

Player

Sport Team Name Affiliated City

Baseball Rockies Denver

Baseball Astros Houston

Baseball Braves Atlanta

Player Name Player Number Position

Jeff Baker 10 Outfield

Matt Holiday 5 Outfield

Chris Ianetta 20 Catcher

Page 20: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 20 Rev 1.1.1 © 2008ITCourseware, LLC

Where Does SQL Fit In?

� The Structured Query Language (SQL) was first developed by IBM in the mid-

1970s for their DB2 database.

� It is typically pronounced as "sequel" or S...Q...L.

� SQL is the international standard language for relational database management

systems.

� SQL is considered a fourth-generation language, more abstract than Java,

COBOL, etc.

� SQL is a non-procedural language that emphasizes what to get, but

not how to get it.

� It is English-like and intuitive.

� SQL statements look more like natural language than many other

programming languages

� SQL is robust enough to be used by:

� Users with non-technical backgrounds.

� Professional developers.

� Database administrators.

� Each vendor has its own implementation of SQL as well as extensions for

greater functionality.

� MySQL implements a subset of SQL:1999.

Page 21: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 2 Introduction to Database Concepts and MySQL

© 2008 ITCourseware, LLC Rev 1.1.1 Page 21

SQL statements can be placed in two main categories:

Users and programmers spend most of their time working with only four of the keywords (SELECT,

INSERT, UPDATE, and DELETE).

Data Manipulation Language (DML)

Query Data Manipulation Transaction Control

SELECT INSERT COMMIT

UPDATE ROLLBACK

DELETE

Data Definition Language (DDL)

Data Definition Data Control

CREATE GRANT

ALTER REVOKE

DROP

Standard SQL Versions

Year Name Alias

1986 SQL-86 SQL-87

1989 SQL-89 FIPS 127-1

1992 SQL-92 SQL2, FIPS 127-2

1999 SQL:1999 SQL3

2003 SQL:2003

2006 SQL:2006

Page 22: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 22 Rev 1.1.1 © 2008ITCourseware, LLC

Database Access

� Before the World Wide Web, the only way to access a database was through a

standalone software application.

� Such applications may be provided by the database vendor, or can be

written in C, C++, Perl, COBOL, Java, or other languages.

� With the World Wide Web, many databases are also accessed from internet-

based applications.

� The user is typically accessing the database through a web page loaded

into a web browser.

� Some of the same underlying languages might be in use on the server.

� These languages have the ability to retrieve data from the database,

and pass that data back to the web browser, through a web server.

� Access to the data is only available through the database server.

� A valid username and password must be provided.

Page 23: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 2 Introduction to Database Concepts and MySQL

© 2008 ITCourseware, LLC Rev 1.1.1 Page 23

Page 24: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 24 Rev 1.1.1 © 2008ITCourseware, LLC

� MySQL is not a database, but a Relational Database Management System

(RDBMS); it's software that enables you to create, maintain, and manage

relational databases.

� MySQL is popular because of its:

� Size — Very modest hardware requirements.

� Speed — Some of the newer features can slow down processing, but

MySQL's modular design allows you to pick and choose.

� Ease of installation — Installation on some platforms is easy (e.g.,

Windows, or a Linux RPM), but others can be more complicated.

� Attention to standards — Even though multiple standards exist for SQL,

MySQL tries to stay current.

� Community relations — MySQL is open-source software and encourages

individual participation.

� There is a large amount of user-contributed software available, and

many local user groups.

� Easy interface to other software — Software interfaces, or drivers, to

MySQL exist for many programming languages.

Why MySQL?

Page 25: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 2 Introduction to Database Concepts and MySQL

© 2008 ITCourseware, LLC Rev 1.1.1 Page 25

Page 26: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 26 Rev 1.1.1 © 2008ITCourseware, LLC

The History of MySQL

� In 1994, Oracle, Sysbase, DB2, and Informix dominated the SQL-based

RDBMS market with their powerful, very resource-intensive, and expensive

systems.

� Postgres was one of a few cheap database engines available, but wasn't

SQL-driven.

� David Hughes developed a DBMS that used SQL to support his Ph.D. work,

based on Postgres.

� To enable SQL, he built a translator called mSQL that converted SQL to

PostQUEL.

� The Postgres engine was too ungainly for his needs, so he incorporated a

DBMS into mSQL.

� In 1979, Michael Widenius, as the only database developer at TcX, developed a

tool called UNIREG for managing databases.

� In 1994, TcX began developing web-based applications, using UNIREG

to support page generation, but UNIREG proved too ungainly.

� Using the best features of UNIREG, and building on the mSQL API,

MySQL 1.0 was released in May, 1995.

� Shortly thereafter, MySQL was released on the Internet, under a flexible

copyright (currently the GPL).

� MySQL AB, established in Sweden by David Axmark, Allan Larsson, and

Michael Widenius, produces MySQL software.

Page 27: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 2 Introduction to Database Concepts and MySQL

© 2008 ITCourseware, LLC Rev 1.1.1 Page 27

Some of MySQL's major features include the following:

� Internals and Portability:

- Written in C and C++, and tested with a broad range of different compilers.

- Works on many different platforms.

- It can easily use multiple CPUs if they are available.

- Uses very fast B-tree disk tables (MyISAM) with index compression.

- Very fast joins using an optimized one-sweep multi-join.

� Statements and Functions (full support is provided for):

- Operators and functions in SELECT list and WHERE clause of queries.

- SQL GROUP BY and ORDER BY clauses.

- LEFT OUTER JOIN and RIGHT OUTER JOIN.

� Security:

- A privilege and password system that allows host-based verification.

- All password traffic is encrypted when you connect to a server.

� Scalability and Limits:

- Handles large databases (e.g., 60,000 tables and about 5 million rows).

- Up to 64 indexes per table are allowed.

� Connectivity:

- Clients can connect to the server using TCP/IP sockets, named pipes, shared-memory, or socket

files, depending on the platform.

- APIs for C, C++, Eiffel, Java, Perl, PHP, Python, Ruby, and Tcl are available.

- Interfaces are available for ODBC (Connector/ODBC (MyODBC)), Java (Connector/J), .NET

(Connector/NET), and others.

� Localization:

- The server can provide error messages to clients in many languages.

- All data is saved in the chosen character set.

� Clients and Tools:

- Command-line tools such as mysql, mysqlimport, and mysqlcheck are available for issuing SQL

statements, importing data, and table maintenance.

- GUI tools such as MySQL Migration Toolkit, MySQL Administrator, and MySQL Query Browser

are also available.

Page 28: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 5 The mysql Command-Line Tool

© 2008 ITCourseware, LLC Rev 1.1.1 Page 77

Chapter 5 - The mysql Command-Line Tool

Objectives

� Use the mysql client.

� Run mysql in batch mode.

� Configure the mysql client prompt.

� Use the SHOW command to display

mysql capabilities.

� Use mysql options.

Page 29: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 78 Rev 1.1.1 © 2008 ITCourseware, LLC

� Establish an interactive connection to the server with the command-line toolmysql.

mysql [-h host] [-u user] [-p db_name]

� An anonymous account can be connected to without specifying ausername.

� If the server is not running, you will get a message saying you can'tconnect.

� Exit the connection by entering QUIT, or Control-D on a UNIX machine.

� Query output from mysql is in tabular form (rows and columns).

� The first row contains labels for the columns.

� Column labels are typically the names of columns from databasetables.

� If you use an expression rather than a table column, the columnlabel is the expression itself.

mysql> SELECT VERSION(), 2+2+----------------+------+| VERSION() | 2+2 |+----------------+------+| 5.0.7-beta-Max | 4 |+----------------+------+

1 row in set (0.01 sec)

� The last line of output shows how many rows were returned and how long thequery took to execute (wall-clock seconds).

� Control the startup of mysql by options on the command line.

� Options exist for controlling the connection, manipulating output,controlling execution, and general operations.

Running the mysql Client

Page 30: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 5 The mysql Command-Line Tool

© 2008 ITCourseware, LLC Rev 1.1.1 Page 79

There are over 50 options that can be supplied when starting mysql. Most have both a long and a short

form. Some of the more interesting ones are:

� Options related to the connection to the server:

--host=host_name, -h host_name Connect to the server on host_name.

--port=port_num, -P port_num Use port_num for TCP/IP communications to the server.

--user=user_name, -u user_name The MySQL username to use for the connection.

--database=db_name, -D db_name The database the server should use by default.

--compress, -C Compress all traffic between client and server if they both

support it.

--wait, -w If a connection can't be established, wait and retry.

--password=password, -ppassword Use the given password to login, or prompt for one if it is

not given.

� Options controlling how many statements are executed:

--execute=statement, -e statement Execute the statement on the command line and quit.

--force, -f Continue running a script even if a SQL error occurs.

� Options controlling how the output is formatted:

--batch, -B Print results using a tab as the column separator.

--html, -H Produce HTML output.

--silent, -s Produce less output (can be used multiple times).

--verbose, -v Produce more output (can be used multiple times).

� Options controlling the general operation of mysql:

--no-beep, -b Don't beep for errors.

--show-warnings Show warnings after every statement.

--sigint-ignore Ignore Control-C.

--version, -V Display version number and exit.

--safe-updates Try to avoid making silly mistakes.

� Options can be grouped. For example, '-v -v -v' could be written as '-vvv'.

Try It:Start mysql using:

mysql -u root (Linux)

mysql (Windows)

Use quit to exit mysql.

Page 31: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 80 Rev 1.1.1 © 2008 ITCourseware, LLC

Customizing the mysql Prompt

� The default mysql prompt is 'mysql> ', but you can change it via any of thefollowing:

� Setting the environment variable MYSQL_PS1.

� Providing a value for the mysql startup option:

--prompt=newprompt

� Issuing the prompt command to mysql:

prompt newprompt

� Specifying prompt with no data returns to the default prompt.

� Setting the prompt option in an option file.

� You can configure the mysql prompt to include various types of information,including the following:

� Information about the server.

� Your username.

� A counter that increments for each statement you issue.

� Date and time information.

� Various punctuation.

� Most commands end with a semicolon, but can span multiple lines.

� If mysql decides you haven't finished the statement, it will change theprompt.

� If you reach one of these prompts because of an incorrect line enteredearlier, you can usually enter \c to cancel out of it.

Page 32: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 5 The mysql Command-Line Tool

© 2008 ITCourseware, LLC Rev 1.1.1 Page 81

Displaying information in the mysql prompt is achieved by placing special character sequences in the

prompt. Some of the more interesting ones follow.

Character sequences that display information about the server:

\v The server version.

\h The server host.

\p The current TCP/IP port or socket file.

\d The default database.

Character sequences that display information about you:

\u Your username.

\U Your full user_name@host_name account name.

Character sequences that display information about your progress:

\c A counter that increments for each statement you issue.

Character sequences that display information about the date and time:

\D The full current date.

\R The current time, in 24-hour military time (0-23).

\m Minutes of the current time.

\s Seconds of the current time.

Character sequences that provide various punctuation:\t A tab character.\n A newline character.

\_ A space.

When mysql can't find the end of your command, it will issue one of the following continuation prompts:

-> Waiting for next line of a multiple-line command.

'> Waiting for next line of a string that began with a single quote.

"> Waiting for next line of a string that began with a double quote.

`> Waiting for next line of an identifier that began with a backtick.

/*> Waiting for next line of a comment that began with /*.

Note that if mysql is waiting for the completion of a string, you can't type \c to cancel the command until

you close the string.

Try It:Enter

mysql --prompt="(\u@\h) [\d]> "

to change the prompt.

Exit mysql to restore the prompt.

Page 33: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 82 Rev 1.1.1 © 2008 ITCourseware, LLC

mysql Commands

� mysql directly processes some commands, and passes SQL on to mysqld.

� There are both long and short forms of all commands run by mysql.

� The long form is not case sensitive; the short form is.

� The long form must be first on the line and can be followed by an

optional semicolon.

� The short form can appear anywhere on the line and should not be

followed by a semicolon.

� Commands run by mysql are available in several categories:

� Managing mysql's operation, e.g., changing the prompt.

� Controlling the handling of output text, e.g., should warnings be issued.

� Managing command entry, e.g., what the statement delimiter is.

� Interacting with server operation, e.g., specifying which database to use.

� On Linux systems, all executed commands are written to ~/.mysql_histfile or the

MYSQL_HISTFILE location.

Page 34: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 5 The mysql Command-Line Tool

© 2008 ITCourseware, LLC Rev 1.1.1 Page 83

Some of the more interesting commands that can be issued to mysql are:

Commands that affect the operation of mysql:

help \h Display help.

? \? Synonym for help.

prompt \R Change the mysql prompt.

source \. Execute an SQL script file. Takes a file name as an argument.

system \! Execute a system shell command. UNIX only.

exit \q Exit mysql.

quit \q Exit mysql.

Commands that affect the handling of output data:

tee \T Set outfile to [outfile] (append mode). Queries and output are flushed

after each command.

notee \t Don't write into outfile.

warnings \W Show warnings after every statement.

nowarning \w Don't show warnings after every statement.

pager \P Set PAGER to [pager]. UNIX only. Does nothing if [pager] isn't

specified. Can also specify pager options here. Will use the PAGER

environment variable if available.

nopager \n Disable pager, print to stdout. UNIX only.

Commands for managing commands:

delimiter \d Set statement delimiter. NOTE: Takes the rest of the line as new delimiter.

print \p Print current command.

clear \c Clear command.

edit \e Edit command with the editor specified by EDITOR. UNIX only.

Commands for interacting with the server:

connect \r Reconnect to the server. Optional arguments are db and host.

use \u Use another database. Takes database name as argument.

ego \G Send command to mysql server, display result vertically.

go \g Send command to mysql server.

status \s Get status information from the server.

External I/O can be manipulated via a pager argument (it's just spun off using popen()). For example:

pager tee ~/res.txt | less -n -i -S

Page 35: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 84 Rev 1.1.1 © 2008 ITCourseware, LLC

Using the Help Command

� Access a directory of help information available from the server with the

command help contents.

� Over a dozen categories are displayed, e.g., Administration, Data Types,

Transactions, etc.

� Asking for help on any category will give you a list of topics within that

category.

help 'data types';

� Search strings are case insensitive.

� You can also ask for help on SQL statements.

help 'create table';

� mysql routes help arguments to the server for a lookup from the help tables.

� Set up help tables as follows:

� For a Windows binary or Linux RPM distribution, the installation process

automatically sets up help.

� For a UNIX binary distribution, run mysql_install_db.

� For a source distribution, run the fill_help_tables.sql file (in the scripts

directory):

mysql -u root mysql < fill_help_tables.sql

Page 36: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 5 The mysql Command-Line Tool

© 2008 ITCourseware, LLC Rev 1.1.1 Page 85

Page 37: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 86 Rev 1.1.1 © 2008 ITCourseware, LLC

Some Useful mysql Options

� mysql can run commands and SQL statements from a script.

mysql db_name < text_file

or

mysql -e "source text_file"

� If the first statement in the text file is USE db_name, it doesn't have to bespecified on the command line.

� By default, output tables are produced without separation lines, with tab-

separated columns.

� Prevent issuing requests that require too many system resources by runningmysql with the --safe-updates option.

� A connection failure from mysql to mysqld will automatically try to reconnectonce.

� If the connection is re-established, various data will be lost.

� Temporary tables, user and session variables, and the autocommitmode will be lost.

� Any transaction in progress will be rolled back.

� Use the --skip-reconnect option to prevent the reconnect attempt.

� mysql normally stores the result of a query before displaying it, possibly usinglarge amounts of memory.

� Using the --quick option when starting mysql causes it to output one lineat a time.

Page 38: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 5 The mysql Command-Line Tool

© 2008 ITCourseware, LLC Rev 1.1.1 Page 87

Scripts are useful for a variety of reasons:

- Repetitive tasks.

- To enhance code reuse and to save typing time.

- To enable scheduled (cron) jobs.

- They provide another way to capture output.

The normal output of mysql is to the screen, which can be changed by specifying:mysql < myscript > an_output_file

mysql < myscript | a_filter

Note that use of the pager internal command will (temporarily) override whatever is specified on the

command line. Also note the tee internal command operates independently of any of this.

When running a script from an interactive session, the script can use a SELECT statement to display its

progress:SELECT 'what to display' AS ' '

To see every command run from a script, use the -vvv option when starting mysql:mysql -vvv < text_file

If you want to run a script and have it display query output that looks like it does interactively, start mysql

with the -t option:

mysql -t < myscript

If a query produces column data that is large, possibly containing new lines, it might be more readable to

display the data in a vertical format. A query result can be displayed vertically by ending the query with \G

instead of a semi-colon.

Running mysql with the --safe-updates option has several effects:

- UPDATE and DELETE cannot be run without specifying WHERE or LIMIT.

- A maximum of 1000 rows can be produced by any SELECT unless LIMIT is specified. The

default limit can be overridden on the command line by specifying --select_limit=value.

- If mysqld determines a multi-table SELECT statement probably needs to access over 1,000,000

rows, it refuses to run the SELECT. The default limit can be overridden on the command line by

specifying --max_join_size=value.

Page 39: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 88 Rev 1.1.1 © 2008 ITCourseware, LLC

Working with a Database

� The USE command specifies the default (current) database for subsequentstatements, and remains in effect until end-of-session or another USE statement.

USE db_name

� The CREATE command builds a new, empty database schema.

CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name

� An error occurs if the database exists and you did not specify IF NOTEXISTS.

� The SHOW command queries mysqld about its capabilities and the data it'smanaging.

� Wildcards are available for pattern.

� % represents zero or more characters.

� _ represents exactly one character.

� Keywords, column names, column aliases, indexes, and stored routinenames are never case sensitive, but trigger names always are.

� SHOW DATABASES displays a list of databases on the MySQL serverhost for which you have privileges.

SHOW {DATABASES | SCHEMAS} [LIKE 'pattern']

� SHOW STATUS displays information about server variables.

SHOW [GLOBAL | SESSION] STATUS [LIKE 'pattern']

� SHOW WARNINGS displays error, warning, and note messages thatresulted from the last command that used a table.

SHOW {WARNINGS | ERRORS} [LIMIT [offset,] row_count]

� The offset specifies where to begin displaying (the first error row is

row 0).

Page 40: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 5 The mysql Command-Line Tool

© 2008 ITCourseware, LLC Rev 1.1.1 Page 89

Creating a directory in the mysqld data directory will show up in SHOW DATABASES.

Making a particular database the default does not preclude you from accessing tables in other databases:

USE db1;

SELECT author_name,editor_name FROM author,db2.editor

WHERE author.editor_id = db2.editor.editor_id;

Try It:Ask about the storage engines supported by your server with the following:

mysql> SHOW ENGINES

Engine Support Comment

MyISAM DEFAULT Default engine as of MySQL 3.23 with great performance

MEMORY YES Hash based, stored in memory, useful for temporary table

EXAMPLE NO Example storage engine

The support level can be: YES (supported and active), NO (not compiled into server), or DISABLED

(supported, but disabled). Engines can be disabled when mysqld is started either explicitly or by omitting

required options.

mysql session mysql session mysql session

mysqld (the server)

db1 (database) db2 (database)

author(table)

editor(table)

Table3

Table2

Table2

These are both sub-directories of the mysql

data directory.

These are each one or morefiles in their database directory.

Page 41: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 90 Rev 1.1.1 © 2008 ITCourseware, LLC

Examining Table Definitions

� SHOW TABLES lists the non-TEMPORARY tables in a given database for

which you have privileges as well as any views in the database.

SHOW [FULL] TABLES [FROM db_name] [LIKE 'pattern']

� The FULL modifier displays a second output column containing BASE

TABLE for a table and VIEW for a view.

� SHOW TABLE STATUS displays one row per table.

SHOW TABLE STATUS [FROM db_name] [LIKE 'pattern']

� This command shows three types of information:

� Table meta-data — Creation time, creation options, which engine,

what version, the collation sequence, etc.

� Row meta-data — Number of, average length, the next

AUTO_INCREMENT value, etc.

� Disk file data — How big, size of the index file, etc.

� SHOW COLUMNS FROM displays information about the columns in a table

or view for which you have privileges.

SHOW [FULL] {COLUMNS | FIELDS} FROM tbl_name [FROM db_name]

[LIKE 'pattern']

� The FULL keyword displays your privileges and any comments.

� DESCRIBE is a shortcut for SHOW COLUMNS FROM.

{DESCRIBE | DESC} tbl_name [col_name | pattern]

Page 42: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 5 The mysql Command-Line Tool

© 2008 ITCourseware, LLC Rev 1.1.1 Page 91

If the following represented a city table:

Asking mysql for details about it might yield the following:mysql> DESCRIBE city

+------------+----------+-------+-------+----------+-------------+

| Field | Type | Null | Key | Default | Extra |

+------------+----------+-------+-------+----------+-------------+

| Id | int(11) | NO | PRI | NULL |auto_increment |

| Name | char(35)| NO | | | |

| Country | char(3) | NO | UNI | | |

| District | char(20)| YES | MUL | | |

| Population | int(11) | NO | | 0 | |

+------------+----------+-------+-------+----------+-------------+

The Null column indicates whether NULL values can be stored in that column in the database.

If a value appears in the Key field, it can be:

PRI — The column is part of the table's primary key.

UNI — The column is part of a UNIQUE index.

MUL — Multiple occurrences of a given value are allowed within the column.

In a composite index, only the leftmost column of the index has an entry in the Key field.

The Default field indicates the default value that is assigned to the column.

dI emaN yrtnuoC tcirtsiD noitalupoP

471 revneD .A.S.U tseW 000,002,1

34 elliveS niapS eporuE 000,004

Page 43: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 92 Rev 1.1.1 © 2008 ITCourseware, LLC

Other SHOW Options

� SHOW CREATE displays the statement that could be used to create that

database.

SHOW CREATE {DATABASE | SCHEMA} db_name

� Similar commands are available to show how to re-create tables and

procedures.

� SHOW GRANTS displays the GRANT statement necessary to duplicate the

privileges of the account being used.

SHOW GRANTS FOR CURRENT_USER;

� SHOW OPEN TABLES shows a count of how many locks and lock requests

are currently in use for each table.

SHOW OPEN TABLES [FROM db_name] [LIKE 'pattern']

� SHOW PROCESSLIST displays the thread(s) associated with the MySQL

account you are using.

SHOW [FULL] PROCESSLIST

� Use help show for a list of many more options.

Page 44: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 5 The mysql Command-Line Tool

© 2008 ITCourseware, LLC Rev 1.1.1 Page 93

mysql> SHOW CREATE TABLE t\G

*************************** 1. row ***************************

Table: t

Create Table: CREATE TABLE t (

id INT(11) default NULL auto_increment,

s char(60) default NULL,

PRIMARY KEY (id)

) ENGINE=MyISAM

mysql> SHOW CREATE FUNCTION test.hello\G

*************************** 1. row ***************************

Function: hello

sql_mode:

Create Function: CREATE FUNCTION `test`.`hello`(s CHAR(20)) RETURNS

CHAR(50) RETURN CONCAT('Hello, ',s,'!')

mysql> SHOW GRANTS FOR 'root'@'localhost';

+---------------------------------------------------------------------+

| Grants for root@localhost |

+---------------------------------------------------------------------+

| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |

Asking for details about a processes will display something similar to the following:

mysql> SHOW FULL PROCESSLISTId User Host db Command Time State Info

1 system user NULL Connect 1030455 Writing to net NULL

3123 dshaw localhost home2k Query 0 NULL SHOW FULL.......

An internal thread will show the User is system user.

The time (in seconds) is how long the process has been in its current state.

There are over 35 different values for State. Some of the more interesting:

- Checking table

- Closing tables (should be fast unless the disk is full)

- Copying to tmp table on disk (the in-memory table grew larger than tmp_table_size)

- Flushing tables (waiting for all threads to complete)

- Locked (the query is locked by another query)

- Sending data (responding to a SELECT)

- Statistics (working to develop a query execution plan)

Most states correspond to very quick operations. If a thread stays in any of these states for many seconds,

there might be a problem that needs to be investigated.

Page 45: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 94 Rev 1.1.1 © 2008 ITCourseware, LLC

Labs

� Run the command

SELECT 5*2, 4/2

within MySQL client, from the command line.

Describe the difference in output when --batch is used as an option. How about when --html is

used instead of --batch?

(Solution: output.txt)

� Start the MySQL client, setting the prompt to be port@host>, where port is the current TCP/IP

port. Without exiting the client, change the prompt to be user(time) :dbname>.

(Solution: prompt.txt)

� What is the difference between the STATUS and the SHOW STATUS commands? Try them out.

(Solution: status.txt)

Page 46: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 5 The mysql Command-Line Tool

© 2008 ITCourseware, LLC Rev 1.1.1 Page 95

Page 47: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 13 Administering a Database and Users

© 2008 ITCourseware, LLC Rev 1.1.1 Page 293

Chapter 13 - Administering a Database and

Users

Objectives

� Configure options for mysqld.

� Backup and restore databases.

� Create and manage user accounts.

� GRANT and REVOKE privileges.

Page 48: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 294 Rev 1.1.1 © 2008 ITCourseware, LLC

� The mysqld server can be configured and run completely manually.

� The mysqld server can be run as a self-managing service.

� On Windows, the Service manager is typically used to configure the

service.

� On UNIX systems, two methods are available for configuring the service.

� On systems that support the Sys V run-level directories, the

mysql.server script is typically installed in the /etc/init.d directory,

and runs mysqld_safe as necessary.

� On other UNIX systems, the mysqld_safe startup script is used to

restart the server and provide for a log file.

� Most options passed to mysqld_safe are passed directly to

mysqld.

� Support for running multiple servers on one machine is provided by the

mysqlmanager program.

� This concept is commonly used for testing a new release away from the

production side, or for different users (or groups of them) each accessing

their own database environment.

� Sharing data directories is not recommended.

� Several scripts and programs support installation and upgrade procedures.

� Several other utilities help with packaging and sharing any customized version

you build.

The Server-Side Programs

Page 49: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 13 Administering a Database and Users

© 2008 ITCourseware, LLC Rev 1.1.1 Page 295

Many of tools provided by MySQL are technology that has been around a long time. More up-to-date

programs are often available. The documentation often points out when this is true, but be sure to look

around.

The current scripts and programs intended for facilitating install and upgrade processes are:

1. mysql_install_db

This Bourne shell script creates the database directory and initializes grant tables with default

privileges if they do not exist. Filesystem privileges are set according to who runs this script, which

should be the same user who will eventually run mysqld.

2. mysql_secure_installation

This Bourne shell script tightens up the security of a MySQL installation. It helps configure the use of

root and anonymous accounts.

3. mysql_upgrade

This pre-compiled binary is used after an upgrade to check tables for incompatibilities. When a

possible incompatibility is found, the table is checked, and repaired as necessary. If changes have

been made to the permissions framework, it also upgrades the grant tables.

4. mysql_tzinfo_to_sql

This pre-compiled binary is used to load time zone tables into the database from the host system. It

is available only on systems that have a zoneinfo database (e.g., Linux).

Interesting extra options available when running mysqld_safe are:

--mysqld-version=suffix Server name to be run.

--nice=priority UNIX priority.

--no-defaults Don't read any option files.

Running mysqlmanager as a service involves the following pieces:

mysql is typically used to talk to mysqlmanager: mysql -port=2273 -user=mysql

START/STOPSHOWSET/UNSETSTATUS

"angel"

--angel-pid-file

mysqlmanager--bind-address, --port--user (to be run as)

Guarded. Monitored every 20 seconds. Its own data directory, log and pid files, connection scheme, etc.

Unguarded. Monitored on demand. Its own data directory, log and pid files, connection scheme, etc.

--defaults-file (changes will be written here)[manager] …]mysqld] …[mysqld1] …[mysqld2] …unguarded

--password-file

--log--pid-file

server a server b

Page 50: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 296 Rev 1.1.1 © 2008 ITCourseware, LLC

Starting the MySQL Server

� The MySQL server is the heart of database operations, and as such, is highly

configurable.

� Several options have to do with what SQL syntax should be allowed and

how data should be validated.

� Other options for mysqld can be categorized as relating to specific

engines, the log files, security, and replication.

� Option groups read by mysqld are [mysqld] and [server].

� mysqld_safe reads from the [mysqld], [server], and [mysqld_safe]

option groups.

� mysqld-server reads from [mysqld] and [mysql.server] option groups.

� Variable values for mysqld operation can be set using --varname=value.

� Maximum values can be established where appropriate using --maximum-

varname=value.

� Diplay variable values using the SHOW command:

SHOW [GLOBAL | SESSION] VARIABLES [LIKE 'pattern']

� The default variable type is SESSION.

Page 51: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 13 Administering a Database and Users

© 2008 ITCourseware, LLC Rev 1.1.1 Page 297

Many features of mysqld depend on the cooperation of the OS. As such, the environment in which mysqld

runs is partially configured with option settings. Some of the more important that you may want to set are:

--bind-address=IP Where to contact the server.

--chroot=path Limits the visible disk environment.

--console On Windows only, send error messages to stdout and stderr.

--datadir=path The data directory.

--external-locking Enables external locking, which requires lockd (may not work on Linux).

--flush Syncs all changes to disk after each SQL statement.

--init-file=filename At startup, reads single-line SQL statements, with no comments.

--open-files-limit=count To overcome "Too many open files" error.

--standalone Use this when running from the command line.

--user={user_name|user_id},-u {user_name|user_id} Effective user.

--skip-networking Use only named pipes or shared memory (Windows) or sockets (UNIX).

This option implies only local clients will be able to connect.

Several communications options are applicable to Windows only.

--enable-named-pipe For clients that want this.

--shared-memory For clients that want this.

--shared-memory-base-name=name Default name is MYSQL (case sensitive).

Some options are specific to different engine types:

--default-storage-engine=type

--skip-innodb

--skip-merge

--transaction-isolation=

{READ-UNCOMMITTED|READ-COMMITTED|REPEATABLE-READ|SERIALIZABLE}

The InnoDB default, REPEATABLE-READ if not

specified.

--myisam-recover[=option[,option]...] Used whenever a MyISAM table is first opened. If this

option is not used, MyISAM tables are not checked at

open time. Specify BACKUP, FORCE to recover without

intervention.

Any combination of the following options is allowed:

- BACKUP (makes a .BAK file if changes will be made)

- FORCE (fixes the table even if more than one row will be lost)

- QUICK (doesn't check the rows if nothing has been deleted)

Page 52: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 298 Rev 1.1.1 © 2008 ITCourseware, LLC

Using SET for Server Options

� The SET command can be used to set both server and session variables.

� When a session is initialized, it begins with a copy of the server variables.

� An altered server variable will only be seen by subsequent sessions.

� Server variables are designated using a prefix (SUPER privilege required).

� Write the variable name with either the word GLOBAL before it or

with a '@@global.' prefix.

� Session variables are the default but can be written using SESSION,

'@@session.', or '@@'.

� When retrieving a variable value, the prefix '@@' looks first in the

session and then in the server.

� The suffixes K, M, and G aren't allowed when using SET.

� Assigning to DEFAULT sets a session variable to the server value, and a

server variable to the compiled-in value.

� The server can be configured for the SQL mode it runs in.

� SQL_MODE values determine what SQL syntax is allowed and how

data should be validated.

� The server startup option --sql-mode=value[[,value]...] sets the global

value.

� SET SQL_MODE='value[[,value]...]' can set either a server or the

session variable.

Page 53: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 13 Administering a Database and Users

© 2008 ITCourseware, LLC Rev 1.1.1 Page 299

The syntax for using SET for variable assignment is:SET variable_assignment [, variable_assignment] ...

variable_assignment:

user_var_name = expr

| [GLOBAL | SESSION] system_var_name = expr

| [@@global. | @@session. | @@]system_var_name = expr

Many SQL_MODE settings are related to compatibility with other database systems, and either limit how

SQL can be written or how data and DDL statements are displayed by the server. Other options are related

to working with date values, math operations, and how invalid or missing input values are handled, e.g.,

wrong data type, out of range value.

Conglomerate settings are shorthand for groups of options, intended to enhance compatibility with particular

database systems. There are conglomerate modes for ANSI, DB2, ORACLE, POSTGRESQL,

TRADITIONAL, and others.

The most common settings are for the following:

ANSI — Standard SQL.

TRADITIONAL — Generates errors instead of warnings for INSERT/UPDATE problems.

STRICT_TRANS_TABLES — If an input value is invalid for an InnoDB table, abort the statement.

For all other types, abort if a single-row statement or first row of a multi-row statement.

Some of the more common variables SET is used for are the following.

AUTOCOMMIT={0|1} InnoDB only (default: 1).

FOREIGN_KEY_CHECKS={0|1} InnoDB only (default: 1) Switching to 1 after loading table data

doesn't cause a scan of those data.

SQL_BUFFER_RESULT={0|1} When 1, forces SELECT results into a temp table, freeing table

locks sooner (default 0)

SQL_SAFE_UPDATES={0|1} When 1, UPDATE and DELETE aborted if no WHERE or

LIMIT clause (default 0)

SQL_SELECT_LIMIT={value|DEFAULT} Max rows to be returned. DEFAULT=unlimited.

Overridden by using a LIMIT clause.

These variables aren't displayed by SHOW VARIABLES, but can be viewed using SELECT.

Page 54: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 300 Rev 1.1.1 © 2008 ITCourseware, LLC

Table Management

� One of the primary administrative tasks for a database is keeping tables healthy.

� Corruption can occur for reasons such as competing processes, power

failures, program bugs, etc.

� Some errors are immediately apparent, others may not appear until

the server is restarted.

� Ordinary use of MyISAM and ARCHIVE tables can leave them

fragmented.

� MySQL provides a variety of SQL tools for MyISAM, InnoDB, and ARCHIVE

table maintenance.

� Join performance can often be improved by running ANALYZE on the

table (not ARCHIVE).

� Performance may be improved after major updates to a table by running

OPTIMIZE.

� You can verify a table is without corruption by running CHECK TABLE.

� Fixing corrupted tables can be done by running REPAIR on the table

(not InnoDB).

� Running CHECKSUM against a table helps you verify the table is what

you think it is.

� Some functions MySQL performs on tables without intervention.

� It doesn't hurt to do manual checks.

Page 55: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 13 Administering a Database and Users

© 2008 ITCourseware, LLC Rev 1.1.1 Page 301

The syntax for running ANALYZE is:ANALYZE TABLE tbl_name [, tbl_name] ...

This command analyzes and stores the distribution of key values, which are used in joins.

The syntax for running OPTIMIZE is:OPTIMIZE TABLE tbl_name [, tbl_name] ...

Deleting large parts of a table or making many changes to variable-length rows can lead to table

fragmentation. If you believe MySQL hasn't cleaned up the table, run this command.

The syntax for running CHECK is:CHECK TABLE tbl_name [, tbl_name] ... [option] ...

The output will list all rows that need repair. The available options are:

FOR UPGRADE Tests whether the table is compatible with the current release.

The remaining options are for MyISAM only:

FAST Checks only tables not properly closed.

CHANGED FAST plus tables changed since the last check.

QUICK Doesn't scan the rows.

MEDIUM (Default) Scans rows for deleted links.

EXTENDED Performs a full key lookup. This should be run only if other options can't fix the

problem.

cron is sometimes used to run: CHECK TABLE tblname FAST QUICK.

Note that some problems reported can't be fixed automatically, e.g., if an error is found in a InnoDB table,

the server will shut down.

The syntax for running REPAIR is:REPAIR TABLE tbl_name [, tbl_name] ... [QUICK] [EXTENDED] [USE_FRM]

Problems noted by CHECK TABLE can usually be repaired with REPAIR, but most of these are

typically fixed automatically. Always make a backup of all table files before running this.

If the server dies during repair, you must do another repair as soon as server comes back up, before any

other operations. The output will list all rows that needed repair. If the last row doesn't say 'OK,' run

myisamchk --safe-recover, which performs operations REPAIR TABLE doesn't do.

The available options are:

QUICK Repairs the index tree only.

EXTENDED Builds the index one row at a time, rather than one index at a time.

USE_FRM Tries to repair the table when the .MYI file isn't available or corrupted. This option

should not be used on compressed data. This option should be used only if normalmodes don't work, as .MYI contains table metadata, which will be lost.

The syntax for running CHECKSUM is:

CHECKSUM TABLE tbl_name [, tbl_name] ... [ QUICK | EXTENDED ]

For MyISAM, specifying CHECKSUM=1 at table creation embeds a managed checksum in the table.

QUICK reports this value. Using EXTENDED causes the entire table to be read.

Page 56: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 302 Rev 1.1.1 © 2008 ITCourseware, LLC

Server Log Files

� The server generates four different logs.

� The error log records problems starting, running, or stopping the server.

� Errors will be directed to stderr if a logfile isn't available.

� By default, the logfile also contains warning messages.

� The general query log records all connections and communications with

clients.

� Statements are placed into the log in the order received, not

executed.

� The slow query log records queries that could be improved.

� The binary log records all statements that change or could potentially

change table data.

� Statements or transactions are placed in the log in the order they are

completed, but before any locks are released or COMMIT is run.

� This log is required for most data recovery operations.

� The utility mysqlbinlog is used to extract the SQL parts of the log.

� All logs are optional except for the error log on Windows, and must be

requested at server startup.

� Be default, all logfiles are written in the server's data directory.

� Maintenance of the logfiles is mostly not automatic, and must be planned for.

Page 57: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 13 Administering a Database and Users

© 2008 ITCourseware, LLC Rev 1.1.1 Page 303

The error log is enabled via the startup option --log-error[=filename], the default filename being:

host_name.err.

The general query log is enabled via the option --log[=filename], -l [filename], the default filename being:

host_name.log.

The slow query log is enabled via the option --log-slow-queries[=filename], the default filename being:

host_name-slow.log. Two additional types of information can be added to the log:

1. Queries that don't use indexes, via the option: --log-queries-not-using-indexes.

2. Slow admin statements, via the option: --log-slow-admin-statements.

The perl script mysqldumpslow is available to make reading this log easier.

The binary log is enabled via the option --log-bin[=basename], the default filename being: host_name-bin.

The server appends a numeric suffix to the filename.

Maintenance is mostly a manual procedure that can be written into a cron script. Linux RPMs provide a

mysql-log-rotate script. Note that on UNIX, you can mv the file while it's in use, but on Windows, you

must first stop the server.

Note that no script should be used without thinking through the consequences, particularly for the binary

logs.

Issuing the SQL command FLUSH LOGS causes the server to write all outstanding log information and

initiate the following logfile roll-over procedure.

1. The general query log and the slow query log are closed and reopened, wiping out all the data.

2. If a filename was provided when the old error log was created, the existing file is renamed with a

-old extension, and the original filename reused.

3. The existing binary logfile is closed and a new one opened, with the numeric suffix on the filename

bumped by one.

When the server is started, rules 1 and 3 apply, but the error logfile is reused. A maximum size can be

specified for the binary log at startup, and the file will be bumped automatically when it fills up.

Page 58: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 304 Rev 1.1.1 © 2008 ITCourseware, LLC

mysqladmin

� MySQL provides a general-purpose administrative tool: mysqladmin.

� Database files are accessed by mysqladmin through a server.

� All operations are specified on the command line.

� Command-line options are available to run operations multiple times.

� A variety of commands query the state of the server, the more important being:

� ping — Is the server alive? 0 if yes, 1 if no.

� status — A short server status message, including uptime and number of

threads, queries processed, slow queries, tables opened, open tables and

flush, refresh and reloads.

� processlist — All active server threads.

� version — Of the server.

� variables — Server variables and values.

� Other commands manipulate the state of the server, the more important being:

� flush-tables — Flushes all the tables.

� flush-logs — Closes and reopens all logs.

� refresh — Flushes all tables and close and reopen log files.

� reload — Reloads grant tables (can be spelled flush-privileges).

� shutdown — Shuts the server down.

� create dbname

Page 59: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 13 Administering a Database and Users

© 2008 ITCourseware, LLC Rev 1.1.1 Page 305

The syntax for running mysqladmin is:mysqladmin [options] command [command-arg] [command [command-arg]] ...

Running a command multiple times can be done using the option--count=N (-c N), which counts the

number of times to run the other commands. This must be accompanied by --sleep=delay (-i delay),

which dictates the seconds between iterations.

An extended version of the server status is available via the extended-status command. This command

adds server variables and values to the output of status. When this command is put in a loop, the

command-line option, --relative, (-r), can be used to report the differences from one iteration to the next.

$ mysqladmin version

mysqladmin Ver 8.41 Distrib 5.0.41, for Win32 on ia32

Copyright (C) 2000-2006 MySQL AB

This software comes with ABSOLUTELY NO WARRANTY. This is free software,

and you are welcome to modify and redistribute it under the GPL license

Server version 5.0.41-community-nt-log

Protocol version 10

Connection localhost via TCP/IP

TCP port 3308

Uptime: 10 days 23 hours 39 min 58 sec

Threads: 2 Questions: 18485 Slow queries: 0 Opens: 370

Flush tables: 1 Open tables: 0 Queries per second avg: 0.019

$ mysqladmin processlist

-----+------+----------------+-------+---------+------+-------+--------+

Id | User | Host | db | Command | Time | State | Info |

-----+------+----------------+-------+---------+------+-------+--------+

151 | ODBC | localhost:1105 | music | Sleep | 4 | | |

152 | ODBC | localhost:1106 | | Query | 0 | | show.. |

-----+------+----------------+-------+---------+------+-------+--------+

Page 60: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 306 Rev 1.1.1 © 2008 ITCourseware, LLC

Backup and Restore

� Making a usable backup always implies obtaining data that's not in the middle of

an update.

� If the server is running, this generally means the use of table locks for all

but InnoDB tables.

� A good procedure for obtaining a full backup of non-InnoDB tables from a

running server is as follows:

� Run LOCK TABLES, using only a READ lock.

� Run FLUSH TABLES, causing the server to sync to the disk.

� Choose a dump method based on what form is needed for the backup.

� SQL-level output can be generated with SELECT INTO

OUTFILE.

� The mysqldump utility will create a copy in various formats.

� A binary copy can be made using OS functionality, e.g., cp on

UNIX (typically not possible for InnoDB).

� A good procedure for InnoDB backups is easier because InnoDB doesn't

require locks when using mysqldump:

mysqldump --single-transaction --all-databases > dateandtime.sql

� Incremental backups for all table types are available via the server's binary log.

� To restore a backup, use the appropriate commands: LOAD DATA INFILE,

cp, etc., and apply the binary logs as necessary.

Page 61: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 13 Administering a Database and Users

© 2008 ITCourseware, LLC Rev 1.1.1 Page 307

Beware when making binary backups that the original files can be corrupt. Check first.

Making use of binary logs to recover to specific points-in-time involves several steps:

1. Start the server with --log-bin[=filename].

2. When you do a full backup, bump the log file in use using FLUSH LOGS.

3. Whenever you want an incremental backup (everything since the last full or incremental), FLUSH

LOGS.

Restoring tables using incremental backups involves applying the incremental logs to the last full backup:

1. Copy the full backup to the data directory.

2. Apply the incremental binary logs, in numeric order, using something similar to:

mysqlbinlog binlog.[0-9]* | mysql

Restoring (or omitting) specific parts of binary logs is possible. Options are available for times: --start-date

and --stop-date, and "positions": --start-position and --stop-position. You'll probably have to read the log

in an editor first to discover what points in time or positions are of interest.

In the case of a server or table crash, before deciding a MyISAM table has to be restored, you should try

to repair it using myisamchk. This requires that the table be disconnected from the server. Try the following

steps, in order, until all errors are fixed:

0. Make a copy of all available files for the corrupted table!

1. Repair only the index file: myisamchk -r -q tbl_name. The -q can be specified twice to correct the

data file if duplicate keys are found.

2. Attempt a simple repair of data and index: myisamchk -r tbl_name. This normally fixes everything

except unique keys that aren't unique.

3. Do a deep repair: myisamchk --safe-recover tbl_name. A much slower algorithm that reads all

the data.

4. You may be unable to run steps 1-3 if the table index is severely corrupted. If the .frm file is intact,

you can completely rebuild the index file: REPAIR TABLE tbl_name USE_FRM. Note that

meta-data in the original index file is lost.

5. If the .frm file is corrupt, but you have a backup, restore the old .frm file and go back to step 4. If

you don't have a backup but can reconstruct the table manually, do so in a separate database

directory, and copy that new .frm file back to the corrupt database. Go back to step 2.

The InnoDB engine automatically repairs InnoDB tables at server startup.

Page 62: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 308 Rev 1.1.1 © 2008 ITCourseware, LLC

Miscellaneous Functions

� The KILL command is used to terminate all or part of a thread.

� Every connection to the server runs as a separate thread, with an ID

known to the server.

� KILL sets a flag which is acted on at the next opportunity.

� If the client continues to run, it is notified that the last statement was

terminated.

� The FLUSH command adds to the functionality provided by mysqladmin

flush-xxx.

� MySQL supports one-way, asynchronous database replication

� A master server is used to drive multiple slave servers.

� Update commands are performed only on the master.

� The master's binary log data is passed to the slaves to keep them

up-to-date.

� 100+ SQL statements, command-line options, and server variables are

used to configure and manage replication.

Page 63: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 13 Administering a Database and Users

© 2008 ITCourseware, LLC Rev 1.1.1 Page 309

The syntax for KILL is:KILL [CONNECTION | QUERY] thread_id

To use KILL you must discover a thread's id, typically by using SHOW PROCESSLIST. Having the

PROCESS privilege allows you to see all processes. The SUPER privilege allows you to KILL any

process.

Both KILL CONNECTION (the default), and KILL QUERY terminate as soon as possible. What this

means depends on the statement currently being executed. Read (e.g., SELECT) and write (e.g.,

UPDATE) operations are done in blocks and processing will be terminated at the next block. Requests for

locks will fail immediately. Any INSERT DELAYED operation inserts everything in memory before

terminating.

In most cases, the client is provided with some type of notification that the statement didn't complete (a

NULL return value, etc.). However, killing REPAIR or OPTIMIZE on a MyISAM table will leave it

corrupt. This could leave updates in non-transactional tables partially finished.

In addition to using FLUSH for HOSTS, LOGS, PRIVILEGES, and STATUS, which work the same as

the similarly-named mysqladmin option, FLUSH can also be used for the following:

{TABLE | TABLES} [tbl_name [,tbl_name] ...] With no table names, closes all open tables. Also

clears the query cache.

TABLES WITH READ LOCK Uses a global read lock, so no new transactions,

etc. can get started. Closes all open tables and

locks all tables in all databases until UNLOCK

TABLES is issued.

USER_RESOURCES Resets all per-hour user resources.

To set up replication, it is necessary to provide a slave server with a copy of tables taken at the start of

binary logging. After that, the slave applies the updates passed to it from the master's binary log and applies

them. When a slave connects to the master, it says what position it has updated to, and is sent all ensuing

updates.

A master can have as many slaves as needed, and a slave can be a master to another slave in a chained

implementation.

There are several important benefits to replication:

� Robustness — You can switch to the slave if trouble arises on the master.

� Speed — Data queries can be spread around.

� Sysadmin issues — Backups can be made from a slave so as to not interfere with master processing.

Page 64: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 310 Rev 1.1.1 © 2008 ITCourseware, LLC

User Account Management

� MySQL passwords have nothing to do with OS passwords.

� MySQL uses its own encryption algorithm: PASSWORD().

� Creating new accounts is typically done with:

CREATE USER accountname [IDENTIFIED BY [PASSWORD] 'password']

� An error occurs if the account already exists.

� CREATE USER creates the account with no privileges.

� A MySQL account is uniquely identified by a username and the host they connect

from: username@hostname.

� Usernames can be up to sixteen characters, single-quoted as necessary.

� Wildcards aren't allowed in usernames (anonymous users are '').

� Wildcards (_ and %) are acceptable in the hostname (e.g.,user@%.abc.com), single-quoted as necessary.

� Specifying a username without a host implies ‘username@%’.

� Removing a user is typically done with: DROP USER accountname.

� This command drops both the user and all associated privileges, butdoesn't close any open sessions or remove any database objects created bythe user.

� A user can be given a new name with: RENAME USER old_accountname TO

new_accountname.

� An error occurs if the old account doesn't exist, or the new account does.

� RENAME does not automatically migrate any privileges to the new user

name.

Page 65: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 13 Administering a Database and Users

© 2008 ITCourseware, LLC Rev 1.1.1 Page 311

When logging in to the server, MySQL programs will use your OS login name if a --user option isn't

provided.

Usernames and passwords are verified in a case-sensitive manner. Hostnames are not.

When running CREATE USER, if a password is provided, and the keyword PASSWORD is used, the

value must come from the PASSWORD() function. If PASSWORD isn't used, specify a plain text value.

A password can also be assigned to an account using any of the following:

SET PASSWORD [FOR user] = PASSWORD('newpassword'); The password is specified as

literal text. A privileged user can

specify a password for a

different user by adding: FOR

'user'@'%'.

mysqladmin -u user -h host password "newpassword" Literal text

GRANT USAGE ON *.* TO 'user'@'%' IDENTIFIED BY 'newpassword'; Literal text

Passwords should be protected. Letting the client ask you for a password is acceptable, but isn't suitable for

scripts. Some suggestions for keeping your password secure:

1. Don't put your password on the command line, as it can be seen by commands like ps. MySQL

clients attempt to overcome this by overwriting the command-line args with zeros during

initialization, but there's still a moment when the original text is visible.

2. Putting your password in an option file is good, but remember file access permissions; should be r

or rw for your account only.

3. Don't put it in the MYSQL_PWD env var, because commands like ps can display environment

variables.

4. The user grant table should never be open to non-administrative accounts. In fact, the old way of

managing user accounts and privileges was to directly manipulate the server's admin tables. This is

still possible.

Page 66: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 312 Rev 1.1.1 © 2008 ITCourseware, LLC

Understanding User Privileges

� The MySQL privilege system is intended to first authenticate a user session and

then apply a set of privileges to all user requests.

� Privileges are stored in six MyISAM tables in the mysql database, and provide

for protection down to the column level.

� The user table determines connections and privileges for all databases.

� The db table determines which users from which hosts can access which

databases.

� Used in conjunction with the db table, the host table allows a user to

connect from multiple hosts.

� The tables_priv and columns_priv tables are similar to the db table, but

more fine-grained.

� The procs_priv table provides privileges to individual functions and

procedures

� The scope of each row in all of the tables is based on hostname, username, and

database except for the user table, which contains passwords instead of

database names.

� The server sorts the contents of each grant table, putting the most specific

entries first.

� When multiple matches are possible, the server uses the first entry that is

applicable.

� At connection time, the requested username and requesting host are used to

lookup a password, which must match.

Page 67: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 13 Administering a Database and Users

© 2008 ITCourseware, LLC Rev 1.1.1 Page 313

When the server sorts the grant tables, the sort keys for each table are:

� user — host, and then username.

� db — host, db, and user.

� host — host and db.

� xxx_priv — host, db, and user.

This can result in some unexpected situations. Suppose that the user table contained the following:+----------+-------+-

| Host | User | ...

+----------+-------+-

| % | dave | ...

| abc.com | | ...

+----------+-------+-

The sort would place the abc.com row ahead of '%'. A connection by dave from abc.com is matched by

the anonymous user row above, whereas a connection by dave from xyz.com is matched by the row with

his name. The implication is you may be authenticated as an account different from what you logged in as.

SELECT CURRENT_USER() to see who you are.

The privileges allowed for any command are approximately a logical combination of privileges at each level.

For example, for a column:

global privileges OR (database privileges AND host privileges) OR table privileges OR column privileges.

A couple of extra rules apply to this computation:

1. If at any stage the privileges are granted, the process short-circuits and allows the command.

2. The user must have a record for the database in question in the db table or the request is denied.

3. The user must have a record for the host/user/db combination, either in the db table or the host

table or the request is denied. Lookups into the host table are made only if the host field in the db

table is blank. Note that computing the AND of db and host tables allows general privileges to be

set in the db table and overruled in the host table.

The OR operations allows commands that require multiple privileges to get them from different places.

The host table is sometimes used to indicate hosts that are not secure:+--------------------+----+-

| Host | Db | ...

+--------------------+----+-

| public.your.domain | % | ... (all privileges set to 'N')

| %.your.domain | % | ... (all privileges set to 'Y')

+--------------------+----+-

Page 68: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 314 Rev 1.1.1 © 2008 ITCourseware, LLC

User Account Rights Management

� GRANT manages user account privileges:

GRANT priv_type [(column_list)] [, priv_type [(column_list)]] ...

ON [object_type] {tbl_name | * | *.* | db_name.*}

TO account [IDENTIFIED BY [PASSWORD] 'password']

[WITH with_option [with_option] ...]

� Privileges are granted at various levels, indicating how much of the

database is being addressed.

� The use of '*' wildcards and the optional clauses determine the

level for the privilege.

� The account is specified as user@host.

� The wildcards _ and % are allowed in hostnames.

� Limited netmask values can be specified when the host is specified

as an IP address.

� REVOKE removes user privileges:

REVOKE priv_type [(column_list)] [, priv_type [(column_list)]] ...

ON [object_type] {tbl_name | * | *.* | db_name.*}

FROM user [, user] ...

� The server reloads the grant tables under the following conditions:

� Whenever GRANT, REVOKE, or SET PASSWORD is run.

� Whenever FLUSH PRIVILEGES or mysqladmin equivalent is run.

� After changing privileges, you should always SHOW GRANTS to verify what

you did.

Page 69: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 13 Administering a Database and Users

© 2008 ITCourseware, LLC Rev 1.1.1 Page 315

The possible values for object_type are TABLE, FUNCTION, or PROCEDURE. If you don't specify

one, MySQL tries to figure it out from the other arguments.

The levels at which privileges can be granted are the:

1. Global level, implying for all databases, e.g., GRANT ALL ON *.*. These privileges are stored in

the mysql user table.

2. Database level, for all objects in a database, e.g., GRANT ALL ON dbname.*. These privileges

are stored in the db table.

3. Table level, for all columns in a table, e.g, GRANT ALL ON dbname.tblname. These privileges

are stored in tables_priv.

4. Column level, identified by column names in the GRANT command. These privileges are stored in

columns_priv.

5. Routine level, identified by the granting of certain privileges. These privileges can be granted at the

global and database level, and are stored in procs_priv.

Note that database names can also use _ and % wildcards when working at the global or database level.

Database and table names are verified in a case sensitive manner. Column names and routine names are not.

Beware that specifying 'ON *' will apply the privileges to the default database, or if there isn't one, apply

them globally.

Privileges can be granted for objects that don't exist yet, allowing accounts to be pre-configured.

When a host is specified as an IP number, a netmask can also be specified indicating how many address bits

to use for the network number. For example:GRANT ALL ON db.* TO david@'192.58.197.0/255.255.255.0';

This allows david to connect from any client host for which the following condition is true:

client_ip & netmask = host_ip.

Note that netmask values can only be 0 or 255.

After the server reloads the grant tables, revised privileges take effect as follows:

1. Global privileges and passwords at the next connect.

2. Database privileges at the next USE.

3. Table and column privileges at the next client request (perhaps in the middle of a session).

When you drop a table or database, the privilege table entries remain. Dropping a user also removes their

privileges.

An account can't GRANT or REVOKE rights it doesn't have itself.

Page 70: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 316 Rev 1.1.1 © 2008 ITCourseware, LLC

User Account Privileges

� The with_option clause of the GRANT statement provides for resource

limitations on a user account.

� The rate a user can connect can be set using

MAX_CONNECTIONS_PER_HOUR count.

� The rate a user can ask questions can be set using

MAX_QUERIES_PER_HOUR count.

� The rate a user can make changes can be set using

MAX_UPDATES_PER_HOUR count.

� The number of simultaneous connections a user can make can be set

using MAX_USER_CONNECTIONS count.

� Setting any count to 0 implies no limit.

� All MAX_xxx counts are per account, and stored in server memory.

� FLUSH USER_RESOURCES or FLUSH PRIVILEGES releases all current

counts, letting all current sessions start counting from zero.

� Some things you can't do using the MySQL privilege system:

� Deny access to a particular user.

� Deny creating or dropping a database if creating and dropping tables is

acceptable.

� Assign passwords to databases, tables, or routines.

Page 71: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 13 Administering a Database and Users

© 2008 ITCourseware, LLC Rev 1.1.1 Page 317

The privileges GRANT and REVOKE can refer to are the following.

Some notes:� Rows with + are the only privileges that can be used on columns.� Rows with – are the only privileges that can be used on stored routines� Rows with * must be granted globally (ON *.*)The SELECT privilege is required only if table rows are selected.

Two pseudo-privileges are also available:1. 'ALL [PRIVILEGES]' to refer to all the privileges (except GRANT OPTION) at the level you're

assigning to (global, database, table, column, routine).2. USAGE, which is a synonym for 'no privileges.' This is useful when you want to use GRANT for

its side effects:GRANT USAGE ON *.* TO 'dave’@’%' WITH MAX_QUERIES_PER_HOUR 100.

Don't give out more privileges than necessary. Normal users probably don't need FILE, GRANT,SHUTDOWN, PROCESS, or SUPER. In addition, granting privileges at the global level to non-adminusers is dangerous, e.g., ALTER, as they could be applied to the mysql database.

CREATE Databases, tables, and indexes

DROP Databases and tables

DELETE Tables

ALTER Tables

INDEX CREATE and DROP index from existing table

CREATE VIEW Views

SHOW VIEW SHOW CREATE VIEW

+ SELECT Tables

+ INSERT Tables (and table maintenance)

+ UPDATE Tables

- GRANT OPTION Enables privleges to be granted

CREATE TEMPORARY TABLES CREATE TEMPORARY TABLE

LOCK TABLES For tables for which SELECT is granted

CREATE USER CREATE, DROP, and RENAME USER

* FILE Create and use files on the server host

* PROCESS Processlist (mysqladmin or SHOW)

* RELOAD mysqladmin flush-xxx, refresh, reload

* SHOW DATABASES For more than your own databases

* SHUTDOWN mysqladmin shutdown

* SUPER KILL and SET GLOBAL

- CREATE ROUTINE Stored routines

- ALTER ROUTINE Stored routines can be altered and dropped

- EXECUTE Stored routines

Page 72: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 318 Rev 1.1.1 © 2008 ITCourseware, LLC

Managing Access to the Database Environment

� Many things can go wrong when a user is trying to access a database, loosely

categorized as:

� Problems related to the server and connections to it.

� Problems with option files.

� Problems with the grant tables.

� Failures produce messages, which should be read carefully.

� Managing a secure server environment involves many concerns, loosely

categorized as:

� Problems related to the server's connections to the internet.

� Password issues.

� The management of rights, both OS and internal.

� Dealing with miscreant users.

� Programmed interfaces.

� MySQL supports the use of SSL connections to the server.

� It's an optional feature and must be compiled in.

� It can be used on a per-connection basis, or be required all the time.

Page 73: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 13 Administering a Database and Users

© 2008 ITCourseware, LLC Rev 1.1.1 Page 319

Inability to connect to the server can be due to multiple causes, for example:

A. The server isn't running.

B. The server isn't configured for your type of connection (OS commands like netstat may help).

C. If no host is specified in the connection request from mysql clients, localhost is used by default. Is

there a localhost <-> 127.0.0.1 disconnect? Try the IP address.

D. If logging in locally without a hostname works but specifying it doesn't, system name resolution is

probably returning a fully qualified value, typically not what you have in the host table.

If you suspect options in an option file are causing the problem, try running without them using the

--no-defaults option.

Permissions issues manifest themselves in many ways. Some examples of things to check:

A. If you've run an upgrade, make sure you've run mysql_upgrade.

B. Was a changed password specified correctly? Look at the user table to see if it's readable (it

shouldn't be).

C. Is there an entry in the user table for who you're trying to connect as?

D. Default permissions include ''@localhost, which may be getting in the way. Try deleting all host

values that contain % or _.

E. If the root account isn't accessible, start the server with --skip-grant-tables and add one or fix the

one that's there.

Security issues related to the server's connection to the internet come in many forms. Two suggestions:

A. Invest in a firewall.

B. Don't transmit unencrypted data over the internet. Use SSL instead.

Passwords can be mishandled in lots of ways. Two suggestions:

A. Ensure every user has one.

B. Don't store plaintext passwords in any database table.

Permissions are perhaps the most commonly mishandled feature. Some suggestions:

A. Run the test: mysql -u root. Does root have a password?

B. Never grant rights to % hosts.

C. Never run the server as root. Set up a separate account for it, e.g., mysql.

D. Ensure the data directory is visible to the server account only.

Issues related to database users come in many forms. Two suggestions:

A. Don't trust any data entered by a user. Validate everything.

B. If data from a web form is being accepted, try entering bogus data.

APIs often provide too much flexibility. Two suggestions:

A. Check the size of data before passing it to MySQL

B. Have your application connect to MySQL using its own username, not yours.

Page 74: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 320 Rev 1.1.1 © 2008 ITCourseware, LLC

Labs

� Change to the mysql database. Display the Host, User, and Password columns of the user table.

(Solution: users.txt)

� Add a new user using the CREATE USER command, without specifying any host for the user.

Examining the user table, what host value was assumed?

(Solution: newuser.txt)

� Partner with another student. Find the network address or hostname of your partner's machine, and

try to log in to their mysql server from your machine, using the new account just created. Can you

do this?

(Solution: remote.txt)

Page 75: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 13 Administering a Database and Users

© 2008 ITCourseware, LLC Rev 1.1.1 Page 321

Page 76: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 16 MySQL Programming Interfaces

© 2008 ITCourseware, LLC Rev 1.1.1 Page 371

Chapter 16 - MySQL Programming Interfaces

Objectives

� Connect MySQL to external programming

interfaces.

Page 77: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 372 Rev 1.1.1 © 2008 ITCourseware, LLC

Database Application Architectures

� Three-tier architecture provides advantages over traditional client/server.

� Thin-clients and simplified servers can be split off from a middle tier.

� The middle tier is the repository for the majority of the business logic.

� The middle tier typically includes a web server, scripts/programs, and a

script processor.

� Most application logic is at this level, including user authentication,

session management, browser request translation, etc.

� Organized support is available from MySQL for two types of middle tier

applications.

� Pure APIs, with which a program could be written to interface between a

web browser and a database server.

� APIs exists for C, C++, Perl, PHP, Python, Eiffel, and Tcl.

� MySQL doesn't force any framework on an object-oriented

programming environment.

� MySQL Connectors connect other database technology to a MySQL

server.

� Connectors exist for working with ODBC, JDBC, .NET, and Visual

Studio.

� Most Connectors go through the C API to get their work done.

� MySQL also supports the use of an embedded server in a C or C++ application.

Page 78: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 16 MySQL Programming Interfaces

© 2008 ITCourseware, LLC Rev 1.1.1 Page 373

The traditional client/server model forced too much processing into one end or the other. The three-tier

model provided for a better distribution of processing requirements. In addition to potentially spreading out

the CPU load, this also provided for more focused programming efforts.

A large percentage of recent development in the three-tier architecture has occured in an open-source

environment. The most popular of these is sometimes referred to as LAMP, in which 'L' refers to Linux, 'A'

to Apache, 'M' to MySQL, and 'P' to PHP, Perl, or Python.

The C API was developed by and is supported by MySQL. Most of the distributed clients are written in C,

and use this API, providing for some measure of standardization. For example, response to environment

variable settings, communication buffer sizes, etc. all is handled by the C API.

The C API is extensive, with mapping of all datatypes, over 70 available functions, access to prepared

statements, etc.

Using any object-oriented language with a database presents particular problems regarding object/relational

modeling. A possible framework might include the following rules:

1. Each class corresponds to a table.

2. Primitive datatypes used for object fields map to table columns.

3. An instance maps to a table row.

4. Inheritance is mapped through foreign keys.

Arguably the most important feature of such an application is to automatically build objects from the

available data, so that the application doesn't have to directly deal with the database. This would imply some

ongoing processing in the application to map some object concepts. For example:

5. Multiple inheritance (many-to-many) should map to the use of JOIN conditions.

Page 79: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 374 Rev 1.1.1 © 2008 ITCourseware, LLC

Connecting MySQL to ODBC

� MySQL provides an ODBC Connector for Windows and UNIX.

� Connector/ODBC is based on ODBC 3.5x, level 1.

� UNIX usage is often utilized to allow Windows applications to access a

UNIX-based database server.

� Connector/ODBC has been tested with 35+ applications.

� Many Microsoft applications use ODBC, for example, Access, Word,

and Excel.

� Connector/ODBC translates as necessary ODBC calls from the application into

SQL calls for the database server.

� An application using ODBC requires several additional component technologies.

� Microsoft provides what is needed on Windows.

� UNIX-based systems typically require downloads from other websites.

� Installing Connector/ODBC is straightforward, similar to the installation of

MySQL.

� Available systems are the same as for MySQL itself.

� Configuring Connector/ODBC involves several steps outside the Connector/

ODBC code.

� A Data Source Name (DSN) must be established.

� An account for each DSN username must be set up inside the

server.

Page 80: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 16 MySQL Programming Interfaces

© 2008 ITCourseware, LLC Rev 1.1.1 Page 375

ODBC was developed from specifications of the SQL Access Group, with function calls, error codes, and

data types specified. It is based on the Call-Level Interface (CLI) specifications from X/Open and ISO/IEC

for database APIs. See http://support.microsoft.com/kb/110093 for more details.

An application that uses the Connector/ODBC API to access the server also makes use of a DSN and a

Driver Manager.

A DSN is a string that identifies a driver (e.g., Connector/ODBC), database, host, and possibly

authentication information.

The Driver Manager is a library that resolves a DSN and loads the called-for driver. All ODBC function

calls made by the application are made using the DSN, which are translated by the Driver Manager to the

appropriate database. Windows provides an ODBC driver manager (odbc32.dll; www.microsoft.com/

data). For UNIX, there are two possibilities: unixODBC (libodbc.so; www.unixodbc.org) or iODBC

(libiodbc.so; www.iodbc.org).

MySQL provides binary and source downloads of Connector/ODBC from dev.mysql.com/downloads.

Installation steps on Windows:

1. Verify MDAC components are good to go. See www.microsoft.com/data for more info.

2. Choose a distribution format to download from dev.mysql.com. The easiest is the MSI installer

package, which relies on the standard Windows installer.

3. Choose an installation type in the installer. The easiest is Typical.

Installation steps on Linux:

1. Choose a distribution format to download from dev.mysql.com. The easiest is RPM.

2. rpm -ivh mysql-connector-odbc-3.51.12.i386.rpm # -Uvh if updating

There is also a tarball format available.

Following installation on either machine type, a DSN must be configured. A DSN can be of three types:

1. A System DSN, available to all users and all applications.

2. A User DSN, available to a particular user

3. A File DSN, stored in a disk file, and available to all users and applications. This option allows

DSN access from a different machine.

On Windows, a DSN can be configured using the ODBC Data Source Administrator, available via the

control panels. Buttons are available for specifying connection information, and 20+ advanced features. Use

the Test and Diagnostics buttons to verify the connection works.

On UNIX, a DSN can be manually configured directly into /usr/local/etc/odbc.ini. There is also a GUI

configuration tool available at www.unixodbc.org/config.html.

Assuming a DSN specifies connecting to adb on the server as auser from amach, using apassword,

privileges should be granted using:GRANT ALL ON adb.* to 'auser'@'amach' IDENTIFIED BY 'apassword';

Page 81: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 376 Rev 1.1.1 © 2008 ITCourseware, LLC

Connecting MySQL to MS/Office and MS/

Access

� Connector/ODBC is well tested with MS/Office and MS/Access.

� A Word application can read a MySQL database for such things as

mailmerge or for table data.

� An Excel application can read a MySQL database for things such as

bringing data directly into a worksheet.

� An Access application can import, export, or directly link into MySQL

databases, allowing updates.

� Importing data from MySQL into MS/Access is straightforward, going through

the Access import procedure.

� Exporting data from MS/Access to MySQL is also straightforward, going

through the Access export procedure.

� Linking directly to MySQL data from MS/Access includes several maintenance

steps.

� Links can go bad with no notification.

� Accessing MySQL data from Excel and Word requires using Microsoft Query

to build a SQL command.

� Microsoft Query is an optional feature and may have to be installed.

� A variety of known limitations exist.

Page 82: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 16 MySQL Programming Interfaces

© 2008 ITCourseware, LLC Rev 1.1.1 Page 377

To import MySQL data into Access, open a database in Access and on the File menu, select Get External

Data/Import. In the Import dialog box, choose Files of Type ODBC Databases, which will list the

available DSNs. Selecting a DSN that requires logging in will allow you to enter username and password.

Choose the tables you want to import and click OK.

To export Access data into MySQL, select the table or query you want to export and on the File menu,

select Export. In the Export Object Type dialog box, enter a name, choose Files of Type ODBC

Databases, and click OK. From the dialog box that pops up, select the Connector/ODBC DSN you

want.

To link directly to MySQL data from Access, open a database in Access and on the File menu, select Get

External Data/Link Tables. In the dialog box, choose File of Type ODBC Databases. Choose a

DSN, and in the Link Tables dialog, select one or more tables. To verify links, open the database in

Access, and on the Tools menu, roll off on Add-ins (or Database Utilities) and click Linked Table

Manager. Select the tables you want to verify and click OK. Access will complain if a linked table isn't

found and allow you to Select New Location of. In the Add-ins dialog you can also check Always

Prompt For A New Location.

Some of the more important limitations and suggestions when using MS/Access:

1. Turn on the Connector/ODBC option to Return matching rows.

2. Any table you want to access should have a PRIMARY key.

3. Any table that you want to update should have a TIMESTAMP column in it.

4. Very big tables may fail with 'ODBC Query Failed.' Fix this by ensuring the advanced options include

bit settings for 10 ('Return Matching Rows'(2) + 'Allow BIG Results'(8)).

There are a variety of recommendations regarding data types BIGINT, BLOB, TIMESTAMP, DATE,

BYTE, and DOUBLE, for example:

5. Don't use SINGLE precision float fields (DOUBLE is okay).

To import MySQL data into Excel, open a worksheet, and on the Data menu, select Import External

Data/New Database Query. In the dialog, choose a DSN. In the Query Wizard, choose the columns of

interest and click NEXT. Choose Filter options (WHERE data), and Sort Order (ORDER BY). Data can

be returned immediately, or kept in Microsoft Query to help refine the query. Essentially the same procedure

is used in Word, except the process begins from the Database toolbar.

Page 83: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 378 Rev 1.1.1 © 2008 ITCourseware, LLC

Connecting to MySQL from Perl

� Perl is a free, open-source, general-purpose programming language.

� Perl can work with most OSes.

� Perl provides for writing code in object-oriented or procedural style.

� The perl community has made over 12,000 libraries and applications

freely available.

� Over 800 of those packages are related to MySQL use.

� Perl was utilized extensively in the early days of CGI programming.

� Over time, several important changes were made to the standard CGI

processing flow to improve efficiency.

� Freely-available perl libraries provide access to a variety of database

architectures.

� The perl approach to database programming is to separate what is written in the

program from what the database understands.

� This approach is intended to simplify changing database vendors as

desired.

� Perl documentation in general is good, and available online.

� The two most commonly used sites for information about perl are

www.perl.com and www.perl.org.

Page 84: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 16 MySQL Programming Interfaces

© 2008 ITCourseware, LLC Rev 1.1.1 Page 379

The repository for contributed perl code is CPAN, the Comprehensive Perl Archive Network:

www.cpan.org. A variety of categorization and search tools are available there to help navigate through the

available modules.

Originally, one of the bigger problems when writing web applications was generating HTML that a web-

browser would understand. The CGI.pm module available on CPAN has the rules for writing HTML

embedded in it. A program that uses this module writes to a more abstract API that, in turn, is translated into

real HTML.

A URL that references a perl CGI script originally caused the web server to invoke the perl interpreter,

which parsed the script and ran it. The more modern approach is to embed the perl interpreter and a

compiled version of each script used in the web server. All incoming requests are then processed much

more efficiently. This approach is commonly used for Apache. The module used in Apache that provides for

this functionality is mod_perl.

Perl has become a popular tool for programming database access outside of web applications.

Perl separates database access into a DBI (Database Independent) part and a DBD (Database Dependent)

part. A programmer typically learns just the DBI part. Calls to DBI are translated as necessary and passed

to the DBD part, which in turn passes requests to its associated database server. Changing database

vendors is supposed to be made easier with this approach, as the DBI part shouldn't change much, if at all.

However, if you anticipate ever switching vendors, beware of writing an application that uses SQL specific

to a particular vendor.

The DBI code is available on CPAN. General information about DBI can be found at dbi.perl.org.

The DBD code for MySQL is DBD::mysql, and is available from CPAN.

Once installed, information for DBI and DBD can be obtained locally using one of the perl documentation

tools:

� perldoc DBI

� perldoc DBI::FAQ

� perldoc DBD::mysql

Page 85: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 380 Rev 1.1.1 © 2008 ITCourseware, LLC

Programming Perl to MySQL

� Perl provides typical programming constructs.

� Variables can be of only a few types, and can be changed on the fly.

� Scalar variables can be integer, float, and string.

� Compound variables can be arrays or objects.

� Conditional tests are available using several varieties of if.

� Loop constructs include for, foreach, and several varieties of while.

� Database access is most commonly written using an object-oriented style.

� This provides simplified access to the methods and attributes of the

objects involved.

� No automatic attempt is made at object/relational modeling.

� SQL statements are typically handed over to DBI/DBD for verification and

encoding before they can be run.

� This approach provides for SQL prepared statements.

� Data retrieval can be one row at a time, or complete tables, into a multi-

dimensional array.

� By default, DBI runs in autocommit mode, but this can be changed as desired.

Page 86: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 16 MySQL Programming Interfaces

© 2008 ITCourseware, LLC Rev 1.1.1 Page 381

Initially, the perl code must request the include of the DBI code:use DBI;

Connecting to a database is then written similar to the following:$dbh=DBI->connect("DBI:mysql:host=localhost;database=dbname","username","pwd")

or die("Connection failure (errnum: $DBI::err):$DBI::errstr\n");

Notice a connection handle is returned. The first argument to connect specifies the type of database being

requested ('mysql' in the above). It is this argument that loads the DBD::mysql code. The two variables in

the die message are taken from the DBI package, in the DBI code.

Queries for the database are typically first prepared by embedding them in a function call:$sth = $dbh->prepare("SELECT * FROM t1");

Notice a statement handle (an object) is returned, or undef/false if the prepare fails. Running the query:$sth->execute();

returns true or false (for errors), and makes the returned data available to other functions via the statement

handle. If the query modified the table, the return value will be the number of rows affected ('0E0' if none),

or 0 (false) if an error.

The SQL handed over to prepare can contain SQL placeholders:$sth = $dbh->prepare("SELECT * FROM t1 WHERE c1=?");

for which data must be supplied in the execute:$sth->execute('abc');

Retrieving rows returned from an execute() can be done using:while (@row = $sth->fetchrow_array) {

print "@row"; # prints all column values, from left to right

}

or using:while ($ref = $sth->fetchrow_hashref) {

print "@ref->{c1,c2}"; # values from requested columns

}

Either of these will read BLOB and other big columns as undef. This behavior can be changed using:$dbh->{LongReadLen} = 1234; # that many bytes will be read

$dbh->{LongTruncOk} = 1; # prevents error conditions on short reads

Note this must be done before running prepare().

Changing commit behavior can be done in the SQL or by setting attributes of the connection:$dbh->{AutoCommit} = 0;

after which it's up to the code to run the following as desired:$dbh->commit();

$dbh->rollback();

Both statement handles and connection handles can and should be released:$sth->finish();

$dbh->disconnect();

Page 87: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 382 Rev 1.1.1 © 2008 ITCourseware, LLC

Connecting to MySQL from PHP

� Providing a database interface in a web environment presents many unique

problems.

� Bookmarking and returning to pages later needs to be reproducible but

not force multiple updates.

� Reloading or refreshing pages should not force multiple updates.

� Giving up midway through an interaction should not leave the database in

an undesirable state.

� Accidental damage or malicious attacks shouldn't be facilitated by data

input procedures.

� PHP is a free, open-source, server-side, HTML-embedded scripting language

for creating dynamic WEB pages.

� PHP is an acronym for "HyperText Preprocessor," that is, PHP code

embedded in a web page is converted into HTML at an early stage.

� PHP can work with most OSes and most web servers, but is commonly

used with Apache.

� PHP can also be used for standalone programs.

� The more recent versions of PHP provide for writing code in object-

oriented style.

� The PHP website provides the code for all supported OSes, but MySQL states

a better version for Windows is available on the MySQL website.

� The mysqli code is built for PHP 5.2 and MySQL 4.1.1 and later.

� It supports the latest authentication protocol and prepared

statements.

Page 88: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 16 MySQL Programming Interfaces

© 2008 ITCourseware, LLC Rev 1.1.1 Page 383

Because HTTP is a stateless protocol, every request made by a web browser must include everything

needed to answer that request. A typical problem that arises is if a request is made to refresh a page, and

the page was updating a database. What should happen?

For the most part, raw HTML, a simple markup language, is what a browser understands. To provide for

more power in the generation of web pages, various technologies have been embedded in standard HTML,

which the server typically has to make sense of. PHP is yet another such markup language. The embedded

commands are processed by the server and used to generate the raw HTML that's sent back to the

browser.

The PHP language provides for general programming tasks, and as such, can connect to a database server

such as mysqld. The most common problems encountered are processing expectations supplied with PHP.

The error "Maximum Execution Time Exceeded" indicates PHP believes it's taking too long to process a

query. Fix this in php.ini, bumping the maximum execution time up from 30 seconds to something higher.

You should probably double the allowed RAM too.

The PHP website provides general information about their interface to MySQL at php.net/mysqli.

The MySQL version of the PHP code is based on the latest bug fixes, etc. but otherwise contains the same

PHP functionality as the version on the PHP site (an older version). Additional details are available at

dev.mysql.com/downloads/connector/php.

The steps to install MySQL's PHP mysqli code on a Windows machine:

1. Unpack the downloaded archive.

2. Copy the php_*.dll to the PHP extension directory. The extension directory is specified in php.ini

(extension_dir).

3. Enable the extension in php.ini: extension=php_mysqli.dll.

4. Copy libmysql.dll to the PHP installation directory.

Installing the mysqli code on a UNIX box requires building the PHP source code, specifically asking for

MySQL support.

Page 89: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

MySQL Administration and Development

Page 384 Rev 1.1.1 © 2008 ITCourseware, LLC

Programming PHP to MySQL

� A web page that requires PHP processing typically has a .php suffix.

� PHP markup in an HTML document is embedded in <?php and ?> tags.

� PHP looks much like Perl, for example $x is a scalar, blocks are written in {},

etc.

� PHP provides typical programming constructs.

� Variables can be of only a few types, and can be changed on the fly.

� Scalar variables can be integer, float, Boolean, and string.

� Compound variables can be arrays or objects.

� Conditional tests are available using if and switch.

� Loop constructs include for, while, do/while, and foreach.

� Two specially named arrays are available for extracting form data.

� PHP is popular in part because it provides a large function library.

� Various functions target use of the MySQL server.

� Functionality is available for supporting user sessions.

� Several functions target securing use of system resources and the MySQL

server.

Page 90: MySQL Administration and DevelopmentPage vi Rev 1.1.1 © 2008 I TCourseware, LLC MySQL Administration and Development Chapter 10 - Advanced SQL Techniques ..... 213 MySQL Pattern Matching

Chapter 16 MySQL Programming Interfaces

© 2008 ITCourseware, LLC Rev 1.1.1 Page 385

Connecting to a server and database might look likeif (!($connection = mysqli_connect("localhost", "username","pwd","dbname")))

die("Cannot connect");

Notice a connection handle is returned.

Querying the database is done by embedding normal MySQL queries in a function call:if (!($result = mysqli_query($connection, "SELECT * FROM t1")))

die("Couldn't run query");

Notice a result handle will be returned for access queries (e.g., SELECT, SHOW, DESCRIBE). For

modification queries (e.g., UPDATE, INSERT, DELETE), true (success) or false is returned.

After running a SELECT, you can determine the number of rows returned using:$num = mysqli_num_rows(result_handle)

This doesn't work for queries that modify the database.

For queries that modify a table, you can retrieve the number of rows affected using:$num = mysqli_affected_rows(connection_handle)

If no rows were affected, 0 will be returned. If an error occurs, -1 will be returned. Every row deleted for a

REPLACE operation will count 2.

Retrieving a row of data from a result handle is done using:array mysqli_fetch_array(result_handle [,MYSQL_NUM|MYSQL_ASSOC])

By default, this call returns each column twice, once indexed numerically, and once associatively, keyed by

the column name. You can request only one form with the second argument. When there are no more rows,

the call returns false. Accessing the column data numerically might look like:$row = mysqli_fetch_array(result);

$columns=mysqli_num_fields(result);

for ($i=0;$i<$columns;$i++)

print "$row[$i]";

Columns can also be retrieved from a result set row using the SELECT arguments as associative array

keys:if (!($result = mysqli_query($connection, "SELECT COUNT(*) FROM t1")))

die("Couldn't run query");

$row = mysqli_fetch_array(result);

print "{$row["COUNT(*)"]}";

Two specially named associative arrays are available for accessing form input:$_GET["fieldname"]

$_POST["fieldname"]

which access data originating from the similarly named submission mechanisms.

User session are managed using:session_start()

which either activates an existing session (based on a cookie by default) or starts a new one. Session

variables are maintained on the server. Sessions should be terminated when you're done:session_destroy()