中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Mysql DBA 高級運維學習之路-mysql數據庫亂碼問題

發布時間:2020-07-05 06:47:42 來源:網絡 閱讀:1058 作者:海風掠過 欄目:MySQL數據庫

1.在mysql數據庫中插入數據不亂碼的方法

1.1 建立測試數據

(1)創建linzhongniao測試數據庫并查看建表語句

mysql> create database linzhongniao;
Query OK, 1 row affected (0.00 sec)
mysql> show create database linzhongniao\G
 *************************** 1. row ***************************
   Database: linzhongniao
Create Database: CREATE DATABASE `linzhongniao` /*!40100 DEFAULT CHARACTER SET latin1 */
1 row in set (0.00 sec)

提示:如果在安裝MySQL時未指定字符集或者指定的是latin字符集,則mysql默認字符集是latin1。

(2)在linzhongniao庫下創建一個student表,并查看表結構和建表語句

mysql> use linzhongniao
Database changed
mysql> show tables;
+--------------------+
| Tables_in_linzhongniao |
+--------------------+
| student|
+--------------------+
1 row in set (0.00 sec)

mysql> create table student( id int(4) NOT NULL AUTO_INCREMENT, name char(20) NOT NULL, PRIMARY KEY(id) );
mysql> desc student;
+-------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra  |
+-------+----------+------+-----+---------+----------------+
| id| int(4)   | NO   | PRI | NULL| auto_increment |
| name  | char(20) | NO   | | NULL||
+-------+----------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
mysql> show create table student\G
*************************** 1. row ***************************
   Table: student
