溫馨提示×

溫馨提示×

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

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

mysql數(shù)據(jù)庫應(yīng)用管理+亂碼+字符集的示例分析

發(fā)布時間:2021-11-25 10:22:52 來源:億速云 閱讀:124 作者:小新 欄目:數(shù)據(jù)庫

這篇文章主要為大家展示了“mysql數(shù)據(jù)庫應(yīng)用管理+亂碼+字符集的示例分析”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“mysql數(shù)據(jù)庫應(yīng)用管理+亂碼+字符集的示例分析”這篇文章吧。

insert

測試表mysql> show create  table test\G

create  table test(

id int(4)  not  null   AUTO_INCREMENT,

name char(20) not null,

primary  key(id)

);

mysql> insert    into   test(id,name)  value(1,'hequan');

mysql> select * from test;

mysql> insert into test(name)  value('hequan');  //ID是自增的,可以插name

mysql>  insert into test  value(3,'hequna'),(4,'hequan');  // 不給列,直接按順序插入

 mysqldump -uroot -p123456 -B oldboy >/tmp/oldboy_bak.sql  //備份數(shù)據(jù)庫 備份用檢查一遍

grep -E -v "#|\/|^$|--"  /tmp/oldboy_bak.sql 


select           from            where 

mysql> select id,name from test  where name='hequan'  and/or  id=4;

mysql> select id,name from test   limit 0,2; //從第0行開始,查2行

mysql> select id,name from test  where id>2 and id<4;

mysql> select id,name from test   order by id     asc/desc;

多表查詢

mysql> select student.Sno,student.Sname,course.Cname,SC.Grade  from student,course,SC   where  student.Sno=SC.Sno and  course.Cno=SC.Cno  order by Sno ;

mysql> explain  select * from test where name='hequan'\G;//執(zhí)行過程  判斷有么有走索引

possible_keys: NULL

rows: 2

mysql> create index index_name on test(name);

possible_keys: index_name

rows: 1


update

mysql> update   test set  name='xx'   where   id=4   ;

 mysql -uroot -p123456 oldboy < /tmp/oldboy_bak.sql //恢復(fù)數(shù)據(jù),增量恢復(fù)。


增量恢復(fù)  

#log-bin=mysql-bin  打開

/application/mysql/data/mysql-bin-hequan.000001

mysqlbinlog mysql-bin-hequan.000001

 mysqladmin -uroot -p123456  flush-log 切割日志

mysql-bin-hequan.000002

 mysqlbinlog -d oldboy mysql-bin-hequan.000001  >bin.sql

把錯誤的語句刪除掉

mysql  -uroot -p123456 oldboy  <bin.sql

binlog只記錄主數(shù)據(jù)庫更改 


delete

mysql> delete from  test  where id=3;   > <

mysql> truncate table test;  //清空表


更改表的字段

mysql> alter table test add sex  char(4)  after name;  //在name后面添加sex  // first


mysql> rename   table test to test1;

mysql> alter table test1 rename to test;


mysql> drop table test;


亂碼

統(tǒng)一字符集   /etc/my.cnf       改動3個           重啟服務(wù)

set  names  utf8;

cat  /etc/sysconfig/i18n              //系統(tǒng)環(huán)境

LANG="zh_CN.UTF-8"

vim  /etc/my.cnf                               //服務(wù)器端 和客戶端

[client]

default-character-set=utf8

[mysqld]

character-set-server=utf8    //5.5

default-character-set=utf8   //5.1

[mysql]

default-character-set=utf8

show  variables like 'character_set%';

SecureCRT客戶端要更改為utf8


 數(shù)據(jù)庫字符集         GBK   UTF-8   latin1

mysql> show character set;

+----------+-----------------------------+---------------------+--------+

| Charset  | Description                           | Default collation   | Maxlen |

+----------+-----------------------------+---------------------+--------+

| utf8     | UTF-8 Unicode                      | utf8_general_ci     |      3 |

