laravel migrate时报错:Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
今天在学习laravel的路由模型绑定时,在按照文档执行php artisan migrate
时报错。
In Connection.php line 664:
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 Connection.php line 458:
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
原来,Laravel 5.4+默认使用utf8mb4字符编码,而不是之前的utf8编码。因此在运行php artisan migrate 会报出错误。
问题根源
MySql支持的utf8编码最大字符长度为3字节,如果遇到4字节的宽字符就会出现插入异常。三个字节UTF-8最大能编码的Unicode字符是0xffff,即Unicode中的基本多文种平面(BMP)。因而包括Emoji表情(Emoji是一种特殊的Unicode编码)在内的非基本多文种平面的Unicode字符都无法使用MySql的utf8字符集存储。
这也应该就是Laravel 5.4+改用4字节长度的utf8mb4字符编码的原因之一。不过要注意的是,只有MySql 5.5.3版本以后才开始支持utf8mb4字符编码(查看版本:selection version();)。如果MySql版本过低,需要进行版本更新。
Laravel同样也可以手动配置迁移生成的默认字符串长度:
如果你正在运行的 MySQL release 版本低于5.7.7 或 MariaDB release 版本低于10.2.2 ,为了MySQL为它们创建索引,你可能需要手动配置迁移生成的默认字符串长度,你可以通过调用 AppServiceProvider 中的 Schema::defaultStringLength 方法来配置它,或者你可以为数据库开启 innodb_large_prefix 选项,有关如何正确开启此选项的说明请查阅数据库文档。
解决办法
1.MySQL版本升级至5.5.3以上
2.手动配置迁移命令migrate生成的默认字符串长度,在AppServiceProvider中调用Schema::defaultStringLength方法来实现配置(如果MySQL版本大于5.7.7就无需配置了):
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);
}
}
究其原因,还是MySQL版本太低了,如果是MySQL5.7.7及以上版本,就没有上面的问题啦,也不需要手动配置迁移命令migrate生成的默认字符串长度。
原文链接: Laravel migrate时报错: Syntax error or access violation: 1071 Specified key was too long
相关文章
- Fix-Dell iDRAC 7 error: RAC0218: The maximum number of user sessions is reached
- Mysql开发实践:error while loading shared libraries: libaio解决方案
- rmt_redis.c:6446 ERROR: Can't handle RDB format version redis-migrate-tool迁移工具报错
- Java-异常Throwable,Exception,Error
- Error running 'JeecgSystemCloudApplication': Command line is too long. Shorten command line for JeecgSystemCloudApplication or also for Spring Boot default configuration?
- Android studio DELETE_FAILED_INTERNAL_ERROR Error while Installing APKs
- Sub-process /usr/bin/dpkg returned an error code (1) 如何解决
- MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about the error
- Error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting
- Image Picker - Error Piking Image "PlatformException(already_active, Image picker is already active, null)"
- JAVAMAIL 530 Error: A secure connection is requiered(such as ssl)
- ERROR 1524 (HY000): Plugin 'auth_socket' is not loaded
- 小程序:开发插件报错Error: Plugin “wxxxxxxxxxxxx/0“ is not defined.
- 关于android studio 出现Error:Execution failed for task ':app:preDebugAndroidTestBuild'. 的解决办法
- 已解决An error ocurred while starting the kernel
- 已解决RuntimeError: CUDA error: device-side assert triggered异常的正确解决方法,亲测有效!!!
- Vue Router4报错 Error: Invalid route component
- Android导入新项目时候报错 Error:Could not find bundletool.jar(com.android.tools.build:bundletool:0.1.0......
- Error: Your project contains C++ files but it is not using a supported native build system
- Error:android-apt plugin is incompatible with the Android Gradle plugin. Please use 'annotationProc
- 关于VC编译器错误 Error C2558
- @angular/material ERROR: this is not a module.
- 单元测试报错 Error running xxx. Command line is too long. Shorten the command line via JAR manife
- Linux执行tar解压报错tar: Error is not recoverable: exiting now