Prevent non-replication writes to MySQL slave?
Solution 1:
Enable the read-only
option in my.cnf. It can also be specified as a flag on the command line using --read-only
with mysqld.
Solution 2:
As an alternative to setting read_only=1
(e.g. when there are other scratchpad/reporting/development databases on the slave instance), I sometimes strip all privileges other than SELECT from all users to the DB that I am replicating.
That is, after running the GRANT command on the master, I run the REVOKE command on the slave.
Solution 3:
As of MySQL 5.7.8, there is now a super_read_only
option, which prevents even SUPER users from performing client updates. It does not disrupt the replication process. As with other settings, it can be set:
- in command line format (
--super_read_only=ON
), - as a variable in my.cnf (
super_read_only=1
), or - from the client prompt (
SET GLOBAL super_read_only = 1;
).
Note that:
- Enabling
super_read_only
implicitly enablesread_only
- Disabling
read_only
implicitly disablessuper_read_only
Some caveats:
- Neither
read_only
norsuper_read_only
will prevent operations on temporary tables. - They will not prevent metadata operations like ANALYZE TABLE and OPTIMIZE TABLE.
- Bugs for certain queries with
super_read_only
enabled have been reported.
Reference: https://www.percona.com/blog/2016/09/27/using-the-super_read_only-system-variable/