MySQL 學習筆記 (一)

来源:https://www.cnblogs.com/xuliuzai/archive/2019/11/27/11695801.html
-Advertisement-
Play Games

1.InnoDB and Online DDL ALTER TABLE tbl_name ADD PRIMARY KEY (column), ALGORITHM=INPLACE, LOCK=NONE; https://dev.mysql.com/doc/refman/8.0/en/innodb-on ...


1.InnoDB and Online DDL

ALTER TABLE tbl_name ADD PRIMARY KEY (column), ALGORITHM=INPLACE, LOCK=NONE;

https://dev.mysql.com/doc/refman/8.0/en/innodb-online-ddl.html

2.TRUNCATE TABLE後可用空間的使用

在innodb_file_per_table=on的條件下,可用空間釋放給了操作系統。而在innodb_file_per_table=OFF(system tablespace)或( general tablespaces)情況下,空間可以從新利用,沒有物理釋放。

https://dev.mysql.com/doc/refman/8.0/en/innodb-truncate-table-reclaim-space.html

3.複製狀態查看

* 從庫查看slave_master_info表:select * from mysql.slave_master_info; 
* 從庫查看slave_relay_log_info表:select * from mysql.slave_relay_log_info; 
* 從庫查看slave_worker_info表:select * from mysql.slave_worker_info; 
* 從庫查看replication_applier_status_by_worker表:select * from performance_schema.replication_applier_status_by_worker; 
* 從庫查看replication_connection_status表:select * from performance_schema.replication_connection_status; 

 4.GTID Sets

來源於同一個Master Server的的GTID,可以構成一個集合:

3E11FA47-71CA-11E1-9E33-C80AA9429562:1-5

The above example represents the first through fifth transactions originating on the MySQL server whose server_uuidis 3E11FA47-71CA-11E1-9E33-C80AA9429562. Multiple single GTIDs or ranges of GTIDs originating from the same server can also be included in a single expression, with the GTIDs or ranges separated by colons, as in the following example:

3E11FA47-71CA-11E1-9E33-C80AA9429562:1-3:11:47-49

A GTID set can include any combination of single GTIDs and ranges of GTIDs, and it can include GTIDs originating from different servers. This example shows the GTID set stored in the gtid_executed system variable (@@GLOBAL.gtid_executed) of a slave that has applied transactions from more than one master:

2174B383-5441-11E8-B90A-C80AA9429562:1-3, 24DA167-0C0C-11E8-8442-00059A3C7B00:1-19

 5.gtid_executed table 

GTIDs are stored in the mysql.gtid_executed table only when gtid_mode is ON or ON_PERMISSIVE. Note that the mysql.gtid_executed table is cleared if you issue RESET MASTER.

Compression of the mysql.gtid_executed table is performed by a dedicated foreground thread namedthread/sql/compress_gtid_table.

SELECT * FROM performance_schema.threads WHERE NAME LIKE '%gtid%'\G

6.關於GTID複製模式的關聯報錯

If any of the transactions that should be sent by the master have been purged from the master's binary log, or added to the set of GTIDs in the gtid_purged system variable by another method, the master sends the errorER_MASTER_HAS_PURGED_REQUIRED_GTIDS to the slave, and replication does not start.  The GTIDs of the missing purged transactions are identified and listed in the master's error log in the warning message ER_FOUND_MISSING_GTIDS.

Attempting to reconnect without the MASTER_AUTO_POSITION option enabled only results in the loss of the purged transactions on the slave. The correct approach to recover from this situation is for the slave to replicate the missing transactions listed in the ER_FOUND_MISSING_GTIDS message from another source, or for the slave to be replaced by a new slave created from a more recent backup. Consider revising the binary log expiration period (binlog_expire_logs_seconds) on the master to ensure that the situation does not occur again.

If during the exchange of transactions it is found that the slave has received or committed transactions with the master's UUID in the GTID, but the master itself does not have a record of them, the master sends the errorER_SLAVE_HAS_MORE_GTIDS_THAN_MASTER to the slave and replication does not start. This situation can occur if a master that does not have sync_binlog=1 set experiences a power failure or operating system crash, and loses committed transactions that have not yet been synchronized to the binary log file, but have been received by the slave. 

https://dev.mysql.com/doc/refman/8.0/en/replication-gtids-auto-positioning.html

7.複製的許可權設置

Most of the steps that follow require the use of the MySQL root account or another MySQL user account that has theSUPER privilege. mysqladmin shutdown requires either the SUPER privilege or the SHUTDOWN privilege.

8.將MySQL 設置為read_only 

Make the servers read-only by setting the read_only system variable to ON on each server by issuing the following:

mysql> SET @@GLOBAL.read_only = ON;

這個命令的重要作用是:

Wait for all ongoing transactions to commit or roll back. Then, allow the slave to catch up with the master. It is extremely important that you make sure the slave has processed all updates before continuing.

