[Switch] Modify MySQL character set under Linux

Modify MySQL character set under Linux:
1. Find the location of MySQL's cnf file
find / -iname '*.cnf' -print
 
/usr/share/mysql/my-innodb-heavy-4G.cnf
/usr/share/mysql/my-large.cnf
/usr/share/mysql/my-small.cnf
/usr/share/mysql/my-medium.cnf
/usr/share/mysql/my-huge.cnf
/usr/share/texmf/web2c/texmf.cnf
/usr/share/texmf/web2c/mktex.cnf
/usr/share/texmf/web2c/fmtutil.cnf
/usr/share/texmf/tex/xmltex/xmltexfmtutil.cnf
/usr/share/texmf/tex/jadetex/jadefmtutil.cnf
/usr/share/doc/MySQL-server-community-5.1.22/my-innodb-heavy-4G.cnf
/usr/share/doc/MySQL-server-community-5.1.22/my-large.cnf
/usr/share/doc/MySQL-server-community-5.1.22/my-small.cnf
/usr/share/doc/MySQL-server-community-5.1.22/my-medium.cnf
/usr/share/doc/MySQL-server-community-5.1.22/my-huge.cnf
 
 

2. Copy one of small.cnf, my-medium.cnf, my-huge.cnf, my-innodb-heavy-4G.cnf to /etc and name it my.cnf
  cp /usr/share/mysql/my -medium.cnf /etc/my.cnf

 

3. Modify my.cnf

vi /etc/my.cnf

  Add default-character-set=utf8 under [client]

  Add default-character-set=utf8 under [mysqld]

 

4. Restart MySQL

  [root@bogon ~]# /etc/rc.d/init.d/mysql restart
  Shutting down MySQL [ OK ]
  Starting MySQL. [ OK ]
  [root@bogon ~]# mysql -u root -p
  Enter password:
  Welcome to the MySQL monitor.  Commands end with ; or \g.
  Your MySQL connection id is 1
  Server version: 5.1.22-rc-community-log MySQL Community Edition (GPL)
  Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

 

 

 

5. Check the character set settings

 

mysql> show variables like 'collation_%';
+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database   | utf8_general_ci |
| collation_server     | utf8_general_ci |
+----------------------+-----------------+
3 rows in set (0.02 sec)
mysql> show variables like 'character_set_%';
+--------------------------+----------------------------+
| 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/ |
+--------------------------+----------------------------+
8 rows in set (0.02 sec)
mysql>

  

 

 
Some other setting methods:
 
Modify the character set of the database
  mysql>use mydb
  mysql>alter database mydb character set utf-8;

create a database to specify the character set of the database
  mysql>create database mydb character set utf-8;
 

Modified by configuration file:

Modify /var/lib/mysql/mydb/db.opt
default-character-set=latin1
default-collation=latin1_swedish_ci
to
default-character-set=utf8
default-collation=utf8_general_ci

Restart MySQL:
[root@bogon ~]# /etc/rc.d/init.d/mysql restart

 

Modify through the MySQL command line:

 

mysql> set character_set_client=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> set character_set_connection=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> set character_set_database=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> set character_set_results=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> set character_set_server=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> set character_set_system=utf8;
Query OK, 0 rows affected (0.01 sec)
mysql> set collation_connection=utf8;
Query OK, 0 rows affected (0.01 sec)
mysql> set collation_database=utf8;
Query OK, 0 rows affected (0.01 sec)
mysql> set collation_server=utf8;
Query OK, 0 rows affected (0.01 sec)
 

 


Check:

mysql> show variables like 'character_set_%';
+--------------------------+----------------------------+
| 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/ |
+--------------------------+----------------------------+
8 rows in set (0.03 sec)
mysql> show variables like 'collation_%';
+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database   | utf8_general_ci |
| collation_server     | utf8_general_ci |
+----------------------+-----------------+
3 rows in set (0.04 sec)
 
 
 

 

 

-------------------------------------------------------------------------

【Reprint of knowledge articles】

MYSQL character set problem

 

 

MySQL's character set support (Character Set Support) has two aspects:

     Character set (Character set) and sorting method (Collation).

The support for character sets is refined to four levels:

     Server (server), database (database), data table (table) and connection (connection).

1. MySQL default character set

  MySQL's character set specification can be refined to a database, a table, a column, and what character set should be used.

However, traditional programs do not use such complicated configurations when creating databases and data tables, they use the default configuration. So, where does the default configuration come from? (1) When compiling MySQL, a default character set is specified, which is latin1;

    (2) When installing MySQL, you can specify a default character set in the configuration file (my.ini), if not specified, this value is inherited from the one specified at compile time;

    (3) When starting mysqld, you can specify a default character set in the command line parameters. If not specified, this value is inherited from the configuration in the configuration file. At this time, character_set_server is set to this default character set;

    (4) When creating a new database, unless explicitly specified, the character set of this database is set to character_set_server by default;

    (5) When a database is selected, character_set_database is set as the default character set of the database;

    (6) When a table is created in this database, the default character set of the table is set to character_set_database, which is the default character set of this database;

    (7) When setting a column in the table, unless explicitly specified, the default character set of this column is the default character set of the table;

To briefly summarize, if nothing is modified, then all fields of all tables in all databases are stored in latin1, but if we install MySQL, we generally choose multi-language support, that is, the installer will automatically Set default_character_set to UTF-8 in the configuration file, which ensures that by default, all fields of all tables in all databases are stored in UTF-8.

2. View the default character set (by default, the character set of mysql is latin1 (ISO_8859_1)

Usually, you can check the character set and sorting method of the system through the following two commands:

 

     mysql> SHOW VARIABLES LIKE 'character%';
+--------------------------+---------------------------------+
| Variable_name            | Value                           |
+--------------------------+---------------------------------+
| character_set_client     | latin1                          |
| character_set_connection | latin1                          |
| character_set_database   | latin1                          |
| character_set_filesystem | binary                          |
| character_set_results    | latin1                          |
| character_set_server     | latin1                          |
| character_set_system     | utf8                            |
| character_sets_dir       | D:"mysql-5.0.37"share"charsets" |
+--------------------------+---------------------------------+
  mysql> SHOW VARIABLES LIKE 'collation_%';
+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database   | utf8_general_ci |
| collation_server     | utf8_general_ci |
+----------------------+-----------------+

 

 

3. Modify the default character set

  (1) The simplest modification method is to modify the character set key value in the my.ini file of mysql,

如    default-character-set = utf8

      character_set_server =  utf8

   After modification, restart the mysql service, service mysql restart

   Use mysql> SHOW VARIABLES LIKE 'character%'; to check and find that the database encoding has been changed to utf8

 

+--------------------------+---------------------------------+
| Variable_name            | Value                           |
+--------------------------+---------------------------------+
| character_set_client     | utf8     

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326469221&siteId=291194637