Centos-Mysql建立資料庫-編碼設定

大向二發表於2019-01-17

建立資料庫

mysqladmin -h `192.168.2.233` -u root -p create namc;

檢視新建立的資料庫編碼

mysql> SHOW VARIABLES LIKE `character%`
-> ;
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | latin1                     |
| character_set_connection | latin1                     |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | latin1                     |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

編碼設定

set character_set_client=utf8;
set character_set_connection=utf8;
set character_set_database=utf8;
set character_set_filesystem=binary;
set character_set_results=utf8;
set character_set_server=utf8;

以上是臨時設定編碼,如果資料庫重新啟動,編碼會還原。

永久設定mysql的配置檔案中的編碼。

vi /etc/my.cnf

在[client]欄位里加入default-character-set=utf8,如下:

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

在[mysqld]欄位里加入character-set-server=utf8,如下:

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

在[mysql]欄位里加入default-character-set=utf8,如下:

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

修改完成後,service mysql restart重啟mysql服務就生效。注意:[mysqld]欄位與[mysql]欄位是有區別的。

使用SHOW VARIABLES LIKE ‘character%’;檢視,發現資料庫編碼全已改成utf8。

+--------------------------+----------------------------+
| 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/ |

如果上面的都修改了還亂碼,那剩下問題就一定在connection連線層上。解決方法是在傳送查詢前執行一下下面這句(直接寫在SQL檔案的最前面):

SET NAMES ‘utf8′;

它相當於下面的三句指令:

SET character_set_client = utf8;
SET character_set_results = utf8;
SET character_set_connection = utf8;

修改ssh顯示中文格式
vim /etc/sysconfig/i18n

LANG="en_US.UTF-8"
SYSFONT="latarcyrheb-sun16"
將以上內容修改為以下內容
LANG="zh_CN.UTF-8"
SYSFONT="zh_CN.UTF-8:zh_CN:zh"


相關文章