Mariadb

為什麼不能將變數相關排序規則設置為 utf8mb4_unicode_ci?

  • September 4, 2021

在此處顯示 mysql 版本:

mysql --version
mysql  Ver 15.1 Distrib 10.5.11-MariaDB, for debian-linux-gnu (x86_64) using  EditLine wrapper

還有我mariadb的配置文件。

cat   /etc/mysql/mariadb.cnf
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]

character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect = 'SET NAMES utf8mb4'
init_connect = 'SET collation_connection = utf8mb4_unicode_ci'


[client-server]
socket = /run/mysqld/mysqld.sock

!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mariadb.conf.d/

進入mysql,發出命令show variables like 'collation%';

+----------------------+--------------------+
| Variable_name        | Value              |
+----------------------+--------------------+
| collation_connection | utf8mb4_general_ci |
| collation_database   | utf8mb4_general_ci |
| collation_server     | utf8mb4_general_ci |
+----------------------+--------------------+
3 rows in set (0.001 sec)

怎樣才能得到下面的結果?

+----------------------+--------------------+
| Variable_name        | Value              |
+----------------------+--------------------+
| collation_connection | utf8mb4_unicode_ci |
| collation_database   | utf8mb4_unicode_ci |
| collation_server     | utf8mb4_unicode_ci |
+----------------------+--------------------+
3 rows in set (0.001 sec)

以普通使用者登錄showkey

debian@debian:~$ mysql -u showkey -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 31
Server version: 10.5.11-MariaDB-1 Debian 11

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show variables like 'collation%';
+----------------------+--------------------+
| Variable_name        | Value              |
+----------------------+--------------------+
| collation_connection | utf8mb4_general_ci |
| collation_database   | utf8mb4_general_ci |
| collation_server     | utf8mb4_general_ci |
+----------------------+--------------------+
3 rows in set (0.001 sec)

連接整理方案一

[client].cnf 文件的選項組中,您可以設置:

init_command = "SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci"

如果您想在 PHP 或其他程式語言中使用相同的連接排序規則,那麼您還需要在其配置或程式碼中進行設置。例如,對於 PHP,您可以執行以下操作:

$mysqli->query("SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci");

連接整理方案二

[mysqld]選項組中,只需添加以下行:

skip-character-set-client-handshake

我相信這將迫使所有客戶端使用伺服器的字元集和排序規則。這樣做的好處是客戶端不需要(不需要)為每個連接發送一個 SET 命令,即在連接準備好之前等待的時間更少,伺服器的工作也更少。

數據庫整理

然後,為數據庫設置排序規則:

ALTER DATABASE test 
 CHARACTER SET = 'utf8mb4' 
 COLLATE = 'utf8mb4_unicode_ci';

伺服器整理

對於伺服器設置,您可以在 mysql CLI 中動態執行此操作:

SET character_set_server = utf8mb4;
SET collation_server = utf8mb4_unicode_ci;

使用GLOBAL關鍵字為所有新連接全域設置它(直到伺服器重新啟動):

SET GLOBAL character_set_server = utf8mb4;
SET GLOBAL collation_server = 'utf8mb4_unicode_ci';

或者,您可以改為[mysqld]在 .cnf 文件的選項組中設置它(如果是這樣,請稍後重新啟動伺服器以使其生效):

[mysqld]
character-set-server = utf8mb4
collation-server = 'utf8mb4_unicode_ci'

結果

這應該給你:

MariaDB [test]> SHOW VARIABLES LIKE 'collation%';
+----------------------+--------------------+
| Variable_name        | Value              |
+----------------------+--------------------+
| collation_connection | utf8mb4_unicode_ci |
| collation_database   | utf8mb4_unicode_ci |
| collation_server     | utf8mb4_unicode_ci |
+----------------------+--------------------+

MariaDB.com 知識庫中提供了相關文件:

init_connect連接為 時被忽略root

root除管理目的外,請勿使用;為應用程序和使用者提供不同的登錄名。

引用自:https://dba.stackexchange.com/questions/298752