Skip to content

Commit

Permalink
Add global variable max_nonsuper_connections
Browse files Browse the repository at this point in the history
Summary:
The new global variable `max_nonsuper_connections` will enforce the limit for
the sum of all non-admin connections. This will be useful to limit regular user
connections while still allow super users to connect to server.
Closes #389

Reviewed By: anca-agape

Differential Revision: D4123732

Pulled By: tianx

fbshipit-source-id: 7ffb140
  • Loading branch information
tianx authored and Facebook Github Bot committed Nov 8, 2016
1 parent 7ef6ede commit 478fa2a
Show file tree
Hide file tree
Showing 13 changed files with 465 additions and 2 deletions.
81 changes: 81 additions & 0 deletions mysql-test/r/max_nonsuper_connections.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
create user test_user@localhost;
grant all on test to test_user@localhost;
create user super_user@localhost;
grant all on *.* to super_user@localhost with grant option;
SET @start_value = @@global.max_nonsuper_connections;
SET @@global.max_nonsuper_connections = 10;
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
10
connection default;
connect con$i, localhost, test_user,,test;
connect con$i, localhost, test_user,,test;
connect con$i, localhost, test_user,,test;
connect con$i, localhost, test_user,,test;
connect con$i, localhost, test_user,,test;
connect con$i, localhost, test_user,,test;
connect con$i, localhost, test_user,,test;
connect con$i, localhost, test_user,,test;
connect con$i, localhost, test_user,,test;
connect con$i, localhost, test_user,,test;
ERROR 08004: Too many connections
connect con_root, localhost, root,,test;
# connection con_root
connection con_root;
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
10
disconnect con_root;
connection default;
connect con_super, localhost, super_user,,test;
connection con_super;
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
10
mysqltest: At line 1: change user failed: Too many connections
disconnect con_super;
connection con10;
connect con11, localhost, test_user,,test;
disconnect con11;
connection con10;
ERROR 08004: Too many connections
connection default;
disconnect con10;
disconnect con9;
disconnect con8;
disconnect con7;
disconnect con6;
disconnect con5;
disconnect con4;
disconnect con3;
disconnect con2;
disconnect con1;
connection default;
connect con$i, localhost, test_user,,test;
connect con$i, localhost, test_user,,test;
connect con$i, localhost, test_user,,test;
connect con$i, localhost, test_user,,test;
connect con$i, localhost, test_user,,test;
connect con$i, localhost, test_user,,test;
connect con$i, localhost, test_user,,test;
connect con$i, localhost, test_user,,test;
connect con$i, localhost, test_user,,test;
connect con$i, localhost, test_user,,test;
ERROR 08004: Too many connections
connection default;
SET @@global.max_nonsuper_connections = @start_value;
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
0
drop user test_user@localhost;
drop user super_user@localhost;
disconnect con10;
disconnect con9;
disconnect con8;
disconnect con7;
disconnect con6;
disconnect con5;
disconnect con4;
disconnect con3;
disconnect con2;
disconnect con1;
4 changes: 4 additions & 0 deletions mysql-test/r/mysqld--help-notwin-profiling.result
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,9 @@ The following options may be given as the first argument:
max_join_size records return an error
--max-length-for-sort-data=#
Max number of bytes in sorted records
--max-nonsuper-connections=#
The maximum number of total active connections for
non-super user (0 = no limit)
--max-prepared-stmt-count=#
Maximum number of prepared statements in the server
--max-relay-log-size=#
Expand Down Expand Up @@ -1745,6 +1748,7 @@ max-error-count 64
max-heap-table-size 16777216
max-join-size 18446744073709551615
max-length-for-sort-data 1024
max-nonsuper-connections 0
max-prepared-stmt-count 16382
max-relay-log-size 0
max-running-queries 0
Expand Down
4 changes: 4 additions & 0 deletions mysql-test/r/mysqld--help-notwin.result
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,9 @@ The following options may be given as the first argument:
max_join_size records return an error
--max-length-for-sort-data=#
Max number of bytes in sorted records
--max-nonsuper-connections=#
The maximum number of total active connections for
non-super user (0 = no limit)
--max-prepared-stmt-count=#
Maximum number of prepared statements in the server
--max-relay-log-size=#
Expand Down Expand Up @@ -1743,6 +1746,7 @@ max-error-count 64
max-heap-table-size 16777216
max-join-size 18446744073709551615
max-length-for-sort-data 1024
max-nonsuper-connections 0
max-prepared-stmt-count 16382
max-relay-log-size 0
max-running-queries 0
Expand Down
102 changes: 102 additions & 0 deletions mysql-test/suite/sys_vars/r/max_nonsuper_connections_basic.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
SET @start_value = @@global.max_nonsuper_connections;
SELECT @start_value;
@start_value
0
SET @@global.max_nonsuper_connections = DEFAULT;
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
0
SET @@global.max_nonsuper_connections = 100000;
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
100000
SET @@global.max_nonsuper_connections = 99999;
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
99999
SET @@global.max_nonsuper_connections = 65536;
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
65536
SET @@global.max_nonsuper_connections = 1;
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
1
SET @@global.max_nonsuper_connections = 2;
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
2
SET @@global.max_nonsuper_connections = TRUE;
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
1
SET @@global.max_nonsuper_connections = FALSE;
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
0
SET @@global.max_nonsuper_connections = -1;
Warnings:
Warning 1292 Truncated incorrect max_nonsuper_connections value: '-1'
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
0
SET @@global.max_nonsuper_connections = 100000000000;
Warnings:
Warning 1292 Truncated incorrect max_nonsuper_connections value: '100000000000'
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
4294967295
SET @@global.max_nonsuper_connections = 10000.01;
ERROR 42000: Incorrect argument type to variable 'max_nonsuper_connections'
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
4294967295
SET @@global.max_nonsuper_connections = -1024;
Warnings:
Warning 1292 Truncated incorrect max_nonsuper_connections value: '-1024'
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
0
SET @@global.max_nonsuper_connections = ON;
ERROR 42000: Incorrect argument type to variable 'max_nonsuper_connections'
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
0
SET @@global.max_nonsuper_connections = 'test';
ERROR 42000: Incorrect argument type to variable 'max_nonsuper_connections'
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
0
SET @@session.max_nonsuper_connections = 4096;
ERROR HY000: Variable 'max_nonsuper_connections' is a GLOBAL variable and should be set with SET GLOBAL
SELECT @@session.max_nonsuper_connections;
ERROR HY000: Variable 'max_nonsuper_connections' is a GLOBAL variable
SET max_nonsuper_connections = 6000;
ERROR HY000: Variable 'max_nonsuper_connections' is a GLOBAL variable and should be set with SET GLOBAL
SELECT @@max_nonsuper_connections;
@@max_nonsuper_connections
0
SET local.max_nonsuper_connections = 7000;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'max_nonsuper_connections = 7000' at line 1
SELECT local.max_nonsuper_connections;
ERROR 42S02: Unknown table 'local' in field list
SET global.max_nonsuper_connections = 8000;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'max_nonsuper_connections = 8000' at line 1
SELECT global.max_nonsuper_connections;
ERROR 42S02: Unknown table 'global' in field list
SELECT max_nonsuper_connections = @@session.max_nonsuper_connections;
ERROR 42S22: Unknown column 'max_nonsuper_connections' in 'field list'
SELECT @@global.max_nonsuper_connections = VARIABLE_VALUE
FROM INFORMATION_SCHEMA.GLOBAL_VARIABLES
WHERE VARIABLE_NAME='max_nonsuper_connections';
@@global.max_nonsuper_connections = VARIABLE_VALUE
1
SELECT @@max_nonsuper_connections = VARIABLE_VALUE
FROM INFORMATION_SCHEMA.SESSION_VARIABLES
WHERE VARIABLE_NAME='max_nonsuper_connections';
@@max_nonsuper_connections = VARIABLE_VALUE
1
SET @@global.max_nonsuper_connections = @start_value;
SELECT @@global.max_nonsuper_connections;
@@global.max_nonsuper_connections
0
95 changes: 95 additions & 0 deletions mysql-test/suite/sys_vars/t/max_nonsuper_connections_basic.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
--source include/load_sysvars.inc

#
# save original value
#
SET @start_value = @@global.max_nonsuper_connections;
SELECT @start_value;


#
# set default value
#
SET @@global.max_nonsuper_connections = DEFAULT;
SELECT @@global.max_nonsuper_connections;


#
# set various values
#
SET @@global.max_nonsuper_connections = 100000;
SELECT @@global.max_nonsuper_connections;
SET @@global.max_nonsuper_connections = 99999;
SELECT @@global.max_nonsuper_connections;
SET @@global.max_nonsuper_connections = 65536;
SELECT @@global.max_nonsuper_connections;
SET @@global.max_nonsuper_connections = 1;
SELECT @@global.max_nonsuper_connections;
SET @@global.max_nonsuper_connections = 2;
SELECT @@global.max_nonsuper_connections;
SET @@global.max_nonsuper_connections = TRUE;
SELECT @@global.max_nonsuper_connections;
SET @@global.max_nonsuper_connections = FALSE;
SELECT @@global.max_nonsuper_connections;


#
# set invalid values
#
# Value truncated
SET @@global.max_nonsuper_connections = -1;
SELECT @@global.max_nonsuper_connections;
# Value truncated
SET @@global.max_nonsuper_connections = 100000000000;
SELECT @@global.max_nonsuper_connections;
--Error ER_WRONG_TYPE_FOR_VAR
SET @@global.max_nonsuper_connections = 10000.01;
SELECT @@global.max_nonsuper_connections;
# Value truncated
SET @@global.max_nonsuper_connections = -1024;
SELECT @@global.max_nonsuper_connections;

