MySQL设置字符集CHARACTER SET

本文地址:http://www.cnblogs.com/yhLinux/p/4036506.html

my.cnf 配置文件中设置相关选项,改变为相应的character set。

设置数据库编码(sudo vi /etc/mysql/my.cnf):

[client]
default-character-set = utf8

[mysqld]
character-set-server = utf8
collation-server = utf8_general_ci

参考资料:

  14.1.4 Character Set

  5.1.4 Server System Variables

  10.5, “Character Set Configuration”

  

  10.1 Character Set Support

  You can specify character sets at the server, database, table, and column level.

  10.1.3 Specifying Character Sets and Collations

  10.1.3.1 Server Character Set and Collation

  MySQL Server has a server character set and a server collation. These can be set at server startup on the command line or in an option file and changed at runtime.

  10.1.3.2 Database Character Set and Collation

  Every database has a database character set and a database collation.

  The character set and collation for the default database can be determined from the values of the character_set_database and collation_database system variables. The server sets these variables whenever the default database changes. If there is no default database, the variables have the same value as the corresponding server-level system variables, character_set_server and collation_server.  

Specify character settings at server startup. To select a character set and collation at server startup, use the --character-set-server and --collation-server options. For example, to specify the options in an option file, include these lines:

[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci

These settings apply server-wide and apply as the defaults for databases created by any application, and for tables created in those databases.

You can force client programs to use specific character set as follows:

[client]
default-character-set=charset_name

This is normally unnecessary. However, when character_set_system differs from character_set_server or character_set_client, and you input characters manually (as database object identifiers, column values, or both), these may be displayed incorrectly in output from the client or the output itself may be formatted incorrectly. In such cases, starting the mysql client with --default-character-set=system_character_set—that is, setting the client character set to match the system character set—should fix the problem.

查看设置结果

更改my.cnf之前:

mysql> SHOW VARIABLES LIKE ‘%char%‘;
+--------------------------+---------------------------------------------------------------+
| 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/local/mysql-5.6.21-linux-glibc2.5-x86_64/share/charsets/ |
+--------------------------+---------------------------------------------------------------+

设置之后,重启mysql服务($ sudo service mysql restart):

mysql> 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/local/mysql-5.6.21-linux-glibc2.5-x86_64/share/charsets/ |
+--------------------------+---------------------------------------------------------------+

时间: 2024-10-27 17:18:07

MySQL设置字符集CHARACTER SET的相关文章

mysql设置字符集

一般情况下,防止程序交互过程中出现乱码情况,所以前后台都会同意编码格式. 因为UTF-8编码方式国际通用,所以我在mysql中设置编码格式UTF-8. 先查看mysql编码格式. show variables like 'character\_set\_%'; 发现各种不一样. ok!现在开始配置: 任务管理器,停掉mysql服务. 在mysql安装目录下找my.ini文件,没有的话新建my.ini文件,并将my-default.ini文件内容复制到my.ini文件中. 1.在配置文件my.in

MySQL的字符集(Character Set)和校对集(Collate)

遇到一个创建数据库的SQL语句 CREATE DATABASE testdb DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; 注意:这里指定字符集是用utf8,而不是utf-8. 对上面的SQL语句中的CHARACTER SET 和 COLLATE产生了兴趣. 简单来说,CHARACTER SET是指字符集,而COLLATE是指校对集(可以理解为排序规则). 1.Character Set 1.1.字符集的4个层次 mysql4.1及其之后

MySQL 设置字符集为utf-8

1.在[client]字段里加入default-character-set=utf8,如下:[client]port = 3306socket = /var/lib/mysql/mysql.sockdefault-character-set=utf82.在[mysqld]字段里加入character-set-server=utf8,如下:[mysqld]port = 3306socket = /var/lib/mysql/mysql.sockcharacter-set-server=utf83.

mysql设置字符 UTF-8

Windows版MySQL设置字符集全部为utf8的方式 MySQL安装目录下的my.ini文件 [client]节点 default-character-set=utf8    (增加) [mysql]节点 default-character-set=utf8    (修改) [mysqld]节点 default-character-set=utf8    (修改) character_set_server=utf8    (增加) 修改完毕后使用 net stop mysql net sta

为 MySQL 设置默认字符集(UTF-8)避免产生乱码

环境:Windows 7+Wamp Server+MySQL 5.7.9 查看MySQL默认编码: SHOW VARIABLES LIKE 'character%' character_set_client为客户端编码方式 character_set_connection为建立连接使用的编码 character_set_database数据库的编码 character_set_results结果集的编码 character_set_server数据库服务器的编码 只要保证统采用的编码方式一致,就

mysql设置utf8字符集

mysql字符集含义请看mysql手册第10.1节Character Set Support mysql的字符集转换过程请看鸟哥博客的这一篇 0. 初始状态 debian使用apt安装的mysql一开始是这样的 可以看到默认情况下 sever的字符集是latin1 db的字符集是latin1 mysql cli的client和connect的字符集是utf8 (pdo连接的client和connect的默认字符集是latin1) 1. server charset server的字符集设置可以通

Ubuntu安装MySQL及设置字符集为UTF-8

一.安装数据库 1.获取并安装mysql $ sudo apt-get install mysql-server 2.设置root密码(在跳出的提示框中输入密码即可) 3.登陆数据库,输入mysql -u root -p,接着出现输入密码的提示,输入完密码回车即可 $ mysql -u root -p Enter password Welcome to the MySQL monitor. 4.查看数据库,输入show databases; mysql > show databases; 5.显

mysql:设置字符集utf8mb4 支持emoji字符

为什么要把数据库的字符集设置成utf8mb4呢?以前一直用的都是utf8啊? 答案在这里:utf8适用于不使用移动设备的互联网交互,utf8mb4适用于当前的移动设备互联网开发,因为移动设备中常常会有表情符号(emoji)的存储,它占用4个字节的存储空间,而utf8是3个字节,这样,用3个字节去存储4个字节的东西,很明显是存不下的,会报错,所以要用utf8mb4,并且utf8mb4是兼容utf8的,那么,就没有理由不用utf8mb4字符集了. 介绍设置MySQL的字符集: 1 在ubuntu安装

在window cmd中设置字符集后,mysql的测试

之前测试过,在centos下,所有字符集全部设为utf8,可以同时满足命令行和网页的需求. 在windows下,由于默认字符集为gbk,所以在命令行操作mysql时,需要设置set character_set_client = gbk; 今天发现,其实是要设置set character_set_results = gbk;即可,其它均为utf8或gbk都行. 另外尝试改变windows字符集,cmd下chcp 65001切换到utf8,但这好像是临时的,由于这次安装的wamp,用的自带的终端,c