diff --git a/mysql-test/r/variables.result b/mysql-test/r/variables.result
index 71e26c42b702de1ddd3d19f4a9dbee5008550d0b..c56ccc3641ae00cc1c2dd6825904c1578fab1b5e 100644
--- a/mysql-test/r/variables.result
+++ b/mysql-test/r/variables.result
@@ -406,3 +406,13 @@ set @a=@b, @b=@a;
 select @a, @b;
 @a	@b
 2	1
+set @@global.global.key_buffer_size= 1;
+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
+set GLOBAL global.key_buffer_size= 1;
+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
+SELECT @@global.global.key_buffer_size;
+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
+SELECT @@global.session.key_buffer_size;
+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
+SELECT @@global.local.key_buffer_size;
+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
diff --git a/mysql-test/t/variables.test b/mysql-test/t/variables.test
index 05ac0427a9201edc9095baaa577827387b80bd2d..78fab3afe423bd3a9689f3e56948577d47098593 100644
--- a/mysql-test/t/variables.test
+++ b/mysql-test/t/variables.test
@@ -285,3 +285,17 @@ select @@global.max_user_connections,@@local.max_join_size;
 set @a=1, @b=2;
 set @a=@b, @b=@a;
 select @a, @b;
+
+#
+# Bug#2586:Disallow global/session/local as structured var. instance names
+#
+--error 1149
+set @@global.global.key_buffer_size= 1;
+--error 1149
+set GLOBAL global.key_buffer_size= 1;
+--error 1149
+SELECT @@global.global.key_buffer_size;
+--error 1149
+SELECT @@global.session.key_buffer_size;
+--error 1149
+SELECT @@global.local.key_buffer_size;
diff --git a/sql/item_func.cc b/sql/item_func.cc
index 878c4d2ea5f3335894450b9534f915d707ec33b0..88c2bf824c684d072bc9c919e5f675cf70bd552d 100644
--- a/sql/item_func.cc
+++ b/sql/item_func.cc
@@ -29,6 +29,16 @@
 #include <ft_global.h>
 
 
+bool check_reserved_words(LEX_STRING *name)
+{
+  if (!my_strcasecmp(system_charset_info, name->str, "GLOBAL") ||
+      !my_strcasecmp(system_charset_info, name->str, "LOCAL") ||
+      !my_strcasecmp(system_charset_info, name->str, "SESSION"))
+    return TRUE;
+  return FALSE;
+}
+
+
 static void my_coll_agg_error(DTCollation &c1, DTCollation &c2,
 			      const char *fname)
 {
@@ -2957,6 +2967,12 @@ Item *get_system_var(THD *thd, enum_var_type var_type, LEX_STRING name,
 			   (uint) strlen(server_version),
 			   system_charset_info);
 
+  if (name.str && component.str && check_reserved_words(&name))
+  {
+    net_printf(thd, ER_SYNTAX_ERROR);
+    return 0;
+  }
+
   Item *item;
   sys_var *var;
   char buff[MAX_SYS_VAR_LENGTH*2+4+8], *pos;
diff --git a/sql/mysql_priv.h b/sql/mysql_priv.h
index 8f86d9990fe4a66aab2ab9d7f57b9869ea6c67d9..56e1a172d0b177dc5ebd1477d080f676a8600749 100644
--- a/sql/mysql_priv.h
+++ b/sql/mysql_priv.h
@@ -751,6 +751,9 @@ bool open_log(MYSQL_LOG *log, const char *hostname,
 /* mysqld.cc */
 extern void yyerror(const char*);
 
+/* item_func.cc */
+extern bool check_reserved_words(LEX_STRING *name);
+
 /* strfunc.cc */
 ulonglong find_set(TYPELIB *typelib,const char *x, uint length,
 		   char **err_pos, uint *err_len, bool *set_warning);
diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy
index f317219bd38cc82f19e1097246f41c9220da2238..0f9147b7834214d87d73ea123c4e09a4e0fb6ba9 100644
--- a/sql/sql_yacc.yy
+++ b/sql/sql_yacc.yy
@@ -5034,6 +5034,11 @@ internal_variable_name:
 	}
 	| ident '.' ident
 	  {
+            if (check_reserved_words(&$1))
+            {
+              net_printf(YYTHD, ER_SYNTAX_ERROR);
+              YYABORT;
+            }
 	    sys_var *tmp=find_sys_var($3.str, $3.length);
 	    if (!tmp)
 	      YYABORT;