Baixe o app para aproveitar ainda mais
Prévia do material em texto
MySQL 5.6 Reference Manual Including MySQL Cluster NDB 7.3 Reference Guide MySQL 5.6 Reference Manual: Including MySQL Cluster NDB 7.3 Reference Guide Abstract This is the MySQL™ Reference Manual. It documents MySQL 5.6 through 5.6.16, as well as MySQL Cluster releases based on version 7.3 of NDBCLUSTER through 5.6.14-ndb-7.3.4. MySQL 5.6 features. This manual describes features that are not included in every edition of MySQL 5.6; such features may not be included in the edition of MySQL 5.6 licensed to you. If you have any questions about the features included in your edition of MySQL 5.6, refer to your MySQL 5.6 license agreement or contact your Oracle sales representative. For release notes detailing the changes in each release, see the MySQL 5.6 Release Notes. For legal information, see the Legal Notices. Document generated on: 2013-11-24 (revision: 36799) General Administrators MySQL Enterprise Developers & Functionality Connectors & APIs HA/Scalability Tutorial Installation & Upgrades MySQL Enterprise Edition MySQL Workbench Connectors and APIs » HA/Scalability Guide Server Administration MySQL Yum Repository MySQL Enterprise Monitor Globalization Connector/J MySQL and DRBD SQL Syntax » MySQL Installer MySQL Enterprise Backup Optimization Connector/ODBC memcached Storage Engines » Security MySQL Enterprise Security Functions and Operators Connector/Net memcached with InnoDB Server Option / Variable Reference » Startup / Shutdown MySQL Enterprise Audit Views and Stored Programs Connector/Python MySQL and Virtualization » Release Notes » Backup and Recovery Overview MySQL Thread Pool Partitioning PHP MySQL Proxy » MySQL Version Reference » MySQL Utilities Precision Math C API Replication FAQs » Linux/Unix Platform Guide Information Schema Connector/C Semisynchronous Replication » Windows Platform Guide Performance Schema Connector/C++ » Mac OS X Platform Guide Spatial Extensions » MySQL for Excel » Solaris Platform Guide Restrictions and Limitations » Building from Source http://dev.mysql.com/doc/relnotes/mysql/5.6/en/ http://dev.mysql.com/doc/workbench/en/wb-intro.html http://dev.mysql.com/doc/workbench/en/wb-intro.html http://dev.mysql.com/doc/mysql-ha-scalability/en/index.html http://dev.mysql.com/doc/mysql-ha-scalability/en/index.html http://dev.mysql.com/doc/mysql-installer/en/ http://dev.mysql.com/doc/mysql-security-excerpt/5.6/en/index.html http://dev.mysql.com/doc/mysql-startstop-excerpt/5.6/en/index.html http://dev.mysql.com/doc/mysql-startstop-excerpt/5.6/en/index.html http://dev.mysql.com/doc/relnotes/mysql/5.6/en/ http://dev.mysql.com/doc/mysql-backup-excerpt/5.6/en/index.html http://dev.mysql.com/doc/mysql-backup-excerpt/5.6/en/index.html http://dev.mysql.com/doc/mysql-backup-excerpt/5.6/en/index.html http://dev.mysql.com/doc/mysqld-version-reference/en/ http://dev.mysql.com/doc/mysqld-version-reference/en/ http://dev.mysql.com/doc/workbench/en/mysql-utilities.html http://dev.mysql.com/doc/mysql-linuxunix-excerpt/5.6/en/index.html http://dev.mysql.com/doc/mysql-linuxunix-excerpt/5.6/en/index.html http://dev.mysql.com/doc/mysql-windows-excerpt/5.6/en/index.html http://dev.mysql.com/doc/mysql-windows-excerpt/5.6/en/index.html http://dev.mysql.com/doc/mysql-macosx-excerpt/5.6/en/index.html http://dev.mysql.com/doc/mysql-macosx-excerpt/5.6/en/index.html http://dev.mysql.com/doc/mysql-for-excel/en/ http://dev.mysql.com/doc/mysql-for-excel/en/ http://dev.mysql.com/doc/mysql-solaris-excerpt/5.6/en/index.html http://dev.mysql.com/doc/mysql-solaris-excerpt/5.6/en/index.html http://dev.mysql.com/doc/mysql-sourcebuild-excerpt/5.6/en/index.html http://dev.mysql.com/doc/mysql-sourcebuild-excerpt/5.6/en/index.html iii Table of Contents Preface and Legal Notices ............................................................................................................... xxv 1 General Information ......................................................................................................................... 1 1.1 About This Manual ............................................................................................................... 2 1.2 Typographical and Syntax Conventions ................................................................................. 3 1.3 Overview of the MySQL Database Management System ........................................................ 4 1.3.1 What is MySQL? ....................................................................................................... 4 1.3.2 The Main Features of MySQL .................................................................................... 6 1.3.3 History of MySQL ...................................................................................................... 9 1.4 What Is New in MySQL 5.6 .................................................................................................. 9 1.5 MySQL Development History .............................................................................................. 19 1.6 MySQL Information Sources ................................................................................................ 20 1.6.1 MySQL Mailing Lists ................................................................................................ 20 1.6.2 MySQL Community Support at the MySQL Forums ................................................... 22 1.6.3 MySQL Community Support on Internet Relay Chat (IRC) .......................................... 23 1.6.4 MySQL Enterprise .................................................................................................... 23 1.7 How to Report Bugs or Problems ........................................................................................ 23 1.8 MySQL Standards Compliance ............................................................................................ 28 1.8.1 What Standards MySQL Follows .............................................................................. 28 1.8.2 Selecting SQL Modes .............................................................................................. 29 1.8.3 Running MySQL in ANSI Mode ................................................................................ 29 1.8.4 MySQL Extensions to Standard SQL ........................................................................ 29 1.8.5 MySQL Differences from Standard SQL .................................................................... 32 1.8.6 How MySQL Deals with Constraints .......................................................................... 36 1.9 Credits ............................................................................................................................... 41 1.9.1 Contributors to MySQL ............................................................................................. 41 1.9.2 Documenters and translators .................................................................................... 45 1.9.3 Packages that support MySQL ................................................................................. 47 1.9.4 Tools that were used to create MySQL ..................................................................... 47 1.9.5 Supporters of MySQL ............................................................................................... 48 2 Installing and Upgrading MySQL .................................................................................................... 49 2.1 General Installation Guidance .............................................................................................. 51 2.1.1 Operating Systems Supported by MySQL Community Server ..................................... 52 2.1.2 Choosing Which MySQL Distribution to Install ........................................................... 53 2.1.3 How to Get MySQL .................................................................................................. 56 2.1.4 Verifying PackageIntegrity Using MD5 Checksums or GnuPG .................................... 56 2.1.5 Installation Layouts .................................................................................................. 65 2.1.6 Compiler-Specific Build Characteristics ..................................................................... 65 2.2 Installing MySQL on Unix/Linux Using Generic Binaries ........................................................ 65 2.3 Installing MySQL on Microsoft Windows .............................................................................. 68 2.3.1 MySQL Installation Layout on Microsoft Windows ...................................................... 70 2.3.2 Choosing An Installation Package ............................................................................. 70 2.3.3 Installing MySQL on Microsoft Windows Using MySQL Installer .................................. 71 2.3.4 MySQL Notifier for Microsoft Windows ...................................................................... 89 2.3.5 Installing MySQL on Microsoft Windows Using a noinstall Zip Archive .................. 100 2.3.6 Troubleshooting a Microsoft Windows MySQL Server Installation .............................. 108 2.3.7 Upgrading MySQL on Windows .............................................................................. 110 2.3.8 Windows Postinstallation Procedures ...................................................................... 111 2.4 Installing MySQL on Mac OS X ......................................................................................... 113 2.4.1 General Notes on Installing MySQL on Mac OS X ................................................... 113 2.4.2 Installing MySQL on Mac OS X Using Native Packages ........................................... 115 2.4.3 Installing the MySQL Startup Item ........................................................................... 117 MySQL 5.6 Reference Manual iv 2.4.4 Installing and Using the MySQL Preference Pane .................................................... 120 2.4.5 Using the Bundled MySQL on Mac OS X Server ..................................................... 122 2.5 Installing MySQL on Linux ................................................................................................. 123 2.5.1 Installing MySQL on Linux Using the MySQL Yum Repository .................................. 123 2.5.2 Replacing a Third-Party Distribution of MySQL Using the MySQL Yum Repository ...... 127 2.5.3 Installing MySQL on Linux Using RPM Packages .................................................... 129 2.5.4 Installing MySQL on Linux Using Debian Packages ................................................. 134 2.5.5 Installing MySQL on Linux Using Native Package Managers ..................................... 134 2.6 Installing MySQL on Solaris and OpenSolaris ..................................................................... 138 2.6.1 Installing MySQL on Solaris Using a Solaris PKG .................................................... 139 2.6.2 Installing MySQL on OpenSolaris Using IPS ............................................................ 140 2.7 Installing MySQL on HP-UX .............................................................................................. 141 2.7.1 General Notes on Installing MySQL on HP-UX ........................................................ 142 2.7.2 Installing MySQL on HP-UX Using DEPOT Packages .............................................. 142 2.8 Installing MySQL on FreeBSD ........................................................................................... 143 2.9 Installing MySQL from Source ........................................................................................... 144 2.9.1 MySQL Layout for Source Installation ..................................................................... 145 2.9.2 Installing MySQL Using a Standard Source Distribution ............................................ 146 2.9.3 Installing MySQL Using a Development Source Tree ............................................... 150 2.9.4 MySQL Source-Configuration Options ..................................................................... 151 2.9.5 Dealing with Problems Compiling MySQL ................................................................ 163 2.9.6 MySQL Configuration and Third-Party Tools ............................................................ 165 2.10 Postinstallation Setup and Testing ................................................................................... 165 2.10.1 Postinstallation Procedures for Unix-like Systems .................................................. 165 2.10.2 Securing the Initial MySQL Accounts ..................................................................... 177 2.11 Upgrading or Downgrading MySQL .................................................................................. 181 2.11.1 Upgrading MySQL ................................................................................................ 182 2.11.2 Downgrading MySQL ............................................................................................ 191 2.11.3 Checking Whether Tables or Indexes Must Be Rebuilt ........................................... 193 2.11.4 Rebuilding or Repairing Tables or Indexes ............................................................ 195 2.11.5 Copying MySQL Databases to Another Machine .................................................... 196 2.12 Environment Variables .................................................................................................... 197 2.13 Perl Installation Notes ..................................................................................................... 199 2.13.1 Installing Perl on Unix .......................................................................................... 199 2.13.2 Installing ActiveState Perl on Windows .................................................................. 200 2.13.3 Problems Using the Perl DBI/DBD Interface ........................................................... 201 3 Tutorial ........................................................................................................................................ 203 3.1 Connecting to and Disconnecting from the Server .............................................................. 203 3.2 Entering Queries ............................................................................................................... 204 3.3 Creating and Using a Database ......................................................................................... 207 3.3.1 Creating and Selecting a Database ......................................................................... 209 3.3.2 Creating a Table .................................................................................................... 209 3.3.3 Loading Data into a Table ...................................................................................... 211 3.3.4 Retrieving Information from a Table ........................................................................ 212 3.4 Getting Information About Databases and Tables ............................................................... 226 3.5 Using mysql in Batch Mode ............................................................................................. 227 3.6 Examples of Common Queries .......................................................................................... 228 3.6.1 The Maximum Value for a Column .......................................................................... 229 3.6.2 The Row Holding the Maximum of a Certain Column ............................................... 229 3.6.3 Maximum of Column per Group .............................................................................. 230 3.6.4 The Rows Holding the Group-wise Maximum of a Certain Column ............................ 230 3.6.5 Using User-Defined Variables................................................................................. 231 3.6.6 Using Foreign Keys ................................................................................................ 231 3.6.7 Searching on Two Keys ......................................................................................... 233 MySQL 5.6 Reference Manual v 3.6.8 Calculating Visits Per Day ...................................................................................... 233 3.6.9 Using AUTO_INCREMENT ........................................................................................ 234 3.7 Using MySQL with Apache ................................................................................................ 236 4 MySQL Programs ........................................................................................................................ 237 4.1 Overview of MySQL Programs .......................................................................................... 238 4.2 Using MySQL Programs .................................................................................................... 242 4.2.1 Invoking MySQL Programs ..................................................................................... 242 4.2.2 Connecting to the MySQL Server ............................................................................ 243 4.2.3 Specifying Program Options ................................................................................... 247 4.2.4 Setting Environment Variables ................................................................................ 260 4.3 MySQL Server and Server-Startup Programs ..................................................................... 262 4.3.1 mysqld — The MySQL Server ............................................................................... 262 4.3.2 mysqld_safe — MySQL Server Startup Script ...................................................... 262 4.3.3 mysql.server — MySQL Server Startup Script ..................................................... 267 4.3.4 mysqld_multi — Manage Multiple MySQL Servers ............................................... 268 4.4 MySQL Installation-Related Programs ................................................................................ 272 4.4.1 comp_err — Compile MySQL Error Message File .................................................. 272 4.4.2 mysqlbug — Generate Bug Report ........................................................................ 273 4.4.3 mysql_install_db — Initialize MySQL Data Directory ......................................... 274 4.4.4 mysql_plugin — Configure MySQL Server Plugins ............................................... 276 4.4.5 mysql_secure_installation — Improve MySQL Installation Security ................ 278 4.4.6 mysql_tzinfo_to_sql — Load the Time Zone Tables ......................................... 279 4.4.7 mysql_upgrade — Check and Upgrade MySQL Tables ......................................... 279 4.5 MySQL Client Programs .................................................................................................... 283 4.5.1 mysql — The MySQL Command-Line Tool ............................................................ 283 4.5.2 mysqladmin — Client for Administering a MySQL Server ........................................ 307 4.5.3 mysqlcheck — A Table Maintenance Program ...................................................... 315 4.5.4 mysqldump — A Database Backup Program .......................................................... 322 4.5.5 mysqlimport — A Data Import Program ............................................................... 341 4.5.6 mysqlshow — Display Database, Table, and Column Information ............................ 347 4.5.7 mysqlslap — Load Emulation Client ..................................................................... 351 4.6 MySQL Administrative and Utility Programs ....................................................................... 358 4.6.1 innochecksum — Offline InnoDB File Checksum Utility .......................................... 358 4.6.2 myisam_ftdump — Display Full-Text Index information .......................................... 359 4.6.3 myisamchk — MyISAM Table-Maintenance Utility ................................................... 360 4.6.4 myisamlog — Display MyISAM Log File Contents .................................................. 377 4.6.5 myisampack — Generate Compressed, Read-Only MyISAM Tables ........................ 378 4.6.6 mysql_config_editor — MySQL Configuration Utility ......................................... 385 4.6.7 mysqlaccess — Client for Checking Access Privileges .......................................... 391 4.6.8 mysqlbinlog — Utility for Processing Binary Log Files .......................................... 393 4.6.9 mysqldumpslow — Summarize Slow Query Log Files ............................................ 413 4.6.10 mysqlhotcopy — A Database Backup Program ................................................... 415 4.6.11 mysql_convert_table_format — Convert Tables to Use a Given Storage Engine ............................................................................................................................ 418 4.6.12 mysql_find_rows — Extract SQL Statements from Files ..................................... 419 4.6.13 mysql_fix_extensions — Normalize Table File Name Extensions ..................... 420 4.6.14 mysql_setpermission — Interactively Set Permissions in Grant Tables .............. 420 4.6.15 mysql_waitpid — Kill Process and Wait for Its Termination ................................ 421 4.6.16 mysql_zap — Kill Processes That Match a Pattern .............................................. 422 4.7 MySQL Program Development Utilities .............................................................................. 422 4.7.1 msql2mysql — Convert mSQL Programs for Use with MySQL ................................ 422 4.7.2 mysql_config — Display Options for Compiling Clients ......................................... 423 4.7.3 my_print_defaults — Display Options from Option Files .................................... 424 4.7.4 resolve_stack_dump — Resolve Numeric Stack Trace Dump to Symbols ............. 425 MySQL 5.6 Reference Manual vi 4.8 Miscellaneous Programs ................................................................................................... 426 4.8.1 perror — Explain Error Codes ............................................................................. 426 4.8.2 replace — A String-Replacement Utility ................................................................ 427 4.8.3 resolveip — Resolve Host name to IP Address or Vice Versa ............................... 427 5 MySQL Server Administration ...................................................................................................... 429 5.1 The MySQL Server ........................................................................................................... 430 5.1.1 Server Option and Variable Reference .................................................................... 430 5.1.2 Server Configuration Defaults ................................................................................. 466 5.1.3 Server Command Options ...................................................................................... 469 5.1.4 Server System Variables ........................................................................................ 506 5.1.5 Using System Variables ......................................................................................... 631 5.1.6 Server Status Variables .......................................................................................... 646 5.1.7 Server SQL Modes ................................................................................................ 675 5.1.8 Server Plugins .......................................................................................................683 5.1.9 IPv6 Support .......................................................................................................... 687 5.1.10 Server-Side Help .................................................................................................. 692 5.1.11 Server Response to Signals ................................................................................. 692 5.1.12 The Shutdown Process ......................................................................................... 693 5.2 MySQL Server Logs ......................................................................................................... 694 5.2.1 Selecting General Query and Slow Query Log Output Destinations ........................... 695 5.2.2 The Error Log ........................................................................................................ 697 5.2.3 The General Query Log .......................................................................................... 698 5.2.4 The Binary Log ...................................................................................................... 700 5.2.5 The Slow Query Log .............................................................................................. 711 5.2.6 Server Log Maintenance ......................................................................................... 713 5.3 Running Multiple MySQL Instances on One Machine .......................................................... 714 5.3.1 Setting Up Multiple Data Directories ........................................................................ 716 5.3.2 Running Multiple MySQL Instances on Windows ...................................................... 717 5.3.3 Running Multiple MySQL Instances on Unix ............................................................ 720 5.3.4 Using Client Programs in a Multiple-Server Environment .......................................... 721 5.4 Tracing mysqld Using DTrace .......................................................................................... 722 5.4.1 mysqld DTrace Probe Reference ........................................................................... 723 6 Security ....................................................................................................................................... 741 6.1 General Security Issues .................................................................................................... 742 6.1.1 Security Guidelines ................................................................................................ 742 6.1.2 Keeping Passwords Secure .................................................................................... 743 6.1.3 Making MySQL Secure Against Attackers ................................................................ 757 6.1.4 Security-Related mysqld Options and Variables ...................................................... 759 6.1.5 How to Run MySQL as a Normal User .................................................................... 760 6.1.6 Security Issues with LOAD DATA LOCAL ................................................................ 760 6.1.7 Client Programming Security Guidelines .................................................................. 761 6.2 The MySQL Access Privilege System ................................................................................ 762 6.2.1 Privileges Provided by MySQL ................................................................................ 764 6.2.2 Privilege System Grant Tables ................................................................................ 768 6.2.3 Specifying Account Names ..................................................................................... 774 6.2.4 Access Control, Stage 1: Connection Verification ..................................................... 776 6.2.5 Access Control, Stage 2: Request Verification ......................................................... 779 6.2.6 When Privilege Changes Take Effect ...................................................................... 780 6.2.7 Causes of Access-Denied Errors ............................................................................ 781 6.3 MySQL User Account Management ................................................................................... 786 6.3.1 User Names and Passwords .................................................................................. 786 6.3.2 Adding User Accounts ............................................................................................ 788 6.3.3 Removing User Accounts ....................................................................................... 792 6.3.4 Setting Account Resource Limits ............................................................................. 792 MySQL 5.6 Reference Manual vii 6.3.5 Assigning Account Passwords ................................................................................ 794 6.3.6 Password Expiration and Sandbox Mode ................................................................ 796 6.3.7 Pluggable Authentication ........................................................................................ 798 6.3.8 Proxy Users ........................................................................................................... 820 6.3.9 Using SSL for Secure Connections ......................................................................... 823 6.3.10 Connecting to MySQL Remotely from Windows with SSH ....................................... 835 6.3.11 MySQL Enterprise Audit Log Plugin ...................................................................... 836 6.3.12 SQL-Based MySQL Account Activity Auditing ........................................................ 853 7 Backup and Recovery .................................................................................................................. 857 7.1 Backup and Recovery Types ............................................................................................. 858 7.2 Database Backup Methods ................................................................................................ 861 7.3 Example Backup and Recovery Strategy ........................................................................... 863 7.3.1 Establishing a Backup Policy .................................................................................. 864 7.3.2 Using Backups for Recovery ................................................................................... 866 7.3.3 Backup Strategy Summary ..................................................................................... 866 7.4 Using mysqldump for Backups ......................................................................................... 867 7.4.1 Dumping Data in SQL Format with mysqldump ....................................................... 867 7.4.2 Reloading SQL-Format Backups ............................................................................. 868 7.4.3 Dumping Data in Delimited-Text Format with mysqldump ........................................ 869 7.4.4 Reloading Delimited-Text Format Backups .............................................................. 870 7.4.5 mysqldump Tips .................................................................................................... 871 7.5 Point-in-Time (Incremental) Recovery Using the Binary Log ................................................ 873 7.5.1 Point-in-Time Recovery Using Event Times ............................................................. 874 7.5.2 Point-in-Time Recovery Using Event Positions ......................................................... 875 7.6 MyISAM Table Maintenance and Crash Recovery ............................................................... 875 7.6.1 Using myisamchk for Crash Recovery .................................................................... 876 7.6.2 How to Check MyISAM Tables for Errors .................................................................877 7.6.3 How to Repair MyISAM Tables ............................................................................... 877 7.6.4 MyISAM Table Optimization .................................................................................... 880 7.6.5 Setting Up a MyISAM Table Maintenance Schedule ................................................. 881 8 Optimization ................................................................................................................................ 883 8.1 Optimization Overview ....................................................................................................... 884 8.2 Optimizing SQL Statements .............................................................................................. 885 8.2.1 Optimizing SELECT Statements .............................................................................. 886 8.2.2 Optimizing DML Statements ................................................................................... 934 8.2.3 Optimizing Database Privileges ............................................................................... 936 8.2.4 Optimizing INFORMATION_SCHEMA Queries ............................................................ 936 8.2.5 Other Optimization Tips .......................................................................................... 941 8.3 Optimization and Indexes .................................................................................................. 944 8.3.1 How MySQL Uses Indexes ..................................................................................... 944 8.3.2 Using Primary Keys ............................................................................................... 945 8.3.3 Using Foreign Keys ................................................................................................ 945 8.3.4 Column Indexes ..................................................................................................... 945 8.3.5 Multiple-Column Indexes ........................................................................................ 947 8.3.6 Verifying Index Usage ............................................................................................ 948 8.3.7 InnoDB and MyISAM Index Statistics Collection ...................................................... 948 8.3.8 Comparison of B-Tree and Hash Indexes ................................................................ 950 8.4 Optimizing Database Structure .......................................................................................... 951 8.4.1 Optimizing Data Size .............................................................................................. 951 8.4.2 Optimizing MySQL Data Types ............................................................................... 953 8.4.3 Optimizing for Many Tables .................................................................................... 955 8.4.4 How MySQL Uses Internal Temporary Tables ......................................................... 956 8.5 Optimizing for InnoDB Tables ........................................................................................... 957 8.5.1 Optimizing Storage Layout for InnoDB Tables ......................................................... 957 MySQL 5.6 Reference Manual viii 8.5.2 Optimizing InnoDB Transaction Management .......................................................... 958 8.5.3 Optimizing InnoDB Logging ................................................................................... 959 8.5.4 Bulk Data Loading for InnoDB Tables .................................................................... 959 8.5.5 Optimizing InnoDB Queries .................................................................................... 961 8.5.6 Optimizing InnoDB DDL Operations ....................................................................... 961 8.5.7 Optimizing InnoDB Disk I/O ................................................................................... 961 8.5.8 Optimizing InnoDB Configuration Variables ............................................................. 963 8.5.9 Optimizing InnoDB for Systems with Many Tables ................................................... 964 8.6 Optimizing for MyISAM Tables ........................................................................................... 964 8.6.1 Optimizing MyISAM Queries .................................................................................... 964 8.6.2 Bulk Data Loading for MyISAM Tables .................................................................... 966 8.6.3 Speed of REPAIR TABLE Statements .................................................................... 967 8.7 Optimizing for MEMORY Tables ........................................................................................... 969 8.8 Understanding the Query Execution Plan ........................................................................... 969 8.8.1 Optimizing Queries with EXPLAIN ........................................................................... 969 8.8.2 EXPLAIN Output Format ........................................................................................ 970 8.8.3 EXPLAIN EXTENDED Output Format ...................................................................... 982 8.8.4 Estimating Query Performance ............................................................................... 984 8.8.5 Controlling the Query Optimizer .............................................................................. 985 8.9 Buffering and Caching ....................................................................................................... 988 8.9.1 The InnoDB Buffer Pool ........................................................................................ 988 8.9.2 The MyISAM Key Cache ......................................................................................... 991 8.9.3 The MySQL Query Cache ...................................................................................... 995 8.9.4 Caching of Prepared Statements and Stored Programs .......................................... 1002 8.10 Optimizing Locking Operations ....................................................................................... 1003 8.10.1 Internal Locking Methods .................................................................................... 1004 8.10.2 Table Locking Issues .......................................................................................... 1006 8.10.3 Concurrent Inserts .............................................................................................. 1007 8.10.4 Metadata Locking ............................................................................................... 1008 8.10.5 External Locking ................................................................................................. 1008 8.11 Optimizing the MySQL Server ........................................................................................ 1010 8.11.1 System Factors and Startup Parameter Tuning .................................................... 1010 8.11.2 Tuning Server Parameters .................................................................................. 1010 8.11.3 Optimizing Disk I/O ............................................................................................. 1019 8.11.4 Optimizing Memory Use ...................................................................................... 1023 8.11.5 Optimizing Network Use ..................................................................................... 1026 8.11.6 The Thread Pool Plugin ...................................................................................... 1029 8.12 Measuring Performance (Benchmarking) ........................................................................ 1034 8.12.1 Measuring the Speed of Expressions and Functions .............................................1035 8.12.2 The MySQL Benchmark Suite ............................................................................. 1035 8.12.3 Using Your Own Benchmarks ............................................................................. 1036 8.12.4 Measuring Performance with performance_schema .......................................... 1037 8.12.5 Examining Thread Information ............................................................................. 1037 9 Language Structure ................................................................................................................... 1053 9.1 Literal Values .................................................................................................................. 1053 9.1.1 String Literals ....................................................................................................... 1053 9.1.2 Number Literals .................................................................................................... 1056 9.1.3 Date and Time Literals ......................................................................................... 1056 9.1.4 Hexadecimal Literals ............................................................................................ 1058 9.1.5 Boolean Literals ................................................................................................... 1059 9.1.6 Bit-Field Literals ................................................................................................... 1059 9.1.7 NULL Values ........................................................................................................ 1060 9.2 Schema Object Names ................................................................................................... 1060 9.2.1 Identifier Qualifiers ............................................................................................... 1062 MySQL 5.6 Reference Manual ix 9.2.2 Identifier Case Sensitivity ...................................................................................... 1062 9.2.3 Mapping of Identifiers to File Names ..................................................................... 1065 9.2.4 Function Name Parsing and Resolution ................................................................. 1067 9.3 Reserved Words ............................................................................................................. 1070 9.4 User-Defined Variables .................................................................................................... 1073 9.5 Expression Syntax .......................................................................................................... 1076 9.6 Comment Syntax ............................................................................................................ 1078 10 Globalization ............................................................................................................................ 1081 10.1 Character Set Support ................................................................................................... 1081 10.1.1 Character Sets and Collations in General ............................................................ 1082 10.1.2 Character Sets and Collations in MySQL ............................................................. 1083 10.1.3 Specifying Character Sets and Collations ............................................................ 1084 10.1.4 Connection Character Sets and Collations ........................................................... 1091 10.1.5 Configuring the Character Set and Collation for Applications ................................. 1094 10.1.6 Character Set for Error Messages ....................................................................... 1096 10.1.7 Collation Issues .................................................................................................. 1097 10.1.8 String Repertoire ................................................................................................ 1106 10.1.9 Operations Affected by Character Set Support ..................................................... 1108 10.1.10 Unicode Support ............................................................................................... 1111 10.1.11 Upgrading from Previous to Current Unicode Support ......................................... 1116 10.1.12 UTF-8 for Metadata .......................................................................................... 1118 10.1.13 Column Character Set Conversion ..................................................................... 1119 10.1.14 Character Sets and Collations That MySQL Supports ......................................... 1120 10.2 Setting the Error Message Language ............................................................................. 1134 10.3 Adding a Character Set ................................................................................................. 1135 10.3.1 Character Definition Arrays ................................................................................. 1137 10.3.2 String Collating Support for Complex Character Sets ............................................ 1138 10.3.3 Multi-Byte Character Support for Complex Character Sets .................................... 1138 10.4 Adding a Collation to a Character Set ............................................................................ 1138 10.4.1 Collation Implementation Types ........................................................................... 1140 10.4.2 Choosing a Collation ID ...................................................................................... 1143 10.4.3 Adding a Simple Collation to an 8-Bit Character Set ............................................. 1144 10.4.4 Adding a UCA Collation to a Unicode Character Set ............................................. 1145 10.5 Character Set Configuration ........................................................................................... 1152 10.6 MySQL Server Time Zone Support ................................................................................ 1153 10.6.1 Staying Current with Time Zone Changes ............................................................ 1155 10.6.2 Time Zone Leap Second Support ........................................................................ 1157 10.7 MySQL Server Locale Support ....................................................................................... 1158 11 Data Types .............................................................................................................................. 1161 11.1 Data Type Overview ...................................................................................................... 1162 11.1.1 Numeric Type Overview ...................................................................................... 1162 11.1.2 Date and Time Type Overview ............................................................................ 1165 11.1.3 String Type Overview ......................................................................................... 1168 11.2 Numeric Types .............................................................................................................. 1171 11.2.1 Integer Types (Exact Value) - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT ........................................................................................................................ 1172 11.2.2 Fixed-Point Types (Exact Value) - DECIMAL, NUMERIC ......................................... 1172 11.2.3 Floating-Point Types (Approximate Value) - FLOAT, DOUBLE ................................. 1173 11.2.4 Bit-Value Type - BIT .......................................................................................... 1173 11.2.5 Numeric Type Attributes ..................................................................................... 1173 11.2.6 Out-of-Range and Overflow Handling ..................................................................1174 11.3 Date and Time Types .................................................................................................... 1176 11.3.1 The DATE, DATETIME, and TIMESTAMP Types .................................................... 1177 11.3.2 The TIME Type .................................................................................................. 1178 MySQL 5.6 Reference Manual x 11.3.3 The YEAR Type .................................................................................................. 1179 11.3.4 YEAR(2) Limitations and Migrating to YEAR(4) .................................................. 1180 11.3.5 Automatic Initialization and Updating for TIMESTAMP and DATETIME .................... 1182 11.3.6 Fractional Seconds in Time Values ..................................................................... 1186 11.3.7 Conversion Between Date and Time Types ......................................................... 1187 11.3.8 Two-Digit Years in Dates .................................................................................... 1189 11.4 String Types ................................................................................................................. 1189 11.4.1 The CHAR and VARCHAR Types ........................................................................... 1189 11.4.2 The BINARY and VARBINARY Types ................................................................... 1191 11.4.3 The BLOB and TEXT Types ................................................................................. 1192 11.4.4 The ENUM Type .................................................................................................. 1194 11.4.5 The SET Type .................................................................................................... 1197 11.5 Data Type Default Values .............................................................................................. 1199 11.6 Data Type Storage Requirements .................................................................................. 1200 11.7 Choosing the Right Type for a Column .......................................................................... 1204 11.8 Using Data Types from Other Database Engines ............................................................ 1204 12 Functions and Operators .......................................................................................................... 1207 12.1 Function and Operator Reference .................................................................................. 1208 12.2 Type Conversion in Expression Evaluation ..................................................................... 1216 12.3 Operators ...................................................................................................................... 1219 12.3.1 Operator Precedence .......................................................................................... 1220 12.3.2 Comparison Functions and Operators .................................................................. 1221 12.3.3 Logical Operators ............................................................................................... 1226 12.3.4 Assignment Operators ........................................................................................ 1228 12.4 Control Flow Functions .................................................................................................. 1230 12.5 String Functions ............................................................................................................ 1232 12.5.1 String Comparison Functions .............................................................................. 1248 12.5.2 Regular Expressions ........................................................................................... 1251 12.6 Numeric Functions and Operators .................................................................................. 1257 12.6.1 Arithmetic Operators ........................................................................................... 1258 12.6.2 Mathematical Functions ...................................................................................... 1260 12.7 Date and Time Functions .............................................................................................. 1269 12.8 What Calendar Is Used By MySQL? .............................................................................. 1292 12.9 Full-Text Search Functions ............................................................................................ 1292 12.9.1 Natural Language Full-Text Searches .................................................................. 1294 12.9.2 Boolean Full-Text Searches ................................................................................ 1297 12.9.3 Full-Text Searches with Query Expansion ............................................................ 1300 12.9.4 Full-Text Stopwords ............................................................................................ 1300 12.9.5 Full-Text Restrictions .......................................................................................... 1304 12.9.6 Fine-Tuning MySQL Full-Text Search .................................................................. 1305 12.9.7 Adding a Collation for Full-Text Indexing .............................................................. 1307 12.10 Cast Functions and Operators ..................................................................................... 1308 12.11 XML Functions ............................................................................................................ 1311 12.12 Bit Functions ............................................................................................................... 1323 12.13 Encryption and Compression Functions ........................................................................ 1325 12.14 Information Functions .................................................................................................. 1332 12.15 GTID Functions ........................................................................................................... 1341 12.16 Miscellaneous Functions .............................................................................................. 1343 12.17 Functions and Modifiers for Use with GROUP BY Clauses .............................................. 1350 12.17.1 GROUP BY (Aggregate) Functions ..................................................................... 1350 12.17.2 GROUP BY Modifiers ........................................................................................ 1355 12.17.3 MySQL Extensions to GROUP BY ...................................................................... 1358 12.18 Spatial Extensions ....................................................................................................... 1359 12.18.1 Introduction to MySQL Spatial Support .............................................................. 1360 MySQL 5.6 Reference Manual xi 12.18.2 The OpenGIS Geometry Model ......................................................................... 1360 12.18.3 Supported Spatial Data Formats ........................................................................ 1366 12.18.4 Creating a Spatially Enabled MySQL Database .................................................. 1368 12.18.5 Spatial Analysis Functions ................................................................................ 1373 12.18.6 Optimizing Spatial Analysis ............................................................................... 1385 12.18.7 MySQL Conformance and Compatibility ............................................................. 1388 12.19 Precision Math ............................................................................................................ 1388 12.19.1 Types of Numeric Values ..................................................................................1389 12.19.2 DECIMAL Data Type Changes ........................................................................... 1389 12.19.3 Expression Handling ......................................................................................... 1390 12.19.4 Rounding Behavior ........................................................................................... 1392 12.19.5 Precision Math Examples .................................................................................. 1393 13 SQL Statement Syntax ............................................................................................................. 1399 13.1 Data Definition Statements ............................................................................................ 1400 13.1.1 ALTER DATABASE Syntax .................................................................................. 1400 13.1.2 ALTER EVENT Syntax ........................................................................................ 1401 13.1.3 ALTER LOGFILE GROUP Syntax ....................................................................... 1403 13.1.4 ALTER FUNCTION Syntax .................................................................................. 1404 13.1.5 ALTER PROCEDURE Syntax ................................................................................ 1405 13.1.6 ALTER SERVER Syntax ...................................................................................... 1405 13.1.7 ALTER TABLE Syntax ........................................................................................ 1405 13.1.8 ALTER TABLESPACE Syntax .............................................................................. 1426 13.1.9 ALTER VIEW Syntax .......................................................................................... 1428 13.1.10 CREATE DATABASE Syntax .............................................................................. 1428 13.1.11 CREATE EVENT Syntax .................................................................................... 1428 13.1.12 CREATE FUNCTION Syntax .............................................................................. 1433 13.1.13 CREATE INDEX Syntax .................................................................................... 1434 13.1.14 CREATE LOGFILE GROUP Syntax .................................................................... 1437 13.1.15 CREATE PROCEDURE and CREATE FUNCTION Syntax ....................................... 1439 13.1.16 CREATE SERVER Syntax .................................................................................. 1444 13.1.17 CREATE TABLE Syntax .................................................................................... 1445 13.1.18 CREATE TABLESPACE Syntax .......................................................................... 1475 13.1.19 CREATE TRIGGER Syntax ................................................................................ 1477 13.1.20 CREATE VIEW Syntax ...................................................................................... 1479 13.1.21 DROP DATABASE Syntax .................................................................................. 1483 13.1.22 DROP EVENT Syntax ........................................................................................ 1484 13.1.23 DROP FUNCTION Syntax .................................................................................. 1484 13.1.24 DROP INDEX Syntax ........................................................................................ 1485 13.1.25 DROP LOGFILE GROUP Syntax ........................................................................ 1485 13.1.26 DROP PROCEDURE and DROP FUNCTION Syntax .............................................. 1486 13.1.27 DROP SERVER Syntax ...................................................................................... 1486 13.1.28 DROP TABLE Syntax ........................................................................................ 1486 13.1.29 DROP TABLESPACE Syntax .............................................................................. 1487 13.1.30 DROP TRIGGER Syntax .................................................................................... 1487 13.1.31 DROP VIEW Syntax .......................................................................................... 1487 13.1.32 RENAME TABLE Syntax .................................................................................... 1488 13.1.33 TRUNCATE TABLE Syntax ................................................................................ 1489 13.2 Data Manipulation Statements ....................................................................................... 1489 13.2.1 CALL Syntax ...................................................................................................... 1489 13.2.2 DELETE Syntax .................................................................................................. 1491 13.2.3 DO Syntax .......................................................................................................... 1496 13.2.4 HANDLER Syntax ................................................................................................ 1496 13.2.5 INSERT Syntax .................................................................................................. 1498 13.2.6 LOAD DATA INFILE Syntax ............................................................................. 1506 MySQL 5.6 Reference Manual xii 13.2.7 LOAD XML Syntax .............................................................................................. 1516 13.2.8 REPLACE Syntax ................................................................................................ 1522 13.2.9 SELECT Syntax .................................................................................................. 1524 13.2.10 Subquery Syntax .............................................................................................. 1544 13.2.11 UPDATE Syntax ................................................................................................ 1556 13.3 MySQL Transactional and Locking Statements ............................................................... 1558 13.3.1 START TRANSACTION, COMMIT, and ROLLBACK Syntax ...................................... 1559 13.3.2 Statements That Cannot Be Rolled Back ............................................................. 1561 13.3.3 Statements That Cause an Implicit Commit .......................................................... 1562 13.3.4 SAVEPOINT, ROLLBACK TO SAVEPOINT, and RELEASE SAVEPOINT Syntax ....... 1563 13.3.5 LOCK TABLES and UNLOCK TABLES Syntax ...................................................... 1564 13.3.6 SET TRANSACTION Syntax ................................................................................ 1570 13.3.7 XA Transactions ................................................................................................. 1572 13.4 Replication Statements .................................................................................................. 1576 13.4.1 SQL Statements for Controlling Master Servers ................................................... 1576 13.4.2 SQL Statements for Controlling Slave Servers ..................................................... 1579 13.5 SQL Syntax for Prepared Statements ............................................................................. 1589 13.5.1 PREPARE Syntax ................................................................................................ 1593 13.5.2 EXECUTE Syntax ................................................................................................ 1593 13.5.3 DEALLOCATE PREPARE Syntax .......................................................................... 1594 13.6 MySQL Compound-Statement Syntax ............................................................................ 1594 13.6.1 BEGIN ... END Compound-Statement Syntax ...................................................1594 13.6.2 Statement Label Syntax ...................................................................................... 1595 13.6.3 DECLARE Syntax ................................................................................................ 1596 13.6.4 Variables in Stored Programs ............................................................................. 1596 13.6.5 Flow Control Statements ..................................................................................... 1598 13.6.6 Cursors .............................................................................................................. 1602 13.6.7 Condition Handling ............................................................................................. 1604 13.7 Database Administration Statements .............................................................................. 1629 13.7.1 Account Management Statements ....................................................................... 1629 13.7.2 Table Maintenance Statements ........................................................................... 1645 13.7.3 Plugin and User-Defined Function Statements ..................................................... 1654 13.7.4 SET Syntax ........................................................................................................ 1657 13.7.5 SHOW Syntax ...................................................................................................... 1660 13.7.6 Other Administrative Statements ......................................................................... 1704 13.8 MySQL Utility Statements .............................................................................................. 1713 13.8.1 DESCRIBE Syntax .............................................................................................. 1713 13.8.2 EXPLAIN Syntax ................................................................................................ 1713 13.8.3 HELP Syntax ...................................................................................................... 1714 13.8.4 USE Syntax ........................................................................................................ 1717 14 Storage Engines ...................................................................................................................... 1719 14.1 Setting the Storage Engine ............................................................................................ 1723 14.2 The InnoDB Storage Engine ......................................................................................... 1724 14.2.1 Introduction to InnoDB ....................................................................................... 1724 14.2.2 InnoDB Concepts and Architecture ..................................................................... 1729 14.2.3 InnoDB Features for Flexibility, Ease of Use and Reliability .................................. 1750 14.2.4 InnoDB Configuration ......................................................................................... 1757 14.2.5 InnoDB Administration ....................................................................................... 1761 14.2.6 InnoDB Tablespace Management ....................................................................... 1762 14.2.7 InnoDB Table Management ................................................................................ 1774 14.2.8 InnoDB Compressed Tables .............................................................................. 1793 14.2.9 InnoDB File-Format Management ....................................................................... 1805 14.2.10 InnoDB Row Storage and Row Formats ............................................................ 1811 14.2.11 InnoDB Disk I/O and File Space Management ................................................... 1812 MySQL 5.6 Reference Manual xiii 14.2.12 InnoDB and Online DDL .................................................................................. 1815 14.2.13 InnoDB Performance Tuning ............................................................................ 1850 14.2.14 InnoDB Startup Options and System Variables .................................................. 1892 14.2.15 InnoDB Backup and Recovery .......................................................................... 1961 14.2.16 InnoDB and MySQL Replication ....................................................................... 1963 14.2.17 InnoDB Integration with memcached ................................................................. 1965 14.2.18 InnoDB Troubleshooting ................................................................................... 1996 14.3 The MyISAM Storage Engine ......................................................................................... 2004 14.3.1 MyISAM Startup Options ..................................................................................... 2006 14.3.2 Space Needed for Keys ...................................................................................... 2008 14.3.3 MyISAM Table Storage Formats .......................................................................... 2008 14.3.4 MyISAM Table Problems ..................................................................................... 2011 14.4 The MEMORY Storage Engine ......................................................................................... 2012 14.5 The CSV Storage Engine ............................................................................................... 2016 14.5.1 Repairing and Checking CSV Tables ................................................................... 2017 14.5.2 CSV Limitations .................................................................................................. 2018 14.6 The ARCHIVE Storage Engine ....................................................................................... 2018 14.7 The BLACKHOLE Storage Engine ................................................................................... 2019 14.8 The MERGE Storage Engine ........................................................................................... 2022 14.8.1 MERGE Table Advantages and Disadvantages ...................................................... 2024 14.8.2 MERGE Table Problems ....................................................................................... 2025 14.9 The FEDERATED Storage Engine ................................................................................... 2027 14.9.1 FEDERATED Storage Engine Overview ................................................................ 2027 14.9.2 How to Create FEDERATED Tables ...................................................................... 2028 14.9.3 FEDERATED Storage Engine Notes and Tips ........................................................ 2031 14.9.4 FEDERATED Storage Engine Resources .............................................................. 2032 14.10 The EXAMPLE Storage Engine ..................................................................................... 2032 14.11 Other Storage Engines ................................................................................................ 2033 14.12 Overview of MySQL Storage Engine Architecture .......................................................... 2033 14.12.1 Pluggable Storage Engine Architecture .............................................................. 2034 14.12.2 The Common Database Server Layer ................................................................ 2034 15 High Availability and Scalability ................................................................................................ 2037 15.1 Oracle VM Template for MySQL Enterprise Edition ......................................................... 2040 15.2 Overview of MySQL with DRBD/Pacemaker/Corosync/Oracle Linux ................................. 2041 15.3 Overview of MySQL with WindowsFailover Clustering .................................................... 2043 15.4 Using MySQL within an Amazon EC2 Instance ............................................................... 2045 15.4.1 Setting Up MySQL on an EC2 AMI ..................................................................... 2046 15.4.2 EC2 Instance Limitations .................................................................................... 2047 15.4.3 Deploying a MySQL Database Using EC2 ........................................................... 2047 15.5 Using ZFS Replication ................................................................................................... 2050 15.5.1 Using ZFS for File System Replication ................................................................ 2052 15.5.2 Configuring MySQL for ZFS Replication .............................................................. 2053 15.5.3 Handling MySQL Recovery with ZFS ................................................................... 2053 15.6 Using MySQL with memcached ..................................................................................... 2054 15.6.1 Installing memcached ......................................................................................... 2055 15.6.2 Using memcached .............................................................................................. 2056 15.6.3 Developing a memcached Application .................................................................. 2076 15.6.4 Getting memcached Statistics ............................................................................. 2102 15.6.5 memcached FAQ ................................................................................................ 2111 15.7 MySQL Proxy ................................................................................................................ 2114 15.7.1 MySQL Proxy Supported Platforms ..................................................................... 2115 15.7.2 Installing MySQL Proxy ....................................................................................... 2115 15.7.3 MySQL Proxy Command Options ........................................................................ 2119 15.7.4 MySQL Proxy Scripting ....................................................................................... 2129 MySQL 5.6 Reference Manual xiv 15.7.5 Using MySQL Proxy ........................................................................................... 2144 15.7.6 MySQL Proxy FAQ ............................................................................................. 2150 16 Replication ............................................................................................................................... 2155 16.1 Replication Configuration ............................................................................................... 2156 16.1.1 How to Set Up Replication .................................................................................. 2157 16.1.2 Replication Formats ............................................................................................ 2167 16.1.3 Replication with Global Transaction Identifiers ...................................................... 2174 16.1.4 Replication and Binary Logging Options and Variables ......................................... 2182 16.1.5 Common Replication Administration Tasks .......................................................... 2257 16.2 Replication Implementation ............................................................................................ 2260 16.2.1 Replication Implementation Details ...................................................................... 2261 16.2.2 Replication Relay and Status Logs ...................................................................... 2262 16.2.3 How Servers Evaluate Replication Filtering Rules ................................................ 2268 16.3 Replication Solutions ..................................................................................................... 2275 16.3.1 Using Replication for Backups ............................................................................. 2276 16.3.2 Using Replication with Different Master and Slave Storage Engines ....................... 2279 16.3.3 Using Replication for Scale-Out ........................................................................... 2281 16.3.4 Replicating Different Databases to Different Slaves .............................................. 2282 16.3.5 Improving Replication Performance ..................................................................... 2283 16.3.6 Switching Masters During Failover ....................................................................... 2284 16.3.7 Setting Up Replication Using SSL ....................................................................... 2286 16.3.8 Semisynchronous Replication .............................................................................. 2288 16.3.9 Delayed Replication ............................................................................................ 2293 16.4 Replication Notes and Tips ............................................................................................ 2294 16.4.1 Replication Features and Issues ......................................................................... 2294 16.4.2 Replication Compatibility Between MySQL Versions ............................................. 2318 16.4.3 Upgrading a Replication Setup ............................................................................ 2319 16.4.4 Troubleshooting Replication ................................................................................ 2321 16.4.5 How to Report Replication Bugs or Problems ....................................................... 2322 17 MySQL Cluster NDB 7.3 .......................................................................................................... 2325 17.1 MySQL Cluster Overview .............................................................................................. 2328 17.1.1 MySQL Cluster Core Concepts ........................................................................... 2330 17.1.2 MySQL Cluster Nodes, Node Groups, Replicas, and Partitions .............................. 2333 17.1.3 MySQL Cluster Hardware, Software, and Networking Requirements ...................... 2336 17.1.4 MySQL Cluster Development History ................................................................... 2338 17.1.5 MySQL Server Using InnoDB Compared with MySQL Cluster .............................. 2338 17.1.6 Known Limitations of MySQL Cluster ................................................................... 2341 17.2 MySQL Cluster Installation ............................................................................................ 2350 17.2.1 The MySQL Cluster Auto-Installer ....................................................................... 2353 17.2.2 Installation of MySQL Cluster on Linux ................................................................ 2369 17.2.3 Installing MySQL Cluster on Windows ................................................................. 2375 17.2.4 Initial Configuration of MySQL Cluster ................................................................. 2384 17.2.5 Initial Startup of MySQL Cluster .......................................................................... 2386 17.2.6 MySQL Cluster Example with Tables and Data .................................................... 2387 17.2.7 Safe Shutdown and Restart of MySQL Cluster ..................................................... 2391 17.2.8 Upgrading and Downgrading MySQL Cluster NDB 7.3 .......................................... 2392 17.3 Configuration of MySQL Cluster NDB 7.3 ....................................................................... 2392 17.3.1 Quick Test Setup of MySQL Cluster ....................................................................
Compartilhar