Tải bản đầy đủ (.pdf) (725 trang)

John wiley sons mysql bible

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (6.29 MB, 725 trang )

4932-4 Cover 5/20/02 10:56 AM Page 1
Perfect Bind • Trim: 7 3/8 x 9 1/4 •
• 4 color process • Yellow prints 110y 15m • + spot varnish (see spot varnish pdf) • Matte laminate

If MySQL can do it, you can do it too . . .

Inside, you’ll find complete coverage of MySQL
• Install MySQL on a Linux, Windows, or Mac system
• Get the scoop on database design
• Take control of MySQL using the command line interface
• Configure MySQL for any sized system and implement security
• Debug databases and servers and tune performance
• Build applications using Perl, PHP, and ODBC
• Find out how to handle MySQL replication

Configure powerful custom
options for administrators and users

• Integrate Linux services with PAM-MySQL/NSS-MySQL
• Harness NuSphere enhancements to MySQL

Bonus CD-ROM





Sample code from the book
Perl DBI
PHP and phpMyAdmin
Apache



Fine-tune
control of
data entry
and tailor
special
reports for
your needs

PC with a Pentium processor running
120 MHz or faster. Linux/Unix or Windows
9X, NT, XP, 2000, Macintosh running Mac OSX.
See About the CD appendix for details.

*85 5 -BAHGIh

$49.99 USA
$74.99 Canada
£38.99 UK incl. VAT

Reader Level:

Shelving Category:

Beginning to Advanced

Database

— Jeremy Zawodny, Senior Editor of Linux Magazine
and the MySQL database expert at Yahoo! Finance


ONE HUNDRED PERCENT

COMPREHENSIVE
AUTHORITATIVE
WHAT YOU NEED
ONE HUNDRED PERCENT

Master the
leading open source
relational database
management system

Tune performance
and security on
database servers

Build MySQL
database applications
using Perl, PHP, Java,
and ODBC

M

ySQL

SUEHRING

www.wiley.com/compbooks
System Requirements:


C O M P R E H E N S I V E



• Use phpMyAdmin for MySQL administration through the Web

100% 100%

MySQL

MySQL is the leading open source relational database management system. It’s powerful, it’s stable, and best
of all, it’s free. Covering everything from RDBMS basics to advanced topics such as replication, database veteran
Steve Suehring shows you how to put together and maintain your own MySQL database system. Whether
you’re a database newcomer who needs help with the command line interface, an administrator who wants tips
on security and performance tuning, or a developer looking for the lowdown on building MySQL-enabled Perl,
PHP, Java, and ODBC applications, this is the only guide you need to harness the power of MySQL.

“The MySQL Bible introduces the essential concepts and
skills you’ll need to get started with MySQL.”

Bible



Sample code
on CD-ROM

ISBN 0-7645-4932-4


,!7IA7G4-fejdci!:p;o;t;T;T

BONUS
CD-ROM!
Perl, sample code
from the book, and more

Steve Suehring



014932-4 FM.F

5/29/02

3:37 PM

Page i

MySQL Bible



014932-4 FM.F

5/29/02

3:37 PM

Page ii



014932-4 FM.F

5/29/02

3:37 PM

Page iii

MySQL Bible


Steve Suehring


014932-4 FM.F

5/29/02

3:37 PM

Page iv

MySQL™ Bible
Published by:
Wiley Publishing, Inc.
909 Third Avenue
New York, NY 10022
www.wiley.com

