MySQL Statements
— SQL, MySQL, Statement — 6 min read
SQL 示例
-- 创建用户CREATE USER 'test'@'localhost' IDENTIFIED BY '123456';-- 修改用户ALTER USER 'test'@'localhost' IDENTIFIED BY '1234567';-- 创建DBCREATE SCHEMA test_schema COLLATE utf8mb4_general_ci;-- 修改DBALTER SCHEMA test_schema COLLATE utf8mb4_general_ci;-- 赋予权限GRANT ALL ON test_schema.* TO 'test'@'localhost';-- 刷新权限flush privileges;-- 收回权限REVOKE ALL ON test_schema.* FROM 'test'@'localhost';flush privileges;-- 删除DBDROP SCHEMA test_schema;-- 删除用户DROP USER 'test'@'localhost';
USER Statement
CREATE
CREATE USER [IF NOT EXISTS] user [auth_option] [, user [auth_option]] ... [REQUIRE {NONE | tls_option [[AND] tls_option] ...}] [WITH resource_option [resource_option] ...] [password_option | lock_option] ...
user: (see Section 6.2.4, “Specifying Account Names”)
auth_option: { IDENTIFIED BY 'auth_string' | IDENTIFIED WITH auth_plugin | IDENTIFIED WITH auth_plugin BY 'auth_string' | IDENTIFIED WITH auth_plugin AS 'auth_string' | IDENTIFIED BY PASSWORD 'auth_string'}
tls_option: { SSL | X509 | CIPHER 'cipher' | ISSUER 'issuer' | SUBJECT 'subject'}
resource_option: { MAX_QUERIES_PER_HOUR count | MAX_UPDATES_PER_HOUR count | MAX_CONNECTIONS_PER_HOUR count | MAX_USER_CONNECTIONS count}
password_option: { PASSWORD EXPIRE | PASSWORD EXPIRE DEFAULT | PASSWORD EXPIRE NEVER | PASSWORD EXPIRE INTERVAL N DAY}
lock_option: { ACCOUNT LOCK | ACCOUNT UNLOCK}
Examples
-
创建用户test,主机localhost,密码123456
CREATE USER 'test'@'localhost' IDENTIFIED BY '123456'; -
创建用户并标记密码为过期,需要在第一次登录后修改密码
CREATE USER 'test'@'localhost' IDENTIFIED BY '123456' PASSWORD EXPIRE; -
创建用户使用sha256_password作为身份验证插件,密码过期时间为180天
CREATE USER 'test'@'localhost'IDENTIFIED WITH sha256_password BY '123456'PASSWORD EXPIRE INTERVAL 180 DAY;
ALTER
ALTER USER [IF EXISTS] user [auth_option] [, user [auth_option]] ... [REQUIRE {NONE | tls_option [[AND] tls_option] ...}] [WITH resource_option [resource_option] ...] [password_option | lock_option] ...
ALTER USER [IF EXISTS] USER() IDENTIFIED BY 'auth_string'
user: (see Section 6.2.4, “Specifying Account Names”)
auth_option: { IDENTIFIED BY 'auth_string' | IDENTIFIED WITH auth_plugin | IDENTIFIED WITH auth_plugin BY 'auth_string' | IDENTIFIED WITH auth_plugin AS 'auth_string'}
tls_option: { SSL | X509 | CIPHER 'cipher' | ISSUER 'issuer' | SUBJECT 'subject'}
resource_option: { MAX_QUERIES_PER_HOUR count | MAX_UPDATES_PER_HOUR count | MAX_CONNECTIONS_PER_HOUR count | MAX_USER_CONNECTIONS count}
password_option: { PASSWORD EXPIRE | PASSWORD EXPIRE DEFAULT | PASSWORD EXPIRE NEVER | PASSWORD EXPIRE INTERVAL N DAY}
lock_option: { ACCOUNT LOCK | ACCOUNT UNLOCK}
Examples
-
修改用户密码
ALTER USER 'test'@'localhost' IDENTIFIED BY '123456';
RENAME
RENAME USER old_user TO new_user [, old_user TO new_user] ...
Examples
-
用户更改名称
DROP USER 'test'@'localhost';
DROP
RENAME USER 'test'@'localhost' TO 'test1'@'127.0.0.1';
Examples
-
删除用户
DROP USER 'test'@'localhost';
DB
CREATE
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name [create_option] ...
create_option: [DEFAULT] { CHARACTER SET [=] charset_name | COLLATE [=] collation_name}
Examples
-
创建DB
CREATE SCHEMA test_schema COLLATE utf8mb4_general_ci;
ALTER
ALTER {DATABASE | SCHEMA} [db_name] alter_option ...ALTER {DATABASE | SCHEMA} db_name UPGRADE DATA DIRECTORY NAME
alter_option: { [DEFAULT] CHARACTER SET [=] charset_name | [DEFAULT] COLLATE [=] collation_name}
Examples
-
修改DB
ALTER SCHEMA test_schema COLLATE utf8mb4_general_ci;
DROP
DROP {DATABASE | SCHEMA} [IF EXISTS] db_name
Examples
-
删除DB
DROP SCHEMA test_schema;
GRANT
GRANT priv_type [(column_list)] [, priv_type [(column_list)]] ... ON [object_type] priv_level TO user [auth_option] [, user [auth_option]] ... [REQUIRE {NONE | tls_option [[AND] tls_option] ...}] [WITH {GRANT OPTION | resource_option} ...]
GRANT PROXY ON user TO user [, user] ... [WITH GRANT OPTION]
object_type: { TABLE | FUNCTION | PROCEDURE}
priv_level: { * | *.* | db_name.* | db_name.tbl_name | tbl_name | db_name.routine_name}
user: (see Section 6.2.4, “Specifying Account Names”)
auth_option: { IDENTIFIED BY 'auth_string' | IDENTIFIED WITH auth_plugin | IDENTIFIED WITH auth_plugin BY 'auth_string' | IDENTIFIED WITH auth_plugin AS 'auth_string' | IDENTIFIED BY PASSWORD 'auth_string'}
tls_option: { SSL | X509 | CIPHER 'cipher' | ISSUER 'issuer' | SUBJECT 'subject'}
resource_option: { | MAX_QUERIES_PER_HOUR count | MAX_UPDATES_PER_HOUR count | MAX_CONNECTIONS_PER_HOUR count | MAX_USER_CONNECTIONS count}
Permissible Privileges for GRANT and REVOKE
Privilege | Meaning and Grantable Levels |
---|---|
ALL [PRIVILEGES] | Grant all privileges at specified access level except GRANT OPTION and PROXY. |
ALTER | Enable use of ALTER TABLE. Levels: Global, database, table. |
ALTER ROUTINE | Enable stored routines to be altered or dropped. Levels: Global, database, routine. |
CREATE | Enable database and table creation. Levels: Global, database, table. |
CREATE ROUTINE | Enable stored routine creation. Levels: Global, database. |
CREATE TABLESPACE | Enable tablespaces and log file groups to be created, altered, or dropped. Level: Global. |
CREATE TEMPORARY TABLES | Enable use of CREATE TEMPORARY TABLE. Levels: Global, database. |
CREATE USER | Enable use of CREATE USER, DROP USER, RENAME USER, and REVOKE ALL PRIVILEGES. Level: Global. |
CREATE VIEW | Enable views to be created or altered. Levels: Global, database, table. |
DELETE | Enable use of DELETE. Level: Global, database, table. |
DROP | Enable databases, tables, and views to be dropped. Levels: Global, database, table. |
EVENT | Enable use of events for the Event Scheduler. Levels: Global, database. |
EXECUTE | Enable the user to execute stored routines. Levels: Global, database, routine. |
FILE | Enable the user to cause the server to read or write files. Level: Global. |
GRANT OPTION | Enable privileges to be granted to or removed from other accounts. Levels: Global, database, table, routine, proxy. |
INDEX | Enable indexes to be created or dropped. Levels: Global, database, table. |
INSERT | Enable use of INSERT. Levels: Global, database, table, column. |
LOCK TABLES | Enable use of LOCK TABLES on tables for which you have the SELECT privilege. Levels: Global, database. |
PROCESS | Enable the user to see all processes with SHOW PROCESSLIST. Level: Global. |
PROXY | Enable user proxying. Level: From user to user. |
REFERENCES | Enable foreign key creation. Levels: Global, database, table, column. |
RELOAD | Enable use of FLUSH operations. Level: Global. |
REPLICATION CLIENT | Enable the user to ask where source or replica servers are. Level: Global. |
REPLICATION SLAVE | Enable replicas to read binary log events from the source. Level: Global. |
SELECT | Enable use of SELECT. Levels: Global, database, table, column. |
SHOW DATABASES | Enable SHOW DATABASES to show all databases. Level: Global. |
SHOW VIEW | Enable use of SHOW CREATE VIEW. Levels: Global, database, table. |
SHUTDOWN | Enable use of mysqladmin shutdown. Level: Global. |
SUPER | Enable use of other administrative operations such as CHANGE MASTER TO, KILL, PURGE BINARY LOGS, SET GLOBAL, and mysqladmin debug command. Level: Global. |
TRIGGER | Enable trigger operations. Levels: Global, database, table. |
UPDATE | Enable use of UPDATE. Levels: Global, database, table, column. |
USAGE | Synonym for “no privileges” |
Examples
-
删除用户
GRANT ALL ON db1.* TO 'test'@'localhost';
REVOKE
REVOKE priv_type [(column_list)] [, priv_type [(column_list)]] ... ON [object_type] priv_level FROM user [, user] ...
REVOKE ALL [PRIVILEGES], GRANT OPTION FROM user [, user] ...
REVOKE PROXY ON user FROM user [, user] ...
Examples
-
收回INSERT权限
REVOKE INSERT ON db1.* FROM 'test'@'localhost';