server数据库压缩空间澳门新葡亰平台官网,MySQ

索引

索引(存储引擎 快速找到记录的一种数据结构,索引的基本功能)

什么是B-Tree

MySQL索引背后的数据结构及算法原理

MySQL性能优化-慢查询分析、优化索引和配置

 1 [root@zlm3 10:48:00 /var/log/masterha/app1]
 2 #nohup masterha_manager --conf=/etc/masterha/app1.conf --global_conf=/etc/masterha/masterha_default.conf &
 3 [1] 9265
 4 nohup: ignoring input and appending output to ‘nohup.out’
 5 
 6 [root@zlm3 10:48:12 /var/log/masterha/app1]
 7 #ls -l
 8 total 24
 9 -rw-r--r-- 1 root root 16370 Aug  3 10:48 app1.log
10 -rw-r--r-- 1 root root    35 Aug  3 10:48 app1.master_status.health //This file is created only when MasterHA-manager is running.It will continuously record the health status between slave and master.
11 -rw------- 1 root root   371 Aug  3 10:48 nohup.out
12 
13 [root@zlm3 10:48:14 /var/log/masterha/app1]
14 #cat nohup.out
15 Fri Aug  3 10:48:12 2018 - [info] Reading default configuration from /etc/masterha/masterha_default.conf..
16 Fri Aug  3 10:48:12 2018 - [info] Reading application default configuration from /etc/masterha/app1.conf..
17 Fri Aug  3 10:48:12 2018 - [info] Reading server configuration from /etc/masterha/app1.conf..
18 ssh_exchange_identification: Connection closed by remote host
19 
20 [root@zlm3 10:48:26 /var/log/masterha/app1]
21 #cat app1.master_status.health 
22 9265    0:PING_OK    master:192.168.1.101
23 [root@zlm3 10:48:31 /var/log/masterha/app1]
24 #ps aux|grep manager
25 root      9265  0.6  2.1 299172 21516 pts/1    S    10:48   0:00 perl /usr/bin/masterha_manager --conf=/etc/masterha/app1.conf --global_conf=/etc/masterha/masterha_default.conf
26 root      9332  0.0  0.0 112640   960 pts/1    R+   10:48   0:00 grep --color=auto manager

压缩空间的方式有两种:1、数据库。2、文件。

 具体类型介绍:

单列索引:不允许为空

 普通索引 不允许有空值

 唯一索引

 主键索引 在 InnoDB 引擎中很重要

组合引擎:多个字段上创建的索引,复合索引时遵循最左前缀原则。

 查询中某个列有范围查询,则其右边的所有列都无法使用查询

全文索引:

空间索引:

参考:细说mysql索引、我的MYSQL学习心得(九) 索引

Execute "masterha_master_switch" script again to generate a failover on zlm2.

有三种可以进行收缩,数据、日志和filestream 数据。

EXPLAIN 字段介绍

 possible_keys:显示可能应用在这张表中的索引。

 key:实际使用的索引。

 key_len:使用的索引的长度,越短越好。

 ref:显示索引的哪一列被使用了。

 rows:MySQL认为必须检索的用来返回请求数据的行数。

 type:使用了何种类型。从最好到最差的连接类型为system、const(常量)、eq_ref、ref、range、index(索引全表扫描)和ALL(全表扫描)。

 

在要收缩空间的数据库上右键--》任务--》收缩--》1、数据库,2文件(自己选择)。然后就等着吧。

日志

错误日志:记录了当 mysqld 启动和停止时,以及服务器在 运行过程中发生任何严重错误时的相关信息。

二进制文件:记录了所有的 DDL(数据定义语言)语句和 DML(数据操纵语言) 语句,不包括数据查询语句。语句以“事件”的形式保存,它描述了数据的更改过程。(定期删除日志,默认关闭)。

查询日志:记录了客户端的所有语句,格式为纯文本格式,可以直接进行读取。(log 日志中记录了所有数据库的操作,对于访问频繁的系统,此日志对系统性能的影响较大,建议关闭,默认关闭)。

慢查询日志:慢查询日志记录了包含所有执行时间超过参数long_query_time(单位:秒)所设置值的 SQL 语句的日志。(纯文本格式)MySQL日志文件之错误日志和慢查询日志详解。

日志文件小结:

 系统故障时,建议首先查看错误日志,以帮助用户迅速定位故障原因。

 记录数据的变更、数据的备份、数据的复制等操作时,打开二进制日志。默认不记录此日志,建议通过--log-bin 选项将此日志打开。

 如果希望记录数据库发生的任何操作,包括 SELECT,则需要用--log 将查询日志打开, 此日志默认关闭,一般情况下建议不要打开此日志,以免影响系统整体性能。

 查看系统的性能问题, 希望找到有性能问题的SQL语 句,需要 用 --log-slow-queries 打开慢查询日志。对于大量的慢查询日志,建议使用 mysqldumpslow 工具 来进行汇总查看。

 

我选择的是数据库收缩,还是很慢的,大家等着吧。

存储引擎

各种存储引擎的区别与联系     (存储数据技术和策略,存储机制、索引技巧、锁定水平等)

数据库存储引擎     show table status 显示表的相关信息

InnoDB与MyISAM的比较(从5.7开始innodb存储引擎成为默认的存储引擎。)

 锁机制:行级锁,表级锁

 事务操作:事务安全,不支持

InnoDB (1)可靠性要求比较高,要求事务;(2)表更新和查询都相当的频繁,并且行锁定的机会比较大的情况。

 MySQL4.1之后每个表的数据和索引存储在一个文件里。

 InnoDB 采用了MVCC来支持高并发,并且实现了四个标准的隔离级别。其默认级别是REPEATABLE READ(可重复读) ,行级锁。

 自动灾难恢复。与其它存储引擎不同,InnoDB表能够自动从灾难中恢复。

 外键约束。MySQL支持外键的存储引擎只有InnoDB。

 支持自动增加列AUTO_INCREMENT属性。

MyIsam  (1)做很多count 的计算;(2)插入不频繁,查询非常频繁;(3)没有事务。

 表存储在两个文件中,数据文件(MYD)和索引文件(MYI)

 表级锁,读=共享锁,写=排它锁。

 适合选择密集型的表,插入密集型的表。

 

