1. 程式人生 > >[原創]MySQL數據庫查詢和LVM備份還原學習筆記記錄

[原創]MySQL數據庫查詢和LVM備份還原學習筆記記錄

des 其他 sql數據庫 fse 三種 nlog mysql創建表 vm備份 show

一、查詢語句類型:
  1)簡單查詢
  2)多表查詢
  3)子查詢

  4)聯合查詢

1)簡單查詢:
  SELECT * FROM tb_name;
  SELECT field1,field2 FROM tb_name;投影
  SELECT [DISTINCT] * FROM tb_name WHERE qualification;選擇

  FROM子句:要查詢的關系
    表、多個表、其他select語句
  WHERE子句:布爾關系表達式
    AND,OR,NOT,BETWEEN...AND...,LIKE ‘‘[%:任意長度任意字符;_:任意單個字符],RLIKE,IN,IS NULL,IS NOT NULL
    ORDER BY field_name {ASC|DESC}
    AS 字段別名
    LIMIT [offset,]Count offset偏移量,Count取幾個
    聚合:
      SUM(),MIN(),MAX(),AVG(),COUNT()
    GROUP BY分組:
      HAVING:過濾[qualification]
  SELECT AVG(Age) FROM students GROUP BY Gender;
  ELECT COUNT(CID1) AS Persons,CID1 FROM students GROUP BY CID1;
  SELECT COUNT(CID1) AS Persons,CID1 FROM students GROUP BY CID1 HAVING Persons>=2;

  SQL語句執行的順序:
    start->from->where->group by->having->order by->select fieldN->LIMIT->end

2)多表查詢:
  連接:
  交叉連接:笛卡爾積
  select * from students,courses;
  自然連接:內連接,外鏈接:左外連接:... LEFT JOIN ... ON ...,右外連接:... RIGHT JOIN ... ON ...,自連接
  select * from students,courses where students.cid1 = courses.cid;
  select Name,Cname from students,courses where students.cid1 = courses.cid;
  select s.Name,c.Cname from students AS s,courses AS c where s.cid1 = c.cid;
  SELECT s.Name,c.Cname FROM students AS s LEFT JOIN courses AS c ON s.CID1=c.CID1;
  SELECT s.Name,c.Cname FROM students AS s RIGHT JOIN courses AS c ON s.CID1=c.CID1;

3)子查詢:
  比較操作中使用子查詢:子查詢只能返回單個值
  IN():使用子查詢
  在FROM中使用子查詢
  select name from students where Age > (select AVG(Age) from students);
  select name from students where Age IN (select Age from tutors);
  select Name,Age from (SELECT Name,Age FROM students) AS t where t.Age >= 20;

4)聯合查詢:
  UNION:
  (select Nmae,Age FROM students) UNION (select Tname,Age from tutors);

二、備份和還原
  備份:
    select * into OUTFILE ‘/path/beifen.txt‘ from tb_name [where clause];
  恢復:
    LOAD DATA INFILE ‘/path/beifen.txt‘ INTO TABLE tb_name;

LVM快照備份和還原:
  前提:
    1、數據文件要在邏輯卷上;
    2、此邏輯卷所在卷組必須有足夠空間使用快照卷;
    3、數據文件和事務日誌要在同一個邏輯卷上;

  步驟:
  1、打開會話,施加讀鎖,鎖定所有表:
    mysql> FLUSH TABLES WITH READ LOCK;
    mysql> FLUSH LOGS;
  2、通過另一個終端,保存二進制日誌文件及相關位置信息:
    $mysql -uroot -p -e "SHOW MASTER STATUS\G" > /path/master.info
  3、創建快照卷:
    #lvcreate -L [size] -s -p r -n LV_NAME /path/source_lv
  4、釋放鎖:
    mysql> UNLOCK TABLES;
  5、掛載快照卷,備份:
    #mount LV_NAME /mnt -o ro
    #cp -a ./* /backup/
    #rm -f /backup/mysql-bin.*
  6、刪除快照卷:
    #lvremove --force LV_NAME
  7、增量備份二進制日誌[從master.info文件的位置開始備份,如果還有其他滾動的二進制文件單獨備份即可]:
    #mysqlbinlog --start-post=107 mysql-bin.000003 mysql-bin.000004> /backup/incremental.sql
  8、還原數據:
    #cp -a /bakcup/* /mysql/data/
    #service mysqld start
    mysql> set sql_log_bin=0;
    mysql> source /backup/incremental.sql;
    mysql> set sql_log_bin=1;
    mysql> SHOW MASTER STATUS\G;

擴展:

MySQL創建表的三種方式:

  1)直接手動創建:

    mysql> create table tb_name(feild1-N);

  2)基於其他表創建新表,這種方式創建的表不會把原表的主鍵,約束,觸發器等復制過來,需要手動在追加。

    mysql> create table test_new as select * from test where 1=2;    //where 1=2只有表結構沒有數據,where 1=1包括表結構和表數據一起復制了

  3)基於原表創建的新表,這種方式使用like關鍵字會將原表的結構都復制過來,包括主鍵,約束,觸發器等。

    mysql> create table test like tb_name;

[原創]MySQL數據庫查詢和LVM備份還原學習筆記記錄