Copyright © 2002 Wiley Publishing, Inc. All rights reserved. No part of this book, including interior design, cover design,
and icons, may be reproduced or transmitted in any form, by any means (electronic, photocopying, recording, or
otherwise) without the prior written permission of the publisher.
Library of Congress Control Number: 2002103290
ISBN: 0-7645-4932-4
Printed in the United States of America
10 9 8 7 6 5 4 3 2 1
1B/RY/QW/QS/IN
Distributed in the United States by Wiley Publishing, Inc.
Distributed by CDG Books Canada Inc. for Canada; by Transworld Publishers Limited in the United Kingdom; by IDG Norge
Books for Norway; by IDG Sweden Books for Sweden; by IDG Books Australia Publishing Corporation Pty. Ltd. for Australia
and New Zealand; by TransQuest Publishers Pte Ltd. for Singapore, Malaysia, Thailand, Indonesia, and Hong Kong; by
Gotop Information Inc. for Taiwan; by ICG Muse, Inc. for Japan; by Intersoft for South Africa; by Eyrolles for France; by
International Thomson Publishing for Germany, Austria, and Switzerland; by Distribuidora Cuspide for Argentina; by LR
International for Brazil; by Galileo Libros for Chile; by Ediciones ZETA S.C.R. Ltda. for Peru; by WS Computer Publishing
Corporation, Inc., for the Philippines; by Contemporanea de Ediciones for Venezuela; by Express Computer Distributors for
the Caribbean and West Indies; by Micronesia Media Distributor, Inc. for Micronesia; by Chips Computadoras S.A. de C.V.
for Mexico; by Editorial Norma de Panama S.A. for Panama; by American Bookshops for Finland.
For general information on Wiley’s products and services please contact our Customer Care department; within the U.S. at
800-762-2974, outside the U.S. at 317-572-3993 or fax 317-572-4002.
For sales inquiries and resellers information, including discounts, premium and bulk quantity sales and foreign language
translations please contact our Customer Care department at 800-434-3422, fax 317-572-4002 or write to Wiley Publishing,
Inc., Attn: Customer Care department, 10475 Crosspoint Boulevard, Indianapolis, IN 46256.
For information on licensing foreign or domestic rights, please contact our Sub-Rights Customer Care department at
650-653-7098.
For information on using Wiley’s products and services in the classroom or for ordering examination copies, please contact
our Educational Sales department at 800-434-2086 or fax 317-572-4005.
For press review copies, author interviews, or other publicity information, please contact our Public Relations department
at 650-653-7000 or fax 650-653-7500.
For authorization to photocopy items for corporate, personal, or educational use, please contact Copyright Clearance

Center, 222 Rosewood Drive, Danvers, MA 01923, or fax 978-750-4470.
LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND AUTHOR HAVE USED THEIR BEST EFFORTS
IN PREPARING THIS BOOK. THE PUBLISHER AND AUTHOR MAKE NO REPRESENTATIONS OR WARRANTIES WITH
RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS BOOK AND SPECIFICALLY DISCLAIM
ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. THERE ARE NO
WARRANTIES WHICH EXTEND BEYOND THE DESCRIPTIONS CONTAINED IN THIS PARAGRAPH. NO WARRANTY
MAY BE CREATED OR EXTENDED BY SALES REPRESENTATIVES OR WRITTEN SALES MATERIALS. THE ACCURACY
AND COMPLETENESS OF THE INFORMATION PROVIDED HEREIN AND THE OPINIONS STATED HEREIN ARE NOT
GUARANTEED OR WARRANTED TO PRODUCE ANY PARTICULAR RESULTS, AND THE ADVICE AND STRATEGIES
CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY INDIVIDUAL. NEITHER THE PUBLISHER NOR AUTHOR
SHALL BE LIABLE FOR ANY LOSS OF PROFIT OR ANY OTHER COMMERCIAL DAMAGES, INCLUDING BUT NOT
LIMITED TO SPECIAL, INCIDENTAL, CONSEQUENTIAL, OR OTHER DAMAGES.
Trademarks: MySQL is a trademark of MySQL AB. All other trademarks are the property of their respective owners. Wiley
Publishing, Inc., is not associated with any product or vendor mentioned in this book.


014932-4 FM.F

5/29/02

3:37 PM

Page v

About the Author
Steve Suehring is a Senior Systems Engineer for a large Internet provider as well as
a consultant for database, security, and Internet projects. Steve has worked with
numerous clients to develop and implement database and Internet projects.
Through these projects, Steve has worked with MySQL, Oracle, and SQL server for
both administration and development. Steve currently holds certifications from

Microsoft and Cisco. Steve has also written articles for Linux Magazine.
When not in front of a computer screen, Steve enjoys spending time with his wife.
Steve also plays guitar, drums, and piano (though seldom simultaneously) and gets
into pickup games of basketball and football whenever possible.


014932-4 FM.F

5/29/02

3:37 PM

Page vi

Credits
Acquisitions Editor
Terri Varveris

Media Development Specialist
Gregory Stephens

Project Editor
Pat O’Brien

Media Development Coordinator
Marisa Pearman

Technical Editor
Scott Hofmann


Project Coordinator
Nancee Reeves

Copy Editor
Barry Childs-Helton

Graphics and Production Specialists
Beth Brooks, Sean Decker,
Joyce Haughey, Jackie Nicholas,
Heather Pope, Betty Schulte

Proof Editor
TECHBOOKS Production Services
Editorial Manager
Mary Beth Wakefield

Quality Control Technicians
Laura Albert, John Greenough,
Andy Hollandbeck, Carl Pierce

Permissions Editor
Laura Moss

Proofreading and Indexing
TECHBOOKS Production Services


014932-4 FM.F

5/29/02


To Rebecca

3:37 PM

Page vii


014932-4 FM.F

5/29/02

3:37 PM

Page viii


014932-4 FM.F

5/29/02

3:37 PM

Page ix

Preface

W

elcome to MySQL Bible. A complete reference for the MySQL database

server and environment. Whether you’re a database administrator looking
to install MySQL, an existing MySQL administrator, a developer looking to use
MySQL as a backend, or a combination of all three, this book can help.

Why I Wrote This Book
I wrote this book to give you one place to go for hands-on examples, reference, and
best-practices. I’ve been working with MySQL in real-world situations for years. I
believe MySQL to be a robust database solution for just about every type of application. Combine the stability of MySQL with its low of ownership and you quickly
come to find it indispensable.
This book looks to provide everything you’ll need to know to get MySQL running
and expand upon that by examining development and best-practices for MySQL
implementations.

What You Need to Know
You don’t need to know anything about MySQL. However, you should have at least a
little familiarity with the concepts of databases. I’ve covered those concepts within
the pages of the book, but knowing a little about databases can’t hurt.
Of course, you need to know how to use a computer.

What You Need to Have
Since MySQL runs on many platforms, you need access to a computer capable of
running Windows 95, 98, NT, 2000, XP, or somewhere in between, Linux, or a Mac
with OS X. Depending on what you want to do with MySQL, you may need more
RAM or a better processor. I have successfully run MySQL in Linux with less than 20
megabytes of RAM and no swap file.


014932-4 FM.F

x


5/29/02

3:37 PM

Page x

MYSQL Bible

The book is written with version 4 of MySQL in mind. Many of the screenshots were
taken using MySQL 3.23.X as version 4 was being stabilized. However, as I wrote it
version 4 was being developed. Like many people, I assisted in testing MySQL 4 and
so I was able to write this book to include those features.

What the Icons Mean
There are a certain number of icons used throughout the book to highlight various
points. This section illustrates what some of those mean.
Note

The Note icon is used to illustrate a point that I thought should be highlighted
separately from the main text.

On the
CD-ROM

You’ll see this icon throughout the book to denote software, scripts, or the like
from a particular section.

Caution


When you see a Warning icon, pay particular attention. These are typically things
that I’ve done wrong in the past. I highlight them so you don’t do the same!

CrossReference

The Cross-Reference icon points you to another section of the book for more information.

How This Book Is Organized
The book is divided into five main parts and a part of appendixes.

Part I: Getting Started
The first section of the book looks at the prerequisites for installing MySQL, the
actual installation of MySQL, and the concepts of database design. Installation is
examined on Linux, Windows, and Mac. Within the Linux chapter, both binary and
source installation of MySQL is examined.

Part II: SQL Essentials
Examination of SQL and some of the tools used with MySQL is the highlight of the
second section. The first chapter within the section gives a full look at the MySQL
command-line interface program. Alternatives to the CLI are examined including


014932-4 FM.F

5/29/02

3:37 PM

Page xi


Preface

WInMySQLAdmin, and MySQLGUI. The second chapter examines various SQL statements in MySQL. The final chapter of the section is covers some topics that fit into
this section and prepare for other sections.

Part III: Administration
In this part, some server configurations are examined along with variables and
options for the MySQL server. MySQL server security is within this section, as well
as debugging and repairing MySQL databases and servers.

Part IV: Development
Four popular methods for developing applications with MySQL are examined in section four. This includes Perl, PHP, connecting MySQL to ODBC, and JDBC with Java.
Notably, I did not include C or C++ into this section as I strongly feel that many
applications for MySQL aren’t developed in C or C++. I will grant that there are
some, but the widespread languages used that I’ve seen are Perl, Java, and PHP.

Part V: Advanced Performance
The final group of chapters looks at some topics that don’t really fit into another
section. These include replication, integration of MySQL with PAM in Linux, and
NuSphere MySQL.

Part VI: Appendixes
The Appendixes include standard MySQL reference material, a nifty Glossary, and
instructions for the CD-ROM.

About the Companion CD-ROM
Much of the software discussed in the book is included on the CD-ROM with this
book. Since software frequently changes versions, you may find a newer version of
the software on the respective websites. The MySQL software can be downloaded
from the MySQL AB web site.

I’ve also included software and scripts that I used within the book as examples.
Also on the CD-ROM are links that I use frequently and most likely highlight within
the pages of the book as well.

xi


014932-4 FM.F

xii

5/29/02

3:37 PM

Page xii

MYSQL Bible

How to Use This Book
If you don’t already know about databases in general, or you’re getting started with
a MySQL system, I recommend reading this book from cover to cover. If you have a
challenging task with an existing MySQL system, try the Index and the Table of
Contents for your specific problem.

Talk To Me
I’d like to hear feedback on this book. Since the book is my first of this length, I’ll be
quite happy to know that someone out there actually read it. While I’ve done my
best to make this book accurate, I won’t be surprised if you find an error, typographical or otherwise. I’ll do what I can to get it fixed, short of going and crossing
out each typo with a marker.

I’ll be happy to try to answer specific questions about MySQL. However, I can’t
guarantee that I’ll be able to answer questions fully.
You can send email to me at:


I also have a website that contains some fun stuff, as well as other projects that I’m
working on:



014932-4 FM.F

5/29/02

3:37 PM

Page xiii

Acknowledgments

T

his book wouldn’t have been possible without help from many people. First, I
need to thank Terri Varveris for her unending patience with a writer on his
first book as well as Pat O’Brien. Everyone at Wiley has been a pleasure to work
with. Adam Goodman and Jeremy Zawodny from Linux Magazine offered some great
help to a fledgling writer.
There are many others who should get some credit and are too numerous to mention by name. Inevitably I’m going to leave someone out and I apologize in advance.
My wife Rebecca exhibited great understanding, not only with this book, but with
me in general. My family, which has always been there to help and offer guidance

deserves thanks, as does my friend Chris Tuescher. He wouldn’t hesitate to do anything to help a friend in need.
The Nightmare Productions and Capitol Entertainment group of people helped
shape who I am. Bob, Mike Feltz, Ernie Taylor, and Chris Tuescher stand out first
and foremost. However, Chris Steffen, Ron Mackay, Chad Chasteen, Pat Quimby,
and the rest of the gang had their parts too. Just one more time at for Mike at
Manawa Middle or Ellen at Mosinee would be nice, wouldn’t it?
Of course, Jim Leu, John Hein, Jeremy Guthrie, Andy Berkvam, Brandon, Dan, Erich,
Mark, Sarah, Justin, Tara, the CORE Digital gang from Stevens Point. Brian and Jill
and Deb Page and Jay Schrank gave me the opportunity and deserve thanks with
everyone else. Will you really ever forget the time we had at CORE?
The crew from ExecPC/Voyager/CoreComm needs thanks, too. Michael Mittelstadt
for having faith in me. AJ, Aaron, Al, Denise, James, Jerry, Joey Z, Matt, Nic, both
Ryans, and the whole gang from #mcp and #jbs. The ExecPC staff and former CORE
Digital staff are some of the smartest people to ever walk the face of the earth .
Professors Riley, Miller, and Cates have been a great influence on my life, as have
Duff Damos and Gary Wescott. Special thanks to 90fm, Nightmare Squad, Jim Oliva,
and John Eckendorf are in order as well for taking the time and effort every year to
bring the World’s Largest Trivia Contest to life. Yes, it really is the World’s Largest.
The book wouldn’t have been written without the never ending lunches from Mike
Mitchell and JJ at Hilltop. Thanks to Tim at Partner’s.


