修改mysql数据库编码
By admin
- One minute read - 195 words修改my.ini文件
加上
default-character-set=gb2312
设定数据库字符集
alter database da_name default character set ‘charset’
1)设置数据库编码 /etc/my.cnf [mysqld] default-character-set=gbk … [client] default-character-set=gbk --------------------------------------- 2)按字符集导出 $mysqldump -u root -p dbname –default-character-set=gbk > a.sql; 3)查看SQL文件的编码 [root@localhost gethtml]# file a.sql a.sql: UTF-8 Unicode … [root@localhost gethtml]# iconv -f utf-8 -t gbk a.sql > a2.sql [root@localhost gethtml]# file a2.sql a2.sql: Non-ISO extended-ASCII English text 这时已经是gbk的编码了 3)导入 查看数据库服务和客户端字符集 mysql> status; Server characterset: gbk Db characterset: gbk Client characterset: latin1 Conn. characterset: latin1 mysql> set names ‘gbk’; //这样 mysql> status; Server characterset: gbk Db characterset: gbk Client characterset: gbk Conn. characterset: gbk 这时才能导数据 mysql> source a.sql; ———————————————————————————- 单独设置某个数据库: alter database testdb character set utf8; 查看mysql支持的编码: show character set;
查看系统的字符集和排序方式的设定可以通过下面的两条命令:
mysql> SHOW VARIABLES LIKE ”character_set_%”;
LINUX 如下:
1)导出数据 [[email protected] ~]$ mysqldump -u root -p dbname –default-character-set=gbk > base_user.sql;
2)查看导出的sql文件的编码 [[email protected] ~]$ file base_user.sql base_user.sql: UTF-8 Unicode text, with very long lines
3)转成要用的编码 [[email protected] ~]$ iconv -f utf-8 -t gbk base_user.sql >base_user_gbk.sql
4)连接数据库并查看当前库的编码 [[email protected] ~]$ mysql -uroot mysql> use testdb;
mysql> status; Server characterset: latin1 Db characterset: gbk Client characterset: latin1 Conn. characterset: latin1
5)设置成需要的编码 mysql>set names ‘gbk’;
6)查看现在的编码 mysql> status; Server characterset: latin1 Db characterset: gbk Client characterset: gbk Conn. characterset: gbk
7)导入我们转换成gbk后的文件 mysql> source base_user_gbk.sql;