week06

简述DDL,DML,DCL,DQL,并且说明mysql各个关键字查询时候的先后顺序

DDL data defination language #数据定义语言 create drop alter
DML data manipulation language #数据操纵语言 insert update delete
DCL data control language #数据控制语言 grant revoke
DQL data query language #数据查询语言
TCL transaction controllanuage #事务控制语言 commit rollback
sql 关键字 select from on join where group by having select order by limit
执行先后顺序

顺序号关键字
1from
2on
3join
4where
5group by
6having
7select
8order by
9limit

自行设计10个sql查询语句,需要用到关键字[GROUP BY/HAVING/ORDER BY/LIMIT],至少同时用到两个。

1
2
3
4
5
6
7
8
9
10
11
select host,count(*) from user group by host;
select host,count(*) as c from user group by host having c >1;
select host,count(*) as c from user group by host order by c;
select host,count(*) as c from user group by host order by c limit 1;
select host,count(*) as c from user group by host having c >1 order by c limit 1;

select db ,sql_text from slow_log where query_time>500 order by ;
select db ,count(query_time) c from slow_log where query_time>500 group by db having c >600 ;
select db ,count(query_time) c from slow_log where query_time>500 group by db having c >600 order by c;
select db ,count(query_time) c from slow_log where query_time>500 group by db having c >600 order by c limit 1;
select db ,count(lock_time) c from slow_log where lock_time>500 group by db having c >600 order by c;

xtrabackup备份和还原数据库练习

移步这里:https://www.baibao.online/2023/07/19/xtrabackup/

实现mysql主从复制,主主复制和半同步复制

移步这里:https://www.baibao.online/2023/07/19/mysql主从同步/

用mycat实现mysql的读写分离

移步这里:https://www.baibao.online/2023/07/20/mycat/

实现openvpn部署

移步这里:https://www.baibao.online/2023/07/20/openvpn/

mysql如何实现崩溃后恢复

数据库崩溃后,文件损坏不能正常启动。
一、innodb 修复方案:
修改innodb_force_recovery参数

  1. (SRV_FORCE_IGNORE_CORRUPT):忽略检查到的corrupt页。
  2. (SRV_FORCE_NO_BACKGROUND):阻止主线程的运行,如主线程需要执行full purge操作,会导致crash。
  3. (SRV_FORCE_NO_TRX_UNDO):不执行事务回滚操作。
  4. (SRV_FORCE_NO_IBUF_MERGE):不执行插入缓冲的合并操作。
  5. (SRV_FORCE_NO_UNDO_LOG_SCAN):不查看重做日志,InnoDB存储引擎会将未提交的事务视为已提交。
  6. (SRV_FORCE_NO_LOG_REDO):不执行前滚的操作。

当设置参数值大于0后,可以对表进行select,create,drop操作,但insert,update或者delete这类操作是不允许的
当innodb_purge_threads 和 innodb_force_recovery一起设置会出现一种loop现象
innodb_purge_threads 尝试清除旧版本的数据,innodb_force_recovery依赖旧版本的数据
恢复方案:
1.1 设置innodb_purge_threads 为1
1.2 设置innodb_force_recovery 为1
1.3 启动数据库
1.4 逐个每个表执行 check table 表名,检查表损坏状态
1.5 mysql8.0 不支持 repair table
修改配置文件 secure-file-priv=/home/mysql/csv
mysql -uroot -p123456@Root -P3306 -D db1 -e “SELECT * FROM users INTO OUTFILE ‘/home/mysql/csv/users.csv’;”

重新创建新表 users
mysql -uroot -p123456@Root -P3306 -D db1 -e “load data infile ‘/home/mysql/csv/users.csv’ into table users;”

myisam和innodb各自在什么场景使用

MyISAM:

适用场景:MyISAM 被广泛用于读取密集型的场景,尤其是静态或只读数据的存储。它适用于需要高性能读取操作和低存储空间占用的应用。例如论坛的帖子、博客文章、新闻文章等场景。
InnoDB:

适用场景:InnoDB 适用于对事务支持和数据完整性有较高要求的应用,尤其是需要支持大量并发写入操作和高度可靠性的应用。它常用于电子商务网站、银行系统、日志记录等场景。