[字符集] 在CentOS8.2上修改MariaDB/MySQL的預設字符集

因為MySQL是瑞典人開發的,所以預設字符集是latin1,與其每次要創建資料庫都要改,不如直接改預設字符集。

(環境:CentOS8.2, MariaDB10.3.17)
  1. 先看現在的預設字符集是什麼
    [user@localhost ~]$ mysql -u user -p
    ...
    ...
    MariaDB [(none)]> \s
    --------------
    mysql  Ver 15.1 Distrib 10.3.17-MariaDB, for Linux (x86_64) using readline 5.1
    
    Connection id:          8
    Current database:
    Current user:           user@localhost
    SSL:                    Not in use
    Current pager:          stdout
    Using outfile:          ''
    Using delimiter:        ;
    Server:                 MariaDB
    Server version:         10.3.17-MariaDB MariaDB Server
    Protocol version:       10
    Connection:             Localhost via UNIX socket
    Server characterset:    latin1
    Db     characterset:    latin1
    Client characterset:    utf8
    Conn.  characterset:    utf8
    UNIX socket:            /var/lib/mysql/mysql.sock
    Uptime:                 8 sec
    
  2. 為了不影響到原先就在的cnf檔,在「/etc/my.cnf.d/」資料夾下創一個「*.cnf」檔(檔名自取),內容如下
    [client]
    default-character-set=utf8mb4        ##會修改Client與Conn.的預設字符集
    [mysqld]
    character-set-server=utf8mb4         ##會修改Server與Db的預設字符集
    collation-server=utf8mb4_unicode_ci  ##會修改預設的校驗比對方法
    
  3. 重啟 MySQL,確認字符集已更改為所設定的
    [user@localhost ~]$ sudo systemctl restart mariadb
    [user@localhost ~]$ mysql -u user -p
    ...
    ...
    MariaDB [(none)]> \s
    --------------
    mysql  Ver 15.1 Distrib 10.3.17-MariaDB, for Linux (x86_64) using readline 5.1
    
    Connection id:          9
    Current database:
    Current user:           user@localhost
    SSL:                    Not in use
    Current pager:          stdout
    Using outfile:          ''
    Using delimiter:        ;
    Server:                 MariaDB
    Server version:         10.3.17-MariaDB MariaDB Server
    Protocol version:       10
    Connection:             Localhost via UNIX socket
    Server characterset:    utf8mb4
    Db     characterset:    utf8mb4
    Client characterset:    utf8mb4
    Conn.  characterset:    utf8mb4
    UNIX socket:            /var/lib/mysql/mysql.sock
    Uptime:                 3 min 39 sec
    

留言

這個網誌中的熱門文章