This blog post will discuss how to use the MySQL super_read_only system variable.
It is well known that replica servers in a master/slave configuration, to avoid breaking replication due to duplicate keys, missing rows or other similar issues, should not receive write queries. It’s a good practice to set read_only=1 on slave servers to prevent any (accidental) writes. Servers acting as replicas will NOT be in read-only mode automatically by default.
Sadly, read_only has a historical issue: users with the SUPER privilege can override the setting and could still run DML queries. Since Percona Server 5.6.21 and MySQL 5.7.8, however, you can use the super_read_only feature to extend the read_only option and apply it to users with SUPER privileges.
Both super_read_only and read_only are disabled by default, and using super_read_only implies that read_only is automatically ON as well. We’ll demonstrate how read_only and super_read only work:
|
1 |
mysql> SET GLOBAL read_only = 1;<br>Query OK, 0 rows affected (0.00 sec)<br> |
As expected, with the read_only variable enabled, users without SUPER privilege won’t be able to INSERT values, and instead, they will get an ERROR 1290 message:
|
1 |
mysql> SELECT @@global.read_only, @@global.super_read_only;<br>+--------------------+--------------------------+<br>| @@global.read_only | @@global.super_read_only |<br>+--------------------+--------------------------+<br>| 1 | 0 |<br>+--------------------+--------------------------+<br>1 row in set (0.01 sec)<br><br>mysql> SHOW GRANTSG<br>*************************** 1. row ***************************<br>Grants for nosuper@localhost: GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE ON *.* TO 'nosuper'@'localhost' IDENTIFIED BY PASSWORD <secret><br>1 row in set (0.00 sec)<br><br>mysql> INSERT INTO test.example VALUES (1);<br>ERROR 1290 (HY000): The MySQL server is running with the --read-only option so it cannot execute this statement<br> |
However, users with SUPER privileges can INSERT values on the table:
|
1 |
mysql> SELECT @@global.read_only, @@global.super_read_only;<br>+--------------------+--------------------------+<br>| @@global.read_only | @@global.super_read_only |<br>+--------------------+--------------------------+<br>| 1 | 0 |<br>+--------------------+--------------------------+<br>1 row in set (0.01 sec)<br><br>mysql> SHOW GRANTSG<br>*************************** 1. row ***************************<br>Grants for super@localhost: GRANT ALL PRIVILEGES ON *.* TO 'super'@'localhost' IDENTIFIED BY PASSWORD '*3E26301B12AE2B8906D9F09785359751700930E8'<br>1 row in set (0.00 sec)<br><br>mysql> INSERT INTO test.example VALUES (1);<br>Query OK, 1 row affected (0.01 sec)<br> |
Now we will enable super_read_only and try to INSERT data again with both users:
|
1 |
mysql> SET GLOBAL super_read_only = 1;<br>Query OK, 0 rows affected (0.00 sec)<br><br>mysql> SELECT @@global.read_only, @@global.super_read_only;<br>+--------------------+--------------------------+<br>| @@global.read_only | @@global.super_read_only |<br>+--------------------+--------------------------+<br>| 1 | 1 |<br>+--------------------+--------------------------+<br>1 row in set (0.00 sec)<br>mysql> SHOW GRANTSG<br>*************************** 1. row ***************************<br>Grants for super@localhost: GRANT ALL PRIVILEGES ON *.* TO 'super'@'localhost' IDENTIFIED BY PASSWORD '*3E26301B12AE2B8906D9F09785359751700930E8'<br>1 row in set (0.00 sec)<br><br>mysql> INSERT INTO test.example VALUES (1);<br>ERROR 1290 (HY000): The MySQL server is running with the --read-only (super) option so it cannot execute this statement |
|
1 |
mysql> SELECT @@global.read_only, @@global.super_read_only;<br>+--------------------+--------------------------+<br>| @@global.read_only | @@global.super_read_only |<br>+--------------------+--------------------------+<br>| 1 | 1 |<br>+--------------------+--------------------------+<br>1 row in set (0.00 sec)<br><br>mysql> SHOW GRANTSG<br>*************************** 1. row ***************************<br>Grants for nosuper@localhost: GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE ON *.* TO 'nosuper'@'localhost' IDENTIFIED BY PASSWORD <secret><br>1 row in set (0.00 sec)<br><br>mysql> INSERT INTO test.example VALUES (1);<br>ERROR 1290 (HY000): The MySQL server is running with the --read-only (super) option so it cannot execute this statement |
As you can see above, now even users with SUPER privileges can’t make updates or modify data. This is useful in replication to ensure that no updates are accepted from the clients, and are only accepted by the master.
When enabling the super_read_only system variable, please keep in mind the following implications:
There are some other implications for read_only that apply to super_read_only as well:
super_read_only on slave servers to ensure that slaves accept updates only from the master server and not from clients.
There are few bugs related to this variable that might be useful to take into consideration if you’re running on Percona Server 5.6:
For more information, please refer to this following documentation links: