MySQL 学习笔记 (一)

MySQL 学习笔记 (一)1.InnoDB and Online DDL ALTER TABLE tbl_name ADD PRIMARY KEY (column), ALGORITHM=INPLACE, LOCK=NONE;

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

代码100分

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:

代码100分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.

代码100分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_%"

 

–个人学习笔记系列,可能比较粗糙,观者见谅。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/10730.html

(0)
上一篇 2022-12-22 19:30
下一篇 2022-12-22 20:30

相关推荐

  • SQL优化这5个极简法则,直接让查询原地起飞![亲测有效]

    SQL优化这5个极简法则,直接让查询原地起飞![亲测有效]SQL 作为关系型数据库的标准语言,是 IT 从业人员必不可少的技能之一。SQL 本身并不难学,编写查询语句也很容易,但是想要编写出能够高效运行的查询语句却有一定的难度。
    查询优化是一个复杂的工程,涉

    2023-05-30
    129
  • Python安装和使用Tkinter图形界面库

    Python安装和使用Tkinter图形界面库a href=”https://beian.miit.gov.cn/”苏ICP备2023018380号-1/a Copyright www.python100.com .Some Rights Reserved.

    2024-02-04
    89
  • Python List使用技巧,让你更高效地处理数据结构

    Python List使用技巧,让你更高效地处理数据结构Python中的List是一种非常常用的数据结构,支持各种基本的操作。在处理数据时,我们通常需要以下几种基本操作。

    2023-12-23
    115
  • Redis必备面试题《难点篇》

    Redis必备面试题《难点篇》Date:2019-11-12 读前思考: redis每次必问的问题,在大脑里面先回想一下,能否答出一二? 题1:Redis雪崩了解么? 题2:了解Redis缓存穿透和击穿么? 题3:你知道Redis

    2022-12-19
    153
  • Babelfish for PostgreSQL[亲测有效]

    Babelfish for PostgreSQL[亲测有效]Babelfish for PostgreSQL开源已快一月,不过全网还没有实践者总结。今天我们就测试看看,Babelfish到底是如何部署与使用的!

    2023-06-15
    161
  • 用Prewitt算子进行图像边缘检测

    用Prewitt算子进行图像边缘检测Prewitt算子是一种常见的边缘检测算法,它基于对图像像素点的导数进行计算,从而找到图像中的边缘部分。其最常用的形式为3×3的模板。Prewitt算子对于图像中的水平边缘和垂直边缘都有很好的检测效果。

    2024-08-01
    34
  • Python 开发工具

    Python 开发工具Python是一种高级、通用、解释型编程语言。它被吉多·范罗苏姆于1989年开发,最初是为了编写一些简单的脚本程序而创建的。后来由于Python具有易读性和简洁性等优点,它被广泛应用于Web开发、数据科学、人工智能等众多领域。Python发展迅速,吸引了众多程序员的关注,也催生了一系列优秀的Python开发工具。

    2024-09-20
    15
  • Python数组插入元素:以指定位置为中心添加。

    Python数组插入元素:以指定位置为中心添加。计算机科学中,数组是一种数据结构,用于存储和处理多个相同类型的数据。Python提供了丰富的数组功能,包括插入元素操作。本文将讲解如何使用Python在数组中以指定位置为中心添加元素。

    2024-07-10
    37

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注