565 10 4MB
English Pages 787
MySQL Reference Manual
c 1997-2001 MySQL AB Copyright
i
Table of Contents 1
General Information About MySQL. . . . . . . . . 1 1.1
1.2
1.3
1.4
MySQL, MySQL AB, and Open Source . . . . . . . . . . . . . . . . . . . 2 1.1.1 What Is MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.1.2 What Is MySQL AB . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.1.3 About This Manual . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.1.4 Conventions Used in This Manual . . . . . . . . . . . . . . . . 5 1.1.5 History of MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 1.1.6 The Main Features of MySQL . . . . . . . . . . . . . . . . . . . 6 1.1.7 How Stable Is MySQL? . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.1.8 How Big Can MySQL Tables Be? . . . . . . . . . . . . . . . 11 1.1.9 Year 2000 Compliance . . . . . . . . . . . . . . . . . . . . . . . . . 11 MySQL Information Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.2.1 Books About MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.2.2 General SQL Information and Tutorials . . . . . . . . . 19 1.2.3 Useful MySQL-related Links . . . . . . . . . . . . . . . . . . . . 19 1.2.4 MySQL Mailing Lists . . . . . . . . . . . . . . . . . . . . . . . . . . 28 1.2.4.1 The MySQL Mailing Lists . . . . . . . . . . . . . 29 1.2.4.2 Asking Questions or Reporting Bugs. . . . 31 1.2.4.3 How to Report Bugs or Problems . . . . . . 31 1.2.4.4 Guidelines for Answering Question on the Mailing List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 MySQL Licensing and Support . . . . . . . . . . . . . . . . . . . . . . . . . . 36 1.3.1 MySQL Licensing Policy . . . . . . . . . . . . . . . . . . . . . . . 36 1.3.2 Copyrights Used by MySQL . . . . . . . . . . . . . . . . . . . . 37 1.3.2.1 Copyright Changes . . . . . . . . . . . . . . . . . . . . 38 1.3.3 Example Licensing Situations . . . . . . . . . . . . . . . . . . . 38 1.3.3.1 Selling Products that use MySQL . . . . . . 38 1.3.3.2 ISP MySQL Services . . . . . . . . . . . . . . . . . . 39 1.3.3.3 Running a Web Server Using MySQL . . . 39 1.3.4 MySQL Licensing and Support Costs. . . . . . . . . . . . 39 1.3.4.1 Payment information . . . . . . . . . . . . . . . . . . 40 1.3.4.2 Contact Information. . . . . . . . . . . . . . . . . . . 41 1.3.5 Types of Commercial Support . . . . . . . . . . . . . . . . . . 42 1.3.5.1 Basic E-mail Support. . . . . . . . . . . . . . . . . . 42 1.3.5.2 Extended E-mail Support . . . . . . . . . . . . . . 43 1.3.5.3 Login Support . . . . . . . . . . . . . . . . . . . . . . . . 43 1.3.5.4 Extended Login Support . . . . . . . . . . . . . . . 44 1.3.5.5 Telephone Support . . . . . . . . . . . . . . . . . . . . 44 1.3.5.6 Support for other table handlers. . . . . . . . 45 How Standards-compatible Is MySQL?. . . . . . . . . . . . . . . . . . . 45 1.4.1 MySQL Extensions to ANSI SQL92 . . . . . . . . . . . . . 45 1.4.2 MySQL Differences Compared to ANSI SQL92. . . 47 1.4.3 Running MySQL in ANSI Mode . . . . . . . . . . . . . . . . 48
ii
1.5
1.6
2
1.4.4 Functionality Missing from MySQL . . . . . . . . . . . . . 48 1.4.4.1 Sub-selects . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 1.4.4.2 SELECT INTO TABLE . . . . . . . . . . . . . . . . . . . 49 1.4.4.3 Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . 49 1.4.4.4 Stored Procedures and Triggers . . . . . . . . 51 1.4.4.5 Foreign Keys . . . . . . . . . . . . . . . . . . . . . . . . . 51 1.4.4.6 Why We Did Not Implement Foreign Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 1.4.4.7 Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 1.4.4.8 ‘--’ as the Start of a Comment . . . . . . . . . 53 1.4.5 What Standards Does MySQL Follow? . . . . . . . . . . 54 1.4.6 How to Cope Without COMMIT/ROLLBACK . . . . . . . . 54 1.4.7 Known errors and design deficiencies in MySQL . . 55 How MySQL Compares to Other Databases . . . . . . . . . . . . . . 59 1.5.1 How MySQL Compares to mSQL . . . . . . . . . . . . . . . . 59 1.5.1.1 How to Convert mSQL Tools for MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 1.5.1.2 How mSQL and MySQL Client/Server Communications Protocols Differ . . . . . . . . . . . 62 1.5.1.3 How mSQL 2.0 SQL Syntax Differs from MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 1.5.2 How MySQL Compares to PostgreSQL . . . . . . . . . . 65 1.5.2.1 MySQL and PostgreSQL development strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 1.5.2.2 Featurewise Comparison of MySQL and PostgreSQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 1.5.2.3 Benchmarking MySQL and PostgreSQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 MySQL and the future (The TODO) . . . . . . . . . . . . . . . . . . . . 72 1.6.1 Things that should be in 4.0 . . . . . . . . . . . . . . . . . . . . 73 1.6.2 Things that must be done in the real near future . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 1.6.3 Things that have to be done sometime . . . . . . . . . . 78 1.6.4 Some things we don’t have any plans to do . . . . . . 79
MySQL Installation . . . . . . . . . . . . . . . . . . . . . . . 80 2.1
2.2
2.3
Quick Standard Installation of MySQL. . . . . . . . . . . . . . . . . . . 2.1.1 Installing MySQL on Linux. . . . . . . . . . . . . . . . . . . . . 2.1.2 Installing MySQL on Windows . . . . . . . . . . . . . . . . . General Installation Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2.1 How to Get MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2.2 Operating Systems Supported by MySQL . . . . . . . 2.2.3 Which MySQL Version to Use . . . . . . . . . . . . . . . . . . 2.2.4 Installation Layouts. . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2.5 How and When Updates Are Released. . . . . . . . . . . 2.2.6 MySQL Binaries Compiled by MySQL AB. . . . . . . Installing a MySQL Source Distribution. . . . . . . . . . . . . . . . . . 2.3.1 Quick Installation Overview . . . . . . . . . . . . . . . . . . . .
80 80 81 82 82 85 87 89 90 90 92 93
iii
2.4
2.5
2.6
2.3.2 Applying Patches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 2.3.3 Typical configure Options . . . . . . . . . . . . . . . . . . . . 95 2.3.4 Installing from the Development Source Tree. . . . . 98 2.3.5 Problems Compiling? . . . . . . . . . . . . . . . . . . . . . . . . . . 99 2.3.6 MIT-pthreads Notes . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Post-installation Setup and Testing . . . . . . . . . . . . . . . . . . . . . 102 2.4.1 Problems Running mysql_install_db . . . . . . . . . 106 2.4.2 Problems Starting the MySQL Server . . . . . . . . . . 107 2.4.3 Starting and Stopping MySQL Automatically . . . 109 Upgrading/Downgrading MySQL . . . . . . . . . . . . . . . . . . . . . . . 110 2.5.1 Upgrading From Version 3.23 to Version 4.0 . . . . 111 2.5.2 Upgrading From Version 3.22 to Version 3.23 . . . 111 2.5.3 Upgrading from Version 3.21 to Version 3.22 . . . . 113 2.5.4 Upgrading from Version 3.20 to Version 3.21 . . . . 113 2.5.5 Upgrading to Another Architecture . . . . . . . . . . . . 114 Operating System Specific Notes . . . . . . . . . . . . . . . . . . . . . . . 115 2.6.1 Linux Notes (All Linux Versions) . . . . . . . . . . . . . . 115 2.6.1.1 Linux Notes for Binary Distributions . . 119 2.6.1.2 Linux x86 Notes . . . . . . . . . . . . . . . . . . . . . 120 2.6.1.3 Linux SPARC Notes . . . . . . . . . . . . . . . . . 121 2.6.1.4 Linux Alpha Notes . . . . . . . . . . . . . . . . . . . 121 2.6.1.5 Linux PowerPC Notes . . . . . . . . . . . . . . . . 121 2.6.1.6 Linux MIPS Notes . . . . . . . . . . . . . . . . . . . 122 2.6.1.7 Linux IA64 Notes . . . . . . . . . . . . . . . . . . . . 122 2.6.2 Windows Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 2.6.2.1 Starting MySQL on Windows 95 or Windows 98 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 2.6.2.2 Starting MySQL on Windows NT or Windows 2000 . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 2.6.2.3 Running MySQL on Windows. . . . . . . . . 124 2.6.2.4 Connecting to a Remote MySQL from Windows with SSH . . . . . . . . . . . . . . . . . . . . . . 125 2.6.2.5 Splitting Data Across Different Disks on Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 2.6.2.6 Compiling MySQL Clients on Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 2.6.2.7 MySQL-Windows Compared to Unix MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 2.6.3 Solaris Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 2.6.3.1 Solaris 2.7/2.8 Notes . . . . . . . . . . . . . . . . . 131 2.6.3.2 Solaris x86 Notes . . . . . . . . . . . . . . . . . . . . 132 2.6.4 BSD Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 2.6.4.1 FreeBSD Notes . . . . . . . . . . . . . . . . . . . . . . 133 2.6.4.2 NetBSD notes . . . . . . . . . . . . . . . . . . . . . . . 134 2.6.4.3 OpenBSD Notes . . . . . . . . . . . . . . . . . . . . . 134 2.6.4.4 OpenBSD 2.5 Notes . . . . . . . . . . . . . . . . . . 134 2.6.4.5 OpenBSD 2.8 Notes . . . . . . . . . . . . . . . . . . 134 2.6.4.6 BSD/OS Notes . . . . . . . . . . . . . . . . . . . . . . 134
iv
2.6.5
2.6.6
2.6.7 2.6.8 2.6.9
3
2.6.4.7 BSD/OS Version 2.x Notes . . . . . . . . . . . 2.6.4.8 BSD/OS Version 3.x Notes . . . . . . . . . . . 2.6.4.9 BSD/OS Version 4.x Notes . . . . . . . . . . . Mac OS X Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.6.5.1 Mac OS X Public Beta . . . . . . . . . . . . . . . 2.6.5.2 Mac OS X Server . . . . . . . . . . . . . . . . . . . . Other Unix Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.6.6.1 HP-UX Notes for Binary Distributions ........................................ 2.6.6.2 HP-UX Version 10.20 Notes. . . . . . . . . . . 2.6.6.3 HP-UX Version 11.x Notes. . . . . . . . . . . . 2.6.6.4 IBM-AIX notes . . . . . . . . . . . . . . . . . . . . . . 2.6.6.5 SunOS 4 Notes . . . . . . . . . . . . . . . . . . . . . . 2.6.6.6 Alpha-DEC-UNIX Notes (Tru64) . . . . . . 2.6.6.7 Alpha-DEC-OSF1 Notes . . . . . . . . . . . . . . 2.6.6.8 SGI Irix Notes . . . . . . . . . . . . . . . . . . . . . . . 2.6.6.9 SCO Notes . . . . . . . . . . . . . . . . . . . . . . . . . . 2.6.6.10 SCO Unixware Version 7.0 Notes . . . . . OS/2 Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . BeOS Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Novell Netware Notes . . . . . . . . . . . . . . . . . . . . . . . . .
134 135 135 135 136 136 136 136 137 137 139 140 141 142 143 144 146 146 147 147
Introduction to MySQL: A MySQL Tutorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 3.1 3.2 3.3
3.4 3.5
Connecting to and Disconnecting from the Server . . . . . . . . 148 Entering Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Creating and Using a Database. . . . . . . . . . . . . . . . . . . . . . . . . 152 3.3.1 Creating and Selecting a Database . . . . . . . . . . . . . 153 3.3.2 Creating a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 3.3.3 Loading Data into a Table . . . . . . . . . . . . . . . . . . . . 155 3.3.4 Retrieving Information from a Table . . . . . . . . . . . 156 3.3.4.1 Selecting All Data . . . . . . . . . . . . . . . . . . . 156 3.3.4.2 Selecting Particular Rows . . . . . . . . . . . . . 157 3.3.4.3 Selecting Particular Columns . . . . . . . . . 158 3.3.4.4 Sorting Rows . . . . . . . . . . . . . . . . . . . . . . . . 159 3.3.4.5 Date Calculations . . . . . . . . . . . . . . . . . . . . 161 3.3.4.6 Working with NULL Values . . . . . . . . . . . . 164 3.3.4.7 Pattern Matching . . . . . . . . . . . . . . . . . . . . 164 3.3.4.8 Counting Rows . . . . . . . . . . . . . . . . . . . . . . 167 3.3.4.9 Using More Than one Table . . . . . . . . . . 169 Getting Information About Databases and Tables . . . . . . . 170 Examples of Common Queries . . . . . . . . . . . . . . . . . . . . . . . . . . 171 3.5.1 The Maximum Value for a Column . . . . . . . . . . . . 172 3.5.2 The Row Holding the Maximum of a Certain Column . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 3.5.3 Maximum of Column per Group . . . . . . . . . . . . . . . 173 3.5.4 The Rows Holding the Group-wise Maximum of a Certain Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
v
3.6 3.7
3.8
4
3.5.5 Using user variables. . . . . . . . . . . . . . . . . . . . . . . . . . . 3.5.6 Using Foreign Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.5.7 Searching on Two Keys . . . . . . . . . . . . . . . . . . . . . . . 3.5.8 Calculating visits per day . . . . . . . . . . . . . . . . . . . . . Using mysql in Batch Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . Queries from Twin Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.7.1 Find all Non-distributed Twins . . . . . . . . . . . . . . . . 3.7.2 Show a Table on Twin Pair Status . . . . . . . . . . . . . Using MySQL with Apache . . . . . . . . . . . . . . . . . . . . . . . . . . . .
174 175 176 177 177 178 179 181 181
MySQL Database Administration . . . . . . . . . 183 4.1
Configuring MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 4.1.1 mysqld Command-line Options . . . . . . . . . . . . . . . . 183 4.1.2 my.cnf Option Files . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 4.1.3 Installing Many Servers on the Same Machine . . 190 4.1.4 Running Multiple MySQL Servers on the Same Machine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 4.2 General Security Issues and the MySQL Access Privilege System. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 4.2.1 General Security Guidelines . . . . . . . . . . . . . . . . . . . 192 4.2.2 How to Make MySQL Secure Against Crackers . . 195 4.2.3 Startup Options for mysqld Concerning Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 4.2.4 What the Privilege System Does . . . . . . . . . . . . . . . 197 4.2.5 How the Privilege System Works . . . . . . . . . . . . . . . 197 4.2.6 Privileges Provided by MySQL . . . . . . . . . . . . . . . . 200 4.2.7 Connecting to the MySQL Server . . . . . . . . . . . . . . 202 4.2.8 Access Control, Stage 1: Connection Verification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 4.2.9 Access Control, Stage 2: Request Verification . . . 206 4.2.10 Causes of Access denied Errors . . . . . . . . . . . . . . 208 4.3 MySQL User Account Management . . . . . . . . . . . . . . . . . . . . . 212 4.3.1 GRANT and REVOKE Syntax . . . . . . . . . . . . . . . . . . . . . 212 4.3.2 MySQL User Names and Passwords . . . . . . . . . . . . 215 4.3.3 When Privilege Changes Take Effect . . . . . . . . . . . 216 4.3.4 Setting Up the Initial MySQL Privileges. . . . . . . . 217 4.3.5 Adding New Users to MySQL . . . . . . . . . . . . . . . . . 218 4.3.6 Setting Up Passwords . . . . . . . . . . . . . . . . . . . . . . . . . 221 4.3.7 Keeping Your Password Secure . . . . . . . . . . . . . . . . 222 4.4 Disaster Prevention and Recovery . . . . . . . . . . . . . . . . . . . . . . 223 4.4.1 Database Backups . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 4.4.2 BACKUP TABLE Syntax . . . . . . . . . . . . . . . . . . . . . . . . . 224 4.4.3 RESTORE TABLE Syntax . . . . . . . . . . . . . . . . . . . . . . . . 224 4.4.4 CHECK TABLE Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . 225 4.4.5 REPAIR TABLE Syntax . . . . . . . . . . . . . . . . . . . . . . . . . 226 4.4.6 Using myisamchk for Table Maintenance and Crash Recovery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 4.4.6.1 myisamchk Invocation Syntax . . . . . . . . . 227
vi
4.5
4.6
4.7
4.8
4.4.6.2 General Options for myisamchk . . . . . . . 228 4.4.6.3 Check Options for myisamchk . . . . . . . . . 229 4.4.6.4 Repair Options for myisamchk . . . . . . . . 230 4.4.6.5 Other Options for myisamchk . . . . . . . . . 231 4.4.6.6 myisamchk Memory Usage . . . . . . . . . . . . 232 4.4.6.7 Using myisamchk for Crash Recovery . . 233 4.4.6.8 How to Check Tables for Errors . . . . . . . 234 4.4.6.9 How to Repair Tables . . . . . . . . . . . . . . . . 234 4.4.6.10 Table Optimization . . . . . . . . . . . . . . . . . 237 4.4.7 Setting Up a Table Maintenance Regimen . . . . . . 237 4.4.8 Getting Information About a Table . . . . . . . . . . . . 238 Database Administration Language Reference . . . . . . . . . . . 244 4.5.1 OPTIMIZE TABLE Syntax . . . . . . . . . . . . . . . . . . . . . . . 244 4.5.2 ANALYZE TABLE Syntax . . . . . . . . . . . . . . . . . . . . . . . . 244 4.5.3 FLUSH Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 4.5.4 KILL Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 4.5.5 SHOW Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 4.5.5.1 Retrieving information about Database, Tables, Columns, and Indexes . . . . . . . . . . . . . 247 4.5.5.2 SHOW TABLE STATUS . . . . . . . . . . . . . . . . . . 248 4.5.5.3 SHOW STATUS . . . . . . . . . . . . . . . . . . . . . . . . . 248 4.5.5.4 SHOW VARIABLES . . . . . . . . . . . . . . . . . . . . . 252 4.5.5.5 SHOW LOGS . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 4.5.5.6 SHOW PROCESSLIST . . . . . . . . . . . . . . . . . . . 261 4.5.5.7 SHOW GRANTS . . . . . . . . . . . . . . . . . . . . . . . . . 262 4.5.5.8 SHOW CREATE TABLE . . . . . . . . . . . . . . . . . . 262 MySQL Localization and International Usage . . . . . . . . . . . 262 4.6.1 The Character Set Used for Data and Sorting . . . 262 4.6.1.1 German character set . . . . . . . . . . . . . . . . 263 4.6.2 Non-English Error Messages . . . . . . . . . . . . . . . . . . . 263 4.6.3 Adding a New Character Set . . . . . . . . . . . . . . . . . . 264 4.6.4 The character definition arrays . . . . . . . . . . . . . . . . 265 4.6.5 String Collating Support . . . . . . . . . . . . . . . . . . . . . . 266 4.6.6 Multi-byte Character Support . . . . . . . . . . . . . . . . . 266 MySQL Server-Side Scripts and Utilities . . . . . . . . . . . . . . . . 267 4.7.1 Overview of the Server-Side Scripts and Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 4.7.2 safe mysqld, the wrapper around mysqld . . . . . . . 268 4.7.3 mysqld multi, program for managing multiple MySQL servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 4.7.4 myisampack, The MySQL Compressed Read-only Table Generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 4.7.5 mysqld-max, An extended mysqld server . . . . . . . 279 MySQL Client-Side Scripts and Utilities . . . . . . . . . . . . . . . . 281 4.8.1 Overview of the Client-Side Scripts and Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 4.8.2 The Command-line Tool . . . . . . . . . . . . . . . . . . . . . . 282 4.8.3 mysqladmin, Administrating a MySQL Server . . 288
vii 4.8.4 Using mysqlcheck for Table Maintenance and Crash Recovery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 4.8.5 mysqldump, Dumping Table Structure and Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292 4.8.6 mysqlhotcopy, Copying MySQL Databases and Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 4.8.7 mysqlimport, Importing Data from Text Files . . . 296 4.8.8 Showing Databases, Tables, and Columns . . . . . . 299 4.8.9 perror, Explaining Error Codes . . . . . . . . . . . . . . . . 299 4.8.10 How to Run SQL Commands from a Text File . . 299 4.9 The MySQL Log Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300 4.9.1 The Error Log. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300 4.9.2 The General Query Log . . . . . . . . . . . . . . . . . . . . . . . 300 4.9.3 The Update Log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 4.9.4 The Binary Update Log . . . . . . . . . . . . . . . . . . . . . . . 301 4.9.5 The Slow Query Log . . . . . . . . . . . . . . . . . . . . . . . . . . 303 4.9.6 Log File Maintenance . . . . . . . . . . . . . . . . . . . . . . . . . 303 4.10 Replication in MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 4.10.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 4.10.2 Replication Implementation Overview . . . . . . . . . 304 4.10.3 How To Set Up Replication . . . . . . . . . . . . . . . . . . 305 4.10.4 Replication Features and Known Problems . . . . 306 4.10.5 Replication Options in my.cnf . . . . . . . . . . . . . . . . 308 4.10.6 SQL Commands Related to Replication . . . . . . . 311 4.10.7 Replication FAQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313 4.10.8 Troubleshooting Replication . . . . . . . . . . . . . . . . . . 316
5
MySQL Optimization . . . . . . . . . . . . . . . . . . . . 319 5.1
5.2
Optimization Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 5.1.1 MySQL Design Limitations/Tradeoffs . . . . . . . . . . 319 5.1.2 Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320 5.1.3 What Have We Used MySQL For? . . . . . . . . . . . . . 321 5.1.4 The MySQL Benchmark Suite . . . . . . . . . . . . . . . . . 321 5.1.5 Using Your Own Benchmarks . . . . . . . . . . . . . . . . . . 323 Optimizing SELECTs and Other Queries . . . . . . . . . . . . . . . . . 323 5.2.1 EXPLAIN Syntax (Get Information About a SELECT) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324 5.2.2 Estimating Query Performance . . . . . . . . . . . . . . . . 329 5.2.3 Speed of SELECT Queries . . . . . . . . . . . . . . . . . . . . . . 329 5.2.4 How MySQL Optimizes WHERE Clauses . . . . . . . . . 330 5.2.5 How MySQL Optimizes DISTINCT . . . . . . . . . . . . . 331 5.2.6 How MySQL Optimizes LEFT JOIN and RIGHT JOIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332 5.2.7 How MySQL Optimizes LIMIT . . . . . . . . . . . . . . . . . 332 5.2.8 Speed of INSERT Queries . . . . . . . . . . . . . . . . . . . . . . 333 5.2.9 Speed of UPDATE Queries . . . . . . . . . . . . . . . . . . . . . . 334 5.2.10 Speed of DELETE Queries . . . . . . . . . . . . . . . . . . . . . 335 5.2.11 Other Optimization Tips . . . . . . . . . . . . . . . . . . . . . 335
viii 5.3
5.4
5.5
5.6
6
Locking Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338 5.3.1 How MySQL Locks Tables . . . . . . . . . . . . . . . . . . . . 338 5.3.2 Table Locking Issues . . . . . . . . . . . . . . . . . . . . . . . . . . 338 Optimizing Database Structure . . . . . . . . . . . . . . . . . . . . . . . . . 340 5.4.1 Design Choices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340 5.4.2 Get Your Data as Small as Possible . . . . . . . . . . . . 340 5.4.3 How MySQL Uses Indexes . . . . . . . . . . . . . . . . . . . . 341 5.4.4 Column Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343 5.4.5 Multiple-Column Indexes. . . . . . . . . . . . . . . . . . . . . . 344 5.4.6 How MySQL Opens and Closes Tables . . . . . . . . . 345 5.4.7 Drawbacks to Creating Large Numbers of Tables in the Same Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346 5.4.8 Why So Many Open tables? . . . . . . . . . . . . . . . . . . . 346 Optimizing the MySQL Server . . . . . . . . . . . . . . . . . . . . . . . . . 346 5.5.1 System/Compile Time and Startup Parameter Tuning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346 5.5.2 Tuning Server Parameters . . . . . . . . . . . . . . . . . . . . . 347 5.5.3 How Compiling and Linking Affects the Speed of MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 5.5.4 How MySQL Uses Memory . . . . . . . . . . . . . . . . . . . . 350 5.5.5 How MySQL uses DNS . . . . . . . . . . . . . . . . . . . . . . . 351 5.5.6 SET Syntax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352 Disk Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354 5.6.1 Using Symbolic Links . . . . . . . . . . . . . . . . . . . . . . . . . 355 5.6.1.1 Using Symbolic Links for Databases . . . 356 5.6.1.2 Using Symbolic Links for Tables . . . . . . 356
MySQL Language Reference . . . . . . . . . . . . . 358 6.1
6.2
Language Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358 6.1.1 Literals: How to Write Strings and Numbers . . . . 358 6.1.1.1 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358 6.1.1.2 Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360 6.1.1.3 Hexadecimal Values . . . . . . . . . . . . . . . . . . 360 6.1.1.4 NULL Values . . . . . . . . . . . . . . . . . . . . . . . . . 360 6.1.2 Database, Table, Index, Column, and Alias Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361 6.1.3 Case Sensitivity in Names . . . . . . . . . . . . . . . . . . . . . 362 6.1.4 User Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362 6.1.5 Comment Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363 6.1.6 Is MySQL Picky About Reserved Words?. . . . . . . 364 Column Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365 6.2.1 Numeric Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370 6.2.2 Date and Time Types . . . . . . . . . . . . . . . . . . . . . . . . . 372 6.2.2.1 Y2K Issues and Date Types. . . . . . . . . . . 373 6.2.2.2 The DATETIME, DATE, and TIMESTAMP Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373 6.2.2.3 The TIME Type . . . . . . . . . . . . . . . . . . . . . . 376 6.2.2.4 The YEAR Type . . . . . . . . . . . . . . . . . . . . . . 377
ix 6.2.3 String Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378 6.2.3.1 The CHAR and VARCHAR Types . . . . . . . . . 378 6.2.3.2 The BLOB and TEXT Types . . . . . . . . . . . . 379 6.2.3.3 The ENUM Type . . . . . . . . . . . . . . . . . . . . . . 380 6.2.3.4 The SET Type . . . . . . . . . . . . . . . . . . . . . . . 381 6.2.4 Choosing the Right Type for a Column. . . . . . . . . 382 6.2.5 Using Column Types from Other Database Engines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382 6.2.6 Column Type Storage Requirements . . . . . . . . . . . 383 6.3 Functions for Use in SELECT and WHERE Clauses . . . . . . . . . 384 6.3.1 Non-Type-Specific Operators and Functions . . . . 385 6.3.1.1 Parenthesis . . . . . . . . . . . . . . . . . . . . . . . . . . 385 6.3.1.2 Comparison Operators . . . . . . . . . . . . . . . 385 6.3.1.3 Logical Operators . . . . . . . . . . . . . . . . . . . . 388 6.3.1.4 Control Flow Functions. . . . . . . . . . . . . . . 389 6.3.2 String Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390 6.3.2.1 String Comparison Functions . . . . . . . . . 397 6.3.2.2 Case Sensitivity . . . . . . . . . . . . . . . . . . . . . . 399 6.3.3 Numeric Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . 400 6.3.3.1 Arithmetic Operations . . . . . . . . . . . . . . . 400 6.3.3.2 Mathematical Functions . . . . . . . . . . . . . . 401 6.3.4 Date and Time Functions . . . . . . . . . . . . . . . . . . . . . 405 6.3.5 Other Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413 6.3.5.1 Bit Functions . . . . . . . . . . . . . . . . . . . . . . . . 413 6.3.5.2 Miscellaneous Functions . . . . . . . . . . . . . . 414 6.4 Data Manipulation: SELECT, INSERT, UPDATE, DELETE . . . . 417 6.4.1 SELECT Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417 6.4.1.1 JOIN Syntax . . . . . . . . . . . . . . . . . . . . . . . . . 421 6.4.2 UNION Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422 6.4.3 INSERT Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423 6.4.3.1 INSERT ... SELECT Syntax . . . . . . . . . . . 424 6.4.4 INSERT DELAYED syntax . . . . . . . . . . . . . . . . . . . . . . . 425 6.4.5 UPDATE Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426 6.4.6 DELETE Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427 6.4.7 TRUNCATE Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428 6.4.8 REPLACE Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429 6.4.9 LOAD DATA INFILE Syntax . . . . . . . . . . . . . . . . . . . . . 429 6.5 Data Definition: CREATE, DROP, ALTER . . . . . . . . . . . . . . . . . . 435 6.5.1 CREATE DATABASE Syntax . . . . . . . . . . . . . . . . . . . . . . 435 6.5.2 DROP DATABASE Syntax . . . . . . . . . . . . . . . . . . . . . . . . 435 6.5.3 CREATE TABLE Syntax . . . . . . . . . . . . . . . . . . . . . . . . . 436 6.5.3.1 Silent Column Specification Changes . . 443 6.5.4 ALTER TABLE Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . 443 6.5.5 RENAME TABLE Syntax . . . . . . . . . . . . . . . . . . . . . . . . . 447 6.5.6 DROP TABLE Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . 447 6.5.7 CREATE INDEX Syntax . . . . . . . . . . . . . . . . . . . . . . . . . 447 6.5.8 DROP INDEX Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . 448 6.6 Basic MySQL User Utility Commands . . . . . . . . . . . . . . . . . . 448
x 6.6.1 USE Syntax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448 6.6.2 DESCRIBE Syntax (Get Information About Columns) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449 6.7 MySQL Transactional and Locking Commands . . . . . . . . . . 449 6.7.1 BEGIN/COMMIT/ROLLBACK Syntax . . . . . . . . . . . . . . . 449 6.7.2 LOCK TABLES/UNLOCK TABLES Syntax . . . . . . . . . . . 450 6.7.3 SET TRANSACTION Syntax . . . . . . . . . . . . . . . . . . . . . . 452 6.8 HANDLER Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452 6.9 MySQL Full-text Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453 6.9.1 Fulltext restrictions . . . . . . . . . . . . . . . . . . . . . . . . . . . 455 6.9.2 Fine-tuning MySQL Full-text Search . . . . . . . . . . . 455 6.9.3 New Features of Full-text Search to Appear in MySQL 4.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455 6.9.4 Full-text Search TODO . . . . . . . . . . . . . . . . . . . . . . . 456
7
MySQL Table Types . . . . . . . . . . . . . . . . . . . . . 457 7.1
7.2 7.3 7.4 7.5
7.6
MyISAM Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457 7.1.1 Space Needed for Keys . . . . . . . . . . . . . . . . . . . . . . . . 460 7.1.2 MyISAM Table Formats . . . . . . . . . . . . . . . . . . . . . . 460 7.1.2.1 Static (Fixed-length) Table Characteristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460 7.1.2.2 Dynamic Table Characteristics . . . . . . . . 461 7.1.2.3 Compressed Table Characteristics . . . . . 462 7.1.3 MyISAM table problems.. . . . . . . . . . . . . . . . . . . . . . 462 7.1.3.1 Corrupted MyISAM tables. . . . . . . . . . . . 463 7.1.3.2 Clients is using or hasn’t closed the table properly. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463 MERGE Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464 ISAM Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466 HEAP Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467 BDB or Berkeley DB Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . 468 7.5.1 Overview of BDB Tables . . . . . . . . . . . . . . . . . . . . . . 468 7.5.2 Installing BDB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468 7.5.3 BDB startup options. . . . . . . . . . . . . . . . . . . . . . . . . . 469 7.5.4 Some characteristic of BDB tables: . . . . . . . . . . . . . . 469 7.5.5 Some things we need to fix for BDB in the near future: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470 7.5.6 Operating systems supported by BDB . . . . . . . . . . 471 7.5.7 Errors You May Get When Using BDB Tables . . 471 InnoDB Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472 7.6.1 InnoDB tables overview . . . . . . . . . . . . . . . . . . . . . . . 472 7.6.2 InnoDB startup options . . . . . . . . . . . . . . . . . . . . . . . 472 7.6.3 Creating InnoDB table space . . . . . . . . . . . . . . . . . . 475 7.6.3.1 If something goes wrong in database creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476 7.6.4 Creating InnoDB tables . . . . . . . . . . . . . . . . . . . . . . . 476 7.6.4.1 Converting MyISAM tables to InnoDB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
xi 7.6.5 Adding and removing InnoDB data and log files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477 7.6.6 Backing up and recovering an InnoDB database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478 7.6.6.1 Checkpoints . . . . . . . . . . . . . . . . . . . . . . . . . 479 7.6.7 Moving an InnoDB database to another machine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480 7.6.8 InnoDB transaction model . . . . . . . . . . . . . . . . . . . . 480 7.6.8.1 Consistent read . . . . . . . . . . . . . . . . . . . . . . 480 7.6.8.2 Locking reads . . . . . . . . . . . . . . . . . . . . . . . . 481 7.6.8.3 Next-key locking: avoiding the phantom problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481 7.6.8.4 Locks set by different SQL statements in InnoDB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482 7.6.8.5 Deadlock detection and rollback . . . . . . . 483 7.6.8.6 An example of how the consistent read works in InnoDB. . . . . . . . . . . . . . . . . . . . . . . . . 483 7.6.9 Performance tuning tips . . . . . . . . . . . . . . . . . . . . . . . 484 7.6.9.1 The InnoDB Monitor . . . . . . . . . . . . . . . . . 485 7.6.1