[MySQL] modifying the default Character Set

Source: Internet
Author: User
Tags mysql commands mysql command line

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 ~] # Sudo/etc/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 ~] # Sudo/etc/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;

Related Article

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.