Fix "1071 Specified key was too long; max key length is 767 bytes" (MySQL, Laravel)
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
- ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes
- ERROR 1071 (42000): Specified key was too long; max key length is 3072 bytes
- Illuminate\Database\QueryException: SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (Connection: mysql, SQL: alter table `users` add unique `users_email_unique`(`email`))
- SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users` add unique `users_email_unique`(`email`))
The index you are creating exceeds the maximum size allowed by MySQL or MariaDB: in utf8mb4, a VARCHAR(255) takes up to 1020 bytes, more than the 767-byte limit of older servers. With Laravel, add Schema::defaultStringLength(191); in AppServiceProvider, or better, move to MySQL 5.7.7+ / MariaDB 10.2.2+, which accept 3072 bytes.
What this error means
InnoDB limits the size of index keys. With the COMPACT and REDUNDANT row formats, the defaults up to MySQL 5.6 and MariaDB 10.1, that limit is 767 bytes; with DYNAMIC or COMPRESSED (and innodb_large_prefix), it rises to 3072 bytes. MySQL reserves the maximum possible size: in utf8mb4 (4 bytes per character, needed for emojis), a VARCHAR(255) counts as 1020 bytes, and a VARCHAR(191) as 764 bytes. Laravel uses utf8mb4 and 255-character strings by default, hence the error on the users_email_unique unique index in the very first migration. The 1000-byte message comes from the MyISAM engine.
Common causes
- Old server: MySQL 5.6 or earlier, MariaDB 10.1 or earlier (often on shared hosting or an old Docker image), with the 767-byte limit.
- Index on a
VARCHAR(255)inutf8mb4: uniqueemailcolumn, Laravelstring()key, composite index over several strings. COMPACTrow format on an existing table, orinnodb_large_prefixdisabled on MySQL 5.6 / MariaDB 10.1.- MyISAM table (1000-byte limit): default engine of an old server or an imported table.
- Index on a very long
VARCHAR, even on a recent server: beyond 768 characters inutf8mb4, the 3072-byte limit is exceeded.
Step-by-step fix
-
Identify the server version and configuration
These queries show the version, the default row format and the character set.
innodb_large_prefixno longer exists on MySQL 8: it is normal if it does not show up.SELECT VERSION(); SHOW VARIABLES LIKE 'innodb_default_row_format'; SHOW VARIABLES LIKE 'innodb_large_prefix'; SHOW VARIABLES LIKE 'character_set_server'; -
Laravel: reduce the default string length
In
app/Providers/AppServiceProvider.php, set the default length to 191 characters (191 × 4 = 764 bytes, under the limit). It applies to the migrations run afterwards.use Illuminate\Support\Facades\Schema; public function boot(): void { Schema::defaultStringLength(191); } -
Or limit only the indexed column
If you prefer to keep 255 characters elsewhere, give an explicit length only to the indexed columns.
$table->string('email', 191)->unique(); -
Run the migration again cleanly
The migration failed halfway: the
userstable may exist without its index, and a new attempt will give "Table already exists". In development only,migrate:freshdrops every table and replays the migrations. In production, drop the incomplete table by hand, then runphp artisan migrate.# development only: drops ALL tables php artisan migrate:fresh -
Without Laravel: switch the table to DYNAMIC or use a prefix index
On MySQL 5.7+ or MariaDB 10.2+, the
DYNAMICformat raises the limit to 3072 bytes. Otherwise, a prefix index only indexes the first 191 characters; be careful, on aUNIQUEindex, uniqueness then only applies to that prefix.ALTER TABLE users ENGINE=InnoDB ROW_FORMAT=DYNAMIC; ALTER TABLE users ADD UNIQUE KEY users_email_unique (email); -- or a prefix index CREATE INDEX users_name_index ON users (name(191)); -
MySQL 5.6 / MariaDB 10.1 server you manage
Enable large index prefixes in
my.cnf([mysqld]section), restart the server, then create the tables withROW_FORMAT=DYNAMIC. With Laravel, set'engine' => 'InnoDB ROW_FORMAT=DYNAMIC'in themysqlconnection ofconfig/database.php. The real fix is still to upgrade the server: these versions no longer get security patches.[mysqld] innodb_file_format = Barracuda innodb_file_per_table = 1 innodb_large_prefix = 1
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- Use MySQL 8 or MariaDB 10.6+ in development and production, with the same version everywhere (pinned Docker image).
- Keep
utf8mb4: going back toutf8(an alias forutf8mb3) works around the error but breaks storage of emojis and some characters. - Give indexed columns a realistic length (
email191 or 254,slug191) instead of 255 by habit.
Frequently asked questions
Why 191 and not 255?
In utf8mb4, each character can take 4 bytes: 191 × 4 = 764 bytes, just under the 767 limit, while 192 × 4 = 768 exceeds it.
Is Schema::defaultStringLength(191) still useful in 2026?
Only if your server is MySQL 5.6 or earlier, or MariaDB 10.1 or earlier. On MySQL 5.7.7+ and MariaDB 10.2.2+, the limit is 3072 bytes and the line is unnecessary.
What does "max key length is 3072 bytes" mean?
Your server is recent, but the index really is too large: a very long string or a composite index over several VARCHAR(255). Reduce the lengths, index a prefix or a hash column.
Useful tools for this error
More Databases (SQL) errors
- Fix "ERROR 1045 (28000): Access denied for user 'root'@'localhost'" (MySQL, MariaDB)
- Fix "ERROR 1062 (23000): Duplicate entry for key" (MySQL, MariaDB, PostgreSQL)
- Fix "Deadlock found when trying to get lock; try restarting transaction" (MySQL, PostgreSQL)
- Fix FATAL: password authentication failed for user "postgres" (PostgreSQL)
- Fix "ERROR 1040: Too many connections" (MySQL) and "too many clients already" (PostgreSQL)
- Fix "ERROR 2006 (HY000): MySQL server has gone away"
- Fix "ERROR 1054 (42S22): Unknown column in 'field list'" (MySQL)
- Fix "ERROR: relation "users" does not exist" (PostgreSQL)