默认登录mysql之后可以通过SHOW VARIABLES语句查看系统变量及其值。
mysql> show variables like ‘%character%';
1. 找到mysql的配置文件,拷贝到etc目录下,第一步很重要
把/usr/share/doc/mysql-server-5.1.52/my-large.cnf 复制到 /etc/my.cnf
即用命令:cp /usr/share/doc/mysql-server-5.1.52/my-large.cnf /etc/my.cnf
2. 打开my.cnf修改编码
在[client]下增加default-character-set=utf8
在[mysqld]下增加default-character-set=utf8
同时加上init_connect=’SET NAMES utf8′ (设定连接mysql数据库时使用utf8编码,以让mysql数据库为utf8运行)
3.重新启动mysql
service mysqld restart
再次输入show variables like ‘%character%';
即使做了以上修改如果直接数据库再创建表,然后存入中文,取出来的会是问号。解决的办法是:创建数据库的时候指明默认字符集为utf8,如:
create database test default character set utf8;
注:有些版本可能不是这样设置,比如:
[client]
#password = your_password
port = 3306
socket = /var/lib/mysql/mysql.sock
default-character-set=gbk
# Here follows entries for some specific programs
# The MySQL server
[mysqld]
port = 3306
socket = /var/lib/mysql/mysql.sock
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M
# Try number of CPU’s*2 for thread_concurrency
thread_concurrency = 8
character_set_server=gbk
这个只需要成这样就可以了