1)MySQL插入中文數(shù)據(jù)亂碼問題:
湘潭網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、APP開發(fā)、成都響應式網(wǎng)站建設等網(wǎng)站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯(lián)自2013年起到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設就選創(chuàng)新互聯(lián)。
模擬亂碼現(xiàn)象: mysql> create database kitty; Query OK, 1 row affected (0.00 sec) mysql> show create database kitty\G; *************************** 1. row *************************** Database: kitty Create Database: CREATE DATABASE `kitty` /*!40100 DEFAULT CHARACTER SET utf8 */ 1 row in set (0.00 sec) ERROR: No query specified mysql> alter database kitty character set latin1; Query OK, 1 row affected (0.01 sec) mysql> flush privileges; mysql> use kitty; Database changed 創(chuàng)建teacher表(字符集為latin1): CREATE TABLE `teacher` ( `id` int(4) NOT NULL, `name` char(20) NOT NULL, `age` tinyint(2) NOT NULL DEFAULT '0', `dept` varchar(16) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1 插入兩條數(shù)據(jù),其中一行帶有中文字符 mysql> insert into teacher values(1,'wanlong','31','Server'),(2,'laomao','31','售后部'); Query OK, 2 rows affected, 1 warning (0.00 sec) Records: 2 Duplicates: 0 Warnings: 1 mysql> flush privileges; Query OK, 0 rows affected (0.00 se 查看發(fā)現(xiàn)有亂碼出現(xiàn): mysql> select * from teacher; +----+---------+-----+--------+ | id | name | age | dept | +----+---------+-----+--------+ | 1 | wanlong | 31 | Server | | 2 | laomao | 31 | ??? | +----+---------+-----+--------+ 2 rows in set (0.00 sec)
2) 如何解決亂碼了
不亂碼的思想:建議中英文混合的環(huán)境,選擇utf-8
客戶端:
set names utf8(臨時生效)
修改my.cnf(永久生效)
[client]
default-character-set=utf8
服務端-庫-表-程序
服務端: [mysqld] default-character-set=utf8(適合5.1及以前的版本) character-set-server=utf8(適合5.5) 庫: 查看數(shù)據(jù)庫的編碼: mysql> show variables like'%char%'; +--------------------------+-------------------------------------------+ | 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 | /application/mysql-5.5.32/share/charsets/ | +--------------------------+-------------------------------------------+ 8 rows in set (0.00 sec) 表: 查看表的編碼: mysql> show create table teacher\G; *************************** 1. row *************************** Table: teacher Create Table: CREATE TABLE `teacher` ( `id` int(4) NOT NULL, `name` char(20) CHARACTER SET latin1 NOT NULL, `age` tinyint(2) NOT NULL DEFAULT '0', `dept` varchar(16) CHARACTER SET latin1 DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.00 sec) ERROR: No query specified mysql> select * from teacher; +----+---------+-----+--------+ | id | name | age | dept | +----+---------+-----+--------+ | 1 | wanlong | 31 | Server | | 2 | laomao | 31 | ??? | | 3 | kobe | 35 | ??? | +----+---------+-----+--------+ 3 rows in set (0.00 sec) mysql> show create table teacher\G; *************************** 1. row *************************** Table: teacher Create Table: CREATE TABLE `teacher` ( `id` int(4) NOT NULL, `name` char(20) CHARACTER SET latin1 NOT NULL, `age` tinyint(2) NOT NULL DEFAULT '0', `dept` varchar(16) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.00 sec) ERROR: No query specified 修改字段的字符集: mysql> alter table `teacher` change `dept` `dept` varchar(16) CHARACTER SET utf8 NOT NULL; mysql> alter table `teacher` change `name` `name` char(20) CHARACTER SET utf8 NOT NULL; mysql> show full columns from teacher; +-------+-------------+-----------------+------+-----+---------+-------+---------------------------------+-- | Field | Type | Collation | Null | Key | Default | Extra | Privileges | C +-------+-------------+-----------------+------+-----+---------+-------+---------------------------------+-- | id | int(4) | NULL | NO | | NULL | | select,insert,update,references | | name | char(20) | utf8_general_ci | NO | | NULL | | select,insert,update,references | | age | tinyint(2) | NULL | NO | | 0 | | select,insert,update,references | | dept | varchar(16) | utf8_general_ci | NO | | NULL | | select,insert,update,references | +-------+-------------+-----------------+------+-----+---------+-------+---------------------------------+-- 4 rows in set (0.00 sec) mysql> select * from teacher; +----+---------+-----+--------+ | id | name | age | dept | +----+---------+-----+--------+ | 1 | wanlong | 31 | Server | | 2 | laomao | 31 | ??? | | 3 | kobe | 35 | ??? | +----+---------+-----+--------+ 3 rows in set (0.00 sec) 測試再次插入帶有中文字符的字段: mysql> insert into teacher values(4,'萬龍',30,'校園網(wǎng)'),(5,'知行',29,'華東院'); Query OK, 2 rows affected (0.01 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> select * from teacher; +----+---------+-----+-----------+ | id | name | age | dept | +----+---------+-----+-----------+ | 1 | wanlong | 31 | Server | | 2 | laomao | 31 | ??? | | 3 | kobe | 35 | ??? | | 4 | 萬龍 | 30 | 校園網(wǎng) | | 5 | 知行 | 29 | 華東院 | +----+---------+-----+-----------+ 5 rows in set (0.00 sec)
3)老數(shù)據(jù)仍舊是亂碼!
說明:
a)對于已有數(shù)據(jù)庫想修改字符集不能直接通過“alter database kitty character set *”或者“alter table tablename character set *”,這兩個命令都沒有更新已有記錄的字符集,而只是對新創(chuàng)建的表或記錄生效。
b)已經(jīng)有記錄的字符的調整,必須先將數(shù)據(jù)導出,經(jīng)過修改字符集后重新導入后才可完成
修改數(shù)據(jù)庫默認編碼:
“alter database kitty character set *”
4)參考解決方法:
如何更改生產(chǎn)MySQL數(shù)據(jù)庫庫表的字符集
1、導出表結構 mysqldump -uroot -predhat12345 -S /data/3306/mysql.sock --default-character-set=latin1 -d kitty>kittytable.sql 說明:-d只導表結構 2、編輯kittytable.sql,將lantin1改成utf8 可以用sed批量修改 3、確保數(shù)據(jù)庫不再更新,導出所有數(shù)據(jù) mysqldump -uroot -predhat12345 -S /data/3306/mysql.sock --quick --no-create-info --extended-insert --default-character-set=latin1 kitty>kittydata.sql 4、打開kittydata.sql,將set names latin1修改成set names utf8 5、刪除原有的庫表及數(shù)據(jù)(需要慎重) 6、建庫 create database kitty default charset utf8; 7、創(chuàng)建表,執(zhí)行kittytable.sql mysql -uroot -predhat12345 -S /data/3306/mysql.sock kitty5)執(zhí)行SQL文件插入中文數(shù)據(jù)不亂碼實戰(zhàn):
a、將需要更新的sql語句放到文本文件中
mysql> system cat test.sql set names utf8; insert into teacher values (6,'張飛',45,'商務部');b、通過source來調用sql文件
mysql> source test.sql Query OK, 0 rows affected (0.00 sec) Query OK, 1 row affected (0.01 sec) mysql> select * from teacher; +----+---------+-----+-----------+ | id | name | age | dept | +----+---------+-----+-----------+ | 1 | wanlong | 31 | Server | | 2 | laomao | 31 | ??? | | 3 | kobe | 35 | ??? | | 4 | 萬龍 | 30 | 校園網(wǎng) | | 5 | 知行 | 29 | 華東院 | | 6 | 張飛 | 45 | 商務部 | +----+---------+-----+-----------+ 6 rows in set (0.00 sec)
本文名稱:mysql字符亂碼
網(wǎng)址分享:http://weahome.cn/article/pojeos.html