mysql默认的编码方式是latin1,通过以下命令查看show variables like char%;或者mysql show variables like character%;+--------------------------+----------
mysql默认的编码方式是latin1,通过以下命令查看
show variables like 'char%';
或者:
show variables like 'char%';
mysql> show variables like "character%";
+--------------------------+----------------------------+
| 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 | latin1 |
| character_sets_dir | /opt/lampp/share/charsets/ |
+--------------------------+----------------------------+
character_set_connection为建立连接使用的编码;
character_set_database数据库的编码;
character_set_results结果集的编码;
character_set_server
修改mysql默认编码方式:
修改mysql的配置文件my.ini,该文件目录一般为/etc/my.ini
找到客户端配置[client]在下面添加default-character-set=utf8默认字符集为utf8在找到[mysqld]添加default-character-set=utf8默认字符集为utf8
init_connect='SET NAMES utf8'(设定连接mysql数据库时使用utf8编码,以让mysql数据库为utf8运行)
修改后大致如下:
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
default-character-set=utf8
init_connect='SET NAMES utf8'
character_set_server=utf8
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[client]
default-character-set=utf8修改好后,重新启动mysql即可,重新查询数据库编码可发现编码方式的改变:
show variables like 'char%';mysql> show variables like 'char%';
+--------------------------+----------------------------+
| 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 | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
查看mysql数据库编码方式:
show create database mydatabase;
show create database redmine;
+----------+------------------------------------------------------------------+
| Database | Create Database |
+----------+------------------------------------------------------------------+
| redmine | CREATE DATABASE `redmine` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+------------------------------------------------------------------+
从以上看出数据库的默认编码方式为latin1