014932-4 FM.F

xiv

5/29/02

3:37 PM


Page xiv

MYSQL Bible

While I was writing the book, Kent Laabs let me borrow one of his drum kits. Quite
a few evenings were spent hammering away. Though Kent may find out by the time
he reads this, I believe I owe him a new cymbal. Who knows, someday maybe Pat
Dunn will let me play with Spicy Tie Band again.
Finally, Eddie Van Halen deserves thanks as well. His music has offered inspiration
throughout the writing of the book and beyond. It’s almost as if I feel the need to
cut him in on the book deal. Almost. If I get that invite to 5150 sometime...


014932-4 FM.F

5/29/02

3:37 PM

Page xv

Contents at a Glance
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii

PART 1: Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Chapter 1: Relational Database Management . . . . . . . . . . . . . . . . . . . . . . 3
Chapter 2: Preparing for Installation . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Chapter 3: Linux Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Chapter 4: Windows Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

Chapter 5: Macintosh Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Chapter 6: Starting MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Chapter 7: Database Concepts and Design . . . . . . . . . . . . . . . . . . . . . . 105

PART II: SQL Essentials . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Chapter 8: Command Line Interface (CLI) . . . . . . . . . . . . . . . . . . . . . . 133
Chapter 9: SQL According to MySQL . . . . . . . . . . . . . . . . . . . . . . . . . 171
Chapter 10: Database and Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231

PART III: Administration . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Chapter 11: Server Configurations . . . . . . . .
Chapter 12: Security . . . . . . . . . . . . . . . . .
Chapter 13: Debugging and Repairing Databases
Chapter 14: Performance Tuning . . . . . . . . .

.
.
.
.

.
.
.
.

.
.
.
.


.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.


.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.


.
.
.
.

.
.
.
.

.
.
.
.

281
305
337
373

PART IV: Development . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
Chapter 15: Perl Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
Chapter 16: PHP Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
Chapter 17: ODBC/JDBC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493

PART V: Advanced Performance . . . . . . . . . . . . . . . . . . . . . . 523
Chapter 18: Replication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525
Chapter 19: Integration of Internet Services . . . . . . . . . . . . . . . . . . . . . 549
Chapter 20: NuSphere Enhanced MySQL . . . . . . . . . . . . . . . . . . . . . . . 571



014932-4 FM.F

5/29/02

3:37 PM

Page xvi

PART VI: Appendixes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593
Appendix A: Application Reference . . . . . . .
Appendix B: Language Reference . . . . . . . .
Appendix C: Function and Operator Reference
Appendix D: Datatype Reference . . . . . . . .
Appendix E: Glossary . . . . . . . . . . . . . . .
Appendix F: About the CD-ROM . . . . . . . . .

.
.
.
.
.
.

.
.
.
.
.
.


.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.

.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.

.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.

.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.


595
611
625
645
653
659

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 661


014932-4 FM.F

5/29/02

3:37 PM

Page xvii


014932-4 FM.F

5/29/02

3:37 PM

Page xviii


014932-4 FM.F


5/29/02

3:37 PM

Page xix

Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
Why I Wrote This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
What You Need to Know . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
What You Need to Have . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
What the Icons Mean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . x
How This Book Is Organized . . . . . . . . . . . . . . . . . . . . . . . . . . . . x
Part I: Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . x
Part II: SQL Essentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . x
Part III: Administration . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
Part IV: Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
Part V: Advanced Performance . . . . . . . . . . . . . . . . . . . . . . . xi
Part VI: Appendixes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
About the Companion CD-ROM . . . . . . . . . . . . . . . . . . . . . . . . . . xi
How to Use This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xii
Talk To Me . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xii

Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii

PART I: Getting Started

1

Chapter 1: Relational Database Management . . . . . . . . . . . . . . . 3

Applications for Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Customer databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Internet service providers’ databases . . . . . . . . . . . . . . . . . . . 5
Criminology and databases . . . . . . . . . . . . . . . . . . . . . . . . . 6
Advantages of using databases . . . . . . . . . . . . . . . . . . . . . . . 7
Comparing SQL Implementations . . . . . . . . . . . . . . . . . . . . . . . . . 7
Oracle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Microsoft SQL Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
PostgreSQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Informix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Introducing MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
MySQL versions and features . . . . . . . . . . . . . . . . . . . . . . . 11
Standards and compatibility . . . . . . . . . . . . . . . . . . . . . . . . 13
MySQL-specific properties . . . . . . . . . . . . . . . . . . . . . . . . . 16
What MySQL Does Best . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
What MySQL Can’t Do — Quite Yet . . . . . . . . . . . . . . . . . . . . 19
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20


014932-4 FM.F

xx

5/29/02

3:37 PM

Page xx

MYSQL Bible


Chapter 2: Preparing for Installation . . . . . . . . . . . . . . . . . . . 21
Prerequisites for MySQL . . . . . . . . . .
Licensing . . . . . . . . . . . . . . .
Support . . . . . . . . . . . . . . . .
Operating system and architecture
Development . . . . . . . . . . . . .
Existing data . . . . . . . . . . . . .
Default language and character set
Management . . . . . . . . . . . . .
Where to Obtain MySQL . . . . . . . . . .
Overview of MySQL Versions . . . . . . .
MySQL numbering scheme . . . . .
Binary versions of MySQL . . . . .
Source-code versions of MySQL . .
MySQL for Windows . . . . . . . . .
MySQL for Mac OS X . . . . . . . .
Summary . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

21

21
22
23
24
26
28
28
30
30
30
30
32
32
34
34

Chapter 3: Linux Installation . . . . . . . . . . . . . . . . . . . . . . . . 35
Common Binary, RPM, and Source MySQL Installation Tasks
MySQL server startup and shutdown . . . . . . . . . . .
Common command-line options for mysqld . . . . . . .
Creating default databases and completing installation
Setting an administrator password for MySQL . . . . .
MySQL Linux Binary Installation . . . . . . . . . . . . . . . . .
MySQL Linux RPM Installation . . . . . . . . . . . . . . . . . .
MySQL Linux Source Installation . . . . . . . . . . . . . . . . .
Choosing Options for MySQL . . . . . . . . . . . . . . .
Compiling MySQL . . . . . . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.

35
35
38
42
44
45
47
48
48
51
52

Chapter 4: Windows Installation . . . . . . . . . . . . . . . . . . . . . . 53
Tasks Common to Binary and Source MySQL Installation
MySQL server administration
the Windows way . . . . . . . . . . . . . . . . . . .
MySQL server startup and shutdown . . . . . . . . .
Common command-line options for mysqld . . . . .
Setting an Administrator password for MySQL . . .
Installing MySQL on Windows 2000 . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . 53
.
.

.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.

.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.


.
.
.
.
.
.

53
56
58
62
63
68

Chapter 5: Macintosh Installation . . . . . . . . . . . . . . . . . . . . . 69
Binary and Source-Code MySQL Installation in Mac OS X:
Tasks in Common . . . . . . . . . . . . . . . . . . . . . .
Setting up MySQL to start automatically . . . . . . .
Shutting down MySQL Server . . . . . . . . . . . . .
Setting an administrator password for MySQL . . .

.
.
.
.

.
.
.

.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.

.

.
.
.
.

.
.
.
.

69
69
73
73


