Error: #1071 - Specified key was too long; max key length is 1000 bytes - mysql 5.0.91

陌路散爱 提交于 2019-12-02 11:17:48

问题


I am using mysql 5.0.91 and I need to save URLs ( some are small and some are very long ). I want to use varchar(2000) but I get an error:

#1071 - Specified key was too long; max key length is 1000 bytes

What is the best way to save URLs on my hosting with mysql 5.0.91 ?


回答1:


Do you use unique=True on your urls column? MySQL is building a unique index on that column, and the number of bytes that it uses per character varies depending on the encoding.

If it is UTF-16, for example, it will use 2 bytes per character, so your varchar(2000) column would be 4000 bytes, and as the error message says, the max key length is 1000 bytes.

So you can switch to UTF-8 and use varchar(900) instead to fix this problem.




回答2:


I ran I that error because I choose utf-16 collation in db - switched back to utf-8 general-ci - and it was gone.




回答3:


Rewrite your email field like:

$table->string('email', 250)->unique();



回答4:


Laravel uses the utf8mb4 character set by default, which includes support for storing "emojis" in the database. If you are running a version of MySQL older than the 5.7.7 release or MariaDB older than the 10.2.2 release, you may need to manually configure the default string length generated by migrations in order for MySQL to create indexes for them. You may configure this by calling the Schema::defaultStringLength method within your AppServiceProvider:

use Illuminate\Support\Facades\Schema;

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


来源:https://stackoverflow.com/questions/10642429/error-1071-specified-key-was-too-long-max-key-length-is-1000-bytes-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!