最近用MYSQL数据库的时候不知道怎么回事,通过SQLEXPLORER插件写进去的中文内容是问号。就从网上搜点资料看了下。其实他就是字符集的问题。
下面介绍下几个MYSQL命令:
1.show character set;或show char set;
查看数据库支持的所有字符集
2.status;或\s;
查看当前状态 里面包括当前的字符集设置
3.show variables like 'char%';
查看系统字符集设置,包括所有的字符集设置
4.show table status from ;
查看sqlstudy数据库中表的字符集设置
5.show full columns from
;
查看表列的字符集设置,关键是在同一个表中,每列可以设置成不同的字符集
知道怎么查看字符集了,下面我来说下如何设置这些字符集(当然全是我这几天从网上整理的,呵呵)
1.修改服务器级
a. 临时更改:mysql>SET GLOBAL character_set_server=utf8;
b. 永久更改:
修改my.ini文件
[mysqld]
default-character-set=utf8
2.修改数据库级
a. 临时更改:mysql>SET GLOBAL character_set_database=utf8;
b. 永久更改:改了服务器级就可以了
3.修改表级
mysql>ALTER TABLE table_name DEFAULT CHARSET utf8;
更改了后永久生效
4.修改列级
修改示例:
mysql>alter table `products` change `products_model` `products_model` varchar( 20 )
character set utf8 collate utf8_general_ci null default null;
更改了后永久生效
5.更改连接字符集
a. 临时更改:mysql> set names utf8;
b. 永久更改:
修改my.ini文件
在[client]中增加:
default-character-set=utf8
执行SQL语句时信息的路径是这样的
信息输入路径:client→connection→server;
信息输出路径:server→connection→results.
问题example:
linux系统默认字符集为utf8,当使用terminal连接mysql后,如果table中有中文,输出查询结果可能产生乱码?
解决方案:
连接mysql后,执行mysql> set names utf8;
以上命令临时性改变的内容如下(client级和connection级characterset):
mysql> status
--------------
mysql Ver 14.14 Distrib 5.1.49, for debian-linux-gnu (i686) using readline 6.1
Connection id: 161
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.1.49-1ubuntu8.1 (Ubuntu)
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: latin1
Db characterset: latin1
Client characterset: latin1
Conn. characterset: latin1
UNIX socket: /var/run/mysqld/mysqld.sock
Uptime: 7 hours 44 min 56 sec
Threads: 4 Questions: 3244 Slow queries: 0 Opens: 516 Flush tables: 1 Open tables: 64 Queries per second avg: 0.116
--------------
mysql> set names utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> status
--------------
mysql Ver 14.14 Distrib 5.1.49, for debian-linux-gnu (i686) using readline 6.1
Connection id: 161
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.1.49-1ubuntu8.1 (Ubuntu)
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: latin1
Db characterset: latin1
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /var/run/mysqld/mysqld.sock
Uptime: 7 hours 53 min 57 sec
Threads: 4 Questions: 3247 Slow queries: 0 Opens: 516 Flush tables: 1 Open tables: 64 Queries per second avg: 0.114
--------------
阅读(625) | 评论(0) | 转发(0) |