Create Table: CREATE TABLE `student` (
  `id` int(4) NOT NULL AUTO_INCREMENT,
  `name` char(20) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

提示:默認建表不指定字符集則繼承庫的字符集,即latin1。

(3)批量插入數據到student表

mysql> insert into student values(1,'zhangsan'),(2,'lisi'),(3,'xiaozhang'),(4,'xiaohong');
Query OK, 4 rows affected (0.01 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select * from student;
+----+-----------+
| id | name  |
+----+-----------+
|  1 | zhangsan  |
|  2 | lisi  |
|  3 | xiaozhang |
|  4 | xiaohong  |
+----+-----------+
4 rows in set (0.00 sec)

提示:數字和英文的數據時正常的,不會有亂碼問題。

(4)插入兩條中文數據

mysql> insert into student values(5,'我是誰');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> insert into student values(6,'你好啊');
Query OK, 1 row affected (0.00 sec)
mysql> select * from student;
+----+-----------+
| id | name  |
+----+-----------+
|  1 | zhangsan  |
|  2 | lisi  |
|  3 | xiaozhang |
|  4 | xiaohong  |
|  5 | ??? |
|  6 | ??? |
+----+-----------+
8 rows in set (0.00 sec)

出現問題:中文內容亂碼

為什么插入中文數據會出現亂碼問題呢?

通過上面的例子我們可以看出客戶端字符集和庫,表字符集不一樣導致亂碼問題,所以我們在插入數據的時候要先查看系統字符集和客戶端,庫表字符集是否一樣,不一樣將字符集修改一致再插入數據。已經插入的數據有亂碼可以將數據導出備份添加修改字符集命令后再重新導入。

1.2 方法一執行set names命令

命令語法:set names 接指定字符集

(1)查看建表語句,注意默認的字符集是latin1

mysql> show create table student\G
*************************** 1. row ***************************
   Table: student
Create Table: CREATE TABLE `student` (
  `id` int(4) NOT NULL AUTO_INCREMENT,
  `name` char(20) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

(2)設置MySQL客戶端的字符集和建表的字符集latin1一致

設置插入數據的字符集為latin

mysql> set names latin1;
Query OK, 0 rows affected (0.00 sec)

(3)再插入一條中文數據

mysql> insert into student values(7,'林中鳥');
Query OK, 1 row affected (0.00 sec)

mysql> select * from student where id='7';
+----+-----------+
| id | name  |
+----+-----------+
|  7 | 林中鳥 |
+----+-----------+
1 row in set (0.00 sec)

提示:不亂碼了,但是以前的數據就沒辦法解決了。

上面的是MySQL命令行插入數據不亂碼的方法,那么如果更新的數據多就需要執行sql文件更新數據了,所以保證執行sql文件也不亂碼怎么辦呢?

1.3 方法二執行sql文件

(1)將要更新的多個sql語句放在文本文件中如test.sql

需要用system命令,執行system命令可以不退出數據庫對系統的文件進行引用和查看。當然也可以退出數據庫這樣會比較麻煩。

mysql> system cat test.sql
set names latin1;
insert into student values(8,'不認識');
mysql> system ls;
beifen.sh  test.sql

提示:必須要加入set names latin1,確保插入數據不亂碼。

(2)在MySQL命令行中通過source調用test.sql文件插入數據

用source命令執行sql文件實現對數據庫的操作,可以恢復數據庫的數據當然也可以退出數據庫用輸入重定向執行sql文件對數據庫的數據進行恢復。

mysql> source test.sql
Query OK, 0 rows affected (0.01 sec)

Query OK, 1 row affected (0.00 sec)

mysql> select * from student where id='8';
+----+-----------+
| id | name  |
+----+-----------+
|  8 | 不認識 |
+----+-----------+
1 row in set (0.00 sec)

小結:執行DQL,DML語句的時候要set names 保持庫和表的字符集一致,還要調整客戶端的字符集。

1.3 方法三添加字符集參數解決導入數據亂碼問題

(1)把要更新的多個SQL語句放入文本中,這次不帶set names latin1

#set names latin1;
insert into student values(9,'小紅');

(2)通過MySQL命令加上字符集參數指定latin1字符集導入test.sql

[root@localhost ~]# mysql -uroot -p123456 --default-character-set=latin1 linzhongniao < test.sql

(3)通過-e參數在mysql庫外查看結果

[root@localhost ~]# mysql -uroot -p123456 -e "select * from linzhongniao.student where id='9'"
+----+--------+
| id | name   |
+----+--------+
|  9 | 小紅 |
+----+--------+

1.4 MySQL插入中文不亂碼5中方法小結

方法一:執行set names命令再插入數據

mysql> set names latin1;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into student values(7,'林中鳥');
Query OK, 1 row affected (0.00 sec)

提示:確保test.sql文件格式正確

方法二:在sql文件中指定set names latin1;然后登錄mysql,通過如下命令執行。

mysql> system cat test.sql
set names latin1;
insert into student values(8,'不認識');
mysql> source test.sql
Query OK, 0 rows affected (0.00 sec)

方法三:在sql文件中指定set names latin1 然后通過mysql導入

[root@localhost ~]# mysql -uroot -p123456  linzhongniao < test.sql
[root@localhost ~]# mysql -uroot -p123456 -e "set names latin1;select * from linzhongniao.student"

提示:這里的linzhongniao是庫名不是表名。

方法四:通過指定mysql命令的字符集參數來實現

#set names latin1;
insert into student values(9,'李四');
[root@localhost ~]# mysql -uroot -p123456 --default-character-set=latin1 linzhongniao < test.sql

方法五:在配置文件里設置客戶端及服務端相關參數

(1)更改my.cnf客戶端client模塊的參數,可以實現set names latin1效果,并永久生效

[client]
default-character-set=latin1

提示:不需要重啟服務,退出重新登陸生效。

(2)在服務端mysqld模塊里面再指定latin1字符集

[mysqld]
default-character-set=latin1適合5.1 及以前呢版本
default-character-server=latin1 適合5.5

2.mysql數據庫字符集知識

2.1 MySQL數據庫字符集介紹

簡單的說是一套文字符號及其編碼、比較規則的集合。MySQL數據庫字符集包括字符集(CHARACTER)和校對規則(COLLATION)兩個概念。其中,字符集是用來定義MySQL數據字符串的存儲方式,而校對規則則是定義比較字符串的方式。前面建庫的語句中CHARACTER SET latin1即為數據庫字符集而COLLATE latin1_swedish_ci 為校對字符集,有關字符集詳細內容參考mysql手冊,第10張字符集章節。

2.2 MySQL數據庫常見字符集介紹

使用MySQL時常用的字符集有下表四種

Mysql DBA 高級運維學習之路-mysql數據庫亂碼問題

2.3 MySQL如何選擇合適的字符集

(1)如果處理各種各樣的文字,發布到不同國家和地區,應選Unicode字符集。對mysql來說就是UTF-8(每個漢字三個字節),如果應用需處理英文,有少量漢字使用UTF-8字符集更好。

(2)如果只需支持中文,并且數據量很大,性能要求也很高,可選GBK(定長,每個漢字占雙字節,英文也占雙字節),處理大量運算,比較順序等定長字符集更快,性能高。

(3)處理移動互聯網業務,可能需要使用utf8mb4字符集。

2.4 查看當前MySQL系統支持的字符集

最常用的有四種:

[root@localhost ~]# mysql -uroot -p123456 -e "SHOW CHARACTER SET;"|egrep "gbk|utf8|latin1"|awk ' {print $0}'
latin1    cp1252 West European      latin1_swedish_ci    1
gbk       GBK Simplified Chinese    gbk_chinese_ci       2
utf8      UTF-8 Unicode             utf8_general_ci      3
utf8mb4   UTF-8 Unicode             utf8mb4_general_ci   4

查看mysql當前的字符集設置情況

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

提示:默認情況下character_set_client,character_set_connection,character_set_results三者的字符集和系統的字符集是一致的,是同時修改的。即為:

[root@localhost ~]# cat /etc/sysconfig/i18n 
LANG="zh_CN.UTF-8"
[root@localhost ~]# echo $LANG
zh_CN.UTF-8

3.mysql數據庫默認設置的字符集是什么?

(1)先看一下mysql默認情況下設置的字符集

mysql> show variables like 'character_set%';
+--------------------------+----------------------------------+
| Variable_name| Value|
+--------------------------+----------------------------------+
| character_set_client | gb2312   |
| character_set_connection | gb2312   |
| character_set_database   | latin1   |
| character_set_filesystem | binary   |
| character_set_results| gb2312   |
| character_set_server | latin1   |
| character_set_system | utf8 |
| character_sets_dir   | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+

(2)不同字符集參數的含義如下

| character_set_client | latin1  客戶端字符集
| character_set_connection | latin1  連接字符集
| character_set_database   | latin1數據庫字符集,配置文件指定或建庫建表指定
| character_set_results| latin1  返回結果字符集
| character_set_server | latin1服務器字符集,配置文件指定或建庫建表指定

更改linux系統字符集變量后,查看MySQL中字符集的變化

[root@localhost ~]# echo $LANG
zh_CN.UTF-8
[root@localhost ~]# mysql -uroot -p123456 -e "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/ |
+--------------------------+----------------------------------+

我們發現character_set_connection,character_set_client,character_set_server 三者的字符集和系統的一致也都改成utf8了。

4.執行set names latin1到底做了什么

無論linux系統的字符集是gb2312還是utf8默認情況下插入數據都是亂碼的。

(1)此時查看數據就是亂碼

mysql> use linzhongniao
Database changed
mysql> select * from student
-> ;
+----+---------------------+
| id | name|
+----+---------------------+
|  1 | zhangsan|
|  2 | lisi|
|  3 | wanger  |
|  4 | xiaozhang   |
|  5 | xiaowang|
|  6 | ??? |
|  7 | ?°?o¢  |
|  8 | ??è?¤èˉ?   |
|  9 | ?????  |
+----+---------------------+
9 rows in set (0.10 sec)

(2)執行完set對應的字符集操作,就解決亂碼問題了

mysql> show create database linzhongniao\G
*************************** 1. row ***************************
   Database: linzhongniao
Create Database: CREATE DATABASE `linzhongniao` /*!40100 DEFAULT CHARACTER SET latin1 */
1 row in set (0.00 sec)
mysql> show create table student\G
*************************** 1. row ***************************
   Table: student
Create Table: CREATE TABLE `student` (
  `id` int(4) NOT NULL AUTO_INCREMENT,
  `name` char(20) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

我們看庫和表的字符集都是latin1,所以執行set names latin1保證字符集一樣就不會亂碼了。

mysql> set names latin1;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from student;
+----+-----------+
| id | name  |
+----+-----------+
|  1 | zhangsan  |
|  2 | lisi  |
|  3 | wanger|
|  4 | xiaozhang |
|  5 | xiaowang  |
|  6 | ???   |
|  7 | 小紅  |
|  8 | 不認識|
|  9 | 李四  |
+----+-----------+

(3)執行完set字符集操作的結果改變了如下字三個字符集character_set_client,character_set_connection,character_set_results的參數。

mysql> show variables like 'character_set%';
+--------------------------+----------------------------------+
| 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/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+

5.mysql命令參數—default-character-set=latin1在做什么?

(1)先查看一下mysql的字符集

[root@localhost ~]# mysql -uroot -p123456 -e "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/ |
+--------------------------+----------------------------------+

(2)帶—default-character-set=latin1 參數登錄mysql

[root@localhost ~]# mysql -uroot -p123456 --default-character-set=latin1
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.5.32 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

(3)現在再查看mysql的字符集

mysql> show variables like 'character_set%';
+--------------------------+----------------------------------+
| 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/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+

(4)帶參數登錄也是臨時修改不帶參數登錄又變回去了

[root@localhost ~]# mysql -uroot -p123456 --default-character-set=latin1 -e "show variables like 'character_set%';"
+--------------------------+----------------------------------+
| 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/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+

[root@localhost ~]# mysql -uroot -p123456 -e "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/ |
+--------------------------+----------------------------------+

6.確保MySQL數據庫插入數據不亂碼解決方案

6.1 統一客戶端字符集

(1)MySQL數據庫的下面幾個字符集(客戶端和服務端)統一成一個字符集才能確保插入的中文數據庫可以正常輸出。當然,linux系統的字符集也要盡可能和數據庫字符集統一。

(2)mysql數據庫字符集的含義:

Variable_name              | Value 
+--------------------------+--------------------------------+
①character_set_client      | latin1  客戶端字符集
②character_set_connection  | latin1  連接字符集
③character_set_database    | latin1   數據庫字符集
④character_set_results     | latin1   返回結果字符集
⑤character_set_server      | latin1   服務器字符集,配置文件制定或建庫建表指定

其中,①②④三個參數默認情況采用linux系統字符集設置,人工登錄數據庫執行set names latin1以及mysql指定字符集登錄操作,都是改變mysql客戶端的client、connection、results3個參數的字符集都為latin1,從而解決插入亂碼問題,這個操作可以在my.cnf配置文件里修改mysql客戶端的字符集,配置方法如下:

[client]
Default-character-set=latin1
提示:不需要重啟
[root@localhost ~]# sed -n "18,22p" /etc/my.cnf 
[client]
#password    = your_password
port        = 3306
socket        = /usr/local/mysql/tmp/mysql.sock
default-character-set = latin1
[root@localhost ~]# mysql -uroot -p123456 -e "show variables like 'character_set%';"
+--------------------------+----------------------------------+
| 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/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+

(3)修改完客戶端字符集不用set查詢表數據就不會亂碼了

[root@localhost ~]# mysql -uroot -p123456 -e "select * from linzhongniao.student;"
+----+-----------+
| id | name  |
+----+-----------+
|  1 | zhangsan  |
|  2 | lisi  |
|  3 | wanger|
|  4 | xiaozhang |
|  5 | xiaowang  |
|  6 | ???   |
|  7 | 小紅|
|  8 | 不認識 |
|  9 | 李四|
+----+-----------+

6.2 統一MySQL服務端字符集

(1) 按下面要求修改my.cnf參數

[mysqld]
default-character-set = latin1 適合5.1及以前版本
character-set-server = utf8 適合5.5版本

(2) 修改前查看當前字符集

[root@localhost ~]# mysql -uroot -p123456 -e "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/ |
+--------------------------+----------------------------------+

(3) 查看修改的參數

[root@localhost ~]# sed -n "26,27p" /etc/my.cnf 
[mysqld]
character-set-server = utf8  

(4) 重啟mysql服務(生產環境是不允許重啟的)

[root@localhost ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL.. SUCCESS!

(5) 查看更改后的字符集

[root@localhost ~]# mysql -uroot -p123456 -e "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/ |
+--------------------------+----------------------------------+

提示:以上在[mysqld]下設置的參數會更改下面2個參數的字符集設置。

| Variable_name            | Value|
| character_set_database   | utf8 |
| character_set_server     | utf8 |

這個時候我們再修改系統字符集mysql數據庫字符集就不亂碼了

[root@localhost ~]# cat /etc/sysconfig/i18n 
LANG="zh_CN.GB2312"
#LANG="zh_CN.UTF-8"
[root@localhost ~]# source /etc/sysconfig/i18n 
[root@localhost ~]# mysql -uroot -p123456 -e "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/ |
+--------------------------+----------------------------------+

6.3 統一字符集方法總結

保證數據庫數據不亂碼的方法:建議中英文環境選擇utf8 ,linux系統,客戶端,服務端,庫,表,程序字符集統一。

(1)Linux系統字符集統一utf8

[root@localhost ~]# cat /etc/sysconfig/i18n 
LANG="zh_CN.UTF-8"

提示linux客戶端也要更改字符集 例如:xshell

Mysql DBA 高級運維學習之路-mysql數據庫亂碼問題

例如:SecureCRT

Mysql DBA 高級運維學習之路-mysql數據庫亂碼問題

(2)Mysql數據庫客戶端

臨時:

set names latin1

永久:

更改my.cnf客戶端模塊的參數,可以實現set names latin1效果,并永久生效。
[client]
Default-character-set=latin1

(3)服務端

更改my.cnf參數

[mysqld]
Default-character-set = latin1 適合5.1及以前版本
character-set-server = latin1  適合5.5

(4)庫表,程序指定字符集建庫

create database linzhongniao_utf8 DEFAULT CHARACTER SET UTF8 COLLATE  后面加校對規則

我們可以show一下查看支持的校對規則

[root@localhost ~]# mysql -uroot -p123456 -e "SHOW CHARACTER SET;"|egrep "gbk|utf8|latin1"|awk ' {print $0}'
latin1    cp1252 West European      latin1_swedish_ci    1
gbk       GBK Simplified Chinese    gbk_chinese_ci       2
utf8      UTF-8 Unicode             utf8_general_ci      3
utf8mb4   UTF-8 Unicode             utf8mb4_general_ci   4

7.如何更改生產MySQL數據庫庫表的字符集

對于已有數據庫想修改字符集不能直接通過“alter database character set ”或者”alter table tablename character set ”,這兩個命令都不能更新已有數據的字符集。而只是對新創建的表或者數據生效。

已經有記錄的字符集的調整必須將數據導出,經過修改字符集之后重新導入才可完成。

修改數據庫默認編碼命令:

alter database [your db name] charset [your character setting]

下面模擬將latin1字符集的數據庫修改成GBK字符集的過程。

(1)導出表結構

使用mysqldump的-d參數導出表結構

mysqldump –uroot –p123456 –-default-character-set=latin1 –d dbname>alltable.sql –-default-character-set=gbk 表示以GBK字符集進行連接 –d只導表結構。

(2)然后編輯alltable.sql將latin1用sed替換成GBK

(3)確保數據不在更新導出所有數據

mysqldmup –uroot –p123456 –-quick –-no-create-info –-extended-insert –-default-character-set=latin1 dbname>alltables.sql

參數說明:

 --quick:用于轉儲大的表,強制mysqldump從服務器一次一行的檢索數據而不是檢索所有行并輸出前CACHE到內存中。

 --no-create-info:不創建CREATE TABLE 語句。

 --extended-insert:使用包括幾個VALUES列表的多行INSERT語法,這樣文件更小節省IO導入數據非常快。

 --default-character-set=latin1按照原有字符集導出數據,這樣導出的文件中,所有中文都是可見的,不會保存成亂碼。

(4)打開alltable.sql將set names latin1修改成set names gbk(或者修改my.cnf配置文件)

(5)建庫

create database dbname default charset gbk;

(6)創建表執行,alltable.sql

mysql –uroot –p123456 dbname<alltable.sql

(7)導入數據

mysql –uroot –p123456 dbname<alltables.sql
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

红桥区| 万山特区| 汪清县| 弥渡县| 玉门市| 青河县| 丰县| 门源| 抚顺市| 元氏县| 会泽县| 汝阳县| 阳城县| 盐亭县| 河北省| 商城县| 科技| 西乡县| 盱眙县| 黄石市| 化隆| 天柱县| 五台县| 庄河市| 乐清市| 肥城市| 佛冈县| 夹江县| 宁乡县| 厦门市| 新蔡县| 老河口市| 菏泽市| 黎川县| 宁国市| 泰来县| 泰顺县| 天峻县| 昭平县| 四平市| 陕西省|