MySQL 1071: Specified key was too long (Laravel migration) - Rocketeers app

  [ Rocketeers ](/)   

[Login](https://rocketeersapp.com/login) 

 On this page

 Knowledge
---------

MySQL 1071: Specified key was too long (Laravel migration)
==========================================================

### [\#Laravel](https://rocketeersapp.com/knowledge/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](https://rocketeersapp.com/author/mark-van-eijk) on June 23, 2026 · 1 minute read

1. [About the error](#content-about-the-error)
2. [Why do I see this error](#content-why-do-i-see-this-error)
3. [Solution](#content-solution)
4. [Recommended: set a default string length](#content-recommended-set-a-default-string-length)
5. [Better long-term: upgrade MySQL](#content-better-long-term-upgrade-mysql)
6. [Per-column alternative](#content-per-column-alternative)

[\#](#content-about-the-error "Permalink")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.

[\#](#content-why-do-i-see-this-error "Permalink")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.

[\#](#content-solution "Permalink")Solution
-------------------------------------------

### [\#](#content-recommended-set-a-default-string-length "Permalink")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

```

### [\#](#content-better-long-term-upgrade-mysql "Permalink")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](/upgrade-mysql-5-7-to-8-0-ubuntu).

### [\#](#content-per-column-alternative "Permalink")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!

  Fill in your email address to receive updates  Subscribe 

#### More in [\#Laravel](https://rocketeersapp.com/knowledge/laravel)

- [How to use different PHP versions with Laravel Valet](https://rocketeersapp.com/knowledge/different-php-versions-laravel-valet)
- [Disable cookies in Laravel](https://rocketeersapp.com/knowledge/disable-cookies-in-laravel)
- [Logging in Laravel](https://rocketeersapp.com/knowledge/laravel-logging)
- [How to check which Laravel version of your app is using](https://rocketeersapp.com/knowledge/check-laravel-version)
- [Disable CSRF in Laravel](https://rocketeersapp.com/knowledge/disable-csrf-in-laravel)
- [Creating an encrypted cookie value in Laravel](https://rocketeersapp.com/knowledge/creating-an-encrypted-cookie-value-in-laravel)

 [View all 19 articles →](https://rocketeersapp.com/knowledge/laravel)
