Documentation Home
MySQL 9.4 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 41.2Mb
PDF (A4) - 41.3Mb
Man Pages (TGZ) - 262.8Kb
Man Pages (Zip) - 368.8Kb
Info (Gzip) - 4.1Mb
Info (Zip) - 4.1Mb


15.7.8.3 FLUSH Statement

FLUSH [NO_WRITE_TO_BINLOG | LOCAL] {flush_option [,flush_option] ...  |tables_option}flush_option: {    BINARY LOGS  | ENGINE LOGS  | ERROR LOGS  | GENERAL LOGS  | LOGS  | PRIVILEGES  | OPTIMIZER_COSTS  | RELAY LOGS [FOR CHANNELchannel]  | SLOW LOGS  | STATUS  | USER_RESOURCES}tables_option: {table_synonym  |table_synonymtbl_name [,tbl_name] ...  |table_synonym WITH READ LOCK  |table_synonymtbl_name [,tbl_name] ... WITH READ LOCK  |table_synonymtbl_name [,tbl_name] ... FOR EXPORT}table_synonym: {    TABLE  | TABLES}

TheFLUSH statement has several variant forms that clear or reload various internal caches, flush tables, or acquire locks. EachFLUSH operation requires the privileges indicated in its description.

Note

It is not possible to issueFLUSH statements within stored functions or triggers. However, you may useFLUSH in stored procedures, so long as these are not called from stored functions or triggers. SeeSection 27.10, “Restrictions on Stored Programs”.

By default, the server writesFLUSH statements to the binary log so that they replicate to replicas. To suppress logging, specify the optionalNO_WRITE_TO_BINLOG keyword or its aliasLOCAL.

Note

FLUSH LOGS,FLUSH BINARY LOGS,FLUSH TABLES WITH READ LOCK (with or without a table list), andFLUSH TABLEStbl_name ... FOR EXPORT are not written to the binary log in any case because they would cause problems if replicated to a replica.

TheFLUSH statement causes an implicit commit. SeeSection 15.3.3, “Statements That Cause an Implicit Commit”.

Themysqladmin utility provides a command-line interface to some flush operations, using commands such asflush-logs,flush-privileges,flush-status, andflush-tables. SeeSection 6.5.2, “mysqladmin — A MySQL Server Administration Program”.

Sending aSIGHUP orSIGUSR1 signal to the server causes several flush operations to occur that are similar to various forms of theFLUSH statement. Signals can be sent by theroot system account or the system account that owns the server process. This enables the flush operations to be performed without having to connect to the server, which requires a MySQL account that has privileges sufficient for those operations. SeeSection 6.10, “Unix Signal Handling in MySQL”.

TheRESET statement is similar toFLUSH. SeeSection 15.7.8.6, “RESET Statement”, for information about usingRESET with replication.

The following list describes the permittedFLUSH statementflush_option values. For descriptions of the permittedtables_option values, seeFLUSH TABLES Syntax.

FLUSH TABLES Syntax

FLUSH TABLES flushes tables, and, depending on the variant used, acquires locks. AnyTABLES variant used in aFLUSH statement must be the only option used.FLUSH TABLE is a synonym forFLUSH TABLES.

Note