选择文件,

备份

数据备份(深入浅出Mysql 27章 备份与恢复)

 全备份与增量备份的比较。

 确保 MySQL 打开 log-bin 选项,有了 BINLOG,MySQL 才可以在必要的时候做完 整恢复,或基于时间点的恢复,或基于位置的恢复。

逻辑备份(将数据库中的数据备份为一个文本文件,备份的文件可以被查 看和编辑。)

物理备份

 冷备份:cp移动数据文件的方法。

 恢复:移动数据文件,使用 mysqlbinlog 工具恢复自备份以来的所有 BINLOG。

 热备份:(将要备份的表加读锁,然后再 cp 数据文件到备份目录。)

 MyISAM:mysqlhotcopy工具。

 ibbackup 是 Innobase 公司(www.innodb.com)的一个热备份工具。

  1 [root@zlm3 10:54:59 /var/log/masterha/app1]
  2 #echo ''> app1.log
  3 
  4 [root@zlm3 10:55:17 /var/log/masterha/app1]
  5 #tail -f app1.log
  6 
  7 Fri Aug  3 10:55:29 2018 - [warning] Got error on MySQL select ping: 2006 (MySQL server has gone away)
  8 Fri Aug  3 10:55:29 2018 - [info] Executing SSH check script: exit 0
  9 Fri Aug  3 10:55:29 2018 - [warning] HealthCheck: SSH to 192.168.1.101 is NOT reachable.
 10 Fri Aug  3 10:55:30 2018 - [warning] Got error on MySQL connect: 2003 (Can't connect to MySQL server on '192.168.1.101' (111))
 11 Fri Aug  3 10:55:30 2018 - [warning] Connection failed 2 time(s)..
 12 Fri Aug  3 10:55:31 2018 - [warning] Got error on MySQL connect: 2003 (Can't connect to MySQL server on '192.168.1.101' (111))
 13 Fri Aug  3 10:55:31 2018 - [warning] Connection failed 3 time(s)..
 14 Fri Aug  3 10:55:32 2018 - [warning] Got error on MySQL connect: 2003 (Can't connect to MySQL server on '192.168.1.101' (111))
 15 Fri Aug  3 10:55:32 2018 - [warning] Connection failed 4 time(s)..
 16 Fri Aug  3 10:55:32 2018 - [warning] Master is not reachable from health checker!
 17 Fri Aug  3 10:55:32 2018 - [warning] Master 192.168.1.101(192.168.1.101:3306) is not reachable!
 18 Fri Aug  3 10:55:32 2018 - [warning] SSH is NOT reachable.
 19 Fri Aug  3 10:55:32 2018 - [info] Connecting to a master server failed. Reading configuration file /etc/masterha/masterha_default.conf and /etc/masterha/app1.conf again, and trying to connect to all servers to check server status..
 20 Fri Aug  3 10:55:32 2018 - [info] Reading default configuration from /etc/masterha/masterha_default.conf..
 21 Fri Aug  3 10:55:32 2018 - [info] Reading application default configuration from /etc/masterha/app1.conf..
 22 Fri Aug  3 10:55:32 2018 - [info] Reading server configuration from /etc/masterha/app1.conf..
 23 Fri Aug  3 10:55:33 2018 - [info] GTID failover mode = 1
 24 Fri Aug  3 10:55:33 2018 - [info] Dead Servers:
 25 Fri Aug  3 10:55:33 2018 - [info]   192.168.1.101(192.168.1.101:3306)
 26 Fri Aug  3 10:55:33 2018 - [info] Alive Servers:
 27 Fri Aug  3 10:55:33 2018 - [info]   192.168.1.102(192.168.1.102:3306)
 28 Fri Aug  3 10:55:33 2018 - [info] Alive Slaves:
 29 Fri Aug  3 10:55:33 2018 - [info]   192.168.1.102(192.168.1.102:3306)  Version=5.7.21-log (oldest major version between slaves) log-bin:enabled
 30 Fri Aug  3 10:55:33 2018 - [info]     GTID ON
 31 Fri Aug  3 10:55:33 2018 - [info]     Replicating from 192.168.1.101(192.168.1.101:3306)
 32 Fri Aug  3 10:55:33 2018 - [info]     Primary candidate for the new Master (candidate_master is set)
 33 Fri Aug  3 10:55:33 2018 - [info] Checking slave configurations..
 34 Fri Aug  3 10:55:33 2018 - [info]  read_only=1 is not set on slave 192.168.1.102(192.168.1.102:3306).
 35 Fri Aug  3 10:55:33 2018 - [info] Checking replication filtering settings..
 36 Fri Aug  3 10:55:33 2018 - [info]  Replication filtering check ok.
 37 Fri Aug  3 10:55:33 2018 - [info] Master is down!
 38 Fri Aug  3 10:55:33 2018 - [info] Terminating monitoring script.
 39 Fri Aug  3 10:55:33 2018 - [info] Got exit code 20 (Master dead).
 40 Fri Aug  3 10:55:33 2018 - [info] MHA::MasterFailover version 0.56.
 41 Fri Aug  3 10:55:33 2018 - [info] Starting master failover.
 42 Fri Aug  3 10:55:33 2018 - [info] 
 43 Fri Aug  3 10:55:33 2018 - [info] * Phase 1: Configuration Check Phase..
 44 Fri Aug  3 10:55:33 2018 - [info] 
 45 Fri Aug  3 10:55:34 2018 - [info] GTID failover mode = 1
 46 Fri Aug  3 10:55:34 2018 - [info] Dead Servers:
 47 Fri Aug  3 10:55:34 2018 - [info]   192.168.1.101(192.168.1.101:3306)
 48 Fri Aug  3 10:55:34 2018 - [info] Checking master reachability via MySQL(double check)...
 49 Fri Aug  3 10:55:34 2018 - [info]  ok.
 50 Fri Aug  3 10:55:34 2018 - [info] Alive Servers:
 51 Fri Aug  3 10:55:34 2018 - [info]   192.168.1.102(192.168.1.102:3306)
 52 Fri Aug  3 10:55:34 2018 - [info] Alive Slaves:
 53 Fri Aug  3 10:55:34 2018 - [info]   192.168.1.102(192.168.1.102:3306)  Version=5.7.21-log (oldest major version between slaves) log-bin:enabled
 54 Fri Aug  3 10:55:34 2018 - [info]     GTID ON
 55 Fri Aug  3 10:55:34 2018 - [info]     Replicating from 192.168.1.101(192.168.1.101:3306)
 56 Fri Aug  3 10:55:34 2018 - [info]     Primary candidate for the new Master (candidate_master is set)
 57 Fri Aug  3 10:55:34 2018 - [info] Starting GTID based failover.
 58 Fri Aug  3 10:55:34 2018 - [info] 
 59 Fri Aug  3 10:55:34 2018 - [info] ** Phase 1: Configuration Check Phase completed.
 60 Fri Aug  3 10:55:34 2018 - [info] 
 61 Fri Aug  3 10:55:34 2018 - [info] * Phase 2: Dead Master Shutdown Phase..
 62 Fri Aug  3 10:55:34 2018 - [info] 
 63 Fri Aug  3 10:55:34 2018 - [info] Forcing shutdown so that applications never connect to the current master..
 64 Fri Aug  3 10:55:34 2018 - [info] Executing master IP deactivation script:
 65 Fri Aug  3 10:55:34 2018 - [info]   /etc/masterha/master_ip_failover --orig_master_host=192.168.1.101 --orig_master_ip=192.168.1.101 --orig_master_port=3306 --command=stop  --orig_master_ssh_port=3306
 66 ssh: connect to host 192.168.1.101 port 3306: Connection refused
 67 Fri Aug  3 10:55:37 2018 - [info]  done.
 68 Fri Aug  3 10:55:37 2018 - [warning] shutdown_script is not set. Skipping explicit shutting down of the dead master.
 69 Fri Aug  3 10:55:37 2018 - [info] * Phase 2: Dead Master Shutdown Phase completed.
 70 Fri Aug  3 10:55:37 2018 - [info] 
 71 Fri Aug  3 10:55:37 2018 - [info] * Phase 3: Master Recovery Phase..
 72 Fri Aug  3 10:55:37 2018 - [info] 
 73 Fri Aug  3 10:55:37 2018 - [info] * Phase 3.1: Getting Latest Slaves Phase..
 74 Fri Aug  3 10:55:37 2018 - [info] 
 75 Fri Aug  3 10:55:37 2018 - [info] The latest binary log file/position on all slaves is mysql-bin.000051:190
 76 Fri Aug  3 10:55:37 2018 - [info] Latest slaves (Slaves that received relay log files to the latest):
 77 Fri Aug  3 10:55:37 2018 - [info]   192.168.1.102(192.168.1.102:3306)  Version=5.7.21-log (oldest major version between slaves) log-bin:enabled
 78 Fri Aug  3 10:55:37 2018 - [info]     GTID ON
 79 Fri Aug  3 10:55:37 2018 - [info]     Replicating from 192.168.1.101(192.168.1.101:3306)
 80 Fri Aug  3 10:55:37 2018 - [info]     Primary candidate for the new Master (candidate_master is set)
 81 Fri Aug  3 10:55:37 2018 - [info] The oldest binary log file/position on all slaves is mysql-bin.000051:190
 82 Fri Aug  3 10:55:37 2018 - [info] Oldest slaves:
 83 Fri Aug  3 10:55:37 2018 - [info]   192.168.1.102(192.168.1.102:3306)  Version=5.7.21-log (oldest major version between slaves) log-bin:enabled
 84 Fri Aug  3 10:55:37 2018 - [info]     GTID ON
 85 Fri Aug  3 10:55:37 2018 - [info]     Replicating from 192.168.1.101(192.168.1.101:3306)
 86 Fri Aug  3 10:55:37 2018 - [info]     Primary candidate for the new Master (candidate_master is set)
 87 Fri Aug  3 10:55:37 2018 - [info] 
 88 Fri Aug  3 10:55:37 2018 - [info] * Phase 3.3: Determining New Master Phase..
 89 Fri Aug  3 10:55:37 2018 - [info] 
 90 Fri Aug  3 10:55:37 2018 - [info] Searching new master from slaves..
 91 Fri Aug  3 10:55:37 2018 - [info]  Candidate masters from the configuration file:
 92 Fri Aug  3 10:55:37 2018 - [info]   192.168.1.102(192.168.1.102:3306)  Version=5.7.21-log (oldest major version between slaves) log-bin:enabled
 93 Fri Aug  3 10:55:37 2018 - [info]     GTID ON
 94 Fri Aug  3 10:55:37 2018 - [info]     Replicating from 192.168.1.101(192.168.1.101:3306)
 95 Fri Aug  3 10:55:37 2018 - [info]     Primary candidate for the new Master (candidate_master is set)
 96 Fri Aug  3 10:55:37 2018 - [info]  Non-candidate masters:
 97 Fri Aug  3 10:55:37 2018 - [info]  Searching from candidate_master slaves which have received the latest relay log events..
 98 Fri Aug  3 10:55:37 2018 - [info] New master is 192.168.1.102(192.168.1.102:3306)
 99 Fri Aug  3 10:55:37 2018 - [info] Starting master failover..
100 Fri Aug  3 10:55:37 2018 - [info] 
101 From:
102 192.168.1.101(192.168.1.101:3306) (current master)
103  +--192.168.1.102(192.168.1.102:3306)
104 
105 To:
106 192.168.1.102(192.168.1.102:3306) (new master)
107 Fri Aug  3 10:55:37 2018 - [info] 
108 Fri Aug  3 10:55:37 2018 - [info] * Phase 3.3: New Master Recovery Phase..
109 Fri Aug  3 10:55:37 2018 - [info] 
110 Fri Aug  3 10:55:37 2018 - [info]  Waiting all logs to be applied.. 
111 Fri Aug  3 10:55:37 2018 - [info]   done.
112 Fri Aug  3 10:55:37 2018 - [info] Getting new master's binlog name and position..
113 Fri Aug  3 10:55:37 2018 - [info]  mysql-bin.000004:190
114 Fri Aug  3 10:55:37 2018 - [info]  All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='192.168.1.102', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='xxx';
115 Fri Aug  3 10:55:37 2018 - [info] Master Recovery succeeded. File:Pos:Exec_Gtid_Set: mysql-bin.000004, 190, 1b7181ee-6eaf-11e8-998e-080027de0e0e:1-3730259
116 Fri Aug  3 10:55:37 2018 - [info] Executing master IP activate script:
117 Fri Aug  3 10:55:37 2018 - [info]   /etc/masterha/master_ip_failover --command=start --ssh_user=root --orig_master_host=192.168.1.101 --orig_master_ip=192.168.1.101 --orig_master_port=3306 --new_master_host=192.168.1.102 --new_master_ip=192.168.1.102 --new_master_port=3306 --new_master_user='zlm' --new_master_password='zlmzlm'  --orig_master_ssh_port=3306  --new_master_ssh_port=3306
118 Unknown option: new_master_ssh_port
119 Set read_only=0 on the new master.
120 ssh_exchange_identification: Connection closed by remote host
121 Fri Aug  3 10:55:37 2018 - [info]  OK.
122 Fri Aug  3 10:55:37 2018 - [info] ** Finished master recovery successfully.
123 Fri Aug  3 10:55:37 2018 - [info] * Phase 3: Master Recovery Phase completed.
124 Fri Aug  3 10:55:37 2018 - [info] 
125 Fri Aug  3 10:55:37 2018 - [info] * Phase 4: Slaves Recovery Phase..
126 Fri Aug  3 10:55:37 2018 - [info] 
127 Fri Aug  3 10:55:37 2018 - [info] 
128 Fri Aug  3 10:55:37 2018 - [info] * Phase 4.1: Starting Slaves in parallel..
129 Fri Aug  3 10:55:37 2018 - [info] 
130 Fri Aug  3 10:55:37 2018 - [info] All new slave servers recovered successfully.
131 Fri Aug  3 10:55:37 2018 - [info] 
132 Fri Aug  3 10:55:37 2018 - [info] * Phase 5: New master cleanup phase..
133 Fri Aug  3 10:55:37 2018 - [info] 
134 Fri Aug  3 10:55:37 2018 - [info] Resetting slave info on the new master..
135 Fri Aug  3 10:55:37 2018 - [info]  192.168.1.102: Resetting slave info succeeded.
136 Fri Aug  3 10:55:37 2018 - [info] Master failover to 192.168.1.102(192.168.1.102:3306) completed successfully.
137 Fri Aug  3 10:55:37 2018 - [info] 
138 
139 ----- Failover Report -----
140 
141 app1: MySQL Master failover 192.168.1.101(192.168.1.101:3306) to 192.168.1.102(192.168.1.102:3306) succeeded
142 
143 Master 192.168.1.101(192.168.1.101:3306) is down!
144 
145 Check MHA Manager logs at zlm3:/var/log/masterha/app1/app1.log for details.
146 
147 Started automated(non-interactive) failover.
148 Invalidated master IP address on 192.168.1.101(192.168.1.101:3306)
149 Selected 192.168.1.102(192.168.1.102:3306) as a new master.
150 192.168.1.102(192.168.1.102:3306): OK: Applying all logs succeeded.
151 192.168.1.102(192.168.1.102:3306): OK: Activated master IP address.
152 192.168.1.102(192.168.1.102:3306): Resetting slave info succeeded.
153 Master failover to 192.168.1.102(192.168.1.102:3306) completed successfully.
154 
155 //Above failover report shows all the evidence and results of automation master switchover.All of the steps are executed successfully.

两个会有不同,数据库会将该数据库所有的未使用空间进行收缩。适用于 数据和日志都有清空或删除。

MySQL索引详解 (一般使用磁盘I/O次数评价索引结构的优劣。)

 磁盘存取原理

 局部性原理与磁盘预读

M 阶 B-Tree

 澳门新葡亰平台官网 1

 根节点至少有2个子树。

 每个非叶子节点由n-1个key和n个指针组成。

 分支节点至少拥有m/2颗子树,最多拥有m个子树。(除根节点和叶子结点外)

 所有叶节点具有相同的深度,等于树高 h。

 每个叶子节点最少包含一个key和两个指针,最多包含2d-1个key和2d个指针。

B+ Tree

 内节点不存储data,只存储key。

 叶子节点不存储指针。

MySQL 索引实现

 MyISAM 索引文件和数据文件是分离,非聚集索引。

 InnoDB 叶节点包含了完整的数据记录,聚集索引。根据主键聚集。

Test 2:Manual master failover

阿里介绍 filestream :https://yq.aliyun.com/ziliao/103097

恢复

     完全恢复

 将备份作为输入执行。

 将备份后执行的日志进行重做。

     不完全恢复(跳过误操作语句,再恢复后 面执行的语句,完成我们的恢复。)

 基于时间点的操作。跳过故障发生时间。

 基于位置的恢复。找到出错语句的位置号,并跳过位置区间。

 

Check the master-slave replication status.

澳门新葡亰平台官网 2

数据类型

MySQL数据类型-菜鸟教程 

MYSQL中数据类型介绍

整数: int(m)里的m是表示数据显示宽度,浮点数,定点数。

字符串:char(n)4.0 n 代表字节,5.0 n 代表字符 (UTF-8=3zj,GBK=2zj)

 char 固定的字符数,空格补上;检索速度快。

 varchar 字符数+1个字节(n<=255)或2个字节(n>255)

 text 字符数+2个字节;不能有默认值;索引要指定前多少个字符;文本方式存储

 blob 二进制方式存储

 

 

事件

     类似于Linux的定时任务,某个时间或者每隔一段时间执行一段SQL代码。

 1 [root@zlm3 07:35:00 ~]
 2 #masterha_check_ssh --conf=/etc/masterha/app1.conf
 3 Fri Aug  3 07:37:13 2018 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
 4 Fri Aug  3 07:37:13 2018 - [info] Reading application default configuration from /etc/masterha/app1.conf..
 5 Fri Aug  3 07:37:13 2018 - [info] Reading server configuration from /etc/masterha/app1.conf..
 6 Fri Aug  3 07:37:13 2018 - [info] Starting SSH connection tests..
 7 Fri Aug  3 07:37:13 2018 - [debug] 
 8 Fri Aug  3 07:37:13 2018 - [debug]  Connecting via SSH from root@192.168.1.101(192.168.1.101:22) to root@192.168.1.102(192.168.1.102:22)..
 9 Fri Aug  3 07:37:13 2018 - [debug]   ok.
10 Fri Aug  3 07:37:14 2018 - [debug] 
11 Fri Aug  3 07:37:13 2018 - [debug]  Connecting via SSH from root@192.168.1.102(192.168.1.102:22) to root@192.168.1.101(192.168.1.101:22)..
12 Fri Aug  3 07:37:13 2018 - [debug]   ok.
13 Fri Aug  3 07:37:14 2018 - [info] All SSH connection tests passed successfully.
14 
15 [root@zlm3 07:37:14 ~]
16 #masterha_check_repl --conf=/etc/masterha/app1.conf --global_conf=/etc/masterha/masterha_default.conf 
17 Fri Aug  3 07:37:37 2018 - [info] Reading default configuration from /etc/masterha/masterha_default.conf..
18 Fri Aug  3 07:37:37 2018 - [info] Reading application default configuration from /etc/masterha/app1.conf..
19 Fri Aug  3 07:37:37 2018 - [info] Reading server configuration from /etc/masterha/app1.conf..
20 Fri Aug  3 07:37:37 2018 - [info] MHA::MasterMonitor version 0.56.
21 Fri Aug  3 07:37:38 2018 - [info] GTID failover mode = 1
22 Fri Aug  3 07:37:38 2018 - [info] Dead Servers:
23 Fri Aug  3 07:37:38 2018 - [info] Alive Servers:
24 Fri Aug  3 07:37:38 2018 - [info]   192.168.1.101(192.168.1.101:3306)
25 Fri Aug  3 07:37:38 2018 - [info]   192.168.1.102(192.168.1.102:3306)
26 Fri Aug  3 07:37:38 2018 - [info] Alive Slaves:
27 Fri Aug  3 07:37:38 2018 - [info]   192.168.1.102(192.168.1.102:3306)  Version=5.7.21-log (oldest major version between slaves) log-bin:enabled
28 Fri Aug  3 07:37:38 2018 - [info]     GTID ON
29 Fri Aug  3 07:37:38 2018 - [info]     Replicating from 192.168.1.101(192.168.1.101:3306)
30 Fri Aug  3 07:37:38 2018 - [info]     Primary candidate for the new Master (candidate_master is set)
31 Fri Aug  3 07:37:38 2018 - [info] Current Alive Master: 192.168.1.101(192.168.1.101:3306)
32 Fri Aug  3 07:37:38 2018 - [info] Checking slave configurations..
33 Fri Aug  3 07:37:38 2018 - [info]  read_only=1 is not set on slave 192.168.1.102(192.168.1.102:3306).
34 Fri Aug  3 07:37:38 2018 - [info] Checking replication filtering settings..
35 Fri Aug  3 07:37:38 2018 - [info]  binlog_do_db= , binlog_ignore_db= 
36 Fri Aug  3 07:37:38 2018 - [info]  Replication filtering check ok.
37 Fri Aug  3 07:37:38 2018 - [info] GTID (with auto-pos) is supported. Skipping all SSH and Node package checking.
38 Fri Aug  3 07:37:38 2018 - [info] Checking SSH publickey authentication settings on the current master..
39 ssh_exchange_identification: Connection closed by remote host
40 Fri Aug  3 07:37:38 2018 - [warning] HealthCheck: SSH to 192.168.1.101 is NOT reachable.
41 Fri Aug  3 07:37:38 2018 - [info] 
42 192.168.1.101(192.168.1.101:3306) (current master)
43  +--192.168.1.102(192.168.1.102:3306)
44 
45 Fri Aug  3 07:37:38 2018 - [info] Checking replication health on 192.168.1.102..
46 Fri Aug  3 07:37:38 2018 - [info]  ok.
47 Fri Aug  3 07:37:38 2018 - [info] Checking master_ip_failover_script status:
48 Fri Aug  3 07:37:38 2018 - [info]   /etc/masterha/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.1.101 --orig_master_ip=192.168.1.101 --orig_master_port=3306  --orig_master_ssh_port=3306
49 Fri Aug  3 07:37:38 2018 - [info]  OK.
50 Fri Aug  3 07:37:38 2018 - [warning] shutdown_script is not defined.
51 Fri Aug  3 07:37:38 2018 - [info] Got exit code 0 (Not master dead).
52 
53 MySQL Replication Health is OK.
54 
55 [root@zlm3 07:40:03 ~]
56 #Fri Aug  3 07:40:03 2018 - [info] Reading default configuration from /etc/masterha/masterha_default.conf..
57 Fri Aug  3 07:40:03 2018 - [info] Reading application default configuration from /etc/masterha/app1.conf..
58 Fri Aug  3 07:40:03 2018 - [info] Reading server configuration from /etc/masterha/app1.conf..
59 ssh_exchange_identification: Connection closed by remote host
60 ^C
61 
62 [root@zlm3 07:40:11 ~]
63 #masterha_check_status --conf=/etc/masterha/app1.conf
64 app1 (pid:5628) is running(0:PING_OK), master:192.168.1.101

前两个比较熟悉,就是对数据和日志占用空间进行收缩,第三个自己也不是很了解,建议百度下,不过很多地方都有讲解。

索引类型:

 B-Tree索引 索引列的顺序影响者是否使用索引。

 哈希索引

 无法用于排序。

 只支持全部匹配。

 只支持等值比较。

 有很多哈希冲突时,效率不太高。

 空间数据索引(R-Tree)无需前缀查询,从所有维度查询数据。

 全文检索 查找文本中的关键词,类似于搜索引擎做的事情。

Observe the app1.log on zlm3.

服务器重启后发现还是没有释放空间,emmmmmm。。。。那就请教百度老师了。

视图 

MySQL数据库视图

MySQL - 视图算法

视图最简单的实现方法是把select语句的结果存放到临时表中。具有性能问题,优化器很难优化临时表上的查询。

 合并算法 :select语句与外部查询视图的select语句进行合并,然后执行。

 临时表算法 :先执行视图的select语句,后执行外部查询的语句。

视图在某些情况下可以提升性能,并和其他提升性能的方式叠加使用。

 视图不可以跨表进行修改数据,

 创建有条件限制的视图时,加上“WITH CHECK OPTION”命令。

 

澳门新葡亰平台官网 3

触发器

 触发器的触发事件 , 可以是 INSERT 、UPDATE 或者 DELETE 。

 触发时间 , 可以是 BEFORE 或者 AFTER。

 同一个表相同触发时间的相同触发事件 , 只能定义一个触发器,只支持基于行触发。

 触发器的原子性,InnoDB支持事务,MyISAM不支持。

 

澳门新葡亰平台官网 4

三范式

三范式定义(范式和反范式)

1NF:每个数据项都是最小单元,不可分割,确定行列之后只能对应一个数据。

2NF:每一个非主属性完全依赖于候选码(属性组的值能唯一的标识一个元组,但是其子集不可以)。

3NF:每一个非主属性既不传递依赖于,也不部分依赖于(主码=候选码为多个市,从中选出一个作为主码)。

BCNF主属性(候选码中的某一个属性)内部也不能部分或传递依赖于码。

4NF :没有多值依赖。

 

网友的解答是,SQL server数据库不会自己压缩已经扩展的空间,如果需要压缩需要手动操作。

数据库ACID

数据库的ACID

数据库事务介绍

原子性(Atomicity)一个事务必须被视为一个不可分割的最小工作单元,整个事务中的所有操作要么全部提交成功,要么全部失败回滚,对于一个事务来说,不可能只执行其中的一部分操作。

一致性(Consistency)数据库总是从一个一致性的状态转换到另一个一致性的状态。

隔离性(Isolation)一个事务所做的修改在最终提交以前,对其他事务是不可见的。

持久性(Durability)一旦事务提交,则其所做的修改不会永久保存到数据库。

4 种隔离级别

MVVC的简单介绍

READ UNCOMMITTED(未提交读)脏读:事务中的修改,即使没有提交,对其他事务也都是可见的。

READ COMMITTED(提交读)不可重复读:事务从开始直到提交之前,所做的任何修改对其他事务都是不可见的。

REPEATABLE READ(可重复读):幻读:一个事务按相同的查询条件读取以前检索过的数据,其他事务插入了满足其查询条件的新数据。产生幻行。

SERIALIZABLE(可串行化) 强制事务串行执行

MVVC是个行级锁的变种,它在普通读情况下避免了加锁操作,自特定情况下加锁

Preface

SQL server数据库,在手动删除或者自动删除数据后,查看数据库物理文件发现占用空间并没有释放,果断采用万能的重启。(反正是自己用的一个服务器,随便玩。如果不是只有自己用的服务器,还是建议让运维或者DBA来做)。

Mysql死锁问题

Mysql悲观锁总结和实践

Mysql乐观锁总结和实践

SELECT ... LOCK IN SHARE MODE SELECT ... FOR UPDATE:(LOCK IN SHARE MODE 在有一方事务要Update 同一个表单时很容易造成死锁)

乐观锁:取锁失败,产生回溯时影响效率。

 取数据时认为其他线程不会对数据进行修改。

 更新时判断是否对数据进行修改,版本号机制或CAS操作。

悲观锁:每次取数据都会加锁。

innodb_lock_wait_timeout 等待锁超时回滚事务:  【超时法】

直观方法是在两个事务相互等待时,当一个等待时间超过设置的某一阀值时,对其中一个事务进行回滚,另一个事务就能继续执行。在innodb中,参数innodb_lock_wait_timeout用来设置超时时间。

wait-for graph算法来主动进行死锁检测:  【等待图法】

innodb还提供了wait-for graph算法来主动进行死锁检测,每当加锁请求无法立即满足需要并进入等待时,wait-for graph算法都会被触发。

  1 [root@zlm3 08:21:27 ~]
  2 #masterha_master_switch --conf=/etc/masterha/app1.conf --global_conf=/etc/masterha/masterha_default.conf --master_state=alive --new_master_host=192.168.1.102 --orig_master_is_new_slave --running_updates_limit=60
  3 Fri Aug  3 08:21:29 2018 - [info] MHA::MasterRotate version 0.56.
  4 Fri Aug  3 08:21:29 2018 - [info] Starting online master switch..
  5 Fri Aug  3 08:21:29 2018 - [info] 
  6 Fri Aug  3 08:21:29 2018 - [info] * Phase 1: Configuration Check Phase..
  7 Fri Aug  3 08:21:29 2018 - [info] 
  8 Fri Aug  3 08:21:29 2018 - [info] Reading default configuration from /etc/masterha/masterha_default.conf..
  9 Fri Aug  3 08:21:29 2018 - [info] Reading application default configuration from /etc/masterha/app1.conf..
 10 Fri Aug  3 08:21:29 2018 - [info] Reading server configuration from /etc/masterha/app1.conf..
 11 Fri Aug  3 08:21:30 2018 - [info] GTID failover mode = 1
 12 Fri Aug  3 08:21:30 2018 - [info] Current Alive Master: 192.168.1.101(192.168.1.101:3306)
 13 Fri Aug  3 08:21:30 2018 - [info] Alive Slaves:
 14 Fri Aug  3 08:21:30 2018 - [info]   192.168.1.102(192.168.1.102:3306)  Version=5.7.21-log (oldest major version between slaves) log-bin:enabled
 15 Fri Aug  3 08:21:30 2018 - [info]     GTID ON
 16 Fri Aug  3 08:21:30 2018 - [info]     Replicating from 192.168.1.101(192.168.1.101:3306)
 17 Fri Aug  3 08:21:30 2018 - [info]     Primary candidate for the new Master (candidate_master is set)
 18 
 19 It is better to execute FLUSH NO_WRITE_TO_BINLOG TABLES on the master before switching. Is it ok to execute on 192.168.1.101(192.168.1.101:3306)? (YES/no): yes
 20 Fri Aug  3 08:21:33 2018 - [info] Executing FLUSH NO_WRITE_TO_BINLOG TABLES. This may take long time..
 21 Fri Aug  3 08:21:33 2018 - [info]  ok.
 22 Fri Aug  3 08:21:33 2018 - [info] Checking MHA is not monitoring or doing failover..
 23 Fri Aug  3 08:21:33 2018 - [error][/usr/share/perl5/vendor_perl/MHA/MasterRotate.pm, ln142] Getting advisory lock failed on the current master. MHA Monitor runs on the current master. Stop MHA Manager/Monitor and try again.
 24 Fri Aug  3 08:21:33 2018 - [error][/usr/share/perl5/vendor_perl/MHA/ManagerUtil.pm, ln177] Got ERROR:  at /usr/bin/masterha_master_switch line 53.
 25 
 26 //It means that we should stop MHA-manager when donging switchover master.
 27 
 28 [root@zlm3 08:21:33 ~]
 29 #masterha_stop --conf=/etc/masterha/app1.conf --global_conf=/etc/masterha/masterha_default.conf
 30 Stopped app1 successfully.
 31 [1]+  Exit 1                  masterha_manager --conf=/etc/masterha/app1.conf --global_conf=/etc/masterha/masterha_default.conf
 32 
 33 [root@zlm3 08:28:07 ~]
 34 #masterha_master_switch --conf=/etc/masterha/app1.conf --global_conf=/etc/masterha/masterha_default.conf --master_state=alive --new_master_host=192.168.1.102 --orig_master_is_new_slave --running_updates_limit=60
 35 Fri Aug  3 08:28:21 2018 - [info] MHA::MasterRotate version 0.56.
 36 Fri Aug  3 08:28:21 2018 - [info] Starting online master switch..
 37 Fri Aug  3 08:28:21 2018 - [info] 
 38 Fri Aug  3 08:28:21 2018 - [info] * Phase 1: Configuration Check Phase..
 39 Fri Aug  3 08:28:21 2018 - [info] 
 40 Fri Aug  3 08:28:21 2018 - [info] Reading default configuration from /etc/masterha/masterha_default.conf..
 41 Fri Aug  3 08:28:21 2018 - [info] Reading application default configuration from /etc/masterha/app1.conf..
 42 Fri Aug  3 08:28:21 2018 - [info] Reading server configuration from /etc/masterha/app1.conf..
 43 Fri Aug  3 08:28:22 2018 - [info] GTID failover mode = 1
 44 Fri Aug  3 08:28:22 2018 - [info] Current Alive Master: 192.168.1.101(192.168.1.101:3306)
 45 Fri Aug  3 08:28:22 2018 - [info] Alive Slaves:
 46 Fri Aug  3 08:28:22 2018 - [info]   192.168.1.102(192.168.1.102:3306)  Version=5.7.21-log (oldest major version between slaves) log-bin:enabled
 47 Fri Aug  3 08:28:22 2018 - [info]     GTID ON
 48 Fri Aug  3 08:28:22 2018 - [info]     Replicating from 192.168.1.101(192.168.1.101:3306)
 49 Fri Aug  3 08:28:22 2018 - [info]     Primary candidate for the new Master (candidate_master is set)
 50 
 51 It is better to execute FLUSH NO_WRITE_TO_BINLOG TABLES on the master before switching. Is it ok to execute on 192.168.1.101(192.168.1.101:3306)? (YES/no): yes
 52 Fri Aug  3 08:28:25 2018 - [info] Executing FLUSH NO_WRITE_TO_BINLOG TABLES. This may take long time..
 53 Fri Aug  3 08:28:25 2018 - [info]  ok.
 54 Fri Aug  3 08:28:25 2018 - [info] Checking MHA is not monitoring or doing failover..
 55 Fri Aug  3 08:28:25 2018 - [info] Checking replication health on 192.168.1.102..
 56 Fri Aug  3 08:28:25 2018 - [info]  ok.
 57 Fri Aug  3 08:28:25 2018 - [info] 192.168.1.102 can be new master.
 58 Fri Aug  3 08:28:25 2018 - [info] 
 59 From:
 60 192.168.1.101(192.168.1.101:3306) (current master)
 61  +--192.168.1.102(192.168.1.102:3306)
 62 
 63 To:
 64 192.168.1.102(192.168.1.102:3306) (new master)
 65  +--192.168.1.101(192.168.1.101:3306)
 66 
 67 Starting master switch from 192.168.1.101(192.168.1.101:3306) to 192.168.1.102(192.168.1.102:3306)? (yes/NO): yes
 68 Fri Aug  3 08:28:31 2018 - [info] Checking whether 192.168.1.102(192.168.1.102:3306) is ok for the new master..
 69 Fri Aug  3 08:28:31 2018 - [info]  ok.
 70 Fri Aug  3 08:28:31 2018 - [info] 192.168.1.101(192.168.1.101:3306): SHOW SLAVE STATUS returned empty result. To check replication filtering rules, temporarily executing CHANGE MASTER to a dummy host.
 71 Fri Aug  3 08:28:31 2018 - [info] 192.168.1.101(192.168.1.101:3306): Resetting slave pointing to the dummy host.
 72 Fri Aug  3 08:28:31 2018 - [info] ** Phase 1: Configuration Check Phase completed.
 73 Fri Aug  3 08:28:31 2018 - [info] 
 74 Fri Aug  3 08:28:31 2018 - [info] * Phase 2: Rejecting updates Phase..
 75 Fri Aug  3 08:28:31 2018 - [info] 
 76 Fri Aug  3 08:28:31 2018 - [info] Executing master ip online change script to disable write on the current master:
 77 Fri Aug  3 08:28:31 2018 - [info]   /etc/masterha/master_ip_online_change --command=stop --orig_master_host=192.168.1.101 --orig_master_ip=192.168.1.101 --orig_master_port=3306 --orig_master_user='zlm' --orig_master_password='zlmzlm' --new_master_host=192.168.1.102 --new_master_ip=192.168.1.102 --new_master_port=3306 --new_master_user='zlm' --new_master_password='zlmzlm' --orig_master_ssh_user=root --new_master_ssh_user=root  --orig_master_ssh_port=3306  --new_master_ssh_port=3306 --orig_master_is_new_slave
 78 Unknown option: new_master_ssh_port
 79 Fri Aug  3 08:28:32 2018 116409 Set read_only on the new master.. ok.
 80 Fri Aug  3 08:28:32 2018 125643 drop vip 10.33.101.239..
 81 ssh_exchange_identification: Connection closed by remote host
 82 Fri Aug  3 08:28:32 2018 142948 Waiting all running 1 threads are disconnected.. (max 1500 milliseconds)
 83 {'Time' => '13435','db' => undef,'Id' => '21','User' => 'repl','State' => 'Master has sent all binlog to slave; waiting for more updates','Command' => 'Binlog Dump GTID','Info' => undef,'Host' => 'zlm3:40535'}
 84 Fri Aug  3 08:28:32 2018 646769 Waiting all running 1 threads are disconnected.. (max 1000 milliseconds)
 85 {'Time' => '13435','db' => undef,'Id' => '21','User' => 'repl','State' => 'Master has sent all binlog to slave; waiting for more updates','Command' => 'Binlog Dump GTID','Info' => undef,'Host' => 'zlm3:40535'}
 86 Fri Aug  3 08:28:33 2018 149221 Waiting all running 1 threads are disconnected.. (max 500 milliseconds)
 87 {'Time' => '13436','db' => undef,'Id' => '21','User' => 'repl','State' => 'Master has sent all binlog to slave; waiting for more updates','Command' => 'Binlog Dump GTID','Info' => undef,'Host' => 'zlm3:40535'}
 88 Fri Aug  3 08:28:33 2018 650816 Set read_only=1 on the orig master.. ok.
 89 Fri Aug  3 08:28:33 2018 653323 Waiting all running 1 queries are disconnected.. (max 500 milliseconds)
 90 {'Time' => '13436','db' => undef,'Id' => '21','User' => 'repl','State' => 'Master has sent all binlog to slave; waiting for more updates','Command' => 'Binlog Dump GTID','Info' => undef,'Host' => 'zlm3:40535'}
 91 Fri Aug  3 08:28:34 2018 154965 Killing all application threads..
 92 Fri Aug  3 08:28:34 2018 167919 done.
 93 Fri Aug  3 08:28:34 2018 - [info]  ok.
 94 Fri Aug  3 08:28:34 2018 - [info] Locking all tables on the orig master to reject updates from everybody (including root):
 95 Fri Aug  3 08:28:34 2018 - [info] Executing FLUSH TABLES WITH READ LOCK..
 96 Fri Aug  3 08:28:34 2018 - [info]  ok.
 97 Fri Aug  3 08:28:34 2018 - [info] Orig master binlog:pos is mysql-bin.000050:2361.
 98 Fri Aug  3 08:28:34 2018 - [info]  Waiting to execute all relay logs on 192.168.1.102(192.168.1.102:3306)..
 99 Fri Aug  3 08:28:34 2018 - [info]  master_pos_wait(mysql-bin.000050:2361) completed on 192.168.1.102(192.168.1.102:3306). Executed 0 events.
100 Fri Aug  3 08:28:34 2018 - [info]   done.
101 Fri Aug  3 08:28:34 2018 - [info] Getting new master's binlog name and position..
102 Fri Aug  3 08:28:34 2018 - [info]  mysql-bin.000003:2321
103 Fri Aug  3 08:28:34 2018 - [info]  All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='192.168.1.102', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='xxx';
104 Fri Aug  3 08:28:34 2018 - [info] Executing master ip online change script to allow write on the new master:
105 Fri Aug  3 08:28:34 2018 - [info]   /etc/masterha/master_ip_online_change --command=start --orig_master_host=192.168.1.101 --orig_master_ip=192.168.1.101 --orig_master_port=3306 --orig_master_user='zlm' --orig_master_password='zlmzlm' --new_master_host=192.168.1.102 --new_master_ip=192.168.1.102 --new_master_port=3306 --new_master_user='zlm' --new_master_password='zlmzlm' --orig_master_ssh_user=root --new_master_ssh_user=root  --orig_master_ssh_port=3306  --new_master_ssh_port=3306 --orig_master_is_new_slave
106 Unknown option: new_master_ssh_port
107 Fri Aug  3 08:28:34 2018 327146 Set read_only=0 on the new master.
108 Fri Aug  3 08:28:34 2018 328259Add vip 10.33.101.239 on p3p1..
109 ssh_exchange_identification: Connection closed by remote host
110 Fri Aug  3 08:28:34 2018 - [info]  ok.
111 Fri Aug  3 08:28:34 2018 - [info] 
112 Fri Aug  3 08:28:34 2018 - [info] * Switching slaves in parallel..
113 Fri Aug  3 08:28:34 2018 - [info] 
114 Fri Aug  3 08:28:34 2018 - [info] Unlocking all tables on the orig master:
115 Fri Aug  3 08:28:34 2018 - [info] Executing UNLOCK TABLES..
116 Fri Aug  3 08:28:34 2018 - [info]  ok.
117 Fri Aug  3 08:28:34 2018 - [info] Starting orig master as a new slave..
118 Fri Aug  3 08:28:34 2018 - [info]  Resetting slave 192.168.1.101(192.168.1.101:3306) and starting replication from the new master 192.168.1.102(192.168.1.102:3306)..
119 Fri Aug  3 08:28:34 2018 - [info]  Executed CHANGE MASTER.
120 Fri Aug  3 08:28:35 2018 - [info]  Slave started.
121 Fri Aug  3 08:28:35 2018 - [info] All new slave servers switched successfully.
122 Fri Aug  3 08:28:35 2018 - [info] 
123 Fri Aug  3 08:28:35 2018 - [info] * Phase 5: New master cleanup phase..
124 Fri Aug  3 08:28:35 2018 - [info] 
125 Fri Aug  3 08:28:35 2018 - [info]  192.168.1.102: Resetting slave info succeeded.
126 Fri Aug  3 08:28:35 2018 - [info] Switching master to 192.168.1.102(192.168.1.102:3306) completed successfully.
127 
128 [root@zlm3 08:28:35 ~]
129 #

脚本之家:

 

本文由澳门新葡亰平台官网发布于数据库,转载请注明出处:server数据库压缩空间澳门新葡亰平台官网,MySQ

TAG标签:
Ctrl+D 将本页面保存为书签,全面了解最新资讯,方便快捷。