--Error ER_WRONG_TYPE_FOR_VAR
SET @@global.max_nonsuper_connections = ON;
SELECT @@global.max_nonsuper_connections;
--Error ER_WRONG_TYPE_FOR_VAR
SET @@global.max_nonsuper_connections = 'test';
SELECT @@global.max_nonsuper_connections;

--Error ER_GLOBAL_VARIABLE
SET @@session.max_nonsuper_connections = 4096;
--Error ER_INCORRECT_GLOBAL_LOCAL_VAR
SELECT @@session.max_nonsuper_connections;

--Error ER_GLOBAL_VARIABLE
SET max_nonsuper_connections = 6000;
SELECT @@max_nonsuper_connections;
--Error ER_PARSE_ERROR
SET local.max_nonsuper_connections = 7000;
--Error ER_UNKNOWN_TABLE
SELECT local.max_nonsuper_connections;
--Error ER_PARSE_ERROR
SET global.max_nonsuper_connections = 8000;
--Error ER_UNKNOWN_TABLE
SELECT global.max_nonsuper_connections;
--Error ER_BAD_FIELD_ERROR
SELECT max_nonsuper_connections = @@session.max_nonsuper_connections;


#
# Check if the value in GLOBAL & SESSION Tables matches values in variable
#
SELECT @@global.max_nonsuper_connections = VARIABLE_VALUE
FROM INFORMATION_SCHEMA.GLOBAL_VARIABLES
WHERE VARIABLE_NAME='max_nonsuper_connections';

SELECT @@max_nonsuper_connections = VARIABLE_VALUE
FROM INFORMATION_SCHEMA.SESSION_VARIABLES
WHERE VARIABLE_NAME='max_nonsuper_connections';


#
# restore
#
SET @@global.max_nonsuper_connections = @start_value;
SELECT @@global.max_nonsuper_connections;
Loading

0 comments on commit 478fa2a

Please sign in to comment.