转载来自:https://www.2cto.com/database/201403/285169.html
安装完的MySQL的默认字符集为 latin1 ,为了要将其字符集改为用户所需要的(比如utf8),就必须改其相关的配置文件;由于linux下MySQL的默认安装目录分布在不同的文件下;不像windows一样放在同一目录下,只需修改其中的my.ini文件,重起后就生效了;所以先来看看linux下MySQL的数据库文件、配置文件和命令文件分别在不同的目录 :
1、数据库目录,其所创建的数据库文件都在该目录下
/var/lib/mysql/
2、配置文件 (mysql.server命令及配置文件所在地)
/usr/share/mysql
3、相关命令(如mysql mysqladmin等)
/usr/bin
4、启动脚本(如mysql启动命令)
/etc/rc.d/init.d/
查看默认字符集
#mysql -u root - p
#(输入密码)
mysql> show variables like 'character_set%';
+--------------------------+----------------------------+
| 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/ |
+--------------------------+----------------------------+
修改字符集 修改 /etc/my.cnf文件
#/etc/my.cnf
[client]
default-character-set=utf8
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
#default-character-set=utf8
character-set-server=utf8
init_connect='SET NAMES utf8'
[mysql]
no-auto-rehash
default-character-set=utf8
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
切记:在mysqld中加入的是 character-set-server=utf8 而不是 default-character-set=utf8
否则会报:Starting MySQL...The server quit without updating PID file [失败]lib/mysql/localhost.localdomain.pid).异常
重起MySQL服务器,使其设置的内容生效
#/etc/init.d/mysql restart