欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MySQL 学习笔记 (一)

程序员文章站 2022-05-07 09:28:16
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_%'

 

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