Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Using the super_read_only System Variable

DZone's Guide to

Using the super_read_only System Variable

This blog post will discuss how to use the MySQL super_read_only system variable to prevent people from making unauthorized DML queries on your slave servers.

· Database Zone
Free Resource

Read why times series is the fastest growing database category.

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:

mysql> SET GLOBAL read_only = 1;
Query OK, 0 rows affected (0.00 sec)


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:

mysql> SELECT @@global.read_only, @@global.super_read_only;
+--------------------+--------------------------+
| @@global.read_only | @@global.super_read_only |
+--------------------+--------------------------+
|                  1 |                        0 |
+--------------------+--------------------------+
1 row in set (0.01 sec)
mysql> SHOW GRANTSG
*************************** 1. row ***************************
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>
1 row in set (0.00 sec)
mysql> INSERT INTO test.example VALUES (1);
ERROR 1290 (HY000): The MySQL server is running with the --read-only option so it cannot execute this statement


However, users with SUPER privileges can INSERT values on the table:

mysql> SELECT @@global.read_only, @@global.super_read_only;
+--------------------+--------------------------+
| @@global.read_only | @@global.super_read_only |
+--------------------+--------------------------+
|                  1 |                        0 |
+--------------------+--------------------------+
1 row in set (0.01 sec)
mysql> SHOW GRANTSG
*************************** 1. row ***************************
Grants for super@localhost: GRANT ALL PRIVILEGES ON *.* TO 'super'@'localhost' IDENTIFIED BY PASSWORD '*3E26301B12AE2B8906D9F09785359751700930E8'
1 row in set (0.00 sec)
mysql> INSERT INTO test.example VALUES (1);
Query OK, 1 row affected (0.01 sec)


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:

  • Setting super_read_only ON implicitly forces read_only ON.
  • Setting read_only OFF implicitly forces super_read_only OFF.

There are some other implications for read_only that apply to  super_read_only as well:

  • Operations on temporary tables are allowed no matter how these variables are set:
    • Updates performed by slave threads are permitted if the server is a replication slave. In replication setups, it can be useful to enable super_read_only on slave servers to ensure that slaves accept updates only from the master server and not from clients.
  • OPTIMIZE TABLE and ANALYZE TABLE operations are allowed as well, since the purpose of the read-only mode is to prevent changes to table structure or contents, but not to table metadata like index stats.
  • You will need to manually disable it when you promote a replica server to the role of master.

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:

Learn how to get 20x more performance than Elastic by moving to a Time Series database.

Topics:
mysql ,database ,read ,super

Published at DZone with permission of Pablo Padua, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}