mysql编码问题

来源:互联网 发布:淘宝推广电话 编辑:程序博客网 时间:2024/06/08 03:09



MAC安装MySQL后不能显示中文,参考了网上给的解决办法,有的没有成功,还导致无法启动mysql server.折腾一番终于解决了,记录下来。 
首先打开终端,登陆:

mysql -uroot -p 或

/usr/local/mysql/bin/mysql -uroot -p

  • 1
  • 1

输入密码后,查看数据库编码:

SHOW VARIABLES LIKE 'character%';
  • 1
  • 1

显示如下:

+--------------------------+----------------------------+| 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/share/mysql/charsets/ |+--------------------------+----------------------------+
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

此时若想暂时修改输入这两行代码

  1. set character_set_database=utf8; 
  2. set character_set_server=utf8; 


CTRL+D退出mysql,打开系统偏好设置,关闭mysql服务。 
然后查看mysql安装位置:

which mysql
  • 1
  • 1

默认安装在/usr/local/mysql/bin/mysql,进入mysql配置文件目录:

cd /usr/local/mysql/support-files
  • 1
  • 1

复制任意.cnf文件到etc目录下,新复制的文件名为my.cnf:

sudo cp my-medium.cnf /etc/my.cnf
  • 1
  • 1

打开my.cnf,一次做如下修改:

  1. 在[client]字段下加上default-character-set=utf8
  2. 在[mysqld]字段下加上character-set-server=utf8
  3. 在[mysql]字段下加上default-character-set=utf8 
    重启mysql服务,重新查看数据库编码SHOW VARIABLES LIKE 'character%';,显示如下:
mysql> SHOW VARIABLES LIKE 'character%';+--------------------------+--------------------------------------------------------+| 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.5.47-osx10.8-x86_64/share/charsets/ |+--------------------------+--------------------------------------------------------+8 rows in set (0.00 sec)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

至此,修改成功

如果修改后登录mysql出现下面错误:

ypdeMacBook-Pro:support-files yp$ mysql -uroot -pmysql: [ERROR] unknown variable 'sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES'
  • 1
  • 2
  • 1
  • 2

删除/etc/my.cnf中的:

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES若不如此操作还可以在workbench建表语句中加入ENGINE=InnoDB DEFAULT CHARSET=utf8;例如CREATE TABLE `category` (  `cid` varchar(32) NOT NULL,  `cname` varchar(20) DEFAULT NULL,#分类名称  PRIMARY KEY (`cid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;另外在c3p0.xml配置文件中需要加入characterEncoding=utf8
  • 1
  • 2

                                         在Mysql workbench中创建表时手动选择默认编码可一次性解决字段乱码问题

原创粉丝点击