014932-4 FM.F

5/29/02

3:37 PM

Page xxi

Contents

Installing MySQL With Mac OS X Server . . .
Installing MySQL Binary on Mac OS X . . . .

Installing MySQL Source Code on Mac OS X
Creating the MySQL User . . . . . . . .
Compiling MySQL . . . . . . . . . . . .
Configuring MySQL . . . . . . . . . . .
Testing MySQL . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

74
74
75
75
77
78
79
80

Chapter 6: Starting MySQL . . . . . . . . . . . . . . . . . . . . . . . . . 81
MySQL Server Administration and Security . . . . . . . . . . . . . . . . . . 81

Using mysqladmin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Setting the root password . . . . . . . . . . . . . . . . . . . . . . . . . 82
Checking MySQL server status . . . . . . . . . . . . . . . . . . . . . . 83
Dealing with inactive connections . . . . . . . . . . . . . . . . . . . . . 84
Shut down the server . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Basic MySQL security . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Frequently Used MySQL Database Functions . . . . . . . . . . . . . . . . . . 88
Creating a database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Creating tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Inserting and updating data . . . . . . . . . . . . . . . . . . . . . . . . 92
Preparing a new user for access to the database . . . . . . . . . . . . 97
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102

Chapter 7: Database Concepts and Design . . . . . . . . . . . . . . . 105
Concepts of Databases . . . . . . . . . . . . . . . . . . . . . .
What is data? . . . . . . . . . . . . . . . . . . . . . . . .
What is a database? . . . . . . . . . . . . . . . . . . . .
Why use a Database? . . . . . . . . . . . . . . . . . . .
How are databases used? . . . . . . . . . . . . . . . . .
Database Design . . . . . . . . . . . . . . . . . . . . . . . . .
The Database Life Cycle . . . . . . . . . . . . . . . . . .
Logical Design . . . . . . . . . . . . . . . . . . . . . . . . . . .
Entities and attributes . . . . . . . . . . . . . . . . . . .
Relationships . . . . . . . . . . . . . . . . . . . . . . . .
Entity-relationship diagrams . . . . . . . . . . . . . . .
Analysis of diagrams . . . . . . . . . . . . . . . . . . . .
Constraints . . . . . . . . . . . . . . . . . . . . . . . . .
Normalization . . . . . . . . . . . . . . . . . . . . . . . .
Data Definition Language and Data Markup Language
Producing the SQL . . . . . . . . . . . . . . . . . . . . .

Physical Design and Implementation . . . . . . . . . . . . . .
Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . .
Implementation . . . . . . . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

105
105
105
106
107
107
108

109
109
112
113
113
119
120
122
122
124
125
129
130

xxi


014932-4 FM.F

xxii

5/29/02

3:37 PM

Page xxii

MYSQL Bible

Part II: SQL Essentials


131

Chapter 8: Command Line Interface (CLI) . . . . . . . . . . . . . . . . 133
Introducing the CLI . . . . . . . . . . . . . . . . . . . . . .
Starting the CLI — the Basics . . . . . . . . . . . . . . . .
Specifying the username . . . . . . . . . . . . . . .
Specifying the password . . . . . . . . . . . . . . .
Specifying the host . . . . . . . . . . . . . . . . . . .
Specifying the database . . . . . . . . . . . . . . . .
MySQL CLI Environment Variables . . . . . . . . . . . . .
Determining and changing default variables . . . .
Using the CLI in Interactive Mode . . . . . . . . . . . . .
Speeding startup of the CLI . . . . . . . . . . . . . .
Making the CLI quieter . . . . . . . . . . . . . . . . .
Using a pager to work with larger amounts of data
Displaying query results vertically . . . . . . . . . .
Using tee to save output . . . . . . . . . . . . . . . .
Printing output in HTML format . . . . . . . . . . .
Suppressing column names . . . . . . . . . . . . . .
Using batch mode to produce tab delimited output
Using the CLI in Non-Interactive Mode . . . . . . . . . . .
Executing a statement . . . . . . . . . . . . . . . . .
Printing HTML output . . . . . . . . . . . . . . . . .
Suppressing column names . . . . . . . . . . . . . .
Printing results vertically . . . . . . . . . . . . . . .
Using batch mode to produce tab-delimited output
Creating a Useful MySQL CLI Environment . . . . . . . .
A basic MySQL CLI configuration . . . . . . . . . .
A MySQL administration configuration . . . . . . .

