我正在尝试向数据库中的表添加外键。让我们考虑以下最小示例:
CREATE DATABASE db_foo;
USE db_foo;
CREATE TABLE a(b VARCHAR(3) PRIMARY KEY);
CREATE TABLE c(d VARCHAR(3), KEY c_ix (d));
ALTER TABLE c ADD CONSTRAINT fk_c_a FOREIGN KEY (d) REFERENCES a(b);
上面给出的代码在版本为5.0.95的MySQL服务器上运行得很好,但是当我在另一台运行MySQL版本5.7.12的服务器上运行它时,会出现以下错误:
ERROR 1142 (42000): REFERENCES command denied to user 'toto'@'xxx.xxx.xx.xx' for table 'a'
所以我的第一个猜测是检查我在两个服务器上是否有相同的权限。这是SHOW GRANTS;
的输出:
MySQL 5.0.95服务器上:
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Grants for toto@% |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'toto'@'%' IDENTIFIED BY PASSWORD '*********' |
| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE ON `db\_%`.* TO 'toto'@'%' |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
MySQL 5.7.12服务器上:
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Grants for toto@% |
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'toto'@'%' |
| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EVENT, TRIGGER ON `db\_%`.* TO 'toto'@'%' |
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
我不知道为什么它会在一个案件中起作用,而在另一个案件中却不起作用,因为事实上,我似乎在两个案件中都有同样的特权。有什么建议吗?
2条答案
按热度按时间qxgroojn1#
在MySQL 5.0/5.1/5.4中,
REFERENCES
grant尚未实现。我第一次看到这个是在MySQL 5.5中,但它仍然不能正常工作。但是,它现在可以正常工作,从那一刻起,你需要向你的用户帐户授予
REFERENCES
权限。参见MySQL 5.5文档:Table 13.3 - Permissible Privileges for GRANT and REVOKE
引用启用外键创建。级别:全局、数据库、表、列。
rn0zuynd2#
你要做的是检查并更新mysql.user表中所需的权限