我想在Ruby on Rails中以日语查询。在我当前的代码中,它不起作用,仅返回一个空集。数据库MYSQL 8.0在docker上使用默认设置运行。我应该更新MYSQL配置吗?
这是我的选择查询和架构。
@internships = Internship.where("subject LIKE :keyword OR content LIKE :keyword", keyword: params[:keyword]).all
create_table "internships", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci" do |t|
t.date "start_date"
t.date "end_date"
t.integer "employment_number"
t.bigint "company_id"
t.text "content"
t.string "subject"
t.integer "job_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.date "deadline_date"
t.text "summary"
t.index ["company_id"], name: "index_internships_on_company_id"
end
mysql> SHOW VARIABLES LIKE 'char%';
+--------------------------+--------------------------------+
| Variable_name | Value |
+--------------------------+--------------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | utf8mb4 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql-8.0/charsets/ |
+--------------------------+--------------------------------+
8 rows in set (0.01 sec)
谢谢你。
[UTF-8是Unicode字符集的编码,它支持世界上几乎所有的语言。
唯一的区别在于对结果进行排序,在其他语言中,不同的字母可能以不同的顺序排列。另外,将a与ä进行比较在另一个排序规则中的行为可能有所不同。
对于BMP字符,utf8和utf8mb4具有相同的存储特征:相同的代码值,相同的编码,相同的长度
对于补充字符,utf8根本无法存储该字符,而utf8mb4需要四个字节来存储它。由于utf8根本无法存储字符,因此utf8列中没有任何补充字符,并且在从旧版MySQL升级utf8数据时,您不必担心转换字符或丢失数据。
因此,最好更改您的character_set_client, character_set_connection, character_set_results
至utf8mb4
ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER TABLE table_name CHANGE column_name column_name VARCHAR(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
> vim /etc/my.cnf
#
[client]
default-character-set = utf8mb4
#
[mysql]
default-character-set = utf8mb4
#
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
> service mysqld restart