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

柔情痞子 提交于 2019-11-28 16:42:43
Flo Doe

To answer your original question, you can put your whole database to read only mode by this commands:

FLUSH TABLES WITH READ LOCK;
SET GLOBAL read_only = 1;

and back to normal mode with:

SET GLOBAL read_only = 0;
UNLOCK TABLES;

Beware that this is an operation which will have deep impact on the behavior of the database. So before executing this, read the available documentation to the commands above. A much more common way is to revoke DML privileges from the specific user and afterwards grant them back.

txyoji

If you're using MySQL 5.6 or newer and InnoDB, you can make a session read-only.

SET SESSION TRANSACTION READ ONLY;

https://dev.mysql.com/doc/refman/5.6/en/set-transaction.html

"READ ONLY" also offers a modest performance benefit.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!