设置 Mysql字符集为utf-8

在Ubuntu 下配置 Mysql 的字符编码。安装完 Mysql 后,系统默认的字符编码是 latin1 ,输入的是中文,可是输出却是一堆乱码。现在要做的就是把 Mysql的默认字符编码设置为支持中文的编码,如 GBK、GB23112、等。

配置MySQL字符编码

编辑mysql的配置文件/etc/mysql/my.conf

1
sudo gedit /etc/mysql/mysql.conf.d/mysqld.cnf

在[mysqld]下加入
character-set-server=utf8
就行了


MySQL服务设置

1
2
3
4
5
6
7
启动:sudo /etc/init.d/mysql start 
停止:sudo /etc/init.d/mysql stop
重启:sudo /etc/init.d/mysql restart

查看
ps -A | grep -i mysql
sudo netstat -ntlp | grep 3306

查看字符集

1
2
3
4
5
\s
status;
mysql> show variables like '%char%';
mysql> show variables like 'collation_%';
mysql> show variables like 'character_set_%';

默认mysqld.cnf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

#
# The MySQL Commercial Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

[mysqld_safe]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
nice = 0

[mysqld]
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
explicit_defaults_for_timestampsudo netstat -ntlp | grep 3306

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1

log-error = /var/log/mysql/error.log

# Recommended in standard MySQL setup
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
文章目录
  1. 1. 配置MySQL字符编码
  2. 2. MySQL服务设置
  3. 3. 查看字符集
  4. 4. 默认mysqld.cnf
,