diff --git a/myisam/mi_key.c b/myisam/mi_key.c
index a9b5a8b279f12dd1b88a49a52abbcaa6a8624e2f..e06239d3bba82d199d7b72c021a64e325f01d503 100644
--- a/myisam/mi_key.c
+++ b/myisam/mi_key.c
@@ -168,13 +168,18 @@ uint _mi_make_key(register MI_INFO *info, uint keynr, uchar *key,
       }
       continue;
     }
-#ifdef NOT_YET_FIXED_LENGTH_KEY
     if (char_length && length > char_length)
     {
       char_length= my_charpos(cs, pos, pos+length, char_length);
-      set_if_smaller(length, char_length);
+      if (char_length < length)
+      {
+        uint diff= length - char_length;
+        memcpy((byte*) key, pos, char_length);
+        cs->cset->fill(cs, key + char_length, diff, ' ');
+        key+= length;
+        continue;
+      }
     }
-#endif
     memcpy((byte*) key, pos, length);
     key+= length;
   }
diff --git a/mysql-test/r/ctype_utf8.result b/mysql-test/r/ctype_utf8.result
index 4d1b5d54bdabdbd177e6cdf3209b6779ac383279..8ad8be26b620573c01e8c465e54db291fe7137a3 100644
--- a/mysql-test/r/ctype_utf8.result
+++ b/mysql-test/r/ctype_utf8.result
@@ -276,3 +276,26 @@ select c cb20 from t1 where c=repeat('b',20);
 cb20
 bbbbbbbbbbbbbbbbbbbb
 drop table t1;
+create table t1 (c char(3) character set utf8, unique (c(2)));
+insert into t1 values ('1'),('2'),('3'),('4'),('x'),('y'),('z');
+insert into t1 values ('a');
+insert into t1 values ('aa');
+insert into t1 values ('aaa');
+ERROR 23000: Duplicate entry 'aaa' for key 1
+insert into t1 values ('b');
+insert into t1 values ('bb');
+insert into t1 values ('bbb');
+ERROR 23000: Duplicate entry 'bbb' for key 1
+insert into t1 values ('邪');
+insert into t1 values ('邪邪');
+insert into t1 values ('邪邪邪');
+ERROR 23000: Duplicate entry '邪邪邪' for key 1
+insert into t1 values ('斜');
+insert into t1 values ('斜斜');
+insert into t1 values ('斜斜斜');
+ERROR 23000: Duplicate entry '斜斜斜' for key 1
+insert into t1 values ('戟�');
+insert into t1 values ('戟');
+insert into t1 values ('戟戟�');
+ERROR 23000: Duplicate entry '戟' for key 1
+drop table t1;
diff --git a/mysql-test/t/ctype_utf8.test b/mysql-test/t/ctype_utf8.test
index 4e130440a244ed8990a2af562b3059fe961b5842..f25a1ecfd0c12ac91a899ccaaf66ed6f96a019a7 100644
--- a/mysql-test/t/ctype_utf8.test
+++ b/mysql-test/t/ctype_utf8.test
@@ -187,3 +187,30 @@ select c cz from t1 where c='z';
 select c ca10 from t1 where c='aaaaaaaaaa';
 select c cb20 from t1 where c=repeat('b',20);
 drop table t1;
+
+#
+# Bug 4521: unique key prefix interacts poorly with utf8
+# Check fixed length keys
+create table t1 (c char(3) character set utf8, unique (c(2)));
+insert into t1 values ('1'),('2'),('3'),('4'),('x'),('y'),('z');
+insert into t1 values ('a');
+insert into t1 values ('aa');
+--error 1062
+insert into t1 values ('aaa');
+insert into t1 values ('b');
+insert into t1 values ('bb');
+--error 1062
+insert into t1 values ('bbb');
+insert into t1 values ('邪');
+insert into t1 values ('邪邪');
+--error 1062
+insert into t1 values ('邪邪邪');
+insert into t1 values ('斜');
+insert into t1 values ('斜斜');
+--error 1062
+insert into t1 values ('斜斜斜');
+insert into t1 values ('戟�');
+insert into t1 values ('戟');
+--error 1062
+insert into t1 values ('戟戟�');
+drop table t1;