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

Tài liệu MySQL Administrator’s Bible- P1 ppt

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 (3.79 MB, 50 trang )

Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
MySQL
®
Administrator’s
Bible
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
MySQL
®
Administrator’s
Bible
Sheeri Cabral
Keith Murphy
Wiley Publishing, Inc.
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
MySQL
®
Administrator’s Bible
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2009 by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-0-470-41691-4
Manufactured in the United States of America
10987654321
Library of Congress Cataloging-in-Publication Data:
Cabral, Sheeri, 1978-


MySQL administrator’s bible / Sheeri Cabral, Keith Murphy.
p. cm.
Includes index.
ISBN 978-0-470-41691-4 (paper/website)
1. MySQL (Electronic resource) 2. Database management. 3. Relational databases. I. Cabral, Sheeri,
1970- II. Title.
QA76.9.D3C3178 2009
005.75

65 — dc22
2009005633
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or
by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permit-
ted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written
permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copy-
right Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600.
Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley
& Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at
/>.
Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties
with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties,
including without limitation warranties of fitness for a particular purpose. No warranty may be created or
extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for
every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal,
accounting, or other professional services. If professional assistance is required, the services of a competent
professional person should be sought. Neither the publisher nor the author shall be liable for damages arising
herefrom. The fact that an organization or Web site is referred to in this work as a citation and/or a potential
source of further information does not mean that the author or the publisher endorses the information the
organization or Web site may provide or recommendations it may make. Further, readers should be aware that
Internet Web sites listed in this work may have changed or disappeared between when this work was written

and when it is read.
For general information on our other products and services please contact our Customer Care Department
within the United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Trademarks: Wiley and the Wiley logo are trademarks or registered trademarks of John Wiley & Sons, Inc.
and/or its affiliates, in the United States and other countries, and may not be used without written permission.
MySQL is a registered trademark of MySQL AB Company. All other trademarks are the property of their respec-
tive owners. Wiley Publishing, Inc. is not associated with any product or vendor mentioned in this book.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be
available in electronic books.
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
To Mitch Cherniack, who taught me databases and inspired my
love of them. In memory of Zvi Kramer and Yehoshua
Kritzer — may I be able to echo their strength and
spirit. — Sheeri Cabral
For Rachel, the love of my life. — Keith Murphy
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
About the Authors
Sheeri K. Cabral is a Database Team Lead at the Pythian Group, a remote database administra-
tion solution. She has a master’s degree in computer science specializing in databases from Bran-
deis University and a career background in systems administration. Unstoppable as a volunteer
and activist since she was 14, Sheeri founded and organizes the Boston MySQL User Group. She
also produces OurSQL: The MySQL Database Podcast for the Community, By the Community
and many workshop videos. These resources are freely available at
www.technocation.org
,
the website of Technocation, Inc. Sheeri is the Treasurer of Technocation, Inc, a not-for-profit
organization providing resources and educational grants for IT professionals. Sheeri currently
lives in the Boston area with her husband Tony Cabral and a myriad assortment of Muppets™.
When she’s not working or volunteering, she reads, knits, listens to podcasts, and enjoys time

with her nieces and nephews.
Keith Murphy is a MySQL database administrator who has been using MySQL server
since 1998. A long-time Linux system administrator, he worked with a number of Internet
companies solving systems issues. More recently he formed Paragon Consulting Services
(

) to provide consulting services for companies seeking training
for MySQL and help with MySQL solutions ranging from everyday database administration
tasks to utilizing ‘‘cloud’’ computing services, performance tuning, and scaling. In addition to
information about the company, Keith blogs at

, which he
updates as frequently as possible with tricks, tips, and general information about MySQL server.
In addition he is the editor of MySQL Magazine (

), the MySQL
community’s digital magazine about all things MySQL. It is his pleasure to work with some of
the finest people in the community in this collaborative effort to increase both the amount and
quality of the available information on MySQL server.
Readers are invited to contact Keith by email at

.
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Credits
Executive Editor
Robert Elliott
Development Editor
Ed Connor
Technical Editors

