ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

解决laravel-admin安装报错1071 Specified key was too long问题

2026/9/14 13:53:22 拓冰建站 浏览量
解决laravel-admin安装报错1071 Specified key was too long问题

在执行php artisan admin:install命令安装laravel-admin的时候,如果你使用的数据库是MySQL v5.7.7以下版本就会报下面的错:

SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (SQL: alter table `users` add unique `users_email_unique`(`email`))

报错原因


laravel 5.4 改变了默认的数据库字符集,现在utf8mb4包括存储emojis支持。如果你运行MySQL v5.7.7或者更高版本,则不需要做任何事情。

解决方法


  • 1.在app\Providers\AppServiceProvider.php文件的boot()方法中添加默认值

    <?phpnamespace App\Providers;use Illuminate\Support\Facades\Schema;
    use Illuminate\Support\ServiceProvider; //add fixed sqlclass AppServiceProvider extends ServiceProvider
    {/*** Register any application services.** @return void*/public function register(){//}/*** Bootstrap any application services.** @return void*/public function boot(){Schema::defaultStringLength(191); //add fixed sql}
    }
    
  • 2.删除已经生成的usermigrate两张数据表,然后重新执行php artisan admin:install,否则会继续报下面的错误

    SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists
    

参考文章:数据库创建特殊字符过长问题 · Issue #1541 · z-song/laravel-admin (github.com)