Mysql数据库字符集问题
修改mysql数据库的默认编码方式
修改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)导出数据
[root@www.cnscn.org ~]$ mysqldump -u root -p dbname --default-character-set=gbk > base_user.sql;
2)查看导出的sql文件的编码
[root@www.cnscn.org ~]$ file base_user.sql
base_user.sql: UTF-8 Unicode text, with very long lines
3)转成要用的编码
[root@www.cnscn.org ~]$ iconv -f utf-8 -t gbk base_user.sql >base_user_gbk.sql
4)连接数据库并查看当前库的编码
[root@www.cnscn.org ~]$ 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;
- 查看数据库
- 查看数据库字符编码
- 通过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>use mydb
mysql>alter database mydb character set utf8;
- 8
创建数据库指定数据库的字符集
mysql>create database mydb character set utf8;
mysql将表的字符编码转换成utf-8
复制代码 代码如下:
alter table tb_anniversary convert to character set utf8;
修改数据库mysql字符编码为UTF8
步骤1:查看当前的字符编码方法
复制代码 代码如下:
mysql> show variables like'character%';
+--------------------------+----------------------------+
|Variable_name |Value |
+--------------------------+----------------------------+
|character_set_client |latin1 |
| character_set_connection |latin1 |
| character_set_database |latin1 |
| character_set_filesystem |binary |
|character_set_results |latin1 |
|character_set_server |latin1 |
|character_set_system |utf8 |
|character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
字符编码为latin1时,用户在给数据表插入一条字符类型(char)的记录,在显示此记录时可能会无法正常显示字符类型的数据,故此处我们将mysql字符编码修改为UTF8。操作如下:
步骤2:修改my.cnf配置文件,修改后如下(/etc/my.cnf):
复制代码 代码如下:
[client]
#password = your_password
port = 3306
socket = /var/lib/mysql/mysql.sock
default-character-set=utf8 #设置默认编码为UTF8