9.shut down the MySQL

shell> mysqladmin -uusername -p shutdown

Then supply this user's password at the prompt.

https://dev.mysql.com/doc/refman/8.0/en/replication-gtids-howto.html

https://www.cnblogs.com/dadonggg/p/8625500.html

10.如何跳過一個GTID

基於GTID的複製,跳過一個事務,需要利用一個空事務來完成。

stop slave;

SET GTID_NEXT='aaa-bbb-ccc-ddd:N';

BEGIN;
COMMIT;

SET GTID_NEXT='AUTOMATIC';

start slave;

https://dev.mysql.com/doc/refman/8.0/en/replication-gtids-failover.html

11.多源複製

In a multi-source replication topology, a slave creates a replication channel for each master that it should receive transactions from.

The error codes and messages that are issued when multi-source replication is enabled specify the channel that generated the error.

https://dev.mysql.com/doc/refman/8.0/en/replication-multi-source.html

 12.顯示創建表的scripts

show create table student;

13 shell 操作mysql

關於salve節點的重新執行SQL的線程

mysql -e 'STOP SLAVE SQL_THREAD;'

14.mysqldump

Run mysqldump to dump your databases. You may either dump all databases or select databases to be dumped. For example, to dump all databases:

mysqldump --all-databases > fulldb.dump

備份資料庫結構,不備份數據

格式:mysqldump -h主機名 -P埠 -u用戶名 -p密碼 --no-data 資料庫名1 資料庫名2 資料庫名3 > 文件名.sql

mysqldump --no-data –databases db1 db2 cmdb > /data/backup/structure.sql

https://dev.mysql.com/doc/refman/8.0/en/replication-solutions-backups-mysqldump.html

https://baijiahao.baidu.com/s?id=1612955427840289665&wfr=spider&for=pc

15.基於既有表創建一個新表

  • create table as 只是複製原數據,其實就是把查詢的結果建一個表
  • create table like 產生與源表相同的表結構,包括索引和主鍵,數據需要用insert into 語句複製進去。例如:
create table newtest like test;
insert into newtest select * from test;

 16.MHA FailOver

MHA 線上切換過程
https://blog.csdn.net/leshami/article/details/45189825

MHA 手動故障轉移

 https://blog.csdn.net/leshami/article/details/45219821

17.GTID模式下配置主從

change master to master_host='172.XXX.XXX.XXX',master_port=????,master_user='XXXX',master_password='XXXXXX',master_auto_position=1;
start slave;

 18.手動啟動MHA Manager

nohup /usr/local/bin/masterha_manager --conf=/etc/mha/app1.cnf  --remove_dead_master_conf --ignore_last_failover < /dev/null > /data/log/mha/manager.log >&1 &

