mysql设置默认编码格式utf-8

前言

  • mysql 5.7.20
  • mac os下演示

最近稍微闲了一阵子,就想去弄一下后台,于是装了mysql数据库,一开始登录mysql、创建用户、授权、创建数据库、创建表等都很顺利,在做表插入的时候发现mysql中文插入报错,于是网上搜索解决办法,发现都是 /usr/local/mysql/support-files 目录下找my-default.cnfmy.cnf文件,奇怪的是这两个文件都没有,

经过一番操作后,发现只需要创建一个 my.cnf 文件就可以了,然后移动到/etc目录下就行了

配置

  • 终端下创建 my.cnf

    cd ~
    touch my.cnf
  • 编辑 my.cnf

    open -a xcode my.cnf

    [client]
    default-character-set=utf8
    [mysqld]
    default-storage-engine=INNODB
    character-set-server=utf8
    collation-server=utf8_general_ci

  • 移动 my.cnf/etc目录下

    mv my.cnf /etc

大功告成,记得重启一下mysql服务,系统偏好设置底下打开MySql,重启后查看一下

/usr/local/mysql/bin/mysql -uwz -p

# 如果配置了.base_profile,那么输入 mysql -uroot -p 就行了
# 输入你的root账户密码

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.20 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

# 再次输入
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/local/mysql-5.7.20-macos10.12-x86_64/share/charsets/ |
+--------------------------+-----------------------------------------------------------+
8 rows in set (0.01 sec)

# 如果提示这样就说明可以了

如果你的/usr/local/mysql/support-files文件夹中有一个my-default.cnf文件,将它复制到桌面更名为my.cnf,最后还是一样移动到/etc目录下

更改当前表字段的编码格式

上述操作后,你发现还是插入不了中文,那么你可以看下当前表所有字段状态

# 选中数据库
use test;
# 查看表字段状态
SHOW FULL COLUMNS FROM 'you table';

+----------+------------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
| Field    | Type             | Collation       | Null | Key | Default | Extra          | Privileges                      | Comment |
+----------+------------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
| id       | int(10) unsigned | NULL            | NO   | PRI | NULL    | auto_increment | select,insert,update,references |         |
| username | varchar(45)      | utf8_general_ci | NO   |     | NULL    |                | select,insert,update,references |         |
| password | varchar(45)      | utf8_general_ci | NO   |     | NULL    |                | select,insert,update,references |         |
+----------+------------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
3 rows in set (0.01 sec)

# 更改字段的编码格式
ALTER TABLE `test`.`table_user` CHANGE COLUMN `username` `username` VARCHAR(45) CHARACTER SET 'utf8_unicode_ci' NOT NULL ;

ALTER TABLE `test`.`table_user` CHANGE COLUMN `password` `password` VARCHAR(45) CHARACTER SET 'utf8_unicode_ci' NOT NULL ;

# 查看表字段状态
SHOW FULL COLUMNS FROM 'you table';

如果字段的编码格式是utf8_general_ci的话,应该不会出现插入报错的情况,请检查你的sql

    原文作者:MySql
    原文地址: https://juejin.im/entry/5a323f3d6fb9a045117126c6
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