Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.5.19 MySQL Community Server (GPL)Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trade of Oracle Corporation and/or its affiliates. Other names may be trades of their respective owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | web | +--------------------+ 2 rows in set (0.11 sec)mysql> create database test -> ; Query OK, 1 row affected (0.06 sec)mysql> use test Database changed mysql> create table Student( -> Sno varchar(10) primary key, -> Sname varchar(20), -> Sage smallint -> ); Query OK, 0 rows affected (0.06 sec)mysql> insert into Student(Sno, Sname, Sage) -> values('123456', 'Linux菜鸟', 22); Query OK, 1 row affected (0.02 sec)mysql> select * from Student; +--------+-----------+------+ | Sno | Sname | Sage | +--------+-----------+------+ | 123456 | Linux菜鸟 | 22 | +--------+-----------+------+ 1 row in set (0.06 sec) 没有任何设置。抄的你的代码
一般就算设置了表的mysql默认字符集为utf8并且通过UTF-8编码发送查询,你会发现存入数据库的仍然是乱码。问题就出在这个connection连接层上。解决方法是在发送查询前执行一下下面这句: SET NAMES 'utf8'; 它相当于下面的三句指令: SET character_set_client = utf8; SET character_set_results = utf8; SET character_set_connection = utf8; 百度来的,不负责
set name 'gbk';
mysql> insert into Student(Sno, Sname, Sage)
-> values('123456', 'Linux菜鸟', 22);
http://blog.csdn.net/ACMAIN_CHM/archive/2009/05/12/4174186.aspx
MySQL 中文显示乱码
character_set_server = utf8
重启mysql登陆后进行我之前建好的这个数据库。
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 | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
看到
character_set_database 还是latin1。
我对齐进行修改
mysql> set character_set_database = utf8;
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/share/mysql/charsets/ |
+--------------------------+----------------------------+
然后进行查询还是出现中文,原因是这样吧:插入到数据库时的数据就是乱码的,现在进行查询也是乱码的。
mysql> select * from student;
+---------+----------+------+
| Sno | Sname | Sage |
+---------+----------+------+
| 12345 | MySQL?? | 22 |
| 123456 | Linux?? | 22 |
| 1234567 | Linux??? | 22 |
+---------+----------+------+
3 rows in set (0.00 sec)但我设置 set character_set_database = utf8;
再进行插入数据,这时进行查询为什么还会有乱码呢?
mysql> insert into Student(Sno, Sname, Sage)
-> values('123', 'New中文', 22);
Query OK, 1 row affected, 1 warning (0.02 sec)mysql> select * from student;
+---------+----------+------+
| Sno | Sname | Sage |
+---------+----------+------+
| 123 | New?? | 22 |
| 12345 | MySQL?? | 22 |
| 123456 | Linux?? | 22 |
| 1234567 | Linux??? | 22 |
+---------+----------+------+
4 rows in set (0.00 sec)查看环境变量全都是utf8啊,为什么还乱码。
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/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
Your MySQL connection id is 2
Server version: 5.5.19 MySQL Community Server (GPL)Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trade of Oracle Corporation and/or its
affiliates. Other names may be trades of their respective
owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| web |
+--------------------+
2 rows in set (0.11 sec)mysql> create database test
-> ;
Query OK, 1 row affected (0.06 sec)mysql> use test
Database changed
mysql> create table Student(
-> Sno varchar(10) primary key,
-> Sname varchar(20),
-> Sage smallint
-> );
Query OK, 0 rows affected (0.06 sec)mysql> insert into Student(Sno, Sname, Sage)
-> values('123456', 'Linux菜鸟', 22);
Query OK, 1 row affected (0.02 sec)mysql> select * from Student;
+--------+-----------+------+
| Sno | Sname | Sage |
+--------+-----------+------+
| 123456 | Linux菜鸟 | 22 |
+--------+-----------+------+
1 row in set (0.06 sec)
没有任何设置。抄的你的代码
+--------------------------+---------------------------------------------------------+
| Variable_name | Value |
+--------------------------+---------------------------------------------------------+
| character_set_client | gbk |
| character_set_connection | gbk |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | gbk |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | D:\Program Files\MySQL\MySQL Server 5.5\share\charsets\ |
+--------------------------+---------------------------------------------------------+
8 rows in set (0.03 sec)mysql>
照我的抄吧,我的是系统默认的,对了,最后一句别抄,那个和电脑有关
对了,你确定你的Linux的终端能显示汉字吗?
我的Linux在文本模式下是不能显示汉字的
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/share/mysql/charsets/ |
改了设置后是
| 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/ |
+--------------------------+----------------------------+
但插入后进行查询还是乱码。
用UTF-8不等于说就能正常显示中文,虽然我讲不出道理……
SET character_set_results = utf8;
SET character_set_connection = utf8;
百度来的,不负责