How do I set MySQL temporarily to read-only through the command line?

前端 未结 3 1456
刺人心
刺人心 2020-12-12 17:33

I\'m creating a bash script which, among other things, gathers some data from a MySQL database. My MySQL user has write privileges, but for safety reasons I would like to te

3条回答
  •  情歌与酒
    2020-12-12 18:02

    Well, if the user right now has all privileges first, you need to revoke it

    $>mysql -u DB_USER -pDB_PASS --execute="REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'YOUR_USER';"
    

    After that you give him, the select permission

    $>mysql -u DB_USER -pDB_PASS --execute="GRANT SELECT ON 'YOUR_DATABASE'@.* TO 'YOUR_USER'@'%';FLUSH PRIVILEGES;"
    

    Do your stuff and after that grant privileges again to the user

    $>mysql -u DB_USER -pDB_PASS --execute="GRANT ALL ON 'YOUR_DATABASE'@.* TO 'YOUR_USER'@'%';FLUSH PRIVILEGES;"
    

    And that's all folks

    NOTE: Review for quotation, perhaps i forgot something

提交回复
热议问题