Modify and view the character set of the mysql database

Source: Internet
Author: User
Tags mysql commands
Modify MySQL character set in Liunx: 1. find the location of MySQL cnf file find-iname *. cnf-printusrsharemysqlmy-innodb-heavy-4G.cnfusrsharemysqlmy-large.cnfusrsharemysqlmy-small.cnfusrsharemysqlmy-medium.cnfusrsharemysqlmy-huge.cnf

Modify MySQL character set in Liunx: 1. 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/ my-huge.cnf/mysql

Modify the MySQL character set in Liunx:
1. Find the cnf File Location of MySQL
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 small. cnf, my-medium.cnf, my-huge.cnf, one of the my-innodb-heavy-4G.cnf to/etc, named my. cnf
Cp/usr/share/mysql/my-medium.cnf/etc/my. cnf

3. modify my. cnf
Vi/etc/my. cnf
Add under [client]
Default-character-set = utf8
Add under [mysqld]
Default-character-set = utf8

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. View 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>

Other settings:

Modify the character set of a Database
Mysql> use mydb
Mysql> alter database mydb character set UTF-8;
Creates a database and specifies the character set of the database.
Mysql> create database mydb character set UTF-8;

Modify the configuration file:
Modify/var/lib/mysql/mydb/db. opt
Default-character-set = latin1
Default-collation = latin1_swedish_ci
Is
Default-character-set = utf8
Default-collation = utf8_general_ci
Restart MySQL:
[Root @ bogon ~] #/Etc/rc. d/init. d/mysql restart

Use the MySQL command line to modify:
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)
View:
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)

-------------------------
[Repost knowledge articles]
MYSQL Character Set Problems

MySQL Character Set Support has two aspects:
Character set and Collation ).
The support for character sets is refined to four layers:
Server, database, table, and connection ).
1. Default MySQL Character Set
MySQL can refine the character set designation to a database, a table, and a column.
However, traditional programs do not use such complex 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 this character set is not specified, this value inherits from the one specified during compilation;
(3) When mysqld is started, you can specify a default character set in the command line parameters. If not, this value inherits from the configuration in the configuration file, character_set_server is set to the 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 to the default Character Set of the database;
(6) When creating a table in the database, the default Character Set of the table is set to character_set_database, which is the default Character Set of the database;
(7) When setting a column in a table, unless explicitly specified, the default character set in this column is the default Character Set of the table;
To sum up, if no changes are made, all columns of all tables in all databases will be used.
Latin1 storage, but if we install MySQL, we usually select multi-language support, that is, the installer will automatically
Default_character_set is set to UTF-8, which ensures that by default, all columns of all tables in all databases are stored in UTF-8.
2. view the default character set. (by default, the mysql character set is latin1 (ISO_8859_1)
Generally, you can run the following two commands to view the character set and sorting method of the system:
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 mysql's my. ini file,
For example, default-character-set = utf8
Character_set_server = utf8
After modification, restart the mysql service, service mysql restart
Use mysql> show variables like 'character % '; Check that the database encoding has been changed to utf8
+ --------- + ----------- +
| 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 | D: "mysql-5.0.37" share "charsets" |
+ --------- + ----------- +
(2) another way to modify the character set is to use mysql commands.
Mysql> SET character_set_client = utf8;

This article is from the ChinaUnix blog, If you view the original, click: http://blog.chinaunix.net/u1/51541/showart_505638.html

Original article address: Modify and view the character set of the mysql database. Thank you for sharing it with the original author.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.