对于数据库级的字符集,如果没有办法重建数据库,可以在my.cnf文件中使用character-set-server = utf8(要重启数据库服务),对于之前已经存在数据库,此参数不产生影响。如果之前的数据库A是latin1,在A中创建的表默认还是latin1,除非指定DEFAULT CHARSET!然而对于新建的数据库,则继承 my.cnf文件中使用character-set-server = utf8!
[root@rac3 ~]# more /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
old_passwords=1
character-set-server = utf8
关闭数据库服务,并重新启动!
[root@rac3 ~]# mysqladmin shutdown
[root@rac3 ~]# mysqld_safe &
[1] 15102
[root@rac3 ~]# Starting mysqld daemon with databases from /var/lib/mysql
[root@rac3 ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.0.45 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> show variables like '%server%';
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| character_set_server | utf8 |
| collation_server | utf8_general_ci |
| server_id | 0 |
+----------------------+-----------------+
3 rows in set (0.00 sec)
--重新建立数据库,查看其默认字符集:为utf8
mysql> create database yql;
Query OK, 1 row affected (0.00 sec)
mysql> use yql;
Database changed
新建表的默认字符集和数据库的默认字符集一致!
mysql> create table t(id int);
Query OK, 0 rows affected (0.00 sec)
mysql> show create table t;
+-------+-------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-------------------------------------------------------------------------------------+
| t | CREATE TABLE `t` (
`id` int(11) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
+-------+-------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> show create database yql \G;
*************************** 1. row ***************************
Database: yql
Create Database: CREATE DATABASE `yql` /*!40100 DEFAULT CHARACTER SET utf8 */
1 row in set (0.00 sec)
ERROR:
No query specified