时间:2021-07-01 10:21:17 帮助过:51人阅读
Mysql字符集的修改及查看问题
修改及查看mysql数据库的字符集
修改my.cnf
vi /etc/my.cnf
在[client]下添加
default-character-set=utf8
在[mysqld]下添加
default-character-set=utf8
5.查看字符集设置
mysql> show variables like 'collation_%';
mysql> show variables like 'character_set_%';
修改数据库的字符集
mysql>use mydb
mysql>alter database mydb character set utf-8;
创建数据库指定数据库的字符集
mysql>create database mydb character set utf-8;
通过配置文件修改:
修改/var/lib/mysql/mydb/db.opt
default-character-set=latin1
default-collation=latin1_swedish_ci
为
default-character-set=utf8
default-collation=utf8_general_ci
通过MySQL命令行修改:
mysql> set character_set_client=utf8;
mysql> set character_set_connection=utf8;
mysql> set character_set_database=utf8;
mysql> set character_set_results=utf8;
mysql> set character_set_server=utf8;
mysql> set character_set_system=utf8;
mysql> set collation_connection=utf8;
mysql> set collation_database=utf8;
mysql> set collation_server=utf8;
查看:
mysql> show variables like 'character_set_%';
mysql> show variables like 'collation_%';
通常,查看系统的字符集和排序方式的设定可以通过下面的两条命令:
mysql> SHOW VARIABLES LIKE 'character%';
mysql> SHOW VARIABLES LIKE 'collation_%';
3.修改默认字符集
(1) 最简单的修改方法,就是修改mysql的my.ini文件中的字符集键值,
如 default-character-set = utf8
character_set_server = utf8
修改完后,重启mysql的服务,service mysql restart
使用 mysql> SHOW VARIABLES LIKE 'character%';查看,发现数据库编码均已改成utf8
(2) 还有一种修改字符集的方法,就是使用mysql的命令
mysql> SET character_set_client = utf8 ;
mysql命令行修改字符编码
1、修改数据库字符编码
mysql> alter database mydb character set utf8 ;
2、创建数据库时,指定数据库的字符编码
mysql> create database mydb character set utf8 ;
3、查看mysql数据库的字符编码
mysql> show variables like 'character%'; //查询当前mysql数据库的所有属性的字符编码
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
4、修改mysql数据库的字符编码
mysql> set character_set_client=utf8 ;
mysql> set character_set_connection=utf8 ;
mysql> set character_set_database=utf8 ;
mysql> set character_set_database=utf8 ;
mysql> set character_set_results=utf8 ;
mysql> set character_set_server=utf8 ;
mysql> set character_set_system=utf8 ;
mysql> show variables like 'character%';
+--------------------------+----------------------------+
| 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)
MySQL用Load Data local infile 导入部分数据后中文乱码
今天在两台MySQL服务器之间导数据,因为另一个MySQL服务器是测试用的,差一个月的数据,从现有MySQL服务器select到一个文件,具体语句是:
select * from news where ine_time>='2010-02-01' and ine_time <'2010-03-01' into outfile '/tmp/newsdata.sql';然后scp到另一个MySQL Server上导入到对应的表中,具体语句如下:
load data local infile '/home/lsanotes/newsdata.sql' into table news;然后刷新访问这台数据库的web页面,发现刚导进来的这一个月的数据都是乱码,而以前其它月份的则正常,用show create table news;查看发现两个服务器中的news表都是utf8,奇怪,把导出的数据转换成utf8,再导入问题仍旧。
后来在数据库中查看刚刚导进的这一个月的数据时,没有执行set names utf8;就可以正常查看中文而不乱码,而其它月份的必须先执行set names utf8;才能看中文而不乱码,但是当我执行过set names utf8;后再看刚刚导进的这一个月的数据却是乱码,看来导进来的数据并不是utf8格式。最后的解决方法是:
load data local infile '/home/lsanotes/newsdata.sql' into table news character set utf8;
bitsCN.com