您的位置:首页 > 数据库 > MySQL

Mac下Mysql中文乱码解决方案

2017-11-08 11:41 176 查看
Mysql在使用过程中,并不能对中文数据进行输入。

以下是笔者的解决方案:

Step1.

开启终端。
在终端中输入mysql -uroot -p
输入密码进入mysql
mysql> 输入当前命令查看编码格式show variables like '%char%';



我们可以看到编码格式错误,database是latin 1不支持中文。


Step2.(在此步骤之前一定要在系统偏好设置里面关闭mysql服务!!!!)

最新的Mysql版本已经没有了my.cnf文件。
所以我们要新建一个my.cnf文件(不要用mac自带的文本编辑,笔者建议用Sublime)
文件内容如下:

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html # *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

#更改数据库编码需要更改的
[mysqld]
character-set-server=utf8

#备份数据库需要更改的
[mysqldump]
user=root
password=123456

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to tu
4000
rn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

直接复制粘贴进my.cnf文件即可保存。

Step3.

进入etc文件夹,方式如下。

command+space➡️/etc/➡️回车

将my.cnf文件粘贴到该文件夹。(需要权限,输入计算机密码即可)

完成。

Step4(检查是否成功,打开mysql服务)

开启终端。

在终端中输入mysql -uroot -p

输入密码进入mysql

mysql> 输入当前命令查看编码格式show variables like '%char%';

如果和下面一致就没问题:

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/local/mysql-5.6.26-osx10.8-x86_64/share/charsets/ |

+--------------------------+--------------------------------------------------------+

8 rows in set (0.00 sec)

接下来就可以对数据库插入中文数据啦,注意如果还是不能插入汉字,重新创建数据库和表就可以了。

道阻且长
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息