[Mysql] mysql意外drop表之后,使用innobackupex恢复


1、使用innodbackupex备份测试删除表:

innobackupex备份某以一张表

1
./innobackupex --defaults-file=/etc/my.cnf --databases="aaaa.test_order" --user=root --password=12345678 --port=3306 /tmp

如果要备份多个表,使用以下命令:

1
./innobackupex --defaults-file=/etc/my.cnf --databases="aaaa.test_order aaaa.test1" --user=root --password=12345678 --port=3306 /tmp

使用innobackupex备份出来的的数据在aaaa文件下面会存在table_name.frm和table_name.ibd两个文件。

2、如果知道表结构重新建表即可,如果表结构也无法获得,可通过该链接,对表结构进行恢复

3、恢复完表结构之后,开始恢复数据,丢弃表空间:

1
2
3
4
5
#为防止新的数据写入,对表加锁:
root@db 16:20: [aaaa]> lock tables tb1 write;

root@db 16:20: [aaaa]> alter table test_order discard tablespace;
Query OK, 0 rows affected (0.54 sec)

将对应的ibd文件拷入对应的数据目录,修改数据文件权限

1
2
shell > cp test_order.ibd /data/mysql/data/aaaa/
shell > chown mysql.mysql test_order.ibd

载入表空间:

1
2
root@db 16:24:  [aaaa]> alter table test_order import tablespace;
Query OK, 0 rows affected, 1 warning (1 min 53.51 sec)

查看有无报错:

1
show warnings;

4、对备份之后的数据进行恢复,查看备份开始的时间点

cat xtrabackup_binlog_info

1
mysql-binlog.000489	748512183

然后查找drop的pos点:

1
mysqlbinlog  -v --base64-output=DECODE-ROWS /data/mysql/log/mysql-binlog.000489 | grep -C 10 -i  "DROP"

找到删除的pos点,如果不在该log文件下,需要根据(show master status)的位置向前依次筛选,找到drop表的记录,内容如下。

1
2
3
server id 1  end_log_pos 748512209 CRC32 0x3fa6b448   Query   thread_id=27    
DROP TABLE `test_order` /* generated by server */
/*!*/;

找到表删除的pos点(748512209),利用binlog2sql生成中间发生sql语句,(binlog2sql方法详解此处

1
python binlog2sql.py  -uroot -p12345678 -daaaa -ttest_order --start-position=748512183 --stop-position=748512209 --start-file='mysql-binlog.000489'  > /tmp/re_aaaa_test_order.sql

生成的sql内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
root@dax-mysql-master binlog2sql]# cat /tmp/re_aaaa_test_order.sql
INSERT INTO `aaaa`.`test_order`(`id`) VALUES (12); #start 3037 end 3222 time 2018-11-06 18:54:21
INSERT INTO `aaaa`.`test_order`(`id`) VALUES (11); #start 3037 end 3222 time 2018-11-06 18:54:21
INSERT INTO `aaaa`.`test_order`(`id`) VALUES (10); #start 3037 end 3222 time 2018-11-06 18:54:21
INSERT INTO `aaaa`.`test_order`(`id`) VALUES (9); #start 3037 end 3222 time 2018-11-06 18:54:21
INSERT INTO `aaaa`.`test_order`(`id`) VALUES (8); #start 3037 end 3222 time 2018-11-06 18:54:21
INSERT INTO `aaaa`.`test_order`(`id`) VALUES (7); #start 3037 end 3222 time 2018-11-06 18:54:21
INSERT INTO `aaaa`.`test_order`(`id`) VALUES (6); #start 3037 end 3222 time 2018-11-06 18:54:21
INSERT INTO `aaaa`.`test_order`(`id`) VALUES (5); #start 3037 end 3222 time 2018-11-06 18:54:21
INSERT INTO `aaaa`.`test_order`(`id`) VALUES (4); #start 3037 end 3222 time 2018-11-06 18:54:21
INSERT INTO `aaaa`.`test_order`(`id`) VALUES (2); #start 2804 end 2949 time 2018-11-06 18:45:45
DELETE FROM `aaaa`.`test_order` WHERE `id`=12 LIMIT 1; #start 2571 end 2716 time 2018-11-06 18:45:39
DELETE FROM `aaaa`.`test_order` WHERE `id`=11 LIMIT 1; #start 2338 end 2483 time 2018-11-06 18:45:33

生成的sql顺序是倒序的,需要重新调整(github上关于binlog2sql的用法未有关于顺序的说明):
sed -i ‘1!G;h;$!d’ /tmp/re_aaaa_test_order.sql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@dax-mysql-master binlog2sql]#  sed -i '1!G;h;$!d' /tmp/re_aaaa_test_order.sql
[root@dax-mysql-master binlog2sql]# cat /tmp/re_aaaa_test_order.sql
DELETE FROM `aaaa`.`test_order` WHERE `id`=11 LIMIT 1; #start 2338 end 2483 time 2018-11-06 18:45:33
DELETE FROM `aaaa`.`test_order` WHERE `id`=12 LIMIT 1; #start 2571 end 2716 time 2018-11-06 18:45:39
INSERT INTO `aaaa`.`test_order`(`id`) VALUES (2); #start 2804 end 2949 time 2018-11-06 18:45:45
INSERT INTO `aaaa`.`test_order`(`id`) VALUES (4); #start 3037 end 3222 time 2018-11-06 18:54:21
INSERT INTO `aaaa`.`test_order`(`id`) VALUES (5); #start 3037 end 3222 time 2018-11-06 18:54:21
INSERT INTO `aaaa`.`test_order`(`id`) VALUES (6); #start 3037 end 3222 time 2018-11-06 18:54:21
INSERT INTO `aaaa`.`test_order`(`id`) VALUES (7); #start 3037 end 3222 time 2018-11-06 18:54:21
INSERT INTO `aaaa`.`test_order`(`id`) VALUES (8); #start 3037 end 3222 time 2018-11-06 18:54:21
INSERT INTO `aaaa`.`test_order`(`id`) VALUES (9); #start 3037 end 3222 time 2018-11-06 18:54:21
INSERT INTO `aaaa`.`test_order`(`id`) VALUES (10); #start 3037 end 3222 time 2018-11-06 18:54:21
INSERT INTO `aaaa`.`test_order`(`id`) VALUES (11); #start 3037 end 3222 time 2018-11-06 18:54:21
INSERT INTO `aaaa`.`test_order`(`id`) VALUES (12); #start 3037 end 3222 time 2018-11-06 18:54:21

5、把这sql文件进入导入即可进行备份后的数据恢复

1
2
3
[root@dax-mysql-master binlog2sql]# mysql -u root -p12345678 aaaa < /tmp/re_aaaa_test_order.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1205 (HY000) at line 1: Lock wait timeout exceeded; try restarting transaction

需要先解锁表:

1
unlock tables;

解锁完成在重新导入:

1
[root@dax-mysql-master binlog2sql]# mysql -u root -p12345678 aaaa < /tmp/re_aaaa_test_order.sql

导入成功。

点击阅读

[Mysql] Can't start group replication on secondary member with single primary-mode while asynchronous replication channels are running


0、mysql-mgr双节点集群,节点信息如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
select * from performance_schema.replication_group_members;
+---------------------------+--------------------------------------+------------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+------------------+-------------+--------------+
| group_replication_applier | bddd9c32-8fee-11e8-ac79-525400edbe8d | dax-mysql-slave | 3306 | ONLINE |
| group_replication_applier | d5bd8edd-9a1d-11e8-993e-525400578639 | dax-mysql-master | 3306 | ONLINE |
+---------------------------+--------------------------------------+------------------+-------------+--------------+
2 rows in set (0.00 sec)
dax-mysql-master为主节点:
SHOW STATUS LIKE 'group_replication_primary_member';
+----------------------------------+--------------------------------------+
| Variable_name | Value |
+----------------------------------+--------------------------------------+
| group_replication_primary_member | d5bd8edd-9a1d-11e8-993e-525400578639 |
+----------------------------------+--------------------------------------+

1、因某种需要需要重启数据库,先后停止从节点group_replication;

1
2
3
stop group_replication;
停止数据库:
/etc/init.d/mysql stop

停止主节点group_replication;

1
2
3
4
5
stop group_replication;
重启数据库
/etc/init.d/mysql restart
启动组复制:
start group_replication;
1
ERROR 3092 (HY000): The server is not configured properly to be an active member of the group. Please see more details on error log.

error.log日志报错:

1
2
3
4
5
6
7
8
9
10
11
12
2018-11-01T06:57:09.766303Z 6 [Note] Plugin group_replication reported: 'Group communication SSL configuration: group_replication_ssl_mode: "DISABLED"'
2018-11-01T06:57:09.768075Z 6 [Note] Plugin group_replication reported: '[GCS] Added automatically IP ranges 127.0.0.1/8,192.168.168.178/24 to the whitelist'
2018-11-01T06:57:09.769536Z 6 [Note] Plugin group_replication reported: '[GCS] Translated 'dax-mysql-master' to 192.168.168.178'
2018-11-01T06:57:09.770000Z 6 [Warning] Plugin group_replication reported: '[GCS] Automatically adding IPv4 localhost address to the whitelist. It is mandatory that it is added.'
2018-11-01T06:57:09.770200Z 6 [Note] Plugin group_replication reported: '[GCS] SSL was not enabled'
2018-11-01T06:57:09.770253Z 6 [Note] Plugin group_replication reported: 'Initialized group communication with configuration: group_replication_group_name: "740442c0-cc67-11e8-993e-525400578639"; group_replication_local_address: "dax-mysql-master:24901"; group_replication_group_seeds: "dax-mysql-master:24901,dax-mysql-slave:24901"; group_replication_bootstrap_group: false; group_replication_poll_spin_loops: 0; group_replication_compression_threshold: 1000000; group_replication_ip_whitelist: "AUTOMATIC"'
2018-11-01T06:57:09.770341Z 6 [Note] Plugin group_replication reported: '[GCS] Configured number of attempts to join: 0'
2018-11-01T06:57:09.770356Z 6 [Note] Plugin group_replication reported: '[GCS] Configured time between attempts to join: 5 seconds'
2018-11-01T06:57:09.770494Z 6 [Note] Plugin group_replication reported: 'Member configuration: member_id: 3306103; member_uuid: "d5bd8edd-9a1d-11e8-993e-525400578639"; single-primary mode: "true"; group_replication_auto_increment_increment: 7; '
2018-11-01T06:57:09.770589Z 6 [ERROR] Plugin group_replication reported: 'Can't start group replication on secondary member with single primary-mode while asynchronous replication channels are running.'
2018-11-01T06:57:09.770682Z 6 [Note] Plugin group_replication reported: 'Requesting to leave the group despite of not being a member'
2018-11-01T06:57:09.770696Z 6 [ERROR] Plugin group_replication reported: '[GCS] The member is leaving a group without being on one.'

尝试重启dax-mysql-slave节点组复制:

1
2
 start group_replication;
ERROR 3092 (HY000): The server is not configured properly to be an active member of the group. Please see more details on error log.

error.log日志报错:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2018-11-01T06:59:20.617005Z 114 [Note] 'CHANGE MASTER TO FOR CHANNEL 'group_replication_applier' executed'. Previous state master_host='<NULL>', master_port= 0, master_log_file='', master_log_pos= 128090715, master_bind=''. New state master_host='<NULL>', master_port= 0, master_log_file='', master_log_pos= 4, master_bind=''.
2018-11-01T06:59:21.516164Z 111 [Note] Plugin group_replication reported: 'Group Replication applier module successfully initialized!'
2018-11-01T06:59:21.516264Z 117 [Note] Slave SQL thread for channel 'group_replication_applier' initialized, starting replication in log 'FIRST' at position 0, relay log './relay-log-group_replication_applier.000002' position: 4
2018-11-01T06:59:21.516277Z 111 [Note] Plugin group_replication reported: 'auto_increment_increment is set to 7'
2018-11-01T06:59:21.516298Z 111 [Note] Plugin group_replication reported: 'auto_increment_offset is set to 3306102'
2018-11-01T06:59:21.553616Z 0 [Note] Plugin group_replication reported: 'XCom protocol version: 3'
2018-11-01T06:59:21.553718Z 0 [Note] Plugin group_replication reported: 'XCom initialized and ready to accept incoming connections on port 24901'
2018-11-01T06:59:21.620998Z 0 [ERROR] Plugin group_replication reported: '[GCS] Error on opening a connection to dax-mysql-master:24901 on local port: 24901.'
2018-11-01T06:59:21.621468Z 0 [ERROR] Plugin group_replication reported: '[GCS] Error on opening a connection to dax-mysql-master:24901 on local port: 24901.'
2018-11-01T06:59:21.621794Z 0 [ERROR] Plugin group_replication reported: '[GCS] Error on opening a connection to dax-mysql-master:24901 on local port: 24901.'
2018-11-01T06:59:21.621988Z 0 [ERROR] Plugin group_replication reported: '[GCS] Error on opening a connection to dax-mysql-master:24901 on local port: 24901.'
2018-11-01T06:59:21.622185Z 0 [ERROR] Plugin group_replication reported: '[GCS] Error on opening a connection to dax-mysql-master:24901 on local port: 24901.'
2018-11-01T06:59:21.622379Z 0 [ERROR] Plugin group_replication reported: '[GCS] Error on opening a connection to dax-mysql-master:24901 on local port: 24901.'
2018-11-01T06:59:21.622658Z 0 [ERROR] Plugin group_replication reported: '[GCS] Error on opening a connection to dax-mysql-master:24901 on local port: 24901.'
2018-11-01T06:59:21.622875Z 0 [ERROR] Plugin group_replication reported: '[GCS] Error on opening a connection to dax-mysql-master:24901 on local port: 24901.'
2018-11-01T06:59:21.623043Z 0 [ERROR] Plugin group_replication reported: '[GCS] Error on opening a connection to dax-mysql-master:24901 on local port: 24901.'
2018-11-01T06:59:21.623201Z 0 [ERROR] Plugin group_replication reported: '[GCS] Error on opening a connection to dax-mysql-master:24901 on local port: 24901.'
2018-11-01T06:59:21.623220Z 0 [ERROR] Plugin group_replication reported: '[GCS] Error connecting to all peers. Member join failed. Local port: 24901'
2018-11-01T06:59:21.624544Z 0 [Warning] Plugin group_replication reported: 'read failed'
2018-11-01T06:59:21.656139Z 0 [ERROR] Plugin group_replication reported: '[GCS] The member was unable to join the group. Local port: 24901'
2018-11-01T07:00:21.516998Z 111 [ERROR] Plugin group_replication reported: 'Timeout on wait for view after joining group'
2018-11-01T07:00:21.517630Z 111 [Note] Plugin group_replication reported: 'Requesting to leave the group despite of not being a member'
2018-11-01T07:00:21.517820Z 111 [ERROR] Plugin group_replication reported: '[GCS] The member is leaving a group without being on one.'
2018-11-01T07:00:21.519413Z 111 [Note] Plugin group_replication reported: 'auto_increment_increment is reset to 1'
2018-11-01T07:00:21.519442Z 111 [Note] Plugin group_replication reported: 'auto_increment_offset is reset to 1'
2018-11-01T07:00:21.521022Z 117 [Note] Error reading relay log event for channel 'group_replication_applier': slave SQL thread was killed
2018-11-01T07:00:21.595775Z 114 [Note] Plugin group_replication reported: 'The group replication applier thread was killed'
2018-11-01T07:10:22.699429Z 111 [Note] Aborted connection 111 to db: 'unconnected' user: 'root' host: 'localhost' (Got timeout reading communication packets)

使用mysql-shell查看集群信息:

1
/data/soft/mysql-shell/bin/mysqlsh --uri repl@dax-mysql-master:3306

尝试查看集群信息:

1
2
cluster = dba.getCluster("prodCluster")
Dba.getCluster: This function is not available through a session to a standalone instance (metadata exists, but GR is not active) (RuntimeError)

提示集群系统没有激活,尝试重启集群系统:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
dba.rebootClusterFromCompleteOutage('prodCluster')
Reconfiguring the cluster 'prodCluster' from complete outage...
The instance 'dax-mysql-slave:3306' was part of the cluster configuration.
Would you like to rejoin it to the cluster? [y/N]: y
WARNING: On instance 'dax-mysql-master:3306' membership change cannot be persisted since MySQL version 5.7.22 does not support the SET PERSIST command (MySQL version >= 8.0.5 required). Please use the <Dba>.configureLocalInstance command locally to persist the changes.
The cluster was successfully rebooted.
<Cluster:prodCluster>
MySQL dax-mysql-master:3306 JS > cluster = dba.getCluster("prodCluster")
<Cluster:prodCluster>
MySQL dax-mysql-master:3306 JS > cluster.status()
{
"clusterName": "prodCluster",
"defaultReplicaSet": {
"name": "default",
"primary": "dax-mysql-master:3306",
"ssl": "DISABLED",
"status": "OK_NO_TOLERANCE",
"statusText": "Cluster is NOT tolerant to any failures.",
"topology": {
"dax-mysql-master:3306": {
"address": "dax-mysql-master:3306",
"mode": "R/W",
"readReplicas": {},
"role": "HA",
"status": "ONLINE"
},
"dax-mysql-slave:3306": {
"address": "dax-mysql-slave:3306",
"mode": "R/O",
"readReplicas": {},
"role": "HA",
"status": "ONLINE"
}
}
},
"groupInformationSourceMember": "mysql://repl@dax-mysql-master:3306"
}

集群状态恢复正常。

点击阅读

[Mysql] mysql使用备份的.frm文件恢复表结构


0、例如备份的表为test_order,则备份出来的.frm文件为test_order.frm

1、在只知道表名的情况下,随意创建一个表名为test_order的表:

1
create table test_order (id1 int(2));

替换test_order.frm文件,替换完成之后重启mysql数据库,查看表信息

1
2
3
4
root@db 11:29:  [test]> desc test_order;
ERROR 1146 (42S02): Table 'test.test_order' doesn't exist
root@db 11:29: [test]> show create table test_order;
ERROR 1146 (42S02): Table 'test.test_order' doesn't exist

报错提示表不存在,之后查看error.log:

