MySQL Inception安裝使用

feelpurple發表於2018-04-10
Inception 是一個集稽核、執行、備份及生成回滾語句於一身的MySQL自動化運維工具,支援MySQL 5.5、5.6以及Percona等主流版本。

測試環境:CentOS 6.9
下載原始碼
[root@MySQL01 ~]# cd /fire/
[root@MySQL01 fire]# mkdir inception
[root@MySQL01 fire]# cd inception/
[root@MySQL01 inception]# git clone
Initialized empty Git repository in /fire/inception/inception/.git/
remote: Counting objects: 2018, done.
remote: Total 2018 (delta 0), reused 0 (delta 0), pack-reused 2018
Receiving objects: 100% (2018/2018), 11.80 MiB | 37 KiB/s, done.
Resolving deltas: 100% (522/522), done.

安裝依賴包
yum install bison
yum install cmake
yum install ncurses-devel
yum install openssl-devel
yum install gcc-c++ libgcc gcc

透過sh inception_build.sh debug指令碼方式安裝
[root@MySQL01 inception]# sh inception_build.sh debug
...
-- Installing: /fire/inception/inception/debug/mysql/man/man1/mysql.1
-- Installing: /fire/inception/inception/debug/mysql/man/man1/mysql_fix_extensions.1
-- Installing: /fire/inception/inception/debug/mysql/man/man1/mysqltest.1
-- Installing: /fire/inception/inception/debug/mysql/man/man1/mysql_client_test.1
-- Installing: /fire/inception/inception/debug/mysql/man/man1/mysql_waitpid.1
-- Installing: /fire/inception/inception/debug/mysql/man/man1/mysql.server.1
-- Installing: /fire/inception/inception/debug/mysql/man/man8/mysqld.8
-- Installing: /fire/inception/inception/debug/mysql/support-files/solaris/postinstall-solaris

預設會安裝在/fire/inception/inception/debug/mysql下面

編輯配置檔案

  1. [root@MySQL01 script]# vim /etc/inc.cnf
  2. [inception]
  3. general_log=1
  4. general_log_file=/var/inception/log/inception.log
  5. port=6690
  6. socket=/fire/inception/inception/debug/mysql/inc.socket
  7. character-set-client-handshake=0
  8. character-set-server=utf8

  9. # MySQL支援字符集
  10. inception_support_charset=utf8

  11. inception_remote_backup_host=192.168.56.102
  12. inception_remote_backup_port=3306
  13. inception_remote_system_user=inception
  14. inception_remote_system_password=inception

  15. #inception_remote_charset=utf8mb4
  16. inception_enable_nullable=0
  17. inception_check_primary_key=1
  18. inception_check_column_comment=1
  19. inception_check_table_comment=1
  20. inception_enable_blob_type=1
  21. inception_check_column_default_value=1

  22. # OSC
  23. inception_osc_on=1
  24. inception_osc_min_table_size=1
  25. inception_osc_bin_dir=/script/percona-toolkit-3.0.2/bin
  26. inception_osc_check_interval=5
  27. inception_osc_chunk_time=0.1

啟動
# /fire/inception/inception/debug/mysql/bin/Inception --defaults-file=/etc/inc.cnf &

執行SQL
編輯python 2指令碼,為一張100萬行的大表新增欄位

  1. [root@MySQL01 script]# vim inception.py
  2. #!/usr/bin/python2.6
  3. #-\*-coding: utf-8-\*-
  4. import MySQLdb
  5. sql='/*--user=neo;--password=neo;--host=192.168.56.101;--execute=1;--port=3306;*/\
  6. inception_magic_start;\
  7. use test;\
  8. alter table item_order add loc5 varchar(30) not null default \'xxx\' comment \'efg\';\
  9. inception_magic_commit;'
  10. try:
  11.     conn=MySQLdb.connect(host='127.0.0.1',user='',passwd='',db='',port=6690)
  12.     cur=conn.cursor()
  13.     ret=cur.execute(sql)
  14.     result=cur.fetchall()
  15.     num_fields = len(cur.description)
  16.     field_names = [i[0] for i in cur.description]
  17.     print field_names
  18.     for row in result:
  19.         print row[0], "|",row[1],"|",row[2],"|",row[3],"|",row[4],"|",
  20.         row[5],"|",row[6],"|",row[7],"|",row[8],"|",row[9],"|",row[10]
  21.     cur.close()
  22.     conn.close()
  23. except MySQLdb.Error,e:
  24.      print "Mysql Error %d: %s" % (e.args[0], e.args[1])

執行指令碼
[root@MySQL01 script]# python2.6 inception.py

