mysql数据库应用管理+乱码+字符集的示例分析

40次阅读
没有评论

共计 4463 个字符,预计需要花费 12 分钟才能阅读完成。

这篇文章主要为大家展示了“mysql 数据库应用管理 + 乱码 + 字符集的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让丸趣 TV 小编带领大家一起研究并学习一下“mysql 数据库应用管理 + 乱码 + 字符集的示例分析”这篇文章吧。

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  // 备份数据库 备份用检查一遍

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

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 // 执行过程   判断有么有走索引

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 // 恢复数据,增量恢复。

增量恢复  

#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 只记录主数据库更改  

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;

乱码

统一字符集   /etc/my.cnf       改动 3 个           重启服务

set  names  utf8;

cat  /etc/sysconfig/i18n              // 系统环境

LANG= zh_CN.UTF-8

vim  /etc/my.cnf                               // 服务器端 和客户端

[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

  数据库字符集         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                             |   数据库字符集

| character_set_filesystem      | binary                             |

| character_set_results            |   utf8                               |         返回结果

| character_set_server            | latin1                             |   服务器字符集

| character_set_system           | utf8                               |

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

+————————–+————————————+

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

set  names  latin1

临时生效

mysql 更改已有数据表的字符集,保留原有数据内容

环境:在应用开始阶段没有正确的设置字符集,在运行一段时间以后才发现存在不能满足需求需要调整,又不想丢弃这段时间的数据,那么就需要进 行字符集的修改。字符集的修改不能直接通过 alter database character set *** 或者 alter table tablename character set *** 命令进行,这两个命令都没有更新已有记录的字符集,而只是对新创建的表或者记录生效。

那么已有记录的字符集调整,需要怎么操作呢?

以下模拟的是将 latin1 字符集的数据库修改成 GBK 字符集的数据库的过程:

(1) 导出表结构

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

其中 –default-character-set=gbk 表示设置以什么字符集连接,- d 表示只导出表结构,不导出数据

(2) 手工修改 createdb.sql 中表结构定义中的字符集为新的字符集

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

(3) 确保记录不再更新,导出所有记录

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

–quick: 该选项用于转储大的表。它强制 Mysqldump 从服务器一次一行的检索表中的行,而不是检索所有的行,兵输出前将它缓存在内存中

–extended-insert: 使用包括几个 values 列表的多行 Insert 语法,这样使转储文件更小,重载文件更快

–no-create-info: 不屑重新创建每个转储表的 create table 语句

–default-character-set=latin1: 按照原有的字符集导出所有数据,这样导出的文件中,所有中文都是可见的,不会保存成乱码

(4) 打开 data.sql, 将 SET NAMES latin1 修改成 SET NAMES gbk

(5) 使用新的字符集创建新的数据库

create database newdatabasename default charset gbk;

(6) 创建表,执行 createdb.sql

mysql -uroot -p newdatabasename createdb.sql

(7) 导入数据,执行 data.sql

mysql -uroot -p newdatabasename data.sql

(8) 查看之前,确认本地环境,是不是都改成了 gbk 模式。可以用   临时更改   测试用

set names   gbk;

以上是“mysql 数据库应用管理 + 乱码 + 字符集的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注丸趣 TV 行业资讯频道!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-19发表,共计4463字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)