Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> create table orders
-> (
-> order_id int not null auto_increment,
-> order_product char(50) not null,
-> order_product_type char(50) not null,
-> cust_id int not null,
-> order_date datetime not null,
-> order_price double not null,
-> order_amount int not null,
-> primart key (order_id),
-> foreign key (cust_id)
-> references customers(cust_id)
-> on delete restrict
-> on update restrict
-> );
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key (order_id),
mysql> create table orders ( order_id int not null auto_increment, order_product char(50) not null, order_product_type char(50) not null, cust_id int not null, order_date datetime not null, order_price double not null, order_amount int not null, primary key (order_id), foreign key (cust_id) references customers(cust_id) on delete restrict on update restrict );
mysql> grant all on mysql_test.* to 'zhangsan'@'localhost';
Query OK, 0 rows affected (0.01 sec)
复制代码
例子4:授予 zhangsan 创建用户的权限
mysql> grant create user on *.* to 'zhangsan'@'localhost';
Query OK, 0 rows affected (0.01 sec)
mysql>
复制代码
"priv_type" 的使用
授予表权限时,可以指定为以下值
SELECT
INSERT
DELETE
UPDATE
REFERENCES
CREATE
ALTER
INDEX
DROP
ALL 或 ALL PRIVILEGES
授予列权限时:
SELECT
INSERT
UPDATE
权限的后面需要加上列名列表 column_list
授予数据库权限时:
SELECT
INSERT
DELETE
UPDATE
REFERENCES
CREATE
ALTER
INDEX
DROP
CREATE TEMPORARY TABLES
CREATE VIEW
SHOW VIEW
CREATE ROUTINE
ALTER ROUTINE
EXECUTE ROUTINE
LOCK TABLES
ALL 或 ALL PRIVILEGES
授予用户权限时:
授予数据库权限时的所有值
CREATE USER
SHOW DATABASES
2 权限的转移
权限的转移可以通过在 GRANT 语句中使用 WITH 子句来实现。
如果将 WITH 子句指定为关键字 "WITH GRANT OPTION",则表示 TO 子句中所指定的所有用户都具有把自己所拥有授予给其他用户的权利,而无论那些其他用户是否拥有该权限。
例子:
~
➜ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1173
Server version: 8.0.32 Homebrew
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> grant select,update on mysql_test.customers to 'zhou'@'localhost' identified by '123' with grant option;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'identified by '123' with grant option' at line 1
# mysql 8.0+ 报错 先创建用户 后授权
mysql> create user 'zhou'@'localhost' identified by '12345678';
Query OK, 0 rows affected (0.01 sec)
mysql> grant select,update on mysql_test.customers to 'zhou'@'localhost' identified by '12345678' with grant option;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'identified by '12345678' with grant option' at line 1
mysql> grant select,update on mysql_test.customers to 'zhou'@'localhost' with grant option;
Query OK, 0 rows affected (0.01 sec)
mysql>
复制代码
3权限的撤销
当需要回收某些特定的权限时
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]...
复制代码
例子:
mysql> revoke select on mysql_test.customers from 'zhou'@'localhost';
Query OK, 0 rows affected (0.00 sec)
mysql>
复制代码
要使用 REVOKE 语句,必须拥有 mysql 数据库的全局 CREATE USER 权限或 UPDATE 权限