Baron Schwartz
Morgan Tocker
Nicklas Westerlund
Production Editor
Liz Britten
Copy Editors
Kim Cofer
Foxxe Editorial Services
Editorial Manager
Mary Beth Wakefield
Production Manager
Tim Tate
Vice President and Executive Group
Publisher
Richard Swadley
Vice President and Executive Publisher
Barry Pruett
Associate Publisher
Jim Minatel
Project Coordinator, Cover
Lynsey Stanford
Proofreader
Jen Larsen, Word One New York
Indexer
Jack Lewis
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Sheeri Cabral
When I was four years old, I refused to let anyone else tie my shoes, because I wanted to do it
myself. There was only one problem — I did not know how to tie my shoes yet. I went around

for months with untied shoes because I made it very clear that I was going to do it. Eventually I
learned to tie my shoes. I also learned that I could do anything I put my mind to.
I could not have accomplished the herculean task of writing a book had I not had the encour-
agement of my stubbornness. To Mom, Dad, Lymor, Noam, Josh, and Elinor, thanks for putting
up with me all these years, letting me try my way so that my failures and successes were my
own, and teaching me to always do a good job so I can be proud of my work. My love of
databases was inspired by the man who taught me the subject at Brandeis University, Professor
Mitch Cherniack. Professionally, I would not be the person I am today without the guidance
and example of Tom Limoncelli, an inspirational role model, great mentor, and good friend; and
Tracy Gangwer, who taught me to be a super DBA/sysadmin by confirming and triple-checking
everything. A huge thank you goes to Keith for being the lead author on this project, and of
course for asking me to become involved. Of course, none of this could have been done without
the support of Wiley Publishing, particularly our editors Ed Connor and Bob Elliott.
I used many two-dimensional resources while writing this book, but there’s a limit to how much
information is in manuals and books. Special thanks are due to Brian Aker for being a constant
fountain of readily available knowledge and Martin Garden for software assistance. Jay Pipes
provided permission for material used in the Query Analysis chapter, Roland Bouman helped
me get a firm grip on the data dictionary, and Mark Atwood answered my questions about how
storage engines work. I was encouraged and supported by Carsten Turner, Ronald Bradford,
Patrick Galbraith, and many fans of the OurSQL Podcast who wrote to me asking when it was
coming back.
Keith Murphy
There are many people involved in any book project. Without the crew of people from Wiley we
could never have finished this book. Thank you, Bob Elliott (our Executive Editor) for taking a
chance on me. I hope you weren’t disappointed. Without our Development Editor, Ed Connor,
I suspect we would have ended up in the ditch — never to get out. And thanks to all the other
people at Wiley who I didn’t have the chance to meet who help make this happen.
xi
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Acknowledgments

Sheeri, I could not have done this alone. I am grateful for all the long hours and the meticulous
attention to detail that you brought to the project. If I ever get the notion to write another book
you will be the first person I call. Of course you don’t have to answer!
And a big thanks for all those in the MySQL community. It is you who help power this crazy
thing we call Open Source. Thank you for taking the time help others learn and become better.
xii
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Introduction .................................................................................................................................xxvii
Part I First Steps with MySQL
Chapter 1: Introduction to MySQL .................................................................................................. 3
Chapter 2: Installing and Upgrading MySQL Server .......................................................................9
Chapter 3: Accessing MySQL ..........................................................................................................49
Part II Developing with MySQL
Chapter 4: How MySQL Extends and Deviates from SQL ............................................................87
Chapter 5: MySQL Data Types .....................................................................................................159
Chapter 6: MySQL Index Types ...................................................................................................219
Chapter 7: Stored Routines, Triggers, and Events .......................................................................241
Chapter 8: MySQL Views ..............................................................................................................301
Chapter 9: Transactions in MySQL .............................................................................................. 319
Part III Core MySQL Administration
Chapter 10: MySQL Server Tuning ..............................................................................................349
Chapter 11: Storage Engines .........................................................................................................375
Chapter 12: Caching with MySQL ............................................................................................... 423
Chapter 13: Backups and Recovery ..............................................................................................439
Chapter 14: User Management ..................................................................................................... 473
Chapter 15: Partitioning ................................................................................................................495
Chapter 16: Logging and Replication ...........................................................................................517
Chapter 17: Measuring Performance ............................................................................................545
Part IV Extending Your Skills
Chapter 18: Query Analysis and Index Tuning ........................................................................... 589

Chapter 19: Monitoring Your Systems ......................................................................................... 633
Chapter 20: Securing MySQL ....................................................................................................... 649
Chapter 21: The MySQL Data Dictionary .................................................................................... 667
Chapter 22: Scaling and High Availability Architectures .............................................................727
Appendix A: MySQL Proxy ...........................................................................................................749
Appendix B: Functions and Operators .........................................................................................783
Appendix C: Resources .................................................................................................................813
Index ..............................................................................................................................................821
xiii
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Introduction...............................xxvii
Part I First Steps with MySQL
Chapter1:IntroductiontoMySQL .....................3
MySQL Mission — Speed, Reliability, and Ease of Use ................................................ 3
Company background ...................................................................................................4
Community and Enterprise server versions ................................................................. 5
The MySQL Community ...................................................................................................... 6
How to contribute .........................................................................................................6
Reasons to contribute ....................................................................................................7
Summary ................................................................................................................................7
Chapter 2: Installing and Upgrading MySQL Server . . . ..........9
Before Installation ................................................................................................................9
Choosing the MySQL version .....................................................................................11
MySQL support ...........................................................................................................12
Downloads ...................................................................................................................12
Installation .......................................................................................................................... 12
MySQL Server installations on Unix ...........................................................................13
MySQL Server Installation on Windows .................................................................... 20
Installing MySQL from a Noinstall Zip Archive .........................................................24

Starting and stopping MySQL from the Windows command line ............................25
Starting and stopping MySQL as a Windows service ................................................26
Initial Configuration ..........................................................................................................29
Unix configuration file ................................................................................................31
Windows configuration file .........................................................................................31
MySQLConfigurationWizardonWindows..................................................................31
Detailed Configuration ................................................................................................32
The Server Type screen ...............................................................................................33
Database Usage screen ................................................................................................ 33
InnoDB Tablespace screen .......................................................................................... 34
Concurrent Connections screen ..................................................................................34
Networking Options and Strict Mode Options screen .............................................. 34
Character Set screen ....................................................................................................35
Service Options screen ................................................................................................35
Security Options screen .............................................................................................. 35
Confirmation screen ....................................................................................................36
xv
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Contents
MySQL Post-Install Configuration on Unix ...................................................................36
Initializing the system tables .......................................................................................36
Setting initial passwords ..............................................................................................37
Root user password assignment ..................................................................................37
Anonymous users ........................................................................................................39
Securing Your System ........................................................................................................40
Windows PATH Variable Configuration ........................................................................ 42
Automated startup .......................................................................................................42
Starting and stopping mysqld on System V-based Unix ........................................... 42
System V run levels .....................................................................................................43
Upgrading mysqld .............................................................................................................. 45

The MySQL changelog ................................................................................................45
Upgrading MySQL on Windows ................................................................................ 46
Troubleshooting ..................................................................................................................47
Summary .............................................................................................................................. 48
Chapter3:AccessingMySQL .......................49
Accessing mysqld with Command-Line Tools ...............................................................49
Frequently used options ..............................................................................................50
Using the command-line mysql client ........................................................................52
mysqladmin — Client for administering a server ......................................................62
GUI Tools ............................................................................................................................ 66
SQLyog ........................................................................................................................ 66
phpMyAdmin ..............................................................................................................69
MySQL Query Browser ............................................................................................... 71
MySQL Administrator .................................................................................................74
MySQL Workbench .....................................................................................................80
Summary .............................................................................................................................. 83
Part II Developing with MySQL
Chapter4:HowMySQLExtendsandDeviatesfromSQL ......... 87
Learning MySQL Language Structure ............................................................................. 88
Comments and portability ..........................................................................................88
Case-sensitivity ............................................................................................................90
Escape characters .........................................................................................................91
Naming limitations and quoting .................................................................................93
Dot notation ................................................................................................................ 95
Time zones .................................................................................................................. 97
Character sets and collations ...................................................................................... 98
Understanding MySQL Deviations ................................................................................105
Privileges and permissions ........................................................................................110
Transaction management .......................................................................................... 110
Check constraints ......................................................................................................111

Upsert statements ......................................................................................................112
xvi
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Contents
Using MySQL Extensions ................................................................................................114
Aliases ........................................................................................................................115
ALTER TABLE extensions .........................................................................................115
CREATE extensions ...................................................................................................118
DML extensions .........................................................................................................119
DROP extensions .......................................................................................................124
The LIMIT extension .................................................................................................125
SELECT extensions ................................................................................................... 126
SELECT...INTOOUTFILE/SELECT...INTODUMPFILE...............................126
SQL_SMALL_RESULT/SQL_BIG_RESULT ...............................................................127
UNION . . . ORDER BY ............................................................................................127
SELECT...FORUPDATE......................................................................................127
SELECT...LOCKINSHAREMODE....................................................................128
DISTINCTROW .........................................................................................................128
SQL_BUFFER_RESULT .............................................................................................129
HIGH_PRIORITY/LOW_PRIORITY ..........................................................................129
Server maintenance extensions ................................................................................. 129
The SET extension and user-defined variables ........................................................ 131
The SHOW extension ............................................................................................... 135
Table definition extensions .......................................................................................147
Table maintenance extensions .................................................................................. 150
Transactional statement extensions .......................................................................... 156
Summary ............................................................................................................................158
Chapter5:MySQLDataTypes ......................159
Looking at MySQL Data Types ......................................................................................159
Character String Types ................................................................................................... 160

Length ........................................................................................................................162
Character string type attributes ................................................................................ 164
National Character String Types ...................................................................................166
Binary Large Object String Types ................................................................................. 168
BLOB values .............................................................................................................. 169
BINARY values ...........................................................................................................169
BINARY length .......................................................................................................... 169
VARBINARY length ................................................................................................... 170
Numeric Types ..................................................................................................................170
Numeric data sizes and ranges .................................................................................172
Numeric data type attributes .................................................................................... 177
Boolean Types ...................................................................................................................180
Datetime Types .................................................................................................................183
Allowed input values .................................................................................................185
Microsecond input ....................................................................................................186
Automatic updates .....................................................................................................187
Conversion issues ......................................................................................................188
Numeric functions and DATETIME types ................................................................188
xvii
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Contents
Other conversion issues ............................................................................................190
Datetime data type attributes ....................................................................................191
The effect of time zones ............................................................................................192
Interval Types ...................................................................................................................193
ENUM and SET Types .....................................................................................................195
Enumerations .............................................................................................................195
ENUM and SET data type attributes ........................................................................198
Choosing SQL Modes ...................................................................................................... 201
Invalid data ................................................................................................................201

SQL modes ................................................................................................................203
Using NULL Values ..........................................................................................................211
Finding an Optimal Data Type for Existing Data ......................................................212
Small data samples and PROCEDURE ANALYSE() .................................................215
Summary ............................................................................................................................217
Chapter6:MySQLIndexTypes......................219
Looking at Keys and Indexes .........................................................................................219
Using Indexes to Speed Up Lookups ............................................................................221
Creating and dropping indexes ................................................................................223
Index order ................................................................................................................225
Index length .............................................................................................................. 226
Index types ................................................................................................................228
Redundant indexes ....................................................................................................230
Creating and Dropping Key Constraints ......................................................................231
Creating and dropping unique key constraints ........................................................231
Creating and dropping foreign key constraints ........................................................232
Foreign key constraints and data changes ................................................................234
Requirements for foreign key constraints .................................................................235
Using FULLTEXT Indexes ...............................................................................................237
Summary ............................................................................................................................239
Chapter 7: Stored Routines, Triggers, and Events . ............241
Comparing Stored Routines, Triggers, and Events .................................................... 241
Using Triggers ...................................................................................................................242
Creating a trigger .......................................................................................................243
Dropping a trigger .....................................................................................................244
Multiple SQL statements in triggers .........................................................................245
Changing a trigger .....................................................................................................246
Triggers on views and temporary tables ...................................................................247
Trigger runtime behavior ..........................................................................................248
Finding all triggers ....................................................................................................252

Trigger storage and backup ......................................................................................252
Triggers and replication ............................................................................................254
Trigger limitations .....................................................................................................254
xviii
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

×