1
2
3
4
2018-11-01T11:29:10.359514Z 2 [Warning] InnoDB: Table test/test_order contains 1 user defined columns in InnoDB, but 25 columns in MySQL. Please check INFORMATION_SCHEMA.INNODB_SYS_COLUMNS and http://dev.mysql.com/doc/refman/5.7/en/innodb-troubleshooting.html for how to resolve the issue.
2018-11-01T11:29:10.359631Z 2 [Warning] InnoDB: Cannot open table test/test_order from the internal data dictionary of InnoDB though the .frm file for the table exists. Please refer to http://dev.mysql.com/doc/refman/5.7/en/innodb-troubleshooting.html for how to resolve the issue.
2018-11-01T11:29:25.514189Z 2 [Note] InnoDB: Table `test`.`test_order` is corrupted. Please drop the table and recreate it
2018-11-01T11:29:25.514313Z 2 [Warning] InnoDB: Cannot open table test/test_order from the internal data dictionary of InnoDB though the .frm file for the table exists. Please refer to http://dev.mysql.com/doc/refman/5.7/en/innodb-troubleshooting.html for how to resolve the issue.

报错提示手动创建的表只有1列,但是mysql中记录的表有25列

2、删除test_order表,并创建一个25列的test_order表:

1
2
3
root@db 11:40:  [test]> drop table test_order;
Query OK, 0 rows affected (0.06 sec)
root@db 11:40: [test]> create table test_order (id1 int(2),id2 int(2),id3 int(2),id4 int(2),id5 int(2),id6 int(2),id7 int(2),id8 int(2),id9 int(2),id10 int(2),id11 int(2),id12 int(2),id13 int(2),id14 int(2),id15 int(2),id16 int(2),id17 int(2),id18 int(2),id19 int(2),id20 int(2),id21 int(2),id22 int(2),id23 int(2),id24 int(2),id25 int(2));

替换test_order.frm文件,替换完成之后重启mysql数据库,查看表信息

1
2
3
4
5
6
7
8
root@db 11:41:  [test]> desc test_order;
ERROR 2013 (HY000): Lost connection to MySQL server during query
root@db 11:42: [test]> show create table test_order;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 2
Current database: test
ERROR 2013 (HY000): Lost connection to MySQL server during query

查看error.log:

1
2
3
4
5
6
7
8
9
10
11
2018-11-01T11:42:25.566095Z 2 [ERROR] Build InnoDB index translation table for Table ./test/test_order failed
2018-11-01T11:42:25.566445Z 2 [ERROR] Table ./test/test_order has no primary key in InnoDB data dictionary, but has one in MySQL! If you created the table with a MySQL version < 3.23.54 and did not define a primary key, but defined a unique key with all non-NULL columns, then MySQL internally treats that key as the primary key. You can fix this error by dump + DROP + CREATE + reimport of the table.
2018-11-01T11:42:25.566519Z 2 [Warning] Table ./test/test_order key_used_on_scan is 0 even though there is no primary key inside InnoDB.
2018-11-01T11:42:25.566576Z 2 [ERROR] InnoDB could not find key no 0 with name PRIMARY from dict cache for table test/test_order
11:42:25 UTC - mysqld got signal 11 ;
This could be because you hit a bug. It is also possible that this binary
or one of the libraries it was linked against is corrupt, improperly built,
or misconfigured. This error can also be caused by malfunctioning hardware.
Attempting to collect some information that could help diagnose the problem.
As this is a crash and something is definitely wrong, the information
collection process might fail.

报错提示test_order表没有主键

3、删除test_order表,并创建一个25列的带有主键的test_order表:

1
2
3
root@db 11:45:  [test]> drop table test_order;
Query OK, 0 rows affected (0.06 sec)
root@db 11:45: [test]> create table test_order (id1 int(2) primary key,id2 int(2),id3 int(2),id4 int(2),id5 int(2),id6 int(2),id7 int(2),id8 int(2),id9 int(2),id10 int(2),id11 int(2),id12 int(2),id13 int(2),id14 int(2),id15 int(2),id16 int(2),id17 int(2),id18 int(2),id19 int(2),id20 int(2),id21 int(2),id22 int(2),id23 int(2),id24 int(2),id25 int(2));

替换test_order.frm文件,替换完成之后重启mysql数据库,查看表信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
root@db 11:46:  [test]> desc test_order;
+-----------------+----------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+----------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| order_no | varchar(24) | NO | UNI | NULL | |
| broker_id | varchar(24) | NO | MUL | NULL | |
| broker_uid | bigint(20) | NO | | NULL | |
| plat_id | varchar(24) | NO | | NULL | |
| price_asset | varchar(8) | NO | | NULL | |
| test_type | varchar(16) | NO | | NULL | |
| order_type | varchar(16) | NO | | NULL | |
| price | decimal(32,20) | NO | | NULL | |
| number | decimal(32,20) | NO | | NULL | |
| test_asset | varchar(8) | NO | MUL | NULL | |
| testd_number | decimal(32,20) | NO | | NULL | |
| over_number | decimal(32,20) | NO | | NULL | |
| testd_money | decimal(32,20) | NO | | NULL | |
| fee_asset | varchar(8) | NO | | NULL | |
| fee | decimal(32,20) | NO | | NULL | |
| broker_fee | decimal(32,20) | NO | | NULL | |
| cloud_fee | decimal(32,20) | NO | | NULL | |
| client_order_no | varchar(36) | NO | | NULL | |
| state | varchar(16) | NO | | NULL | |
| send_state | varchar(16) | NO | | NULL | |
| error_code | varchar(24) | YES | | NULL | |
| error_msg | varchar(64) | YES | | NULL | |
| create_time | datetime | NO | | NULL | |
| update_time | datetime | NO | | NULL | |
+-----------------+----------------+------+-----+---------+----------------+
25 rows in set (0.01 sec)

