方法1:
一、查看数据库编码格式
1
|
mysql> show variables like 'character_set_database' ;
|
二、查看数据表的编码格式
1
|
mysql> show create table <表名>;
|
三、创建数据库时指定数据库的字符集
1
|
mysql> create database <数据库名> character set utf8;
|
四、创建数据表时指定数据表的编码格式
1
2
3
4
5
|
create table tb_books (
name varchar (45) not null ,
price double not null ,
bookCount int not null ,
author varchar (45) not null ) default charset = utf8;
|
五、修改数据库的编码格式
1
|
mysql> alter database <数据库名> character set utf8;
|
六、修改数据表格编码格式
1
|
mysql> alter table <表名> character set utf8;
|
七、修改字段编码格式
1
2
3
|
mysql> alter table <表名> change <字段名> <字段名> <类型> character set utf8;
mysql> alter table user change username username varchar (20) character set utf8 not null ;
|
方法2:
更改配置文件,mac系统下:
1 | sudo vi /etc/my.cnf |
编辑,加入:
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
character-set-server=utf8
即:
这里特别要注意[mysqld]标签下一定要是character-set-server=utf8,不然mysql无法启动
show variables like 'character_set_%'; +--------------------------+-----------------------------------------------------------+ | Variable_name | Value | +--------------------------+-----------------------------------------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | latin1 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | latin1 | | character_set_system | utf8 | | character_sets_dir | /usr/local/mysql-5.7.17-macos10.12-x86_64/share/charsets/ | +--------------------------+-----------------------------------------------------------+ |
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/local/mysql-5.7.17-macos10.12-x86_64/share/charsets/ | +--------------------------+-----------------------------------------------------------+ |
前后对比,更改成功
用pycharm连接mysql数据库时,要注意添加 charset="utf8",即:
cnx = pymysql.connect(user="root",passwd="root",host="localhost",db="db_name",charset="utf8")
不然中文无法导入数据库
----清空数据表
truncate table tenement_message;
-----查找mysql文件夹
which mysql