以下所有的操作均为mysql5.5环境下
一、创建一个数据库,默认字符集是latin1
mysql> create database dbatest;
mysql> show create database dbatest\G
mysql> show create database dbatest\G
*************************** 1. row ***************************
Database: dbatest
Create Database: CREATE DATABASE `dbatest` /*!40100 DEFAULT CHARACTER SET latin1 */
1 row in set (0.00 sec)
创建一个表
mysql> show create table student\G
*************************** 1. row ***************************
Table: student
Create Table: CREATE TABLE `student` (
`id` int(4) NOT NULL AUTO_INCREMENT,
`name` char(20) NOT NULL,
`age` tinyint(2) NOT NULL DEFAULT '0',
`dept` varchar(16) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
二、创建数据库
mysql> insert into student values (1,'amsilence');
ERROR 1136 (21S01): Column count doesn't match value count at row 1
mysql> insert into student values (1,'amsilence',20,'one');
Query OK, 1 row affected (0.00 sec)
mysql> select * from student;
+----+-----------+-----+------+
| id | name | age | dept |
+----+-----------+-----+------+
| 1 | amsilence | 20 | one |
+----+-----------+-----+------+
1 row in set (0.00 sec)
插入中文查看,发现中文乱码
mysql> insert into student values (2,'化繁为简',20,'two');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> select * from student;
+----+-----------+-----+------+
| id | name | age | dept |
+----+-----------+-----+------+
| 1 | amsilence | 20 | one |
| 2 | ???? | 20 | two |
+----+-----------+-----+------+
2 rows in set (0.00 sec)
三、中文乱码解决办法
方法1:mysql> set names latin1;
结果:
mysql> insert into student values (3,'化繁为简',20,'three');
Query OK, 1 row affected (0.01 sec)
mysql> select * from student;
+----+--------------+-----+-------+
| id | name | age | dept |
+----+--------------+-----+-------+
| 1 | amsilence | 20 | one |
| 2 | ???? | 20 | two |
| 3 | 化繁为简 | 20 | three |
+----+--------------+-----+-------+
3 rows in set (0.00 sec)
方法2:创建一个.sql文件,在mysql中导入
mysql> system cat ~/test.sql
set names latin1;
insert into student values (4,'简单',20,'666');
source导入数据
mysql> source ~/test.sql
Query OK, 0 rows affected (0.00 sec)
Query OK, 1 row affected (0.01 sec)
结果:
mysql> select * from student;
+----+--------------+-----+-------+
| id | name | age | dept |
+----+--------------+-----+-------+
| 1 | amsilence | 20 | one |
| 2 | ???? | 20 | two |
| 3 | 化繁为简 | 20 | three |
| 4 | 简单 | 20 | 666 |
+----+--------------+-----+-------+
4 rows in set (0.00 sec)
方法3:使用mysql导入数据库时加字符集参数选项
参数:
[root@mysql-dba ~]# mysql -uroot -p123456 --default-character-set=latin1 dbatest < test.sql
例子:
[root@mysql-dba ~]# cat test.sql
insert into student values (5,'简单2',20,'666');
insert into student values (6,'简单3',20,'666');
insert into student values (7,'简单4',20,'666');
insert into student values (8,'简单5',20,'666');
查看数据库:
注意:查看的时候需要将数据库里面的字符集设置latin1,不然显示会乱码
mysql> set names latin1;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from student;
+----+--------------+-----+-------+
| id | name | age | dept |
+----+--------------+-----+-------+
| 1 | amsilence | 20 | one |
| 2 | ???? | 20 | two |
| 3 | 化繁为简 | 20 | three |
| 4 | 简单 | 20 | 666 |
| 5 | 简单2 | 20 | 666 |
| 6 | 简单3 | 20 | 666 |
| 7 | 简单4 | 20 | 666 |
| 8 | 简单5 | 20 | 666 |
+----+--------------+-----+-------+
8 rows in set (0.00 sec)
方法4:在配置文件里设置修改客户端和服务端相关参数(永久生效)
修改my.cnf配置文件
在[mysqld]这个区块内添加参数
character-set-server=latin1
在[client]区块里面添加
default-character-set=latin1
小结:不乱码就是统一字符集,服务端、客户端、库、表、程序都要统一
如果里面已经存在数据,那么需要将数据导出重新建立数据库在导入数据。
相关文章
- web---资源的下载及中文乱码问题
- [LUA ERROR] [string "cocos/cocos2d/Cocos2dConstants.lua"]:620: attempt to index a nil value解决办法
- 关于cocos2d实现H5声音播放存在问题的解决办法
- COCOS2D 事件冒泡规律 和 CCMenu一直响应事件的解决办法
- COCOS2D 事件冒泡规律 和 CCMenu一直响应事件的解决办法
- Cocos2D v3.4.9粒子效果不能显示的原因分析及解决办法
- cocos2d Slider 透明滑动部件无法生成解决办法
- 电脑每次开机都出现check file system on:C 的解决办法
- Mysql数据库备份恢复
- Jenkins master slave设置遇到的坑以及解决办法