Skip to content
  1. LuzineDebug
  2. Databases (SQL)
  3. SQLSTATE[42000]: Syntax error or access violatio…
Databases (SQL) Intermediate ≈ 10 min to fix

Fix "1071 Specified key was too long; max key length is 767 bytes" (MySQL, Laravel)

Error message
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`))
In short

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.

Be the first to confirm this fix

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

  1. 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.
  2. Index on a VARCHAR(255) in utf8mb4: unique email column, Laravel string() key, composite index over several strings.
  3. COMPACT row format on an existing table, or innodb_large_prefix disabled on MySQL 5.6 / MariaDB 10.1.
  4. MyISAM table (1000-byte limit): default engine of an old server or an imported table.
  5. Index on a very long VARCHAR, even on a recent server: beyond 768 characters in utf8mb4, the 3072-byte limit is exceeded.

Step-by-step fix

  1. Identify the server version and configuration

    These queries show the version, the default row format and the character set. innodb_large_prefix no longer exists on MySQL 8: it is normal if it does not show up.

    sql
    SELECT VERSION();
    SHOW VARIABLES LIKE 'innodb_default_row_format';
    SHOW VARIABLES LIKE 'innodb_large_prefix';
    SHOW VARIABLES LIKE 'character_set_server';
  2. 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.

    php
    use Illuminate\Support\Facades\Schema;
    
    public function boot(): void
    {
        Schema::defaultStringLength(191);
    }
  3. Or limit only the indexed column

    If you prefer to keep 255 characters elsewhere, give an explicit length only to the indexed columns.

    php
    $table->string('email', 191)->unique();
  4. Run the migration again cleanly

    The migration failed halfway: the users table may exist without its index, and a new attempt will give "Table already exists". In development only, migrate:fresh drops every table and replays the migrations. In production, drop the incomplete table by hand, then run php artisan migrate.

    bash
    # development only: drops ALL tables
    php artisan migrate:fresh
  5. Without Laravel: switch the table to DYNAMIC or use a prefix index

    On MySQL 5.7+ or MariaDB 10.2+, the DYNAMIC format raises the limit to 3072 bytes. Otherwise, a prefix index only indexes the first 191 characters; be careful, on a UNIQUE index, uniqueness then only applies to that prefix.

    sql
    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));
  6. 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 with ROW_FORMAT=DYNAMIC. With Laravel, set 'engine' => 'InnoDB ROW_FORMAT=DYNAMIC' in the mysql connection of config/database.php. The real fix is still to upgrade the server: these versions no longer get security patches.

    ini
    [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 to utf8 (an alias for utf8mb3) works around the error but breaks storage of emojis and some characters.
  • Give indexed columns a realistic length (email 191 or 254, slug 191) 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

All Databases (SQL) errors →