13.1.2 ALTER DATABASE Statement语句

ALTER {DATABASE | SCHEMA} [db_name]
alter_option ...
alter_option: {
    [DEFAULT] CHARACTER SET [=] charset_name
  | [DEFAULT] COLLATE [=] collation_name
  | [DEFAULT] ENCRYPTION [=] {'Y' | 'N'}
  | READ ONLY [=] {DEFAULT | 0 | 1}
}

ALTER DATABASE enables you to change the overall characteristics of a database. ALTER DATABASE使您能够更改数据库的总体特征。These characteristics are stored in the data dictionary. 这些特征存储在数据字典中。This statement requires the ALTER privilege on the database. 此语句需要数据库的ALTER权限。ALTER SCHEMA is a synonym for ALTER DATABASE.ALTER SCHEMAALTER DATABASE的同义词。

If the database name is omitted, the statement applies to the default database. 如果省略了数据库名称,则该语句将应用于默认数据库。In that case, an error occurs if there is no default database.在这种情况下,如果没有默认数据库,则会发生错误。

For any alter_option omitted from the statement, the database retains its current option value, with the exception that changing the character set may change the collation and vice versa.对于语句中省略的任何alter_option,数据库将保留其当前选项值,但更改字符集可能会更改排序规则,反之亦然。

Character Set and Collation Options字符集和排序规则选项

The CHARACTER SET option changes the default database character set. CHARACTER SET选项更改默认的数据库字符集。The COLLATE option changes the default database collation. COLLATE选项更改默认的数据库排序规则。For information about character set and collation names, see Chapter 10, Character Sets, Collations, Unicode.有关字符集和排序规则名称的信息,请参阅第10章,字符集,排序规则,Unicode

To see the available character sets and collations, use the SHOW CHARACTER SET and SHOW COLLATION statements, respectively. 要查看可用的字符集和排序规则,请分别使用SHOW CHARACTER SET语句和SHOW COLLATION语句。See Section 13.7.7.3, “SHOW CHARACTER SET Statement”, and Section 13.7.7.4, “SHOW COLLATION Statement”.请参阅第13.7.7.3节,“显示字符集语句”第13.7.7.4节,“显示排序规则语句”

A stored routine that uses the database defaults when the routine is created includes those defaults as part of its definition. 在创建例程时使用数据库默认值的存储例程将这些默认值作为其定义的一部分。(In a stored routine, variables with character data types use the database defaults if the character set or collation are not specified explicitly. (在存储例程中,如果未显式指定字符集或排序规则,则具有字符数据类型的变量将使用数据库默认值。See Section 13.1.17, “CREATE PROCEDURE and CREATE FUNCTION Statements”.) 参见第13.1.17节,“创建过程和创建函数语句”。)If you change the default character set or collation for a database, any stored routines that are to use the new defaults must be dropped and recreated.如果更改数据库的默认字符集或排序规则,则必须删除并重新创建要使用新默认值的所有存储例程。

Encryption Option加密选项

The ENCRYPTION option, introduced in MySQL 8.0.16, defines the default database encryption, which is inherited by tables created in the database. MySQL 8.0.16中引入的ENCRYPTION选项定义了默认的数据库加密,由数据库中创建的表继承。The permitted values are 'Y' (encryption enabled) and 'N' (encryption disabled). 允许的值为'Y'(启用加密)和'N'(禁用加密)。Only newly created tables inherit the default database encryption. 只有新创建的表继承默认的数据库加密。For existing tables associated with the database, their encryption remains unchanged. 对于与数据库关联的现有表,它们的加密保持不变。If the table_encryption_privilege_check system variable is enabled, the TABLE_ENCRYPTION_ADMIN privilege is required to specify a default encryption setting that differs from the value of the default_table_encryption system variable. 如果启用了table_encryption_privilege_check系统变量,则需要TABLE_ENCRYPTION_ADMIN权限来指定与默认default_table_encryption系统变量的值不同的默认加密设置。For more information, see Defining an Encryption Default for Schemas and General Tablespaces.有关更多信息,请参见为模式和常规表空间定义加密默认值

Read Only Option只读选项

The READ ONLY option, introduced in MySQL 8.0.22, controls whether to permit modification of the database and objects within it. MySQL 8.0.22中引入的READ ONLY控制是否允许修改数据库和其中的对象。The permitted values are DEFAULT or 0 (not read only) and 1 (read only). 允许的值为DEFAULT0(非只读)和1(只读)。This option is useful for database migration because a database for which READ ONLY is enabled can be migrated to another MySQL instance without concern that the database might be changed during the operation.此选项对于数据库迁移非常有用,因为启用READ ONLY的数据库可以迁移到另一个MySQL实例,而不必担心在操作过程中数据库可能会更改。

With NDB Cluster, making a database read only on one mysqld server is synchronized to other mysqld servers in the same cluster, so that the database becomes read only on all mysqld servers.使用NDB Cluster,使一个mysqld服务器上的数据库成为只读的,这将与同一集群中的其他mysqld服务器同步,从而使数据库在所有mysqld服务器上成为只读的。

The READ ONLY option, if enabled, is displayed in the INFORMATION_SCHEMA SCHEMATA_EXTENSIONS table. READ ONLY选项(如果启用)将显示在INFORMATION_SCHEMA SCHEMATA_EXTENSIONS表中。See Section 26.3.32, “The INFORMATION_SCHEMA SCHEMATA_EXTENSIONS Table”.请参阅第26.3.32节,“INFORMATION_SCHEMA SCHEMATA_EXTENSIONS表”

The READ ONLY option cannot be enabled for these system schemas: mysql, information_schema, performance_schema.不能为以下系统架构启用READ ONLY选项:mysqlinformation_schemaperformance_schema