登入Inception檢視OSC的執行過程

  1. # /mysql_software_56/bin/mysql -uroot -h 127.0.0.1 -P 6690
  2. mysql> inception get osc processlist\G
  3. *************************** 1. row ***************************
  4.     DBNAME: test
  5.  TABLENAME: item_order
  6.    COMMAND: alter table item_order add loc5 varchar(30) not null default 'xxx' comment 'efg'
  7.    SQLSHA1: *862B1979B3751217FE56799A0216A2629F2FFD4C
  8.    PERCENT: 100
  9. REMAINTIME: 00:00
  10. INFOMATION: No slaves found. See --recursion-method if host MySQL01 has slaves.
  11. Not checking slave lag because no slaves were found and --check-slave-lag was not specified.
  12. Operation, tries, wait:
  13.   analyze_table, 10, 1
  14.   copy_rows, 10, 0.25
  15.   create_triggers, 10, 1
  16.   drop_triggers, 10, 1
  17.   swap_tables, 10, 1
  18.   update_foreign_keys, 10, 1
  19. Not updating foreign keys because --alter-foreign-keys-method=none. Foreign keys that reference the table will no longer work.
  20. Altering `test`.`item_order`...
  21. Creating new table...
  22. CREATE TABLE `test`.`_item_order_new` (
  23.   `order_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '訂單編號',
  24.   `loc_id` tinyint(3) unsigned NOT NULL COMMENT '地區編號',
  25.   `order_create_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '訂單生成日期',
  26.   `order_expire_date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '訂單過期日期',
  27.   `item_id` int(10) unsigned NOT NULL COMMENT '商品編號',
  28.   `item_cnt` int(10) unsigned NOT NULL COMMENT '商品數量',
  29.   `order_status` tinyint(3) unsigned NOT NULL COMMENT '訂單狀態,0-失效,1-交易成功',
  30.   `tran_amount` bigint(20) unsigned NOT NULL COMMENT '交易金額',
  31.   PRIMARY KEY (`order_id`),
  32.   KEY `idx_order_loc_status` (`loc_id`,`order_status`,`order_expire_date`),
  33.   KEY `idx_order_loc_exp` (`loc_id`,`order_expire_date`),
  34.   KEY `idx_order_stat_loc_item` (`order_status`,`loc_id`,`item_id`,`tran_amount`),
  35.   KEY `idx_item_id` (`item_id`)
  36. ) ENGINE=InnoDB AUTO_INCREMENT=1000001 DEFAULT CHARSET=utf8 COMMENT='訂單表'
  37. Created new table test._item_order_new OK.
  38. Altering new table...
  39. ALTER TABLE `test`.`_item_order_new` add loc5 varchar(30) not null default 'xxx' comment 'efg'
  40. Altered `test`.`_item_order_new` OK.
  41. 2018-04-07T02:03:33 Creating triggers...
  42. CREATE TRIGGER `pt_osc_test_item_order_del` AFTER DELETE ON `test`.`item_order` FOR EACH ROW DELETE IGNORE FROM `test`.`_item_order_new` WHERE `test`.`_item_order_new`.`order_id` <=> OLD.`order_id`
  43. CREATE TRIGGER `pt_osc_test_item_order_upd` AFTER UPDATE ON `test`.`item_order` FOR EACH ROW BEGIN DELETE IGNORE FROM `test`.`_item_order_new` WHERE !(OLD.`order_id` <=> NEW.`order_id`) AND `test`.`_item_order_new`.`order_id` <=> OLD.`order_id`;REPLACE INTO `test`.`_item_order_new` (`order_id`, `loc_id`, `order_create_date`, `order_expire_date`, `item_id`, `item_cnt`, `order_status`, `tran_amount`) VALUES (NEW.`order_id`, NEW.`loc_id`, NEW.`order_create_date`, NEW.`order_expire_date`, NEW.`item_id`, NEW.`item_cnt`, NEW.`order_status`, NEW.`tran_amount`);END
  44. CREATE TRIGGER `pt_osc_test_item_order_ins` AFTER INSERT ON `test`.`item_order` FOR EACH ROW REPLACE INTO `test`.`_item_order_new` (`order_id`, `loc_id`, `order_create_date`, `order_expire_date`, `item_id`, `item_cnt`, `order_status`, `tran_amount`) VALUES (NEW.`order_id`, NEW.`loc_id`, NEW.`order_create_date`, NEW.`order_expire_date`, NEW.`item_id`, NEW.`item_cnt`, NEW.`order_status`, NEW.`tran_amount`)
  45. 2018-04-07T02:03:33 Created triggers OK.
  46. 2018-04-07T02:03:33 Copying approximately 1000219 rows...
  47. INSERT LOW_PRIORITY IGNORE INTO `test`.`_item_order_new` (`order_id`, `loc_id`, `order_create_date`, `order_expire_date`, `item_id`, `item_cnt`, `order_status`, `tran_amount`) SELECT `order_id`, `loc_id`, `order_create_date`, `order_expire_date`, `item_id`, `item_cnt`, `order_status`, `tran_amount` FROM `test`.`item_order` FORCE INDEX(`PRIMARY`) WHERE ((`order_id` >= ?)) AND ((`order_id` <= ?)) LOCK IN SHARE MODE /*pt-online-schema-change 5178 copy nibble*/
  48. SELECT /*!40001 SQL_NO_CACHE */ `order_id` FROM `test`.`item_order` FORCE INDEX(`PRIMARY`) WHERE ((`order_id` >= ?)) ORDER BY `order_id` LIMIT ?, 2 /*next chunk boundary*/
  49. 2018-04-07T02:05:07 Copied rows OK.
  50. 2018-04-07T02:05:07 Swapping tables...
  51. RENAME TABLE `test`.`item_order` TO `test`.`_item_order_old`, `test`.`_item_order_new` TO `test`.`item_order`
  52. 2018-04-07T02:05:10 Swapped original and new tables OK.
  53. 2018-04-07T02:05:10 Dropping old table...
  54. SET foreign_key_checks=0
  55. DROP TABLE IF EXISTS `test`.`_item_order_old`
  56. 2018-04-07T02:05:11 Dropped old table `test`.`_item_order_old` OK.
  57. 2018-04-07T02:05:11 Dropping triggers...
  58. DROP TRIGGER IF EXISTS `test`.`pt_osc_test_item_order_del`;
  59. DROP TRIGGER IF EXISTS `test`.`pt_osc_test_item_order_upd`;
  60. DROP TRIGGER IF EXISTS `test`.`pt_osc_test_item_order_ins`;
  61. 2018-04-07T02:05:11 Dropped triggers OK.
  62. # Event Count
  63. # ====== =====
  64. # INSERT 727
  65. Successfully altered `test`.`item_order`.

  66. 1 row in set (0.00 sec)

語句執行完成
[root@MySQL01 script]# python2.6 inception.py 
['ID', 'stage', 'errlevel', 'stagestatus', 'errormessage', 'SQL', 'Affected_rows', 'sequence', 'backup_dbname', 'execute_time', 'sqlsha1']
1 | RERUN | 0 | Execute Successfully | None | 2 | EXECUTED | 0 | Execute Successfully
Backup successfully | None |

備份及回滾語句的生成條件:
線上伺服器必須要開啟 binlog,在啟動時需要設定引數log_bin、log_bin_index等關於 binlog 的引數。不然不會備份及生成回滾語句。
引數binlog_format必須要設定為 mixed 或者 row 模式。
引數 server_id 必須要設定為非0及非1。

  1. mysql> show global variables like '%log%bin%';
  2. +---------------------------------+------------------------------------+
  3. | Variable_name | Value |
  4. +---------------------------------+------------------------------------+
  5. | log_bin | ON |
  6. | log_bin_basename | /mysql_56_3306/log/mysql-bin |
  7. | log_bin_index | /mysql_56_3306/log/mysql-bin.index |
  8. | log_bin_trust_function_creators | ON |
  9. | log_bin_use_v1_row_events | OFF |
  10. | sql_log_bin | ON |
  11. +---------------------------------+------------------------------------+
  12. 6 rows in set (0.00 sec)

  13. mysql> show global variables like 'binlog_format';
  14. +---------------+-------+
  15. | Variable_name | Value |
  16. +---------------+-------+
  17. | binlog_format | ROW |
  18. +---------------+-------+
  19. 1 row in set (0.00 sec)

  20. mysql> show global variables like 'server_id';
  21. +---------------+-------+
  22. | Variable_name | Value |
  23. +---------------+-------+
  24. | server_id | 102 |
  25. +---------------+-------+
  26. 1 row in set (0.00 sec)

檢視備份表

  1. mysql> show databases;
  2. +--------------------------+
  3. | Database |
  4. +--------------------------+
  5. | information_schema |
  6. | 192_168_56_101_3306_test |
  7. | inception |
  8. | mysql |
  9. | performance_schema |
  10. | sale |
  11. | test |
  12. +--------------------------+
  13. 8 rows in set (0.00 sec)

  14. mysql> use 192_168_56_101_3306_test
  15. Reading table information for completion of table and column names
  16. You can turn off this feature to get a quicker startup with -A

  17. Database changed
  18. mysql> show tables;
  19. +------------------------------------+
  20. | Tables_in_192_168_56_101_3306_test |
  21. +------------------------------------+
  22. | $_$inception_backup_information$_$ |
  23. | item_order |
  24. +------------------------------------+
  25. 2 rows in set (0.00 sec)

  26. mysql> select * from item_order;
  27. +----+-----------------------------------------------------+-----------------+
  28. | id | rollback_statement | opid_time |
  29. +----+-----------------------------------------------------+-----------------+
  30. | 1 | ALTER TABLE `test`.`item_order` DROP COLUMN `loc5`; | 1523037912_28_1 |
  31. +----+-----------------------------------------------------+-----------------+
  32. 1 row in set (0.00 sec)

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26506993/viewspace-2152755/,如需轉載,請註明出處,否則將追究法律責任。

相關文章