diff --git a/myisam/mi_check.c b/myisam/mi_check.c
index a1c3698b3e9f7fa25d7efee8a88702a1624c5cc4..1f6089d0a3c3e25f79a480521069e33e73c91682 100644
--- a/myisam/mi_check.c
+++ b/myisam/mi_check.c
@@ -1585,7 +1585,7 @@ int mi_sort_index(MI_CHECK *param, register MI_INFO *info, my_string name)
   int old_lock;
   MYISAM_SHARE *share=info->s;
   MI_STATE_INFO old_state;
-  DBUG_ENTER("sort_index");
+  DBUG_ENTER("mi_sort_index");
 
   if (!(param->testflag & T_SILENT))
     printf("- Sorting index for MyISAM-table '%s'\n",name);
@@ -1664,7 +1664,7 @@ err:
 err2:
   VOID(my_delete(param->temp_filename,MYF(MY_WME)));
   DBUG_RETURN(-1);
-} /* sort_index */
+} /* mi_sort_index */
 
 
 	 /* Sort records recursive using one index */
@@ -1672,7 +1672,7 @@ err2:
 static int sort_one_index(MI_CHECK *param, MI_INFO *info, MI_KEYDEF *keyinfo,
 			  my_off_t pagepos, File new_file)
 {
-  uint length,nod_flag,used_length;
+  uint length,nod_flag,used_length, key_length;
   uchar *buff,*keypos,*endpos;
   uchar key[MI_MAX_POSSIBLE_KEY_BUFF];
   my_off_t new_page_pos,next_page;
@@ -1693,7 +1693,7 @@ static int sort_one_index(MI_CHECK *param, MI_INFO *info, MI_KEYDEF *keyinfo,
 		llstr(pagepos,llbuff));
     goto err;
   }
