今天看到语句set global read_only=on; 以为这条语句只对设置变量之后的连接才有效果。
因为记得set global 就是只对本设置之后再次连接到数据库上的连接才有效,对于已经存在的连接,没有效果。
查看了一下文档:
If you change a global system variable, the value is remembered and used for new connections until
the server restarts.
但是测试read_only:
[mysql@localhost ~]$ mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.23-enterprise-commercial-advanced-log MySQL Enterprise Server - Advanced Edition (Commercial)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> grant all on a.* to test identified by 'test';
Query OK, 0 rows affected (0.00 sec)
mysql> select user,host ,super_priv from mysql.user;
+-------+-----------------------+------------+
| user | host | super_priv |
+-------+-----------------------+------------+
| root | localhost | Y |
| root | localhost.localdomain | Y |
| root | 127.0.0.1 | Y |
| root | ::1 | Y |
| test | % | N |
| ttt | % | Y |
| ttttt | % | Y |
+-------+-----------------------+------------+
7 rows in set (0.00 sec)
[mysql@localhost ~]$ mysql -u test -ptest
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.23-enterprise-commercial-advanced-log MySQL Enterprise Server - Advanced Edition (Commercial)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use a;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+-------------+
| Tables_in_a |
+-------------+
| aa |
| aaaa在 |
| t |
| t2 |
| t3 |
| t3_new |
| tt |
+-------------+
7 rows in set (0.00 sec)
mysql> desc t;
+---------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+-------+
| sysdate | int(11) | YES | | NULL | |
+---------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)
mysql> insert into t values (1);
Query OK, 1 row affected (0.06 sec)
这时在另一个有super权限会话中:
mysql> set global read_only=on;
Query OK, 0 rows affected (0.00 sec)
然后回到已经连接的test用户中去:
mysql> insert into t values (1);
ERROR 1290 (HY000): The MySQL server is running with the --read-only option so it cannot execute this statement
mysql> show variables like '%read_only%';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| innodb_read_only | OFF |
| read_only | ON |
| tx_read_only | OFF |
+------------------+-------+
3 rows in set (0.00 sec)
因为记得set global 就是只对本设置之后再次连接到数据库上的连接才有效,对于已经存在的连接,没有效果。
查看了一下文档:
If you change a global system variable, the value is remembered and used for new connections until
the server restarts.
但是测试read_only:
[mysql@localhost ~]$ mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.23-enterprise-commercial-advanced-log MySQL Enterprise Server - Advanced Edition (Commercial)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> grant all on a.* to test identified by 'test';
Query OK, 0 rows affected (0.00 sec)
mysql> select user,host ,super_priv from mysql.user;
+-------+-----------------------+------------+
| user | host | super_priv |
+-------+-----------------------+------------+
| root | localhost | Y |
| root | localhost.localdomain | Y |
| root | 127.0.0.1 | Y |
| root | ::1 | Y |
| test | % | N |
| ttt | % | Y |
| ttttt | % | Y |
+-------+-----------------------+------------+
7 rows in set (0.00 sec)
[mysql@localhost ~]$ mysql -u test -ptest
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.23-enterprise-commercial-advanced-log MySQL Enterprise Server - Advanced Edition (Commercial)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use a;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+-------------+
| Tables_in_a |
+-------------+
| aa |
| aaaa在 |
| t |
| t2 |
| t3 |
| t3_new |
| tt |
+-------------+
7 rows in set (0.00 sec)
mysql> desc t;
+---------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+-------+
| sysdate | int(11) | YES | | NULL | |
+---------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)
mysql> insert into t values (1);
Query OK, 1 row affected (0.06 sec)
这时在另一个有super权限会话中:
mysql> set global read_only=on;
Query OK, 0 rows affected (0.00 sec)
然后回到已经连接的test用户中去:
mysql> insert into t values (1);
ERROR 1290 (HY000): The MySQL server is running with the --read-only option so it cannot execute this statement
mysql> show variables like '%read_only%';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| innodb_read_only | OFF |
| read_only | ON |
| tx_read_only | OFF |
+------------------+-------+
3 rows in set (0.00 sec)
再次查看文档:
原来read_only的范围只有 global 。没有session级别。对于别的只有global级别的变量,这种同样有效。
