您的位置:首页 > 数据库 > MySQL

mysql数据库应用管理+乱码+字符集

2016-05-16 15:18 435 查看
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<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;//执行过程 判断有么有走索引
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