19.查看某資料庫下所有表的具體信息(information_schema.TABLES

 SELECT * FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'XXXXdb';

例如查看資料庫中以winxin開頭的各表的數據量

 SELECT table_name,table_rows FROM information_schema.tables WHERE TABLE_name like 'winxin%' ORDER BY  table_rows DESC;

20.生成批量修改表的SQL語句

例如:生成清空分庫分表中的ABC開頭某類表

SELECT CONCAT( 'truncate table ', table_name, ';' ) 
FROM information_schema.tables
WHERE table_name LIKE 'ABC_%' and  table_name  not LIKE 'terminal_user_%' ;

如果還要加上庫名,例如刪除某類表

SELECT CONCAT('drop table QQ_weixin_co.', table_name, ';') 
FROM information_schema.tables 
WHERE table_schema = 'QQ_weixin_co' AND table_name LIKE 'ABC_%'

 

--個人學習筆記系列,可能比較粗糙,觀者見諒。


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 乙太網驅動的流程淺析(一) Ifconfig主要流程 Author:張昺華 Email:[email protected] Time:2019年3月23日星期六 此文也在我的個人公眾號以及《Linux內核之旅》上有發表: "乙太網驅動流程淺析(一) ifconfig主要流程" 很喜歡一群人在研究技術, ...
  • 7. vi--終端中的編輯器¶ 目標¶ vi簡介 打開和新建文件 三種工作模式 常用命令 分屏命令 常用命令速查圖 7.1 vi簡介¶ 7.1.1 學習vi目的¶ 在工作中,要對伺服器上的文件進行簡單的修改,可以使用ssh遠程登錄到伺服器上,並且使用vi進行快遞的編輯即可 常見需要修改的文件包括: ...
  • 6 軟體安裝¶ 6.1 通過apt 安裝/卸載軟體¶ apt是Advanced Packaging Tool,是Linux下的一款安裝包管理工具 可以在終端中方便的安裝/卸載/更新軟體包 # 1.安裝軟體 $ sudo apt install 軟體包 # 2.卸載軟體 $ sudo apt remo ...
  • 其他命令¶ 目標¶ 查找文件 find 軟鏈接 in 打包和壓縮 tar 軟體安裝 apt-get 01.查找文件¶ find命令功能非常強大,通常在特定的目錄下搜索符合條件的文件 序號命令作用 01 find [路徑] -name'*.py' 查找指定路徑下拓展名是.py的文件,包括子目錄 如果省 ...
  • 說到 pipe 大家可能都不陌生,經典的pipe調用配合fork進行父子進程通訊,簡直就是Unix程式的標配。 然而Solaris上的pipe卻和Solaris一樣是個奇葩(雖然Solaris前途黯淡,但是不妨礙我們從它裡面挖掘一些有價值的東西), 有著和一般pipe諸多的不同之處,本文就來說說So ...
  • 介紹: DHCP服務作用(動態主機配置協議) 為大量客戶機自動分配地址、提供幾種管理 減輕管理和維護成本、提高網路配置效率 可分配的地址信息主要包括: 網卡的IP地址、子網掩碼 對應的網路地址、廣播地址 預設網關地址 DNS伺服器地址 引導文件、TFTP伺服器地址 原理: 1、客戶端尋找伺服器(發送 ...
  • # ### part1 單表查詢# sql 查詢語句的完整語法 ''' select .. from .. where .. group by .. having .. order by .. limit .. ''' # 一.where 條件的使用 """功能:對錶中的數據進行篩選過濾""" "" ...
  • 點擊web application ...
一周排行
    -Advertisement-
    Play Games
  • 前言 在我們開發過程中基本上不可或缺的用到一些敏感機密數據,比如SQL伺服器的連接串或者是OAuth2的Secret等,這些敏感數據在代碼中是不太安全的,我們不應該在源代碼中存儲密碼和其他的敏感數據,一種推薦的方式是通過Asp.Net Core的機密管理器。 機密管理器 在 ASP.NET Core ...
  • 新改進提供的Taurus Rpc 功能,可以簡化微服務間的調用,同時可以不用再手動輸出模塊名稱,或調用路徑,包括負載均衡,這一切,由框架實現並提供了。新的Taurus Rpc 功能,將使得服務間的調用,更加輕鬆、簡約、高效。 ...
  • 順序棧的介面程式 目錄順序棧的介面程式頭文件創建順序棧入棧出棧利用棧將10進位轉16進位數驗證 頭文件 #include <stdio.h> #include <stdbool.h> #include <stdlib.h> 創建順序棧 // 指的是順序棧中的元素的數據類型,用戶可以根據需要進行修改 ...
  • 前言 整理這個官方翻譯的系列,原因是網上大部分的 tomcat 版本比較舊,此版本為 v11 最新的版本。 開源項目 從零手寫實現 tomcat minicat 別稱【嗅虎】心有猛虎,輕嗅薔薇。 系列文章 web server apache tomcat11-01-官方文檔入門介紹 web serv ...
  • C總結與剖析:關鍵字篇 -- <<C語言深度解剖>> 目錄C總結與剖析:關鍵字篇 -- <<C語言深度解剖>>程式的本質:二進位文件變數1.變數:記憶體上的某個位置開闢的空間2.變數的初始化3.為什麼要有變數4.局部變數與全局變數5.變數的大小由類型決定6.任何一個變數,記憶體賦值都是從低地址開始往高地 ...
  • 如果讓你來做一個有狀態流式應用的故障恢復,你會如何來做呢? 單機和多機會遇到什麼不同的問題? Flink Checkpoint 是做什麼用的?原理是什麼? ...
  • C++ 多級繼承 多級繼承是一種面向對象編程(OOP)特性,允許一個類從多個基類繼承屬性和方法。它使代碼更易於組織和維護,並促進代碼重用。 多級繼承的語法 在 C++ 中,使用 : 符號來指定繼承關係。多級繼承的語法如下: class DerivedClass : public BaseClass1 ...
  • 前言 什麼是SpringCloud? Spring Cloud 是一系列框架的有序集合,它利用 Spring Boot 的開發便利性簡化了分散式系統的開發,比如服務註冊、服務發現、網關、路由、鏈路追蹤等。Spring Cloud 並不是重覆造輪子,而是將市面上開發得比較好的模塊集成進去,進行封裝,從 ...
  • class_template 類模板和函數模板的定義和使用類似,我們已經進行了介紹。有時,有兩個或多個類,其功能是相同的,僅僅是數據類型不同。類模板用於實現類所需數據的類型參數化 template<class NameType, class AgeType> class Person { publi ...
  • 目錄system v IPC簡介共用記憶體需要用到的函數介面shmget函數--獲取對象IDshmat函數--獲得映射空間shmctl函數--釋放資源共用記憶體實現思路註意 system v IPC簡介 消息隊列、共用記憶體和信號量統稱為system v IPC(進程間通信機制),V是羅馬數字5,是UNI ...