一、查看mysql編碼(默認latin1)python
show variables like 'collation_%';
+--------------------------+----------------------------+ | 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/ | +--------------------------+----------------------------+ 8 rows in set (0.00 sec)
二、臨時修改編碼(重啓後失效)mysql
mysql> alter database name character set utf8; mysql> set character_set_connection=utf8; Query OK, 0 rows affected (0.00 sec) mysql> set character_set_connection=utf8; Query OK, 0 rows affected (0.00 sec) mysql> set character_set_results=utf8; Query OK, 0 rows affected (0.00 sec) mysql> set character_set_server=utf8; Query OK, 0 rows affected (0.00 sec)
三、永久設置編碼(修改配置文件,重啓Mysqld服務)linux
win系統: mysql目錄下my.ini [client] port=3306 default-character-set=utf8 #客戶端字符類型,與服務端一致就行,建議utf8 [mysqld] port=3306 character_set_server=utf8 #服務端字符類型,建議utf8 basedir=D:/MySql/mysql-5.6.17-winx64 #解壓根目錄 datadir=D:/MySql/mysql-5.6.17-winx64/data #解壓根目錄/data sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES [WinMySQLAdmin] D:/MySql/mysql-5.6.17-winx64/bin/mysqld.exe #解壓根目錄/bin/mysqld.exe
linux系統 $ vi /etc/my.cnf [mysqld] default-character-set=utf8 character_set_server=utf8 init_connect='SET NAMES utf8' 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 [mysql] default-character-set=utf8 [musql.server] default-character-set=utf8 [client] default-character-set=utf8 [mysqld_safe] default-character-set=utf8 log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
結果:sql
mysql> show variables like 'character_set_%'; +--------------------------+----------------------------+ | 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數據庫和表並設置編碼數據庫
CREATE DATABASE `dbname` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE TABLE IF NOT EXISTS test_tb( id int(10) PRIMARY KEY NOT NULL AUTO_INCREMENT, name varchar(20) NOT NULL) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; 或 CREATE TABLE IF NOT EXISTS test_tb( id int(10) PRIMARY KEY NOT NULL AUTO_INCREMENT, name varchar(20) NOT NULL); -- 修改表編碼 ALTER TABLE `test_tb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; --修改列編碼 ALTER TABLE `test_tb` CHANGE `name` `name` VARCHAR( 100 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL;