Knowledge
MySQL 1071: Specified key was too long (Laravel migration)
#Laravel
This migration error happens on older MySQL with the utf8mb4 charset, where indexed string columns exceed the index size limit. The fix is a one-line default in your service provider.
Published by Mark van Eijk on June 23, 2026 · 1 minute read
- About the error
- Why do I see this error
- Solution
- Recommended: set a default string length
- Better long-term: upgrade MySQL
- Per-column alternative
About the error
Running php artisan migrate on a fresh install against older MySQL throws:
SQLSTATE[42000]: Syntax error or access violation: 1071
Specified key was too long; max key length is 767 bytes
It almost always fails on the users table when creating a unique index on the email or another VARCHAR(255) column.
Why do I see this error
Laravel defaults to the utf8mb4 charset (proper 4-byte Unicode, needed for emoji and many scripts). In utf8mb4 each character can take up to 4 bytes, so a VARCHAR(255) index needs 255 × 4 = 1020 bytes.
On MySQL before 5.7.7 (and MariaDB before 10.2), the per-index limit without large-prefix support is 767 bytes, less than 1020, so the index creation fails.
Solution
Recommended: set a default string length
Laravel ships with a one-line fix. In app/Providers/AppServiceProvider.php, cap the default string length so indexed VARCHAR columns fit:
use Illuminate\Support\Facades\Schema;
public function boot(): void
{
Schema::defaultStringLength(191);
}
191 × 4 = 764 bytes, just under the 767 limit. Then re-run the migration:
php artisan migrate:fresh
Better long-term: upgrade MySQL
The 767-byte limit is a limitation of old versions. MySQL 5.7.7+ and 8.0 raise it to 3072 bytes with the InnoDB large prefix, and the error disappears without capping your column lengths. See upgrading MySQL 5.7 to 8.0 on Ubuntu.
Per-column alternative
If you only need it on a specific migration, set the length on the column directly instead of globally:
$table->string('email', 191)->unique();
Subscribe to our newsletter
Do you want to receive regular updates with fresh and exclusive content to learn more about web development, hosting, security and performance? Subscribe now!