CenOS下mysql5.6修改字符编码,解决sql语句中带有中文,会乱码问题

开发环境eclipse、工程编码UTF-8。由于mysql数据库中,表名中带有中文,所以在使用sql查询该表时,sql中也会相应带有该中文表名。

但是sql传递到mysql数据库进行查询时,其中的中文部分变为乱码,导致找不到该表而报错。参考网上的解决办法,列出自己的解决过程。

1、查看字符集

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/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)

2、修改字符集

进入mysql安装目录,我的是/usr/local/mysql

# vi my.cnf 

添加如下内容:

[client]
port = 3306
socket = /var/lib/mysql/mysql.sock
default-character-set=utf8


[mysqld]
port = 3306
socket = /var/lib/mysql/mysql.sock
character-set-server=utf8


[mysql]
no-auto-rehash
default-character-set=utf8

3、重启

# service mysqld stop

# service mysqld start

4、登录报错

# mysql -u root -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

5、
# ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock

6、再次登录,进入数据库。查看字符集

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/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)

已经改过来了

7.再次运行java代码进行查询,没报错,得到久违的结果!

你可能感兴趣的:(mysql,编码)