The descriptions here that indicate tables are flushed by closing them apply differently forInnoDB, which flushes table contents to disk but leaves them open. This still permits table files to be copied while the tables are open, as long as other activity does not modify them.

  • FLUSH TABLES

    Closes all open tables, forces all tables in use to be closed, and flushes the prepared statement cache.

    This operation requires theFLUSH_TABLES orRELOAD privilege.

    For information about prepared statement caching, seeSection 10.10.3, “Caching of Prepared Statements and Stored Programs”.

    FLUSH TABLES is not permitted when there is an activeLOCK TABLES ... READ. To flush and lock tables, useFLUSH TABLEStbl_name ... WITH READ LOCK instead.

  • FLUSH TABLEStbl_name [,tbl_name] ...

    With a list of one or more comma-separated table names, this operation is likeFLUSH TABLES with no names except that the server flushes only the named tables. If a named table does not exist, no error occurs.

    This operation requires theFLUSH_TABLES orRELOAD privilege.

  • FLUSH TABLES WITH READ LOCK

    Closes all open tables and locks all tables for all databases with a global read lock.

    This operation requires theFLUSH_TABLES orRELOAD privilege.

    This operation is a very convenient way to get backups if you have a file system such as Veritas or ZFS that can take snapshots in time. UseUNLOCK TABLES to release the lock.

    FLUSH TABLES WITH READ LOCK acquires a global read lock rather than table locks, so it is not subject to the same behavior asLOCK TABLES andUNLOCK TABLES with respect to table locking and implicit commits:

    FLUSH TABLES WITH READ LOCK does not prevent the server from inserting rows into the log tables (seeSection 7.4.1, “Selecting General Query Log and Slow Query Log Output Destinations”).

  • FLUSH TABLEStbl_name [,tbl_name] ... WITH READ LOCK

    Flushes and acquires read locks for the named tables.

    This operation requires theFLUSH_TABLES orRELOAD privilege. Because it acquires table locks, it also requires theLOCK TABLES privilege for each table.

    The operation first acquires exclusive metadata locks for the tables, so it waits for transactions that have those tables open to complete. Then the operation flushes the tables from the table cache, reopens the tables, acquires table locks (likeLOCK TABLES ... READ), and downgrades the metadata locks from exclusive to shared. After the operation acquires locks and downgrades the metadata locks, other sessions can read but not modify the tables.

    This operation applies only to existing base (non-TEMPORARY)tables. If a name refers to a base table, that table is used. If it refers to aTEMPORARY table, it is ignored. If a name applies to a view, anER_WRONG_OBJECT error occurs. Otherwise, anER_NO_SUCH_TABLE error occurs.

    UseUNLOCK TABLES to release the locks,LOCK TABLES to release the locks and acquire other locks, orSTART TRANSACTION to release the locks and begin a new transaction.

    ThisFLUSH TABLES variant enables tables to be flushed and locked in a single operation. It provides a workaround for the restriction thatFLUSH TABLES is not permitted when there is an activeLOCK TABLES ... READ.

    This operation does not perform an implicitUNLOCK TABLES, so an error results if you perform the operation while there is any activeLOCK TABLES or use it a second time without first releasing the locks acquired.

    If a flushed table was opened withHANDLER, the handler is implicitly flushed and loses its position.

  • FLUSH TABLEStbl_name [,tbl_name] ... FOR EXPORT

    ThisFLUSH TABLES variant applies toInnoDB tables. It ensures that changes to the named tables have been flushed to disk so that binary table copies can be made while the server is running.

    This operation requires theFLUSH_TABLES orRELOAD privilege. Because it acquires locks on tables in preparation for exporting them, it also requires theLOCK TABLES andSELECT privileges for each table.

    The operation works like this:

    1. It acquires shared metadata locks for the named tables. The operation blocks as long as other sessions have active transactions that have modified those tables or hold table locks for them. When the locks have been acquired, the operation blocks transactions that attempt to update the tables, while permitting read-only operations to continue.

    2. It checks whether all storage engines for the tables supportFOR EXPORT. If any do not, anER_ILLEGAL_HA error occurs and the operation fails.

    3. The operation notifies the storage engine for each table to make the table ready for export. The storage engine must ensure that any pending changes are written to disk.

    4. The operation puts the session in lock-tables mode so that the metadata locks acquired earlier are not released when theFOR EXPORT operation completes.

    This operation applies only to existing base (non-TEMPORARY) tables. If a name refers to a base table, that table is used. If it refers to aTEMPORARY table, it is ignored. If a name applies to a view, anER_WRONG_OBJECT error occurs. Otherwise, anER_NO_SUCH_TABLE error occurs.

    InnoDB supportsFOR EXPORT for tables that have their own.ibd file file (that is, tables created with theinnodb_file_per_table setting enabled).InnoDB ensures when notified by theFOR EXPORT operation that any changes have been flushed to disk. This permits a binary copy of table contents to be made while theFOR EXPORT operation is in effect because the.ibd file is transaction consistent and can be copied while the server is running.FOR EXPORT does not apply toInnoDB system tablespace files, or toInnoDB tables that haveFULLTEXT indexes.

    FLUSH TABLES ...FOR EXPORT is supported for partitionedInnoDB tables.

    When notified byFOR EXPORT,InnoDB writes to disk certain kinds of data that is normally held in memory or in separate disk buffers outside the tablespace files. For each table,InnoDB also produces a file namedtable_name.cfg in the same database directory as the table. The.cfg file contains metadata needed to reimport the tablespace files later, into the same or different server.

    When theFOR EXPORT operation completes,InnoDB has flushed alldirty pages to the table data files. Anychange buffer entries are merged prior to flushing. At this point, the tables are locked and quiescent: The tables are in a transactionally consistent state on disk and you can copy the.ibd tablespace files along with the corresponding.cfg files to get a consistent snapshot of those tables.

    For the procedure to reimport the copied table data into a MySQL instance, seeSection 17.6.1.3, “Importing InnoDB Tables”.

    After you are done with the tables, useUNLOCK TABLES to release the locks,LOCK TABLES to release the locks and acquire other locks, orSTART TRANSACTION to release the locks and begin a new transaction.

    While any of these statements is in effect within the session, attempts to useFLUSH TABLES ... FOR EXPORT produce an error:

    FLUSH TABLES ... WITH READ LOCKFLUSH TABLES ... FOR EXPORTLOCK TABLES ... READLOCK TABLES ... WRITE

    WhileFLUSH TABLES ... FOR EXPORT is in effect within the session, attempts to use any of these statements produce an error:

    FLUSH TABLES WITH READ LOCKFLUSH TABLES ... WITH READ LOCKFLUSH TABLES ... FOR EXPORT