A client-only MySQL CLI configuration . . . . . . .
Your MySQL CLI configuration . . . . . . . . . . . .
Common CLI Errors . . . . . . . . . . . . . . . . . . . . . .
Access Denied errors . . . . . . . . . . . . . . . . .
Alternatives to the CLI . . . . . . . . . . . . . . . . . . . .
The basics of MySQLGUI . . . . . . . . . . . . . . .
Running SQL statements with MySQLGUI . . . . . .
Administration with MySQLGUI . . . . . . . . . . .
The basics of phpMyAdmin . . . . . . . . . . . . . .
Running SQL statements with phpMyAdmin . . . .
Administration with phpMyAdmin . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

133
136
137
138
139
140
141
141
146
147
147
147
148
149
150
150
150

152
152
152
154
155
155
156
156
157
157
157
158
158
160
160
163
166
168
168
169
169

Chapter 9: SQL According to MySQL . . . . . . . . . . . . . . . . . . . 171
Utility and Administrative Statements and Commands . . . . . . . . . . . 171
SHOW statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
USE/CONNECT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179


014932-4 FM.F


5/29/02

3:37 PM

Page xxiii

Contents

DESCRIBE . . . . . . . . . . . . . . . . . . . . .
KILL . . . . . . . . . . . . . . . . . . . . . . . .
OPTIMIZE TABLE <tablename> . . . . . . . . .
Data Definition Language . . . . . . . . . . . . . . .
Deleting tables . . . . . . . . . . . . . . . . . .
Deleting and creating databases . . . . . . . .
Creating tables . . . . . . . . . . . . . . . . . .
Altering tables . . . . . . . . . . . . . . . . . .
Data Markup Language . . . . . . . . . . . . . . . . .
Inserting data into the database with INSERT
Gathering data with SELECT . . . . . . . . . .
Updating data with the UPDATE statement . .
Delete . . . . . . . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

180
180
180
181
181
182
183
185
191
191
197
228
229
230

Chapter 10: Databases and Data . . . . . . . . . . . . . . . . . . . . . 231
Choosing the Right MySQL Table Type . . . . .
MySQL table types . . . . . . . . . . . . . .
MyISAM . . . . . . . . . . . . . . . . . . . .
MERGE . . . . . . . . . . . . . . . . . . . . .
ISAM . . . . . . . . . . . . . . . . . . . . . .
HEAP . . . . . . . . . . . . . . . . . . . . . .

InnoDB . . . . . . . . . . . . . . . . . . . . .
BerkeleyDB . . . . . . . . . . . . . . . . . .
Administering Your Database with mysqladmin
Common switches . . . . . . . . . . . . . .
Special switches . . . . . . . . . . . . . . .
Functions with mysqladmin . . . . . . . .
Exporting Data . . . . . . . . . . . . . . . . . . .
mysqldump . . . . . . . . . . . . . . . . . .
SELECT INTO OUTFILE . . . . . . . . . . .
Importing Data . . . . . . . . . . . . . . . . . . .
CLI . . . . . . . . . . . . . . . . . . . . . . .
LOAD DATA INFILE . . . . . . . . . . . . . .
mysqlimport . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

Part III: Administration

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

231
232
234
240
240
241
242
245
246
246
247
249
260
260
271
273
273
274
276
277

279

Chapter 11: Server Configurations . . . . . . . . . . . . . . . . . . . . 281
MySQL Server Variables . . . . . . . . .
Viewing current server variables .
MySQL Benchmarking and Testing . . .
Requirements for the applications

Running the tests . . . . . . . . . .

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.

.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.

.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.

.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.

.

281
281
292
292
293

xxiii


Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×