來源:h4ck0ne 發(fā)布時(shí)間:2018-12-08 11:39:34 閱讀量:1164
修改mysql數(shù)據(jù)庫的默認(rèn)編碼方式
修改my.ini文件
加上
default-character-set=gb2312
設(shè)定數(shù)據(jù)庫字符集
alter database da_name default character set 'charset'
1)設(shè)置數(shù)據(jù)庫編碼 /etc/my.cnf
[mysqld]
default-character-set=gbk
...
[client]
default-character-set=gbk
---------------------------------------
2)按字符集導(dǎo)出
$mysqldump -u root -p dbname --default-character-set=gbk > a.sql;
3)查看SQL文件的編碼
[root@localhost gethtml]# file a.sql
a.sql: UTF-8 Unicode ...
[root@localhost gethtml]# iconv -f utf-8 -t gbk a.sql > a2.sql
[root@localhost gethtml]# file a2.sql
a2.sql: Non-ISO extended-ASCII English text 這時(shí)已經(jīng)是gbk的編碼了
3)導(dǎo)入
查看數(shù)據(jù)庫服務(wù)和客戶端字符集
mysql> status;
Server characterset: gbk
Db characterset: gbk
Client characterset: latin1
Conn. characterset: latin1
mysql> set names 'gbk'; //這樣
mysql> status;
Server characterset: gbk
Db characterset: gbk
Client characterset: gbk
Conn. characterset: gbk
這時(shí)才能導(dǎo)數(shù)據(jù)
mysql> source a.sql;
----------------------------------------------------------------------------------
單獨(dú)設(shè)置某個(gè)數(shù)據(jù)庫:
alter database testdb character set utf8;
查看mysql支持的編碼:
show character set;
查看系統(tǒng)的字符集和排序方式的設(shè)定可以通過下面的兩條命令:
mysql> SHOW VARIABLES LIKE ''character_set_%'';
LINUX 如下:
1)導(dǎo)出數(shù)據(jù)
[root@www.cnscn.org ~]$ mysqldump -u root -p dbname --default-character-set=gbk > base_user.sql;
2)查看導(dǎo)出的sql文件的編碼
[root@www.cnscn.org ~]$ file base_user.sql
base_user.sql: UTF-8 Unicode text, with very long lines
3)轉(zhuǎn)成要用的編碼
[root@www.cnscn.org ~]$ iconv -f utf-8 -t gbk base_user.sql >base_user_gbk.sql
4)連接數(shù)據(jù)庫并查看當(dāng)前庫的編碼
[root@www.cnscn.org ~]$ mysql -uroot
mysql> use testdb;
mysql> status;
Server characterset: latin1
Db characterset: gbk
Client characterset: latin1
Conn. characterset: latin1
5)設(shè)置成需要的編碼
mysql>set names 'gbk';
6)查看現(xiàn)在的編碼
mysql> status;
Server characterset: latin1
Db characterset: gbk
Client characterset: gbk
Conn. characterset: gbk
7)導(dǎo)入我們轉(zhuǎn)換成gbk后的文件
mysql> source base_user_gbk.sql;
查看數(shù)據(jù)庫
查看數(shù)據(jù)庫字符編碼
通過MySQL命令行修改:(編碼可選)
mysql> set character_set_client=utf8;
mysql> set character_set_connection=utf8;
mysql> set character_set_database=utf8;
mysql> set character_set_results=utf8;
mysql> set character_set_server=utf8;
mysql> set character_set_system=utf8;
mysql> set collation_connection=utf8;
mysql> set collation_database=utf8;
mysql> set collation_server=utf8;
當(dāng)然還有:
修改數(shù)據(jù)庫的字符集mysql>use mydb
mysql>alter database mydb character set utf8;
8
創(chuàng)建數(shù)據(jù)庫指定數(shù)據(jù)庫的字符集
mysql>create database mydb character set utf8;
mysql將表的字符編碼轉(zhuǎn)換成utf-8
復(fù)制代碼 代碼如下:
alter table tb_anniversary convert to character set utf8;
修改數(shù)據(jù)庫mysql字符編碼為UTF8
步驟1:查看當(dāng)前的字符編碼方法
復(fù)制代碼 代碼如下:
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/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
字符編碼為latin1時(shí),用戶在給數(shù)據(jù)表插入一條字符類型(char)的記錄,在顯示此記錄時(shí)可能會(huì)無法正常顯示字符類型的數(shù)據(jù),故此處我們將mysql字符編碼修改為UTF8。操作如下:
步驟2:修改my.cnf配置文件,修改后如下(/etc/my.cnf):
復(fù)制代碼 代碼如下:
[client]
#password = your_password
port = 3306
socket = /var/lib/mysql/mysql.sock
default-character-set=utf8 #設(shè)置默認(rèn)編碼為UTF8
在線
客服
服務(wù)時(shí)間:周一至周日 08:30-18:00
選擇下列產(chǎn)品馬上在線溝通:
客服
熱線
7*24小時(shí)客服服務(wù)熱線
關(guān)注
微信
關(guān)注官方微信