mysql中的只读表

时间:2012-09-10 10:18:40

标签: mysql database

我正在使用mysql数据库。因为我有一个名为tbl_user的表,我需要将其作为只读表更改为每个用户。如何将表更改为只读?

5 个答案:

答案 0 :(得分:5)

要授予所有用户选择权,请使用public而不是完整的用户列表

REVOKE ALL ON tbl_user FROM PUBLIC
GRANT SELECT ON tbl_user TO PUBLIC

答案 1 :(得分:3)

使用REVOKE命令并撤消该表中用户的INSERT / UPDATE / DELETE权限

REVOKE
    priv_type [(column_list)]
      [, priv_type [(column_list)]] ...
    ON [object_type] priv_level
    FROM user [, user] ...

参考Documentation

答案 2 :(得分:3)

您需要为所有用户对此表进行REVOKE更新/修改/ GRANT权限并添加SELECT。

REVOKE ALL ON readonly_table FROM tbl_user ;
GRANT SELECT ON readonly_table TO tbl_user;

如果用户在readonly_table上有grant选项,请执行以下命令:

REVOKE GRANT OPTION ON readonly_table FROM tbl_user;

了解The MySQL Access Privilege SystemREVOKE documentation

答案 3 :(得分:3)

除了撤销权限外,另一种方法是使用触发器

/* triggers */
delimiter //

DROP TRIGGER IF EXISTS stop_table_insert;
CREATE TRIGGER stop_table_insert
  BEFORE INSERT ON `table`
FOR EACH ROW
BEGIN
   SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Database maintainance';
END;//

DROP TRIGGER IF EXISTS stop_table_update;
CREATE TRIGGER stop_table_update
  BEFORE UPDATE ON `table`
FOR EACH ROW
BEGIN 
   SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Database maintainance';
END;//

DROP TRIGGER IF EXISTS stop_table_delete;
CREATE TRIGGER stop_table_delete
  BEFORE DELETE ON `table`
FOR EACH ROW
BEGIN
   SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Database maintainance';
END;//


delimiter ;

答案 4 :(得分:2)

GRANT SELECT ON tbl_user TO user;
GRANT SELECT, INSERT, UPDATE, DELETE ON tbl_user TO user;