In ALTER DATABASE statements, the READ ONLY option interacts with other instances of itself and with other options as follows:ALTER DATABASE语句中,READ ONLY选项与自身的其他实例以及其他选项交互,如下所示:

Enabling READ ONLY affects all users of the database, with these exceptions that are not subject to read-only checks:启用READ ONLY会影响数据库的所有用户,但以下例外情况不受只读检查的影响:

Other than for the excepted operations just listed, enabling READ ONLY prohibits write operations to the database and its objects, including their definitions, data, and metadata. 除了刚刚列出的例外操作之外,启用READ ONLY将禁止对数据库及其对象(包括其定义、数据和元数据)执行写操作。The following list details affected SQL statements and operations:以下列表详细说明了受影响的SQL语句和操作:

An ALTER DATABASE statement blocks until all concurrent transactions that have already accessed an object in the database being altered have committed. ALTER DATABASE语句阻塞,直到已访问被更改数据库中对象的所有并发事务都已提交。Conversely, a write transaction accessing an object in a database being altered in a concurrent ALTER DATABASE blocks until the ALTER DATABASE has committed.相反,在并发ALTER DATABASE中访问正在更改的数据库中的对象的写事务会阻塞,直到ALTER DATABASE已提交。

If the Clone plugin is used to clone a local or remote data directory, the databases in the clone retain the read-only state they had in the source data directory. 如果克隆插件用于克隆本地或远程数据目录,则克隆中的数据库将保留其在源数据目录中的只读状态。The read-only state does not affect the cloning process itself. 只读状态不影响克隆过程本身。If it is not desirable to have the same database read-only state in the clone, the option must be changed explicitly for the clone after the cloning process has finished, using ALTER DATABASE operations on the clone.如果不希望克隆中具有相同的数据库只读状态,则必须在克隆过程完成后,使用对克隆的ALTER DATABASE操作显式更改克隆的选项。

When cloning from a donor to a recipient, if the recipient has a user database that is read only, cloning fails with an error message. Cloning may be retried after making the database writable.从捐赠者克隆到收件人时,如果收件人的用户数据库为只读,克隆将失败并显示错误消息。使数据库可写后,可以重试克隆。

READ ONLY is permitted for ALTER DATABASE, but not for CREATE DATABASE. ALTER DATABASE允许READ ONLY,但CREATE DATABASE不允许READ ONLYHowever, for a read-only database, the statement produced by SHOW CREATE DATABASE does include READ ONLY=1 within a comment to indicate its read-only status:但是,对于只读数据库,SHOW CREATE database生成的语句在注释中包含READ ONLY=1,以指示其只读状态:

mysql> ALTER DATABASE mydb READ ONLY = 1;
mysql> SHOW CREATE DATABASE mydb\G
*************************** 1. row ***************************
       Database: mydb
Create Database: CREATE DATABASE `mydb`
                 /*!40100 DEFAULT CHARACTER SET utf8mb4
                          COLLATE utf8mb4_0900_ai_ci */
                 /*!80016 DEFAULT ENCRYPTION='N' */
                 /* READ ONLY = 1 */

If the server executes a CREATE DATABASE statement containing such a comment, the server ignores the comment and the READ ONLY option is not processed. 如果服务器执行包含此类注释的CREATE DATABASE语句,则服务器将忽略该注释,并且不会处理READ ONLY选项。This has implications for mysqldump and mysqlpump, which use SHOW CREATE DATABASE to produce CREATE DATABASE statements in dump output:这对mysqldumpmysqlpump有影响,它们使用SHOW CREATE DATABASE在转储输出中生成CREATE DATABASE语句:

Suppose that mydb is read only and you dump it as follows:假设mydb是只读的,您将其转储如下:

shell> mysqldump --databases mydb > mydb.sql

A restore operation later must be followed by ALTER DATABASE if mydb should still be read only:如果mydb仍应为只读,则以后的还原操作后必须执行ALTER DATABASE

shell> mysql
mysql> SOURCE mydb.sql;
mysql> ALTER DATABASE mydb READ ONLY = 1;

MySQL Enterprise Backup is not subject to this issue. MySQL企业备份不受此问题的影响。It backs up and restores a read-only database like any other, but enables the READ ONLY option at restore time if it was enabled at backup time.它像任何其他数据库一样备份和还原只读数据库,但如果在备份时启用了READ ONLY选项,则在还原时启用该选项。

ALTER DATABASE is written to the binary log, so a change to the READ ONLY option on a replication source server also affects replicas. ALTER DATABASE写入二进制日志,因此对复制源服务器上的READ ONLY选项的更改也会影响副本。To prevent this from happening, binary logging must be disabled prior to execution of the ALTER DATABASE statement. 为了防止这种情况发生,必须在执行ALTER DATABASE语句之前禁用二进制日志记录。For example, to prepare for migrating a database without affecting replicas, perform these operations:例如,要准备迁移数据库而不影响副本,请执行以下操作:

  1. Within a single session, disable binary logging and enable READ ONLY for the database:在单个会话中,对数据库禁用二进制日志记录并启用READ ONLY

    mysql> SET sql_log_bin = OFF;
    mysql> ALTER DATABASE mydb READ ONLY = 1;
  2. Dump the database, for example, with mysqldump or mysqlpump:例如,使用mysqldumpmysqlpump转储数据库:

    shell> mysqldump --databases mydb > mydb.sql
  3. Within a single session, disable binary logging and disable READ ONLY for the database:在单个会话中,对数据库禁用二进制日志记录和READ ONLY

    mysql> SET sql_log_bin = OFF;
    mysql> ALTER DATABASE mydb READ ONLY = 0;