Laravel 5.4运行migrate命令的时候,错误信息为1071 Specified key was too long的解决办法
Laravel 5.4运行migrate命令报错1071 Specified key was too long解决方案如下:
D:\Laravel5.4>php artisan migrate
Migration table created successfully.
[Illuminate\Database\QueryException]
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`))
[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes
Laravel 默认使用 utf8mb4 字符,包括支持在数据库存储「表情」。如果你正在运行的 MySQL release 版本低于5.7.7 或 MariaDB release 版本低于10.2.2 ,为了MySQL为它们创建索引,你可能需要手动配置迁移生成的默认字符串长度,你可以通过调用 AppServiceProvider 中的 Schema::defaultStringLength 方法来配置它:
use Illuminate\Support\Facades\Schema;
/**
* 引导任何应用程序服务。
*
* @return void
*/
public function boot()
{
Schema::defaultStringLength(191);
}
或者你可以为数据库开启 innodb_large_prefix 选项,有关如何正确开启此选项的说明请查阅数据库文档。
修改文件:\app\Providers\AppServiceProvider.php
修改之后的源代码:
- <?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Schema::defaultStringLength(191);
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}
重新运行php artisan migrate命令创建数据库
D:\phpStudy\Laravel5.4>php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated: 2014_10_12_100000_create_password_resets_table
文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/115431.html<