广告
返回顶部
首页 > 资讯 > 数据库 >MySQL 学习笔记 (一)
  • 114
分享到

MySQL 学习笔记 (一)

MySQL学习笔记(一) 2016-12-04 09:12:52 114人浏览 绘本
摘要

1.InnoDB and Online DDL ALTER TABLE tbl_name ADD PRIMARY KEY (column), ALGoRITHM=INPLACE, LOCK=NONE; https://dev.Mysql.c

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_%"

 

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

您可能感兴趣的文档:

--结束END--

本文标题: MySQL 学习笔记 (一)

本文链接: https://www.lsjlt.com/news/2711.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • MySQL 学习笔记 (一)
    1.InnoDB and Online DDL ALTER TABLE tbl_name ADD PRIMARY KEY (column), ALGORITHM=INPLACE, LOCK=NONE; https://dev.mysql.c...
    99+
    2016-12-04
    MySQL 学习笔记 (一)
  • Mysql命令学习笔记(一)
    一、安装/卸载Mysql数据库    #安装Mysql数据库    D:\mysql\bin>mysqld --inst...
    99+
    2022-10-18
  • MySQL学习笔记
    作者: Grey 原文地址:MySQL学习笔记 说明 注:本文中的SQL语句如果用到了特定方言,都是基于MySQL数据库。 关于DDL DDL 的英文全称是 Data Definition Language,中文是数据定义语言。它定义了...
    99+
    2015-01-17
    MySQL学习笔记
  • MySQL 学习笔记
    😀😀😀创作不易,各位看官点赞收藏. 文章目录 MySQL 学习笔记1、`DQL` 查询语句1.1、基本查询1.2、函数查询1.2.1、单行函数1...
    99+
    2023-10-01
    mysql 学习 笔记
  • NumPy 学习笔记(一)
    NumPy:   1、NumPy 是一个功能强大的第三方库(需要自己安装),主要用于对多维数组执行计算;      它提供了大量的库函数和操作,可以帮助程序员更轻松地进行数值计算   2、可以和另外两个第三方库 SciPy 和 Matpl...
    99+
    2023-01-31
    学习笔记 NumPy
  • NumPy学习笔记(一)
    # NumPy### 安装- 通过安装Anaconda安装NumPy,一个开源的Python发行版本,其包含了conda、Python等180多个科学包及其依赖项,包含了大量的科学计算相关的包,其中就包括NumPy- 通过pip安装, ...
    99+
    2023-01-31
    学习笔记 NumPy
  • MySQL 学习笔记(五)
    mysqldump 与 --set-gtid-purged 设置 (1)  mysqldump The mysqldump client utility performs logical backups, producing a set ...
    99+
    2022-01-27
    MySQL 学习笔记(五)
  • MySQL学习笔记-day01
    1、数据库概述及数据准备 1.1、SQL概述 SQL,全称Structured Query Language,SQL用来和数据库打交道,完成和数据库的通信,SQL是一套标准。(90%以上的SQL都是通用的)。 SQL:结构化语言,是一门标...
    99+
    2017-10-15
    MySQL学习笔记-day01
  • MySQL学习笔记-day03
    1、约束 1.1、唯一性约束(unique) 唯一性约束修饰的字段具有唯一性,不能重复。但可以为NULL。 案例:给某一列添加unique drop table if exists t_user; create table t_...
    99+
    2020-11-18
    MySQL学习笔记-day03
  • MySQL学习笔记 初涉MySQL
    1.在Linux下安装MySQL# yum -y install mysql mysql-server mysql-devel &nbs...
    99+
    2022-10-18
  • MySQL学习笔记(17):MySQL Utilities
    本文更新于2020-04-05,使用MySQL 5.7,操作系统为Deepin 15.4。 目录安装分类mysqldbcompare——数据库比较工具mysqldbcopy——数据库复制工具mysqldiff——数据库对象定义比较...
    99+
    2015-05-06
    MySQL学习笔记(17):MySQL Utilities
  • 超全MySQL学习笔记
    目录MyISAM和InnoDB性能下降SQL慢的原因:Mysql执行顺序SQLJoin索引索引的优劣1.优势2.劣势索引分类创建删除查看mysql索引结构那些情况建索引哪些情况不要建...
    99+
    2022-11-12
  • MySQL学习笔记(3):SQL
    本文章更新于2020-06-14,使用MySQL 5.7,操作系统为Deepin 15.9。 目录DDL语句创建数据库删除数据库修改数据库创建表删除表修改表创建索引删除索引创建视图修改视图删除视图存储过程和函数创建事件修改事件删除...
    99+
    2022-04-25
    MySQL学习笔记(3):SQL
  • 学习笔记:MYSQL查询
    前言:之前花费两天晚上看了一遍Mysql必知必会,没想到后面效果太差。不如跟着网课视频敲一遍和完成练习题目(练习题没写注释就不记录了),再记下笔记。 一、基本的查询select语句 语法: select 查询列表 from 表名...
    99+
    2015-12-27
    学习笔记:MYSQL查询
  • mysql学习笔记二 DQL
    -- 查询编号为1004的学生的姓名和生日 select name,birthday from student where id=1004; -- 查询年龄大于18的学生信息 select...
    99+
    2018-07-25
    mysql学习笔记二 DQL
  • 赞!7000 字学习笔记,一天搞定 MySQL
    MySQL数据库简介 MySQL近两年一直稳居第二,随时有可能超过Oracle计晋升为第一名,因为MySQL的性能一直在被优化,同时安全机制也是逐渐成熟,更重要的是开源免费的。 MySQL是一种关系数据库管理系统,关系数据库将数据...
    99+
    2019-02-27
    赞!7000 字学习笔记,一天搞定 MySQL
  • mysql学习笔记一 基础知识及DDL
    规范 一条语句要以分号(;)结束 sql可以单行和多行书写,一般通过缩进和换行书写提高代码可读性 sql的关键词一般都要全大写 分类 DDL(Data Defination Language)  数据定义语言  用于操作数...
    99+
    2019-12-24
    mysql学习笔记一 基础知识及DDL
  • Spring学习笔记事务(一)
    鲁春利的工作笔记,好记性不如烂笔头事务是一系列操作组成的工作单元,是不可分割的,即要么所有操作都做,要么所有操作都不做,这就是事务。事务必需满足ACID(原子性、一致性、隔离性和持久性)特性,缺一不可:&n...
    99+
    2022-10-18
  • 第一周Python学习笔记
     Python 基本语法: ①  Python程序的格式:1.用代码高亮来标识函数丶语句等等 本身的代码高亮并没有实际的意义,只是用来辅助编程人员和阅读人员 更好的识别    2.程序以缩进来标识语句,缩进用来标识代码间的层次关系,缩进的...
    99+
    2023-01-30
    学习笔记 第一周 Python
  • 【K210】K210学习笔记一——sensor
    【K210】K210学习笔记一——sensor 前言sensor的配置模块导入模块配置模块各配置解释 完整源码 前言 本人大四学生,电赛生涯已经走到尽头,一路上踩过不少坑,但运气也不错...
    99+
    2023-08-31
    学习 python 人工智能 单片机 图像处理
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作