Mysql database default encoding

Keywords: MySQL encoding Database Unix

View the encoding used

1. View with status

mysql> status

--------------
mysql  Ver 14.14 Distrib 5.6.44, for Linux (x86_64) using  EditLine wrapper

Connection id:      22
Current database:   
Current user:       root@localhost
SSL:            Not in use
Current pager:      stdout
Using outfile:      ''
Using delimiter:    ;
Server version:     5.6.44 MySQL Community Server (GPL)
Protocol version:   10
Connection:     Localhost via UNIX socket
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    utf8
Conn.  characterset:    utf8
UNIX socket:        /var/run/mysqld/mysqld.sock
Uptime:         6 hours 31 min 31 sec

Threads: 1  Questions: 905  Slow queries: 1  Opens: 74  Flush tables: 1  Open tables: 67  Queries per second avg: 0.038
--------------

Server characterset: latin1
Db characterset: Latin1 (database encoding format)
Client characterset: utf8
Conn. Characterset: utf8 (Encoding Format for Access)

2. Use show variables

show variables like 'character'

mysql> show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

/ Modify configuration information in etc/my.cnf file

  1. Use default configuration information

    • Local access to your own database: Server characterset: Latin1

      status

      Server characterset:   latin1
      Db     characterset:   latin1
      Client characterset:   utf8
      Conn.  characterset:   utf8

      show variables like 'character%'

      +--------------------------+----------------------------+
      
      | Variable_name            | Value                      |
      
      +--------------------------+----------------------------+
      
      | character_set_client     | utf8                       |
      
      | character_set_connection | utf8                       |
      
      | character_set_database   | latin1                     |
      
      | character_set_filesystem | binary                     |
      
      | character_set_results    | utf8                       |
      
      | character_set_server     | latin1                     |
      
      | character_set_system     | utf8                       |
      
      | character_sets_dir       | /usr/share/mysql/charsets/ |
      
      +--------------------------+----------------------------+
      

      Create a database encodtest to view its encoding information

      Server characterset:   latin1
      Db     characterset:   latin1
      Client characterset:   utf8
      Conn.  characterset:   utf8
      mysql> show variables like 'character%';
      +--------------------------+----------------------------+
      | Variable_name            | Value                      |
      +--------------------------+----------------------------+
      | character_set_client     | utf8                       |
      | character_set_connection | utf8                       |
      | character_set_database   | latin1                     |
      | character_set_filesystem | binary                     |
      | character_set_results    | utf8                       |
      | character_set_server     | latin1                     |
      | character_set_system     | utf8                       |
      | character_sets_dir       | /usr/share/mysql/charsets/ |
      +--------------------------+----------------------------+

      character_set_database is the encoding format of the database

      Create a test table to see its default encoding format

      show create table test;

      +-------+-------------------------------
      | Table | Create Table                                                                                               |
      +-------+-------------------------------
      | test  | CREATE TABLE `test` (
      `id` int(11) NOT NULL,
      PRIMARY KEY (`id`)
      ) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
      +-------+-------------------------------

      DEFAULT CHARSET=latin1 The default encoding format is latin1, which is the same as the database encoding format.

  2. Change configuration file / etc/my.cnf

    Add settings:

    [client]
    default-character-set = utf8
    [mysqld]
    character-set-server=utf8
    collation-server=utf8_unicode_ci
    • Local access to database

      View the encoding format:

      status

      Server characterset:   utf8
      Db     characterset:   utf8
      Client characterset:   utf8
      Conn.  characterset:   utf8

      show variables like 'char%';

      +--------------------------+----------------------------+
      | Variable_name            | Value                      |
      +--------------------------+----------------------------+
      | character_set_client     | utf8                       |
      | character_set_connection | utf8                       |
      | character_set_database   | utf8                       |
      | character_set_filesystem | binary                     |
      | character_set_results    | utf8                       |
      | character_set_server     | utf8                       |
      | character_set_system     | utf8                       |
      | character_sets_dir       | /usr/share/mysql/charsets/ |
      +--------------------------+----------------------------+

      Create a database to view its encoding

      Same as above

      Create table view encoding:

      +-------+------------------------------------------------------
      | Table | Create Table                                                                                                                     |
      +-------+------------------------------------------------------
      | test  | CREATE TABLE `test` (
      `id` int(11) NOT NULL,
      PRIMARY KEY (`id`)
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |
      +-------+-----------------------------------------------------
      

      DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci The default encoding format is the same as the configuration

Posted by Xproterg^vi on Thu, 03 Oct 2019 10:09:06 -0700