| gbk      | GBK Simplified Chinese       | gbk_chinese_ci      |      2 |

| latin1   | cp1252 West European        | latin1_swedish_ci   |      1 |

| utf8mb4  | UTF-8 Unicode               | utf8mb4_general_ci  |      4 |


mysql> show  variables like 'character_set%';

+--------------------------+------------------------------------+

| Variable_name            | Value                              |

+--------------------------+------------------------------------+

| character_set_client             | utf8                               |    客戶端

| character_set_connection    | utf8                               |   鏈接

| character_set_database       | latin1                             |  數(shù)據(jù)庫字符集

| character_set_filesystem      | binary                             |

| character_set_results            |   utf8                               |        返回結(jié)果

| character_set_server            | latin1                             |  服務(wù)器字符集

| character_set_system           | utf8                               |

| character_sets_dir                   | /application/mysql/share/charsets/ |

+--------------------------+------------------------------------+

 mysql -uroot -p123456  --default-character-set=latin1

set  names  latin1

臨時生效


mysql更改已有數(shù)據(jù)表的字符集,保留原有數(shù)據(jù)內(nèi)容

環(huán)境:在應(yīng)用開始階段沒有正確的設(shè)置字符集,在運行一段時間以后才發(fā)現(xiàn)存在不能滿足需求需要調(diào)整,又不想丟棄這段時間的數(shù)據(jù),那么就需要進 行字符集的修改。字符集的修改不能直接通過"alter database character set *** " 或者 "alter table tablename character set *** "命令進行,這兩個命令都沒有更新已有記錄的字符集,而只是對新創(chuàng)建的表或者記錄生效。

那么已有記錄的字符集調(diào)整,需要怎么操作呢?

以下模擬的是將latin1字符集的數(shù)據(jù)庫修改成GBK字符集的數(shù)據(jù)庫的過程:

(1) 導(dǎo)出表結(jié)構(gòu)

mysqldump -uroot -p --default-character-set=gbk    -d   name   > createdb.sql

其中--default-character-set=gbk表示設(shè)置以什么字符集連接,-d表示只導(dǎo)出表結(jié)構(gòu),不導(dǎo)出數(shù)據(jù)

(2) 手工修改createdb.sql中表結(jié)構(gòu)定義中的字符集為新的字符集

sed -i  's#latin1#gbk#g'  createdb.sql

(3) 確保記錄不再更新,導(dǎo)出所有記錄

mysqldump -uroot -p --quick --no-create-info --extended-insert --default-character-set=latin1 name > data.sql

--quick:該選項用于轉(zhuǎn)儲大的表。它強制Mysqldump從服務(wù)器一次一行的檢索表中的行,而不是檢索所有的行,兵輸出前將它緩存在內(nèi)存中

--extended-insert:使用包括幾個values列表的多行Insert語法,這樣使轉(zhuǎn)儲文件更小,重載文件更快

--no-create-info:不屑重新創(chuàng)建每個轉(zhuǎn)儲表的create table語句

--default-character-set=latin1:按照原有的字符集導(dǎo)出所有數(shù)據(jù),這樣導(dǎo)出的文件中,所有中文都是可見的,不會保存成亂碼

(4) 打開data.sql,將SET NAMES latin1 修改成SET NAMES gbk

(5) 使用新的字符集創(chuàng)建新的數(shù)據(jù)庫

create database newdatabasename default charset gbk;

(6) 創(chuàng)建表,執(zhí)行createdb.sql

mysql -uroot -p newdatabasename < createdb.sql

(7) 導(dǎo)入數(shù)據(jù),執(zhí)行data.sql

mysql -uroot -p newdatabasename < data.sql

(8)查看之前,確認(rèn)本地環(huán)境,是不是都改成了gbk模式。可以用  臨時更改  測試用

set names   gbk;

以上是“mysql數(shù)據(jù)庫應(yīng)用管理+亂碼+字符集的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI