首先是查看编码:SHOW VARIABLES LIKE 'character%';
第一种是在当前状态下有效的,重启就不行了。
SET character_set_client = utf8;
SET character_set_connection = utf8;
SET character_set_database = utf8;
SET character_set_results = utf8;
SET character_set_server = utf8;
第二种是在配置文件里面修改,永久有效
找到mysql安装包,点开my.ini
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
default-character-set=utf8
以上3个section都要加default-character-set=utf8,平时我们可能只加了mysqld一项。
然后重启mysql,执行
但是可恶的事情又来了,
|character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8
注意 该配置| character_set_server | latin1 无法设置成UTF8 交互时候仍然会出现乱码。
找到下面这东东
X:\%path%\MySQL\MySQL Server 5.0\bin\MySQLInstanceConfig.exe
重新启动设置,将默认编码设置为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 | C:\Program Files\MySQL\MySQL Server 5.0\share\charsets\ |
+--------------------------+---------------------------------------------------------+
8 rows in set
注意:
1、建表时加utf8,表字段的Collation可加可不加,不加时默认是utf8_general_ci了。
CREATE TABLE `tablename4` ( `id` int(11) NOT NULL AUTO_INCREMENT, `varchar1` varchar(255) DEFAULT NULL, `varbinary1` varbinary(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
2、网页xxx.php/jsp保存时选择utf8编码,页头最好加上
header('conten-type:text/html;charset=utf-8');
在执行CRUD操作前先执行一下
mysql_query("set names utf8");
-------------------------
连接数据库设置编码
jdbc:mysql://地址:3306/数据库名?characterEncoding=utf8
-------------------------
java中的常用编码UTF-8;GBK;GB2312;ISO-8859-1;
对应mysql数据库中的编码utf8;gbk;gb2312;latin1
转载自:http://www.blogjava.net/qileilove/archive/2013/06/14/400541.html
1、建表时加utf8,表字段的Collation可加可不加,不加时默认是utf8_general_ci了。
CREATE TABLE `tablename4` ( `id` int(11) NOT NULL AUTO_INCREMENT, `varchar1` varchar(255) DEFAULT NULL, `varbinary1` varbinary(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
2、网页xxx.php/jsp保存时选择utf8编码,页头最好加上
header('conten-type:text/html;charset=utf-8');
在执行CRUD操作前先执行一下
mysql_query("set names utf8");
-------------------------
连接数据库设置编码
jdbc:mysql://地址:3306/数据库名?characterEncoding=utf8
-------------------------
java中的常用编码UTF-8;GBK;GB2312;ISO-8859-1;
对应mysql数据库中的编码utf8;gbk;gb2312;latin1
重新启动设置,将默认编码设置为utf8.这样就能达到我们所要的效果了。重新启动设置,将默认编码设置为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 | C:\Program Files\MySQL\MySQL Server 5.0\share\charsets\ |
+--------------------------+---------------------------------------------------------+
8 rows in set
重新启动设置,将默认编码设置为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 | C:\Program Files\MySQL\MySQL Server 5.0\share\charsets\ |
+--------------------------+---------------------------------------------------------+
8 rows in set
找到下面这东东找到下面这东东
X:\%path%\MySQL\MySQL Server 5.0\bin\MySQLInstanceConfig.exe
重新启动设置,将默认编码设置为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 | C:\Program Files\MySQL\MySQL Server 5.0\share\charsets\ |
+--------------------------+---------------------------------------------------------+
8 rows in set
找到下面这东东
X:\%path%\MySQL\MySQL Server 5.0\bin\MySQLInstanceConfig.exe
重新启动设置,将默认编码设置为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 | C:\Program Files\MySQL\MySQL Server 5.0\share\charsets\ |
+--------------------------+---------------------------------------------------------+
8 rows in set
找到下面这东东
X:\%path%\MySQL\MySQL Server 5.0\bin\MySQLInstanceConfig.exe
重新启动设置,将默认编码设置为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 | C:\Program Files\MySQL\MySQL Server 5.0\share\charsets\ |
+--------------------------+---------------------------------------------------------+
8 rows in set