How to fix Laravel 5.4: Specified key was too long error

in Laravel/Tutorials & Samples

As mentioned here, Laravel 5.4 made a change to the default database character set, and it’s now utf8mb4 instead of plain utf8. This switch to utf8mb4 works fine for new applications that are running MySQL v5.7.7 and higher.

Howver, anybody running MariaDB or older versions of MySQL might see the following error when trying to run migrations:

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))

SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

Although this tutorial and the migrations update guide say to edit your AppServiceProvider.php file and inside the boot method set a default string length like so:

use Illuminate\Support\Facades\Schema;

public function boot()

I found that this solution did not fully fix the problem and it was something of a hack. The proper fix is to actually edit your database.php file inside the config foler. Inside the file, I simply set the MySQL character set back to UTF8 and this fixes the problem at the core. So here is a sample of my database.php file:

    'connections' => [


        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,

This should resolve the “Specified key was too long error” problem cleanly for MariaDB and older versions of MySQL.


Mifty Yusuf is a Montreal-based software developer who enjoys playing with new web technologies as well as comic books and illustrations. He beleives that, no matter what the question is, the answer is always Batman!

1 Comment

  1. This is (presumably) because each character takes up exactly 4 bytes, and the maximum key length is measured in bytes, not characters. So the key length will by 191 * 4 = 764 bytes, just a smidgen under the maximum 767 bytes the database will support. Solutions need explanations IMO if they are to contribute to the shared knowledge here, and not just provide procedures . But a handy fix nonetheless. Jason Jun 15 ’17 at 10:07

Leave a Reply

Your email address will not be published.


This site uses Akismet to reduce spam. Learn how your comment data is processed.

Latest from Laravel

Go to Top