萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> mysql教程 >> Liunx如何修改MySQL字符集

Liunx如何修改MySQL字符集

今天發現在linux中我mysql數據庫中的中文全部都是亂碼了,我查看了mysql字符集默認為latin1了,下面我來給大家介紹幾種修改方法。

Linux下使用service mysql restart命令重啟MySql服務

新建的數據庫都會使用到這個字符集做為默認字符集

修改MySql服務器各個數據庫的字符集

如果在設置服務器字符集之前,存在了數據庫。需要更改數據庫的字符集:

 代碼如下 復制代碼

use datatable_name;
alter database character set gbk;

改完字符集的其它問題

字符集修改好了以後發現使用存儲過程做新增操作時,還會出現中文亂碼,但是直接寫SQL語句就不會出現亂碼。

查看字符集設置

 代碼如下 復制代碼 mysql> show variables like 'collation_%';
+----------------------+-----------------+
| Variable_name         | Value            |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database    | utf8_general_ci |
| collation_server      | utf8_general_ci |
+----------------------+-----------------+
3 rows in set (0.02 sec)
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/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.02 sec)
mysql>

1.查找MySQL的cnf文件的位置

 代碼如下 復制代碼

find / -iname '*.cnf' -print

/usr/share/mysql/my-innodb-heavy-4G.cnf
/usr/share/mysql/my-large.cnf
/usr/share/mysql/my-small.cnf
/usr/share/mysql/my-medium.cnf
/usr/share/mysql/my-huge.cnf
/usr/share/texmf/web2c/texmf.cnf
/usr/share/texmf/web2c/mktex.cnf
/usr/share/texmf/web2c/fmtutil.cnf
/usr/share/texmf/tex/xmltex/xmltexfmtutil.cnf
/usr/share/texmf/tex/jadetex/jadefmtutil.cnf
/usr/share/doc/MySQL-server-community-5.1.22/my-innodb-heavy-4G.cnf
/usr/share/doc/MySQL-server-community-5.1.22/my-large.cnf
/usr/share/doc/MySQL-server-community-5.1.22/my-small.cnf
/usr/share/doc/MySQL-server-community-5.1.22/my-medium.cnf
/usr/share/doc/MySQL-server-community-5.1.22/my-huge.cnf

2. 拷貝

 代碼如下 復制代碼

small.cnf、my-medium.cnf、my-huge.cnf、my-innodb-heavy-4G.cnf其中的一個到/etc下,命名為my.cnf

cp /usr/share/mysql/my-medium.cnf /etc/my.cnf

3. 修改my.cnf

 代碼如下 復制代碼

vi /etc/my.cnf
在[client]下添加
default-character-set=utf8
在[mysqld]下添加
default-character-set=utf8

4.重新啟動MySQL

 代碼如下 復制代碼

[root@bogon ~]# /etc/rc.d/init.d/mysql restart
Shutting down MySQL                                         [ 確定 ]
Starting MySQL.                                             [ 確定 ]
[root@bogon ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 1
Server version: 5.1.22-rc-community-log MySQL Community Edition (GPL)
Type 'help;' or 'h' for help. Type 'c' to clear the buffer.

copyright © 萬盛學電腦網 all rights reserved