-  if ((nod_flag=mi_test_if_nod(buff)))
+  if ((nod_flag=mi_test_if_nod(buff)) || keyinfo->flag & HA_FULLTEXT)
   {
     used_length=mi_getint(buff);
     keypos=buff+2+nod_flag;
@@ -1704,7 +1704,7 @@ static int sort_one_index(MI_CHECK *param, MI_INFO *info, MI_KEYDEF *keyinfo,
       {
 	next_page=_mi_kpos(nod_flag,keypos);
 	_mi_kpointer(info,keypos-nod_flag,param->new_file_pos); /* Save new pos */
-	if (sort_one_index(param,info,keyinfo,next_page, new_file))
+	if (sort_one_index(param,info,keyinfo,next_page,new_file))
 	{
 	  DBUG_PRINT("error",("From page: %ld, keyoffset: %d  used_length: %d",
 			      (ulong) pagepos, (int) (keypos - buff),
@@ -1714,11 +1714,25 @@ static int sort_one_index(MI_CHECK *param, MI_INFO *info, MI_KEYDEF *keyinfo,
 	}
       }
       if (keypos >= endpos ||
-	  ((*keyinfo->get_key)(keyinfo,nod_flag,&keypos,key)) == 0)
+	  (key_length=(*keyinfo->get_key)(keyinfo,nod_flag,&keypos,key)) == 0)
 	break;
-#ifdef EXTRA_DEBUG
-      assert(keypos <= endpos);
-#endif
+      DBUG_ASSERT(keypos <= endpos);
+      if (keyinfo->flag & HA_FULLTEXT)
+      {
+        uint off;
+        int  subkeys;
+        get_key_full_length_rdonly(off, key);
+        subkeys=ft_sintXkorr(key+off);
+        if (subkeys < 0)
+        {
+          next_page= _mi_dpos(info,0,key+key_length);
+          _mi_dpointer(info,keypos-nod_flag-info->s->rec_reflength,
+                       param->new_file_pos); /* Save new pos */
+          if (sort_one_index(param,info,&info->s->ft2_keyinfo,
+                             next_page,new_file))
+            goto err;
+        }
+      }
     }
   }
 
diff --git a/mysql-test/r/fulltext2.result b/mysql-test/r/fulltext2.result
index 0fdb7d4dbd1f9a3b5427363afd6d96f24210c20c..0b1d8eb9a155200c2589ef50ef117f0b04cc582b 100644
--- a/mysql-test/r/fulltext2.result
+++ b/mysql-test/r/fulltext2.result
@@ -7,6 +7,15 @@ FULLTEXT KEY (a)
 repair table t1 quick;
 Table	Op	Msg_type	Msg_text
 test.t1	repair	status	OK
+check table t1;
+Table	Op	Msg_type	Msg_text
+test.t1	check	status	OK
+optimize table t1;
+Table	Op	Msg_type	Msg_text
+test.t1	optimize	status	OK
+check table t1;
+Table	Op	Msg_type	Msg_text
+test.t1	check	status	OK
 select count(*) from t1 where match a against ('aaaxxx');
 count(*)
 260
diff --git a/mysql-test/t/fulltext2.test b/mysql-test/t/fulltext2.test
index cab1d096fe77f66867a56871aa4f9c8ac62fbc39..1d3a5307412c7502b86739c2ff5f45c2a2e5fa52 100644
--- a/mysql-test/t/fulltext2.test
+++ b/mysql-test/t/fulltext2.test
@@ -44,6 +44,9 @@ while ($1)
 
 # converting to two-level
 repair table t1 quick;
+check table t1;
+optimize table t1; # BUG#5327 - mi_sort_index() of 2-level tree
+check table t1;
 
 select count(*) from t1 where match a against ('aaaxxx');
 select count(*) from t1 where match a against ('aaayyy');
@@ -102,6 +105,11 @@ CREATE TABLE t1 (
   FULLTEXT KEY (a)
 ) ENGINE=MyISAM;
 
+#
+# now same as about but w/o repair table
+# 2-level tree created by mi_write
+#
+
 # two-level entry, second-level tree with depth 2
 --disable_query_log
 let $1=260;
diff --git a/scripts/mysql_create_system_tables.sh b/scripts/mysql_create_system_tables.sh
index e95d65a1224c61d3a0ea857133f2b29d86297c4f..f524b32238806abddbc086ba844f307f8034987d 100644
--- a/scripts/mysql_create_system_tables.sh
+++ b/scripts/mysql_create_system_tables.sh
@@ -68,7 +68,7 @@ then
   c_d="$c_d   Lock_tables_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
   c_d="$c_d PRIMARY KEY Host (Host,Db,User),"
   c_d="$c_d KEY User (User)"
-  c_d="$c_d )"
+  c_d="$c_d ) engine=MyISAM"
   c_d="$c_d CHARACTER SET utf8 COLLATE utf8_bin"
   c_d="$c_d comment='Database privileges';"
   
@@ -98,7 +98,7 @@ then
   c_h="$c_h  Create_tmp_table_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
   c_h="$c_h  Lock_tables_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
   c_h="$c_h  PRIMARY KEY Host (Host,Db)"
-  c_h="$c_h )"
+  c_h="$c_h ) engine=MyISAM"
   c_h="$c_h CHARACTER SET utf8 COLLATE utf8_bin"
   c_h="$c_h comment='Host privileges;  Merged with database privileges';"
 fi
@@ -142,7 +142,7 @@ then
   c_u="$c_u   max_updates int(11) unsigned DEFAULT 0  NOT NULL,"
   c_u="$c_u   max_connections int(11) unsigned DEFAULT 0  NOT NULL,"
   c_u="$c_u   PRIMARY KEY Host (Host,User)"
-  c_u="$c_u )"
+  c_u="$c_u ) engine=MyISAM"
   c_u="$c_u CHARACTER SET utf8 COLLATE utf8_bin"
   c_u="$c_u comment='Users and global privileges';"
 
@@ -182,7 +182,7 @@ then
   c_f="$c_f   dl char(128) DEFAULT '' NOT NULL,"
   c_f="$c_f   type enum ('function','aggregate') NOT NULL,"
   c_f="$c_f   PRIMARY KEY (name)"
-  c_f="$c_f )"
+  c_f="$c_f ) engine=MyISAM"
   c_f="$c_f CHARACTER SET utf8 COLLATE utf8_bin"
   c_f="$c_f   comment='User defined functions';"
 fi
@@ -204,7 +204,7 @@ then
   c_t="$c_t   Column_priv set('Select','Insert','Update','References') DEFAULT '' NOT NULL,"
   c_t="$c_t   PRIMARY KEY (Host,Db,User,Table_name),"
   c_t="$c_t   KEY Grantor (Grantor)"
-  c_t="$c_t )"
+  c_t="$c_t ) engine=MyISAM"
   c_t="$c_t CHARACTER SET utf8 COLLATE utf8_bin"
   c_t="$c_t   comment='Table privileges';"
 fi
@@ -224,7 +224,7 @@ then
   c_c="$c_c   Timestamp timestamp(14),"
   c_c="$c_c   Column_priv set('Select','Insert','Update','References') DEFAULT '' NOT NULL,"
   c_c="$c_c   PRIMARY KEY (Host,Db,User,Table_name,Column_name)"
-  c_c="$c_c )"
+  c_c="$c_c ) engine=MyISAM"
   c_c="$c_c CHARACTER SET utf8 COLLATE utf8_bin"
   c_c="$c_c   comment='Column privileges';"
 fi
@@ -244,7 +244,7 @@ then
   c_ht="$c_ht   url              varchar(128) not null,"
   c_ht="$c_ht   primary key      (help_topic_id),"
   c_ht="$c_ht   unique index     (name)"
-  c_ht="$c_ht )"
+  c_ht="$c_ht ) engine=MyISAM"
   c_ht="$c_ht CHARACTER SET utf8"
   c_ht="$c_ht   comment='help topics';"
 fi
@@ -264,7 +264,7 @@ then
   c_hc="$c_hc   url                varchar(128) not null,"
   c_hc="$c_hc   primary key        (help_category_id),"
   c_hc="$c_hc   unique index       (name)"
-  c_hc="$c_hc )"
+  c_hc="$c_hc ) engine=MyISAM"
   c_hc="$c_hc CHARACTER SET utf8"
   c_hc="$c_hc   comment='help categories';"
 fi
@@ -280,7 +280,7 @@ then
   c_hk="$c_hk   name             varchar(64) not null,"
   c_hk="$c_hk   primary key      (help_keyword_id),"
   c_hk="$c_hk   unique index     (name)"
-  c_hk="$c_hk )"
+  c_hk="$c_hk ) engine=MyISAM"
   c_hk="$c_hk CHARACTER SET utf8"
   c_hk="$c_hk   comment='help keywords';"
 fi
@@ -295,7 +295,7 @@ then
   c_hr="$c_hr   help_topic_id    int unsigned not null references help_topic,"
   c_hr="$c_hr   help_keyword_id  int unsigned not null references help_keyword,"
   c_hr="$c_hr   primary key      (help_keyword_id, help_topic_id)"
-  c_hr="$c_hr )"
+  c_hr="$c_hr ) engine=MyISAM"
   c_hr="$c_hr CHARACTER SET utf8"
   c_hr="$c_hr   comment='keyword-topic relation';"
 fi
@@ -310,8 +310,7 @@ then
   c_tzn="$c_tzn   Name char(64) NOT NULL,"
   c_tzn="$c_tzn   Time_zone_id int unsigned NOT NULL,"
   c_tzn="$c_tzn   PRIMARY KEY Name (Name)"
-  c_tzn="$c_tzn )"
-  c_tzn="$c_tzn CHARACTER SET utf8"
+  c_tzn="$c_tzn ) engine=MyISAM CHARACTER SET utf8"
   c_tzn="$c_tzn   comment='Time zone names';"
   
   if test "$1" = "test" 
@@ -333,8 +332,7 @@ then
   c_tz="$c_tz   Time_zone_id int unsigned NOT NULL auto_increment,"
   c_tz="$c_tz   Use_leap_seconds enum('Y','N') DEFAULT 'N' NOT NULL,"
   c_tz="$c_tz   PRIMARY KEY TzId (Time_zone_id)"
-  c_tz="$c_tz )"
-  c_tz="$c_tz CHARACTER SET utf8"
+  c_tz="$c_tz ) engine=MyISAM CHARACTER SET utf8"
   c_tz="$c_tz   comment='Time zones';"
   
   if test "$1" = "test" 
@@ -355,8 +353,7 @@ then
   c_tzt="$c_tzt   Transition_time bigint signed NOT NULL,"
   c_tzt="$c_tzt   Transition_type_id int unsigned NOT NULL,"
   c_tzt="$c_tzt   PRIMARY KEY TzIdTranTime (Time_zone_id, Transition_time)"
-  c_tzt="$c_tzt )"
-  c_tzt="$c_tzt CHARACTER SET utf8"
+  c_tzt="$c_tzt ) engine=MyISAM CHARACTER SET utf8"
   c_tzt="$c_tzt   comment='Time zone transitions';"
   
   if test "$1" = "test" 
@@ -578,8 +575,7 @@ then
   c_tztt="$c_tztt   Is_DST tinyint unsigned DEFAULT 0 NOT NULL,"
   c_tztt="$c_tztt   Abbreviation char(8) DEFAULT '' NOT NULL,"
   c_tztt="$c_tztt   PRIMARY KEY TzIdTrTId (Time_zone_id, Transition_type_id)"
-  c_tztt="$c_tztt )"
-  c_tztt="$c_tztt CHARACTER SET utf8"
+  c_tztt="$c_tztt ) engine=MyISAM CHARACTER SET utf8"
   c_tztt="$c_tztt   comment='Time zone transition types';"
   
   if test "$1" = "test" 
@@ -615,8 +611,7 @@ then
   c_tzls="$c_tzls   Transition_time bigint signed NOT NULL,"
   c_tzls="$c_tzls   Correction int signed NOT NULL,"
   c_tzls="$c_tzls   PRIMARY KEY TranTime (Transition_time)"
-  c_tzls="$c_tzls )"
-  c_tzts="$c_tzts CHARACTER SET utf8"
+  c_tzls="$c_tzls ) engine=MyISAM CHARACTER SET utf8"
   c_tzls="$c_tzls   comment='Leap seconds information for time zones';"
   
   if test "$1" = "test"