root@db 11:46: [test]> show create table test_order\G
*************************** 1. row ***************************
Table: test_order
Create Table: CREATE TABLE `test_order` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`order_no` varchar(24) NOT NULL,
`broker_id` varchar(24) NOT NULL,
`broker_uid` bigint(20) NOT NULL,
`plat_id` varchar(24) NOT NULL,
`price_asset` varchar(8) NOT NULL,
`test_type` varchar(16) NOT NULL,
`order_type` varchar(16) NOT NULL,
`price` decimal(32,20) NOT NULL,
`number` decimal(32,20) NOT NULL,
`test_asset` varchar(8) NOT NULL,
`testd_number` decimal(32,20) NOT NULL,
`over_number` decimal(32,20) NOT NULL,
`testd_money` decimal(32,20) NOT NULL,
`fee_asset` varchar(8) NOT NULL,
`fee` decimal(32,20) NOT NULL,
`broker_fee` decimal(32,20) NOT NULL,
`cloud_fee` decimal(32,20) NOT NULL,
`client_order_no` varchar(36) NOT NULL,
`state` varchar(16) NOT NULL,
`send_state` varchar(16) NOT NULL,
`error_code` varchar(24) DEFAULT NULL,
`error_msg` varchar(64) DEFAULT NULL,
`create_time` datetime NOT NULL,
`update_time` datetime NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `test_order_order_no_uindex` (`order_no`),
UNIQUE KEY `test_order_broker_id_client_order_no_uindex` (`broker_id`,`client_order_no`),
KEY `multi_price_test_id_uid_state_index` (`test_asset`,`price_asset`,`broker_uid`,`broker_id`,`state`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
1 row in set (0.01 sec)

查看error.log有无其他报错:

1
2
3
4
5
6
2018-11-01T11:45:09.272752Z 2 [ERROR] Build InnoDB index translation table for Table ./test/test_order failed
2018-11-01T11:45:09.272904Z 2 [ERROR] InnoDB: MySQL and InnoDB data dictionaries are out of sync. Unable to find the AUTOINC column id in the InnoDB table `test`.`test_order`. We set the next AUTOINC column value to 0, in effect disabling the AUTOINC next value generation.
2018-11-01T11:45:09.272935Z 2 [Note] InnoDB: You can either set the next AUTOINC value explicitly using ALTER TABLE or fix the data dictionary by recreating the table.
2018-11-01T11:45:09.272959Z 2 [ERROR] InnoDB: Table test/test_order contains 1 indexes inside InnoDB, which is different from the number of indexes 4 defined in MySQL
2018-11-01T11:45:09.272976Z 2 [ERROR] InnoDB could not find key no 1 with name test_order_order_no_uindex from dict cache for table test/test_order
2018-11-01T11:45:09.272988Z 2 [ERROR] Table test/test_order contains fewer indexes inside InnoDB than are defined in the MySQL .frm file. Have you mixed up .frm files from different installations? Please refer to http://dev.mysql.com/doc/refman/5.7/en/innodb-troubleshooting.html for how to resolve the issue.

log日志依旧有报错,提示innodb只有一个索引,但mysql下存在4个索引,但是已经通过show create table test_order获取到了建表的语句,因此可以通过上面的建表语句,重新创建test_order表了。

点击阅读

[Linux] 使用certbot为域名生成免费证书(nginx版)


1、下载letencrypt,用于生产免费证书工具:

1
2
3
cd /data/soft
wget https://dl.eff.org/certbot-auto
chmod a+x certbot-auto

2、修改域名对应的配置文件,添加下面内容

1
2
3
4
5
6
7
8
9
10
server {
listen 80;
server_name test.com;
...
location ~ /.well-known {
root /data/soft;
allow all;
}
...
}

上面配置的 root /data/soft
信息目录最好不要与其他location指定的目录相同,且确保各个目录存在目录,如果目录相同的情况下可能会遇到以下问题:

1
2
3
4
5
6
7
8
9
10
11
[quote]Failed authorization procedure. test.com (http-01): urn:ietf:params:acme:error:unauthorized :: The client lacks sufficient authorization :: Invalid response from http://mydomain.fr/.well-known/acme-challenge/Xefe-sxGfexdcdezDEUJZRfexjfeeloekcdsesx [2001:1600:4:1::b]: 404
IMPORTANT NOTES:
The following errors were reported by the server:
Domain: test.com
Type: unauthorized
Detail: Invalid response from
http://mydomain.fr/.well-known/acme-challenge/Xefe-sxGfexdcdezDEUJZRfexjfeeloekcdsesx
[2001:1600:4:1::b]: 404
To fix these errors, please make sure that your domain name was
entered correctly and the DNS A/AAAA record(s) for that domain
contain(s) the right IP address.[/quote]

如果服务器的80端口被限制,会提示如下错误:

1
2
http://demo.broker.masterdax.com/.well-known/acme-challenge/pIV-Rh1355xsh8xJFHhB0llri6HS8S2yOSYRE9N5D5I:
Timeout during connect (likely firewall problem)

3、执行生成证书命令:

1
/data/soft/certbot-auto certonly --email 123456@qq.com --agree-tos --webroot -w /data/soft/ -d test.com --dry-run

第一次尝试生成证书最好加上–dry-run参数,如果le生成证书次数(包括报错的次数)每天有上限,添加–dry-run调试没有问题之后再真正生成证书

1
/data/soft/certbot-auto certonly --email 123456@qq.com --agree-tos --webroot -w /data/soft/ -d test.com

4、查看生成的证书

1
/data/soft/certbot-auto certificates

5、修改nginx配置文件,添加https相关配置信息,http相关配置加上跳转到https:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
server {
listen 80;
server_name test.com;
rewrite ^(.*)$ https://$server_name$1 permanent;
...
location ~ /.well-known {
root /data/soft;
allow all;
}
...
}

server {
listen 443 ssl;
server_name test.com;
....
ssl_certificate ssl/fullchain.pem;
ssl_certificate_key ssl/privkey.pem;
...
}

6、将生成的证书添加软连接到nginx配置文件指定的路径:

1
2
ln -s /etc/letsencrypt/live/test.com/fullchain.pem /usr/local/openresty/nginx/conf/ssl/fullchain.pem
ln -s /etc/letsencrypt/live/test.com/privkey.pem /usr/local/openresty/nginx/conf/ssl/privkey.pem

7、因为le生成的证书有效期为90天,需要添加定时任务,使其证书自动更新:

cat /data/soft/cron-cerbot.sh

1
2
#!/bin/bash
/data/soft/certbot-auto renew

授权文件执行权限:

1
chmod a+x /data/soft/cron-cerbot.sh

添加到crontab,每周日凌晨定期更新
crontab -l

1
0 0 * * 0 /data/soft/cron-cerbot.sh

点击阅读

[Oracle] oracle 静默安装


1、安装数据库所需依赖包

1
2
3
4
5
yum install -y gcc*
yum install -y glibc*
yum install -y compat-libstdc++-33 elfutils-libelf-devel libaio-devel compat-libcap1
yum install -y sysstat
yum install -y smartmontools

2、创建用户,并修改用户名密码

1
2
3
4
/usr/sbin/groupadd -g 505 oinstall
/usr/sbin/groupadd -g 502 dba
/usr/sbin/groupadd -g 503 oper
/usr/sbin/useradd -u 506 -g oinstall -G dba,oper oracle

配置用户名密码

1
passwd oracle

3、配置相关数据库目录权限

1
2
3
4
5
6
7
mkdir -p /data/u01/app/oracle
chown oracle.oinstall -R /data/u01/app/oracle
chmod 775 /data/u01/app/oracle
mkdir -p /data/u01/app/oracle/product/11.2.0.4/dbhome_1/
chown oracle.oinstall -R /data/u01/app/oracle/product/11.2.0.4/dbhome_1/
chmod 775 /data/u01/app/oracle/product/11.2.0.4/dbhome_1/
chown oracle.oinstall -R /data/u01

4、修改系统参数:

4.1、关闭selinux

1
setenforce 0

4.2、关闭防火墙

1
iptables -F

4.3、设置用户连接限制
cat /etc/security/limits.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
oracle soft nproc 16384
oracle hard nproc 16384
oracle soft nofile 65536
oracle hard nofile 65536
oracle soft memlock 8088608
oracle hard memlock 8088608
oracle hard stack 10240
grid soft nproc 16384
grid hard nproc 16384
grid soft nofile 65536
grid hard nofile 65536
grid soft memlock 8088608
grid hard memlock 8088608
grid hard stack 10240

oracle用户最大能开启的进程数不超过16384,最大能打开的文件数不超过65536。至于soft和hard的区别,不同于磁盘配额中的软限制和硬限制。普通用户可以调整自己的softlimit但最高不能超过hardlimit,而且除了root以外的普通用户也不能够随意更改hard limit。该调整完成之后一般可以使用ulimit命令查看。
针对nofile,这个只是基于用户层面的限制和调整方法。基于系统层面的限制和调整方法是修改/etc/sysctl.conf文件,直接改fs.file-max参数,调整之后sysctl –p生效
memlock参数随着服务器内存不同,进行调整,该参数要偏小于实际的物理内存,本文以(8g内存为例)

4.4、为使/etc/security/limits.conf文件配置生效,需要确保pam模块pam_limits.so被加入到启动文件中:
grep ‘pam_limits.so’ /etc/pam.d/login

1
session    required     pam_limits.so

pam_limits.so模块对用户使用系统资源的情况进行限制,也可以使用在对一般应用程序使用的资源限制方面。举例来说,如果需要在SSH服务器上对来自不同用户的ssh访问进行限制,就可以调用该模块来实现相关功能。例如,当需要限制用户admin登录到SSH服务器时的最大连接数(防止同一个用户开启过多的登录进程),就可以在/etc/pam.d/sshd 文件中增加一行对 pam_limits.so 模块的调用:
然后在/etc/security/limits.conf文件中增加一行对admin用户产生的连接数进行限定:
admin hard maxlogins 2
完成之后重启服务器端的sshd服务。
之后我们可以看到,从客户端以admin身份登录SSH服务器时,在客户端上可以打开两个控制台登录。但当客户端开启第三个登录窗口的时候会被服务器拒绝,但其它用户不会受到限制。

4.5、设置用户最大进程数和进程可以打开的最大文件描述符的数量:
cat /etc/profile

1
2
3
4
5
6
7
8
if [ $USER = "oracle" -o $USER = "grid" ]; then
if [ $SHELL = "/bin/ksh" ]; then
ulimit -p 16384
ulimit -n 65536
else
ulimit -u 16384 -n 65536
fi
fi

4.6、设置内核参数
cat /etc/sysctl.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
kernel.shmmax = 7730941132
kernel.shmall = 15586574
kernel.shmmni = 4096
kernel.sem = 250 32000 100 128
fs.file-max = 6815744
fs.aio-max-nr = 1048576
net.ipv4.ip_local_port_range = 9000 65500
net.core.rmem_default = 1048576
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 1048576
vm.min_free_kbytes = 524288
vm.swappiness = 60

使配置生效

1
sysctl -p

安装过程中shmmax,shmall参数设置过小,导致dbca创建数据库的时候报错ORA-12547:TNS:lost contact
图形化安装,oracle有一个runfix脚本,调整这些参数,参考response下的文件,如果文件里面的参数小于参考文件的参数,会进行调整,如果文件参数大于参考文件参数,则不会进行调整。

SHMMAX应该比SGA区大,否则会引发性能的下降,shmmax 指的是单个共享内存段的最大尺寸, 设置shmmax=1G,sga分配了1.2G,当启动实例的时候就分配 2 块共享内存给Oracle

kernel.shmall=(SHMMAX/PAGE_SIZE):共享内存总量,以页为单位。Linux 共享内存页大小为4KB, 共享内
存段的大小都是共享内存页大小的整数倍。一个共享内存段的最大大小是16G,那么需
要共享内存页数是 16GB/4KB=16777216KB/4KB=4194304 (页),也就是64Bit 系统下
16GB 物理内存,设置 kernel.shmall = 4194304 才符合要求

SHMMIN= 最小的内存段的大小

kernel.shmmni:共享内存段的最大数量,shmmni 缺省值 4096 ,一般肯定是够用了

kernel.sem(SEMMSL SEMMNS SEMOPM SEMMNI):
SEMMSL,每个信号量集中的最大信号量数,应该设置为服务器中各个实例中PROCESSES参数的和+10;SEMMNS,系统中信号量的最大数,参数应设置为SEMMSL*SEMMNI。
SEMOPM,每个信号量调用所包含的最大操作数。
SEMMNI:系统中信号量集的最大数。
swappiness的值的大小对如何使用swap分区是有着很大的联系的。swappiness=0的时候表示最大限度使用物理内存,然后才是 swap空间,swappiness=100的时候表示积极的使用swap分区,并且把内存上的数据及时的搬运到swap空间里面。linux的基本默认设置为60

5、修改oracle用户环境变量

su - oracle
vim ~/.bash_profile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
export THREADS_FLAG=native
export ORACLE_BASE=/data/u01/app/oracle
export ORACLE_HOME=$ORACLE_BASE/product/11.2.0.4/dbhome_1
export PATH=$ORACLE_HOME/bin:$ORACLE_HOME/OPatch:$PATH:/usr/sbin
export LD_LIBRARY_PATH=$ORACLE_HOME/lib
export ORACLE_SID=boston
export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK
export NLS_DATE_FORMAT="YYYY:MM:DD HH24:MI:SS"
export EDITOR=vi
set -o vi
umask 022
alias bdump="cd $ORACLE_BASE/diag/rdbms/otcdb/${ORACLE_SID}/trace"
alias sql='sqlplus "/ as sysdba"'

使环境变量生效

1
source ~/.bash_profile

6、解压软件包,在database/response目录下找到db_install.rsp文件

编辑db_install.rsp文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#选择安装类型:1.INSTALL_DB_SWONLY只装数据库软件 2.INSTALL_DB_AND_CONFIG安装数据库软件并建库 3.UPGRADE_DB升级数据库
oracle.install.option=INSTALL_DB_SWONLY
#指定操作系统主机名,通过hostname命令获得
ORACLE_HOSTNAME=dax-mysql-slave
#指定oracle inventory目录的所有者,通常会是oinstall或者dba
UNIX_GROUP_NAME=oinstall
#指定产品清单oracle inventory目录的路径,如果是Win平台下可以省略
INVENTORY_LOCATION=/data/u01/app/oracle/oraInventory
#指定数据库语言,可以选择多个,用逗号隔开。选择en, zh_CN(英文和简体中文)
SELECTED_LANGUAGES=en,zh_CN
# Specify the complete path of the OracleHome.设置ORALCE_HOME的路径
ORACLE_HOME=/data/u01/app/oracle/product/11.2.0.4/dbhome_1
# Specify the complete path of the OracleBase. 设置ORALCE_BASE的路径
ORACLE_BASE=/data/u01/app/oracle
#选择Oracle安装数据库软件的版本(企业版,标准版,标准版1),不同的版本功能不同
oracle.install.db.InstallEdition=EE
#是否自定义Oracle的组件,如果选择false,则会使用默认的组件
#如果选择true否则需要自己在下面一条参数将要安装的组件一一列出。
#安装相应版权后会安装所有的组件,后期如果缺乏某个组件,再次安装会非常的麻烦。
oracle.install.db.EEOptionsSelection=false
# oracle.install.db.EEOptionsSelection=true的话下面的安装组件会安装
oracle.install.db.optionalComponents=oracle.rdbms.partitioning:11.2.0.4.0,oracle.oraolap:11.2.0.4.0,oracle.rdbms.dm:11.2.0.4.0,oracle.rdbms.dv:11.2.0.4.0,oracle.rdbms.lbac:11.2.0.4.0,oracle.rdbms.rat:11.2.0.4.0
#指定拥有OSDBA、OSOPER权限的用户组,通常会是dba组
oracle.install.db.DBA_GROUP=dba
oracle.install.db.OPER_GROUP=oinstall
#如果是RAC的安装,在这里指定所有的节点
oracle.install.db.CLUSTER_NODES=
oracle.install.db.isRACOneInstall=
oracle.install.db.isRACOneInstall=
--------安装数据库选项
#选择数据库的用途,一般用途/事物处理,数据仓库
oracle.install.db.config.starterdb.type=GENERAL_PURPOSE
# Specify the Starter Database GlobalDatabase Name. 指定GlobalName
oracle.install.db.config.starterdb.globalDBName=boston
# Specify the Starter Database SID.指定SID
oracle.install.db.config.starterdb.SID=boston
#选择字符集。不正确的字符集会给数据显示和存储带来麻烦无数。
#通常中文选择的有ZHS16GBK简体中文库,建议选择unicode的AL32UTF8国际字符集
oracle.install.db.config.starterdb.characterSet=AL32UTF8
#11g的新特性自动内存管理,也就是SGA_TARGET和PAG_AGGREGATE_TARGET都#不用设置了,Oracle会自动调配两部分大小。
oracle.install.db.config.starterdb.memoryOption=true
#指定Oracle自动管理内存的大小,最小是256MB
oracle.install.db.config.starterdb.memoryLimit=5120
#是否载入模板示例
oracle.install.db.config.starterdb.installExampleSchemas=false
# These settings may also be disabled. 是否启用安全设置
oracle.install.db.config.starterdb.enableSecuritySettings=true
#设置数据库用户密码
oracle.install.db.config.starterdb.password.ALL=oracle
#数据库本地管理工具DB_CONTROL,远程集中管理工具GRID_CONTROL
oracle.install.db.config.starterdb.control=DB_CONTROL
#.设置自动备份,和OUI里的自动备份一样。
oracle.install.db.config.starterdb.automatedBackup.enable=false
#自动备份会启动一个job,指定启动JOB的系统用户ID
oracle.install.db.config.starterdb.automatedBackup.osuid=
#自动备份会开启一个job,需要指定OSUser的密码
oracle.install.db.config.starterdb.automatedBackup.ospwd=
#自动备份,要求指定使用的文件系统存放数据库文件还是ASM
oracle.install.db.config.starterdb.storageType=
#使用文件系统存放数据库文件才需要指定数据文件、控制文件、Redo log的存放目录
oracle.install.db.config.starterdb.fileSystemStorage.dataLocation=
#使用文件系统存放数据库文件才需要指定备份恢复目录
oracle.install.db.config.starterdb.fileSystemStorage.recoveryLocation=
#使用ASM存放数据库文件才需要指定存放的磁盘组
oracle.install.db.config.asm.diskGroup=
#使用ASM存放数据库文件才需要指定ASM实例密码
oracle.install.db.config.asm.ASMSNMPPassword=
#指定metalink账户用户名
MYORACLESUPPORT_USERNAME=
# 指定metalink账户密码
MYORACLESUPPORT_PASSWORD=
# 用户是否可以设置metalink密码
SECURITY_UPDATES_VIA_MYORACLESUPPORT=
# False表示不需要设置安全更新,注意,在11.2的静默安装中疑似有一个BUG
******# Response File中必须指定为true,否则会提示错误,不管是否正确填写了邮件地址
DECLINE_SECURITY_UPDATES=true
#代理服务器名
PROXY_HOST=
#代理服务器端口
PROXY_PORT=
#代理服务器用户名
PROXY_USER=
#代理服务器密码
PROXY_PWD=

开始安装:

1
./runInstaller -silent -responseFile /data/soft/database/response/db_install.rsp

如果是测试环境可能碰到下面的问题:

1
2
3
4
5
6
7
8
9
10
Checking swap space: 0 MB available, 150 MB required.    Failed <<<<
解决方法:
dd if=/dev/zero of=/data/swapfile bs=1M count=1024
mkswap /data/swapfile
swapon /data/swapfile

在/etc/fstab添加下面内容
/data/swapfile swap swap defaults 0 0
>>>>>
`

oracle安装到最后提示,使用root用户执行以下两个脚本:

1
2
/data/u01/app/oracle/oraInventory/orainstRoot.sh
/data/u01/app/oracle/product/11.2.0.4/dbhome_1/root.sh

7、静默配置监听

通过response文件运行netca, 生成sqlnet.ora和listener.ora文件, 位于$ORACLE_HOME/network/admin目录下:

1
2
3
4
# su - oracle
$ORACLE_HOME/bin/netca -silent -responsefile /data/soft/database/response/netca.rsp
ll $ORACLE_HOME/network/admin/*.ora
lsnrctl status

8、创建数据库

修改配置文件dbca.rsp

1
2
3
4
5
6
7
8
9
10
11
12
13
[GENERAL]
RESPONSEFILE_VERSION = "11.2.0"
OPERATION_TYPE = "createDatabase"
[CREATEDATABASE]
GDBNAME = "boston.us.oracle.com"
TEMPLATENAME = "General_Purpose.dbc"
SID = "boston"
SYSPASSWORD = "oracle"
SYSTEMPASSWORD = "oracle"
SYSMANPASSWORD = "oracle"
DBSNMPPASSWORD = "oracle"
CHARACTERSET = "ZHS16GBK"
TOTALMEMORY = "2048"

运行命令开始创建数据库:

1
dbca -silent -createDatabase -responseFile /data/soft/database/response/createdbca.rsp

如果不适用配置文件,也可使用下面命令直接指定参数:

1
##dbca -silent -createDatabase -templateName $ORACLE_HOME/assistants/dbca/templates/General_Purpose.dbc -gdbName orcogg -sid orcogg  -sysPassword oracle -systemPassword oracle -datafileDestination /u01/app/oradata/orcogg -characterSet GBK16 -TOTALMEMORY 2048

各参数含义如下:
-silent 表示以静默方式安装
-responseFile 表示使用哪个响应文件,必需使用绝对路径
RESPONSEFILE_VERSION 响应文件模板的版本,该参数不要更改
OPERATION_TYPE 安装类型,该参数不要更改
GDBNAME 全局数据库名,点号前面默认是db_name,点号后面默认就是db_domain
TEMPLATENAME 建库模板名,参考各模板定义:$ORACLE_HOME/assistants/dbca/templates/*.dbc
CHARACTERSET 字符集,默认是WE8MSWIN1252
TOTALMEMORY 实例内存,默认是服务器物理内存的40%

点击阅读

[Mysql] 测试mysql where条件执行顺序对sql查询效率的影响


1、创建基表测试数据

1
2
3
4
5
6
7
8
CREATE TABLE `test1` (
`id` int(7) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`age` int(4) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=37 DEFAULT CHARSET=utf8mb4;

insert into test1(name,age) values('lucy',10),('bobo',18),('david',20),('tom',21),('dobu',22),('dali',12);

2、创建中间表

1
2
3
4
5
6
 CREATE TABLE `testfororder` (
`id` int(7) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`age` int(4) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14699990 DEFAULT CHARSET=utf8mb4;

创建存储过程对中间表插入数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
drop procedure if exists test;
delimiter //
create procedure test()
begin
declare i int;
set i=0;
while i<300000 do
insert into testfororder(newid,name,age) select concat(i,name),FLOOR(18 + (RAND() * 12)) from test1;
set i=i+1;
end while;
end//
delimiter ;
调用存储过程:
call test();

3、创建测试表:

1
2
3
4
5
6
7
8
9
CREATE TABLE `sequence` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`newid` int(10) NOT NULL,
`name` varchar(20) DEFAULT NULL,
`age` int(4) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=89179437 DEFAULT CHARSET=utf8mb4;

insert into sequence(newid,name,age) select * from testfororder;

数据量根据自己需求可以多次导入

4、查看sequence测试表数据:

1
2
3
4
5
6
7
select count(*) from sequence;
+----------+
| count(*) |
+----------+
| 12600000 |
+----------+
1 row in set (2.83 sec)

age条件筛选数据量

1
2
3
4
5
6
7
select count(*) from sequence where age in (26,19,22,20,28,29,25);
+----------+
| count(*) |
+----------+
| 7341992 |
+----------+
1 row in set (3.82 sec)

newid条件筛选数量

1
2
3
4
5
6
7
select count(*) from sequence where newid in (116670,116677,116684,116691,116698,116705,116719,116726);
+----------+
| count(*) |
+----------+
| 56 |
+----------+
1 row in set (3.28 sec)

5、where条件(age,newid)在没有创建索引的情况下:

age在前,newid在后,查看执行计划:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
explain select * from sequence where age in (26,19,22,20,28,29,25) and newid in (116670,116677,116684,116691,116698,116705,116719,116726);
+----+-------------+----------+------------+------+---------------+------+---------+------+----------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+------+---------------+------+---------+------+----------+----------+-------------+
| 1 | SIMPLE | sequence | NULL | ALL | NULL | NULL | NULL | NULL | 12292293 | 25.00 | Using where |
+----+-------------+----------+------------+------+---------------+------+---------+------+----------+----------+-------------+
1 row in set, 1 warning (0.01 sec)

select * from sequence where age in (26,19,22,20,28,29,25) and newid in (116670,116677,116684,116691,116698,116705,116719,116726);
+----------+--------+-----------+------+
| id | newid | name | age |
+----------+--------+-----------+------+
| 100003 | 116670 | 2381lucy | 26 |
...
| 76679487 | 116719 | 2382lucy | 29 |
+----------+--------+-----------+------+
49 rows in set (5.36 sec)

newid在前,age在后:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
explain select * from sequence where  newid in (116670,116677,116684,116691,116698,116705,116719,116726) and age in (26,19,22,20,28,29,25);
+----+-------------+----------+------------+------+---------------+------+---------+------+----------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+------+---------------+------+---------+------+----------+----------+-------------+
| 1 | SIMPLE | sequence | NULL | ALL | NULL | NULL | NULL | NULL | 12292293 | 25.00 | Using where |
+----+-------------+----------+------------+------+---------------+------+---------+------+----------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

select * from sequence where newid in (116670,116677,116684,116691,116698,116705,116719,116726) and age in (26,19,22,20,28,29,25);
+----------+--------+-----------+------+
| id | newid | name | age |
+----------+--------+-----------+------+
| 100003 | 116670 | 2381lucy | 26 |
...
| 76679487 | 116719 | 2382lucy | 29 |
+----------+--------+-----------+------+
49 rows in set (4.47 sec)

没有任何索引的情况下,where后面跟的条件从左到右,返回数据越小的条件在前面,效率会优于返回数据较大的条件在前面。

6、where条件(age)在没有创建索引,newid创建索引的情况下:

创建newid的索引:

1
create index newid_ind on sequence(newid);

age在前,newid在后:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
explain select * from sequence where age in (26,19,22,20,28,29,25) and newid in (116670,116677,116684,116691,116698,116705,116719,116726);
+----+-------------+----------+------------+-------+---------------+-----------+---------+------+------+----------+------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+-------+---------------+-----------+---------+------+------+----------+------------------------------------+
| 1 | SIMPLE | sequence | NULL | range | newid_ind | newid_ind | 4 | NULL | 56 | 50.00 | Using index condition; Using where |
+----+-------------+----------+------------+-------+---------------+-----------+---------+------+------+----------+------------------------------------+
1 row in set, 1 warning (0.01 sec)

select * from sequence where age in (26,19,22,20,28,29,25) and newid in (116670,116677,116684,116691,116698,116705,116719,116726);
+----------+--------+-----------+------+
| id | newid | name | age |
+----------+--------+-----------+------+
| 100003 | 116670 | 2381lucy | 26 |
...
| 76679487 | 116719 | 2382lucy | 29 |
+----------+--------+-----------+------+
49 rows in set (0.01 sec)

newid在前,age在后:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
explain select * from sequence where  newid in (116670,116677,116684,116691,116698,116705,116719,116726) and age in (26,19,22,20,28,29,25);
+----+-------------+----------+------------+-------+---------------+-----------+---------+------+------+----------+------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+-------+---------------+-----------+---------+------+------+----------+------------------------------------+
| 1 | SIMPLE | sequence | NULL | range | newid_ind | newid_ind | 4 | NULL | 56 | 50.00 | Using index condition; Using where |
+----+-------------+----------+------------+-------+---------------+-----------+---------+------+------+----------+------------------------------------+
1 row in set, 1 warning (0.01 sec)

select * from sequence where newid in (116670,116677,116684,116691,116698,116705,116719,116726) and age in (26,19,22,20,28,29,25);
+----------+--------+-----------+------+
| id | newid | name | age |
+----------+--------+-----------+------+
| 100003 | 116670 | 2381lucy | 26 |
...
| 76679487 | 116719 | 2382lucy | 29 |
+----------+--------+-----------+------+
49 rows in set (0.00 sec)

在newid创建索引的情况下,where后面的查询条件前后顺序对查询效率影响不大。

7、where条件(newid)在没有创建索引,age创建索引的情况下:

删除newid的索引:

1
drop index newid_ind on sequence;

创建age的索引:

1
create index age_ind on sequence(age);

age在前,newid在后:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
explain select * from sequence where age in (26,19,22,20,28,29,25) and newid in (116670,116677,116684,116691,116698,116705,116719,116726);
+----+-------------+----------+------------+------+---------------+------+---------+------+----------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+------+---------------+------+---------+------+----------+----------+-------------+
| 1 | SIMPLE | sequence | NULL | ALL | age_ind | NULL | NULL | NULL | 12292293 | 50.00 | Using where |
+----+-------------+----------+------------+------+---------------+------+---------+------+----------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

select * from sequence where age in (26,19,22,20,28,29,25) and newid in (116670,116677,116684,116691,116698,116705,116719,116726);
+----------+--------+-----------+------+
| id | newid | name | age |
+----------+--------+-----------+------+
| 100003 | 116670 | 2381lucy | 26 |
...
| 76679487 | 116719 | 2382lucy | 29 |
+----------+--------+-----------+------+
49 rows in set (4.97 sec)

newid在前,age在后:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
explain select * from sequence where  newid in (116670,116677,116684,116691,116698,116705,116719,116726) and age in (26,19,22,20,28,29,25);
+----+-------------+----------+------------+------+---------------+------+---------+------+----------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+------+---------------+------+---------+------+----------+----------+-------------+
| 1 | SIMPLE | sequence | NULL | ALL | age_ind | NULL | NULL | NULL | 12292293 | 50.00 | Using where |
+----+-------------+----------+------------+------+---------------+------+---------+------+----------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

select * from sequence where newid in (116670,116677,116684,116691,116698,116705,116719,116726) and age in (26,19,22,20,28,29,25);
+----------+--------+-----------+------+
| id | newid | name | age |
+----------+--------+-----------+------+
| 100003 | 116670 | 2381lucy | 26 |
...
| 76679487 | 116719 | 2382lucy | 29 |
+----------+--------+-----------+------+
49 rows in set (4.82 sec)

创建age索引之后,执行计划会默认使用索引执行,但是因为age的筛选出来的数据量比较大,使用索引也不是特别理想。

8、where条件(newid,age)创建联合索引的情况下:

删除age索引,创建newid,age的联合索引:

1
2
drop index age_ind on sequence;
create index newid_age_ind on sequence(newid,age);

age在前,newid在后:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
explain select * from sequence where age in (26,19,22,20,28,29,25) and newid in (116670,116677,116684,116691,116698,116705,116719,116726);
+----+-------------+----------+------------+-------+---------------+---------------+---------+------+------+----------+-----------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+-------+---------------+---------------+---------+------+------+----------+-----------------------+
| 1 | SIMPLE | sequence | NULL | range | newid_age_ind | newid_age_ind | 9 | NULL | 98 | 100.00 | Using index condition |
+----+-------------+----------+------------+-------+---------------+---------------+---------+------+------+----------+-----------------------+

select * from sequence where age in (26,19,22,20,28,29,25) and newid in (116670,116677,116684,116691,116698,116705,116719,116726);
+----------+--------+-----------+------+
| id | newid | name | age |
+----------+--------+-----------+------+
| 100003 | 116670 | 2381lucy | 26 |
...
| 76679487 | 116719 | 2382lucy | 29 |
+----------+--------+-----------+------+
49 rows in set (0.00 sec)

newid在前,age在后:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
explain select * from sequence where  newid in (116670,116677,116684,116691,116698,116705,116719,116726) and age in (26,19,22,20,28,29,25);
+----+-------------+----------+------------+-------+---------------+---------------+---------+------+------+----------+-----------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+-------+---------------+---------------+---------+------+------+----------+-----------------------+
| 1 | SIMPLE | sequence | NULL | range | newid_age_ind | newid_age_ind | 9 | NULL | 98 | 100.00 | Using index condition |
+----+-------------+----------+------------+-------+---------------+---------------+---------+------+------+----------+-----------------------+
1 row in set, 1 warning (0.00 sec)

select * from sequence where newid in (116670,116677,116684,116691,116698,116705,116719,116726) and age in (26,19,22,20,28,29,25);
+----------+--------+-----------+------+
| id | newid | name | age |
+----------+--------+-----------+------+
| 100003 | 116670 | 2381lucy | 26 |
...
| 76679487 | 116719 | 2382lucy | 29 |
+----------+--------+-----------+------+
49 rows in set (0.01 sec)

联合索引存在的情况下,where后面的查询条件前后顺序对查询效率影响不大,但是创建了联合索引之后要注意查询条件问题,如果是以(newid,age)的顺序创建的联合索引,如果where查询条件后面有newid会使用联合索引,但是如果where查询条件后面之后age则不会引用联合索引:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
explain select * from sequence where  newid in (116670,116677,116684,116691,116698,116705,116719,116726);
+----+-------------+----------+------------+-------+---------------+---------------+---------+------+------+----------+-----------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+-------+---------------+---------------+---------+------+------+----------+-----------------------+
| 1 | SIMPLE | sequence | NULL | range | newid_age_ind | newid_age_ind | 4 | NULL | 56 | 100.00 | Using index condition |
+----+-------------+----------+------------+-------+---------------+---------------+---------+------+------+----------+-----------------------+
1 row in set, 1 warning (0.00 sec)

explain select * from sequence where age in (26,19,22,20,28,29,25);
+----+-------------+----------+------------+------+---------------+------+---------+------+----------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+------+---------------+------+---------+------+----------+----------+-------------+
| 1 | SIMPLE | sequence | NULL | ALL | NULL | NULL | NULL | NULL | 12292293 | 50.00 | Using where |
+----+-------------+----------+------------+------+---------------+------+---------+------+----------+----------+-------------+
1 row in set, 1 warning (0.01 sec)

点击阅读

[Mysql] 测试mysql or和in对sql查询效率的影响


1、创建基表测试数据

1
2
3
4
5
6
7
8
CREATE TABLE `test1` (
`id` int(7) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`age` int(4) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=37 DEFAULT CHARSET=utf8mb4;

insert into test1(name,age) values('lucy',10),('bobo',18),('david',20),('tom',21),('dobu',22),('dali',12);

2、创建中间表

1
2
3
4
5
6
 CREATE TABLE `testfororder` (
`id` int(7) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`age` int(4) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14699990 DEFAULT CHARSET=utf8mb4;

创建存储过程对中间表插入数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
drop procedure if exists test;
delimiter //
create procedure test()
begin
declare i int;
set i=0;
while i<300000 do
insert into testfororder(newid,name,age) select concat(i,name),FLOOR(18 + (RAND() * 12)) from test1;
set i=i+1;
end while;
end//
delimiter ;
调用存储过程:
call test();

3、创建测试表:

1
2
3
4
5
6
7
8
9
CREATE TABLE `sequence` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`newid` int(10) NOT NULL,
`name` varchar(20) DEFAULT NULL,
`age` int(4) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=89179437 DEFAULT CHARSET=utf8mb4;

insert into sequence(newid,name,age) select * from testfororder;

数据量根据自己需求可以多次导入

4、查看sequence测试表数据:

1
2
3
4
5
6
7
select count(*) from sequence;
+----------+
| count(*) |
+----------+
| 12600000 |
+----------+
1 row in set (2.83 sec)

5、在newid没有索引的情况下,in和or的对比:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
repl@db 16:14:  [aaaa]> select * from sequence where  newid in (116670,116677,116684,116691,116698,116705,116719,116726);
+----------+--------+-----------+------+
| id | newid | name | age |
+----------+--------+-----------+------+
| 100003 | 116670 | 2381lucy | 26 |
...
| 76679494 | 116726 | 2382bobo | 21 |
+----------+--------+-----------+------+
56 rows in set (4.47 sec)

select * from sequence where newid =116670 or newid = 116677 or newid = 116684 or newid =116691 or newid = 116698 or newid=116705 or newid=116719 or newid=116726;
+----------+--------+-----------+------+
| id | newid | name | age |
+----------+--------+-----------+------+
| 100003 | 116670 | 2381lucy | 26 |
...
| 76679494 | 116726 | 2382bobo | 21 |
+----------+--------+-----------+------+
56 rows in set (6.77 sec)

没有索引的情况下使用in查询效率高于or

6、创建newid索引,再次进行测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 create index newid_ind on sequence(newid);
select * from sequence where newid in (116670,116677,116684,116691,116698,116705,116719,116726);
+----------+--------+-----------+------+
| id | newid | name | age |
+----------+--------+-----------+------+
| 100003 | 116670 | 2381lucy | 26 |
...
| 76679494 | 116726 | 2382bobo | 21 |
+----------+--------+-----------+------+
56 rows in set (0.00 sec)

select * from sequence where newid =116670 or newid = 116677 or newid = 116684 or newid =116691 or newid = 116698 or newid=116705 or newid=116719 or newid=116726;
+----------+--------+-----------+------+
| id | newid | name | age |
+----------+--------+-----------+------+
| 100003 | 116670 | 2381lucy | 26 |
...
| 76679494 | 116726 | 2382bobo | 21 |
+----------+--------+-----------+------+
56 rows in set (0.01 sec)

在newid存在索引的情况下,使用or和in查询对sql查询效率影响较小。

点击阅读

[Mysql] Old incarnation found while trying to add node


0、集群环境介绍

1
2
3
4
mysql-mgr单主集群:
mw-mysql-1:primary(节点1)
mw-mysql-2:second (节点2)
mw-mysql-3:second (节点3)

1、收到zabbix报警mysql-mgr集群有一个节点down,查看节点信息,内容如下

1
2
3
4
5
6
7
8
select * from performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | 77039511-8e42-11e8-b6d4-000d3aa1a189 | mw-mysql-1 | 3306 | ONLINE |
| group_replication_applier | 81143c99-8e3d-11e8-a501-000d3aa1b575 | mw-mysql-2 | 3306 | ONLINE |
| group_replication_applier | 8bab920f-8e3d-11e8-a045-000d3aa09b70 | mw-mysql-3 | 3306 | UNREACHABLE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+

因为还有两个节点节点存活,表示集群还是可用的。(后面才发现只依靠这个信息是错误的)

2、节点3集群状态变为不可达,查看节点3的日志:

1
2
3
4
5
6
7
 waited (count) when Workers occupied = 590 waited when Workers occupied = 11412795300
2018-10-27T06:56:10.116159Z 0 [Warning] Plugin group_replication reported: 'The member with address mw-mysql-2:3306 has already sent the stable set. Therefore discarding the second message.'
xdr_bytes: out of memory
xdr_bytes: out of memory
xdr_bytes: out of memory
2018-10-27T06:58:03.284079Z 0 [Note] Plugin group_replication reported: 'dispatch_op /export/home/pb2/build/sb_0-27500212-1520171728.22/mysql-5.7.22/rapid/plugin/group_replication/libmysqlgcs/src/bindings/xcom/xcom/xcom_base.c:3810 die_op executed_msg={53f6a873 47559528 0} delivered_msg={53f6a873 47559528 0} p->synode={53f6a873 47559501 0} p->delivered_msg={53f6a873 47559525 0} p->max_synode={53f6a873 47559528 1} '
2018-10-27T06:58:03.285626Z 0 [Note] Plugin group_replication reported: 'dispatch_op /export/home/pb2/build/sb_0-27500212-1520171728.22/mysql-5.7.22/rapid/plugin/group_replication/libmysqlgcs/src/bindings/xcom/xcom/xcom_base.c:3810 die_op executed_msg={53f6a873 47559528 0} delivered_msg={53f6a873 47559528 0} p->synode={53f6a873 47559501 0} p->delivered_msg={53f6a873 47559525 0} p->max_synode={53f6a873 47559528 2} '

3、节点3因为内存不足导致节点集群进程down掉,重新启动mysql,尝试将节点3加入集群,提示加入失败:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2018-10-27T07:49:40.493156Z 33 [Note] Plugin group_replication reported: 'Group communication SSL configuration: group_replication_ssl_mode: "DISABLED"'
2018-10-27T07:49:40.493724Z 33 [Note] Plugin group_replication reported: '[GCS] Added automatically IP ranges 10.1.150.12/24,10.1.150.16/24,127.0.0.1/8 to the whitelist'
2018-10-27T07:49:40.494212Z 33 [Note] Plugin group_replication reported: '[GCS] Translated 'mw-mysql-3' to 10.1.150.16'
2018-10-27T07:49:40.494374Z 33 [Warning] Plugin group_replication reported: '[GCS] Automatically adding IPv4 localhost address to the whitelist. It is mandatory that it is added.'
2018-10-27T07:49:40.494426Z 33 [Note] Plugin group_replication reported: '[GCS] SSL was not enabled'
2018-10-27T07:49:40.494453Z 33 [Note] Plugin group_replication reported: 'Initialized group communication with configuration: group_replication_group_name: "9275d4e4-8e42-11e8-b217-000d3aa1a189"; group_replication_local_address: "mw-mysql-3:24901"; group_replication_group_seeds: "mw-mysql-1:24901,mw-mysql-2:24901,mw-mysql-3:24901"; group_replication_bootstrap_group: false; group_replication_poll_spin_loops: 0; group_replication_compression_threshold: 1000000; group_replication_ip_whitelist: "AUTOMATIC"'
2018-10-27T07:49:40.494471Z 33 [Note] Plugin group_replication reported: '[GCS] Configured number of attempts to join: 0'
2018-10-27T07:49:40.494476Z 33 [Note] Plugin group_replication reported: '[GCS] Configured time between attempts to join: 5 seconds'
2018-10-27T07:49:40.494526Z 33 [Note] Plugin group_replication reported: 'Member configuration: member_id: 3306101; member_uuid: "8bab920f-8e3d-11e8-a045-000d3aa09b70"; single-primary mode: "true"; group_replication_auto_increment_increment: 1; '
2018-10-27T07:49:40.494937Z 94 [Note] 'CHANGE MASTER TO FOR CHANNEL 'group_replication_applier' executed'. Previous state master_host='<NULL>', master_port= 0, master_log_file='', master_log_pos= 4, master_bind=''. New state master_host='<NULL>', master_port= 0, master_log_file='', master_log_pos= 4, master_bind=''.
2018-10-27T07:49:40.545004Z 97 [Note] Slave SQL thread for channel 'group_replication_applier' initialized, starting replication in log 'FIRST' at position 0, relay log './relay-log-group_replication_applier.000508' position: 4
2018-10-27T07:49:40.545034Z 33 [Note] Plugin group_replication reported: 'Group Replication applier module successfully initialized!'
2018-10-27T07:49:40.545059Z 33 [Note] Plugin group_replication reported: 'auto_increment_increment is set to 1'
2018-10-27T07:49:40.545063Z 33 [Note] Plugin group_replication reported: 'auto_increment_offset is set to 3306101'
2018-10-27T07:49:40.545486Z 0 [Note] Plugin group_replication reported: 'XCom protocol version: 3'
2018-10-27T07:49:40.545513Z 0 [Note] Plugin group_replication reported: 'XCom initialized and ready to accept incoming connections on port 24901'
2018-10-27T07:49:40.762663Z 0 [Warning] Plugin group_replication reported: 'read failed'
2018-10-27T07:49:40.780216Z 0 [ERROR] Plugin group_replication reported: '[GCS] The member was unable to join the group. Local port: 24901'

4、因为在节点3查不到任何有用的报错信息,尝试在节点1查看有没有其他报错,看到一条比较奇怪的报错:

1
[Note] Plugin group_replication reported: 'Old incarnation found while trying to add node mw-mysql-3:24901 15406269616484810.'

官方文档没有关于这个信息的任何提示,在这个链接https://dba.stackexchange.com/questions/214779/how-to-delete-previous-incarnation-in-mysql-w-group-replication查到碰到这个问题只能重启集群。

5、查看节点2的信息,error.log没有任何更新,正常情况下如果集群节点正常应该会每个120s左右,会刷新一下信息:

1
[Note] Multi-threaded slave statistics for channel 'group_replication_applier': seconds elapsed = 131; events assigned = 3687425; worker queues filled over overrun level = 0; waited due a Worker queue full = 0; waited due the total size = 0; waited at clock conflicts = 65388998400 waited (count) when Workers occupied = 25728 waited when Workers occupied = 124142987600

6、查看节点1应用连接是正常的,但是dml操作一直hang主,没有任何结果,重新启动应用程序问题照旧。感觉集群状态虽然查询正常,但是已经不能提供对外服务了,最后决定把所有的库stop,重新集群系统,最后集群恢复正常,对外提供服务正常,查询集群服务正常:

1
2
3
4
5
6
7
8
select * from performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | 77039511-8e42-11e8-b6d4-000d3aa1a189 | mw-mysql-1 | 3306 | ONLINE |
| group_replication_applier | 81143c99-8e3d-11e8-a501-000d3aa1b575 | mw-mysql-2 | 3306 | ONLINE |
| group_replication_applier | 8bab920f-8e3d-11e8-a045-000d3aa09b70 | mw-mysql-3 | 3306 | ONLINE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+

点击阅读

[Mysql] mysql数据库使用innobackupex和mysqldump备份恢复的对比


1、查看原库数据文件大小:

1
2
du -sh *|grep data
23G data

2、使用innodbackex备份数据

在percona官方下载xtrabackup软件,并解压

1
2
wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.9/binary/tarball/percona-xtrabackup-2.4.9-Linux-x86_64.tar.gz
tar -zxvf percona-xtrabackup-2.4.9-Linux-x86_64.tar.gz

开始执行备份,指定配置文件、用户名、密码、端口、备份的目录

1
./innobackupex --defaults-file=/etc/my.cnf --user=root --password=12345678 --port=3306 /data/

(总体耗时25分钟左右,备份文件大小23G)

3、采用innodbackex备份出来的数据去恢复,将数据文件拷贝到节点1,执行恢复操作:

先把事务日志恢复(–apply-log)

1
/data/soft/percona-xtrabackup/bin/innobackupex --defaults-file=/etc/my.cnf --user=root --password=12345678 --port=3306 --apply-log /data/2018-09-14_13-34-38

恢复数据之前需要清除数据目录下的所有数据:

1
rm -rf /data/mysql/data/*

开始恢复数据

1
/data/soft/percona-xtrabackup/bin/innobackupex --defaults-file=/etc/my.cnf --user=root --password=12345678 --port=3306 --copy-back --rsync /data/2018-09-14_13-34-38

数据恢复完成之后,删除datadir下的事务日志log文件(innodb引擎才会有)

1
2
shell > cd /data/mysql/data
shell > rm -rf ib_logfile*

设置数据目录权限

1
2
shell > chown -R mysql:mysql /data/mysql/data
shell > /data/mysql/bin/mysqld_safe --defaults-file=/data/mysql/etc/my.cnf &

启动节点1的mysql

1
/etc/init.d/mysql restart

恢复总体耗时(35分钟)

4、原库使用mysqldump方法备份一份数据,用于恢复节点2:

原库操作

1
/data/mysql/bin/mysqldump --all-databases --set-gtid-purged=ON --single-transaction -uroot -p12345678 > /db/test.dump

备份需要指定–set-gtid-purged=ON参数在备份文件中输出global.gtid_purged信息,
指定–single-transaction参数避免备份过程中产生锁
(备份耗时10分钟,备份文件大小11G)

5、将mysqldump备份出的数据拷贝到节点2,节点2进行恢复操作:

如果从库@@GLOBAL.GTID_EXECUTED值不为空需要执行reset master;

1
2
3
4
root@db 07:55:  [(none)]> stop group_replication;
Query OK, 0 rows affected (1.01 sec)
root@db 07:55: [(none)]> reset master;
Query OK, 0 rows affected (0.05 sec)

开始执行恢复

1
2
[root@dax-mysql-mha bin]# mysql -u root -p12345678 < /data/test.dump
mysql: [Warning] Using a password on the command line interface can be insecure.

总体耗时(60分钟左右)

6、关于使用mysqldump备份恢复和innobackupex备份恢复的比较:

1
2
3
数据文件目录大小23G情况下,mysqldump逻辑备份出来数据大小11G,备份耗时十分钟,恢复耗时一小时;
innobackupex物理备份备份出来数据大小23G,备份耗时25分钟,恢复耗时35分钟。
使用物理备份备份时间比逻辑备份时间较长,但恢复时间会小于逻辑备份时间,数据量比较大的情况下使用innobackupex会更好一点。

点击阅读

[Mysql] yum安装mysql-client客户端(centos7)


1、配置yum源

1
2
3
4
5
6
echo "[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/\$basearch/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql" > /etc/yum.repos.d/mysql-community.repo

2、查看配置文件是否正确:

1
more /etc/yum.repos.d/mysql-community.repo

3、安装mysql-client:

1
2
yum clean all
yum install mysql-community-client

点击阅读

Proudly powered by Hexo and Theme by Lap
本站访客数人次
© 2020 zeven0707's blog