PDOException::(“SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes”) in Laravel install

While installing Laravel found the following Error :

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 (SQL: alter table `users` add unique `users_email_unique`(`email`))

Why this Happen :

According to the Laravel Docs, this problem occurs if you’re using MariaDB or MySQL versions older than 5.7.7.

Solution :

To fix the error, go to

app/Providers/AppServiceProvider.php

and add a boot function as follows. (Make sure to import the Schema facade from

Illuminate\Support\Facades\Schema

Full Code will be :

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
  Schema::defaultStringLength(191);
        //
    }
}

 

And After This If Still Not working then Follow the Process :

File: config/database.php

change the following lines

FROM

'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',

 

TO

'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',

 

Thanks

Was this article helpful?

Related Articles

Leave A Comment?

You must be logged in to post a comment.