dbdeployer MySQL沙盒部署詳解

easydba發表於2020-09-27

一、工具介紹

  • 前幾日用mysql-sandbox來搭建MySQL8.0新版本時發現用不了,提示需要使用dbdeployer才行,瞬間覺得mysql-sandbox不香了,只好咬咬牙來熟悉dbdeployer了。
  • dbdeployer是使用go語言重構的sandbox,和sandbox同一個作者。
  • 當然,dbdeployer延續了sandbox所有功能。可實現一鍵部署不同架構、不同版本的資料庫環境。如,MySQL 主從複製、GTID 模式複製、MySQL 組複製(單主模式、多主模式等)
  • 完整的資料庫型別支援及版本,可在安裝完 dbdeployer 後使用dbdeployer admin capabilities命令進行檢視,以下是當前已支援資料庫及元件型別
  1. -- pxc (Percona XtraDB Cluster)
  2. -- mysql-shell
  3. -- mysql (MySQL server)
  4. -- percona (Percona Server)
  5. -- mariadb
  6. -- tidb (TiDB isolated server)
  7. -- ndb (MySQL NDB Cluster)

二、工具安裝

  • OS:centos8
  • dbdeployer:1.54.0
  • mysql:8.0.20

2.1 dbdeployer工具下載

直接到github下載release包即可:

https://github.com/datacharmer/dbdeployer/releases

2.2 解壓

軟體解壓後實際只有一個單獨的編譯好的可執行檔案

tar -zxvf dbdeployer-1.54.0.linux.tar.gz

2.3 賦予可執行許可權

chmod +x dbdeployer-1.54.0.linux

2.4 移動到系統可執行目錄下方便使用

mv dbdeployer-1.54.0.linux /usr/local/bin/dbdeployer

2.5 驗證是否可以使用

[root@db01 tmp]# dbdeployer --version
dbdeployer version 1.54.0
[root@db01 tmp]# dbdeployer --help
dbdeployer makes MySQL server installation an easy task.
Runs single, multiple, and replicated sandboxes.

Usage:
  dbdeployer [command]

Available Commands:
  admin           sandbox management tasks
  cookbook        Shows dbdeployer samples
  defaults        tasks related to dbdeployer defaults
  delete          delete an installed sandbox
  delete-binaries delete an expanded tarball
  deploy          deploy sandboxes
  downloads       Manages remote tarballs
  export          Exports the command structure in JSON format
  global          Runs a given command in every sandbox
  help            Help about any command
  import          imports one or more MySQL servers into a sandbox
  info            Shows information about dbdeployer environment samples
  init            initializes dbdeployer environment
  sandboxes       List installed sandboxes
  unpack          unpack a tarball into the binary directory
  update          Gets dbdeployer newest version
  usage           Shows usage of installed sandboxes
  use             uses a sandbox
  versions        List available versions

Flags:
      --config string           configuration file (default "/root/.dbdeployer/config.json")
  -h, --help                    help for dbdeployer
      --sandbox-binary string   Binary repository (default "/root/opt/mysql")
      --sandbox-home string     Sandbox deployment directory (default "/root/sandboxes")
      --shell-path string       Which shell to use for generated scripts (default "/usr/bin/bash")
      --skip-library-check      Skip check for needed libraries (may cause nasty errors)
  -v, --version                 version for dbdeployer

Use "dbdeployer [command] --help" for more information about a command.

2.6 初始化環境

安裝dbdeployer之後,可以立即使用以下命令為操作做好準備的環境,此命令會建立必要的目錄,然後下載最新的MySQL二進位制檔案,在預設的位置解壓它們。

dbdeployer init

三、基本使用

3.1 自行下載MySQL並解壓

訪問 https://downloads.mysql.com/archives/community/ 可下載各不同的MySQL

dbdeployer unpack /tmp/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz

3.2 使用dbdeployer工具下載MySQL並解壓

3.2.1 檢視dbdeployer工具支援下載的軟體包
[root@mysql8 ~]# dbdeployer downloads list
Available tarballs  ()
                              name                                 OS     version     flavor        size   minimal 
---------------------------------------------------------------- ------- --------- ------------- -------- ---------
 tidb-master-linux-amd64.tar.gz                                   Linux     3.0.0   tidb           26 MB           
 mysql-8.0.16-linux-glibc2.12-x86_64.tar.xz                       Linux    8.0.16   mysql         461 MB           
 mysql-8.0.16-linux-x86_64-minimal.tar.xz                         Linux    8.0.16   mysql          44 MB   Y       
 mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz                       Linux    5.7.27   mysql         645 MB           
 mysql-8.0.17-linux-glibc2.12-x86_64.tar.xz                       Linux    8.0.17   mysql         480 MB           
 mysql-8.0.17-linux-x86_64-minimal.tar.xz                         Linux    8.0.17   mysql          45 MB   Y       
 mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz                       Linux    5.7.26   mysql         645 MB           
 mysql-5.6.44-linux-glibc2.12-x86_64.tar.gz                       Linux    5.6.44   mysql         329 MB           
 mysql-5.5.62-linux-glibc2.12-x86_64.tar.gz                       Linux    5.5.62   mysql         199 MB           
 mysql-8.0.15-linux-glibc2.12-x86_64.tar.xz                       Linux    8.0.15   mysql         376 MB           
 mysql-8.0.13-linux-glibc2.12-x86_64.tar.xz                       Linux    8.0.13   mysql         394 MB           
 mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz                       Linux    5.7.25   mysql         645 MB           
 mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz                       Linux    5.6.43   mysql         329 MB           
 mysql-5.5.61-linux-glibc2.12-x86_64.tar.gz                       Linux    5.5.61   mysql         199 MB           
 mysql-5.1.73-linux-x86_64-glibc23.tar.gz                         Linux    5.1.73   mysql         134 MB           
 mysql-5.0.96.tar.xz                                              Linux    5.0.96   mysql         5.5 MB   Y       
 mysql-5.1.72.tar.xz                                              Linux    5.1.72   mysql          10 MB   Y       
 mysql-5.5.61.tar.xz                                              Linux    5.5.61   mysql         6.6 MB   Y       
 mysql-5.5.62.tar.xz                                              Linux    5.5.62   mysql         6.6 MB   Y       
 mysql-5.6.43.tar.xz                                              Linux    5.6.43   mysql         9.0 MB   Y       
 mysql-5.6.44.tar.xz                                              Linux    5.6.44   mysql         9.1 MB   Y       
 mysql-5.7.25.tar.xz                                              Linux    5.7.25   mysql          23 MB   Y       
 mysql-5.7.26.tar.xz                                              Linux    5.7.26   mysql          23 MB   Y       
 mysql-5.0.96-linux-x86_64-glibc23.tar.gz                         Linux    5.0.96   mysql         127 MB           
 mysql-4.1.22.tar.xz                                              Linux    4.1.22   mysql         4.6 MB   Y       
 mysql-cluster-gpl-7.6.10-linux-glibc2.12-x86_64.tar.gz           Linux    7.6.10   ndb           916 MB           
 mysql-cluster-8.0.16-dmr-linux-glibc2.12-x86_64.tar.gz           Linux    8.0.16   ndb           1.1 GB           
 mysql-cluster-gpl-7.6.11-linux-glibc2.12-x86_64.tar.gz           Linux    7.6.11   ndb           916 MB           
 mysql-cluster-8.0.17-rc-linux-glibc2.12-x86_64.tar.gz            Linux    8.0.17   ndb           1.1 GB           
 mysql-shell-8.0.17-linux-glibc2.12-x86-64bit.tar.gz              Linux    8.0.17   mysql-shell    30 MB           
 mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz                       Linux    5.7.28   mysql         725 MB           
 mysql-8.0.18-linux-glibc2.12-x86_64.tar.xz                       Linux    8.0.18   mysql         504 MB           
 mysql-8.0.18-linux-x86_64-minimal.tar.xz                         Linux    8.0.18   mysql          48 MB   Y       
 mysql-8.0.19-linux-x86_64-minimal.tar.xz                         Linux    8.0.19   mysql          45 MB           
 mysql-cluster-8.0.19-linux-glibc2.12-x86_64.tar.gz               Linux    8.0.19   ndb           1.2 GB           
 mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz                       Linux    5.7.29   mysql         665 MB           
 mysql-cluster-8.0.20-linux-glibc2.12-x86_64.tar.gz               Linux    8.0.20   ndb           1.2 GB           
 mysql-8.0.20-linux-x86_64-minimal.tar.xz                         Linux    8.0.20   mysql          44 MB   Y       
 mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz                       Linux    5.7.30   mysql         660 MB           
 mysql-8.0.21-linux-glibc2.17-x86_64-minimal.tar.xz               Linux    8.0.21   mysql          48 MB   Y       
 Percona-Server-8.0.20-11-Linux.x86_64.glibc2.12-minimal.tar.gz   Linux    8.0.20   percona       103 MB   Y       
 mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz                       Linux    5.7.31   mysql         376 MB           
 mysql-shell-8.0.21-linux-glibc2.12-x86-64bit.tar.gz              Linux    8.0.21   shell          43 MB       
3.2.2 下載並解壓指定軟體包

使用dbdeployer downloads get file_name,從上面的列表中複製需要下載的版本並貼上檔名。例如:

[root@mysql8 ~]# dbdeployer downloads get-unpack  mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
Downloading mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
.........105 MB.........210 MB.........315 MB.....  376 MB
File /root/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz downloaded
Checksum matches
Unpacking tarball mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz to $HOME/opt/mysql/5.7.31
.........100.........200.........300302
Renaming directory /root/opt/mysql/mysql-5.7.31-linux-glibc2.12-x86_64 to /root/opt/mysql/5.7.31

3.3 檢查DBdeployer的已安裝/可用的tarball二進位制軟體包列表

[root@mysql8 ~]# dbdeployer versions
Basedir: /root/opt/mysql
5.7.31  8.0.20  8.0.21 
[root@mysql8 ~]# tree $HOME/opt -L 2
/root/opt
└── mysql
    ├── 5.7.31
    ├── 8.0.20
    └── 8.0.21

3.4 快速部署例項dbdeployer deploy -h

3.4.1 該命令可以部署單個,多個或複製的MySQL沙箱例項
multiple    建立多個獨立的mysql
replication 建立複製環境的mysql
single      建立單節點的mysql
3.4.2 如果要使用多個相同版本的沙箱,且沒有任何複製關係,請使用dbdeployer deploy multiple -h,例如:
dbdeployer deploy multiple 8.0.21
ps -ef | grep mysqld

預設建立三個節點,我們可以用-n選項來指定需要建立的節點數,--force強制覆蓋之前的安裝

dbdeployer deploy multiple -n 2 --force 8.0.21
3.4.3 如果需要安裝一主多從的複製環境,請使用dbdeployer deploy replication -h,例如:
dbdeployer deploy replication 5.7.31
[root@mysql8 rsandbox_5_7_31]# ./status_all 
REPLICATION  /root/sandboxes/rsandbox_5_7_31
master : master on  -  port	19832 (19832)
node1 : node1 on  -  port	19833 (19833)
node2 : node2 on  -  port	19834 (19834)

預設為一主兩從三節點版,同樣可以使用-n選項來指定節點數,--gtid指定為gtid複製模式

dbdeployer deploy replication 5.7.31 -n 2 --gtid --force
[root@mysql8 rsandbox_5_7_31]# ./status_all 
REPLICATION  /root/sandboxes/rsandbox_5_7_31
master : master on  -  port	19832 (19832)
node1 : node1 on  -  port	19833 (19833)
3.4.4 如果只想測試單個例項MySQL,請使用dbdeployer deploy single -h,例如:
dbdeployer deploy single 8.0.20
3.4.5 部署一套多主MGR叢集
dbdeployer deploy --topology=group replication 8.0.20

四、例項組操作

一鍵部署完成後會在 $HOME/sandboxes 目錄下生成各例項組對應的資料目錄,該目錄包含以下資訊(部分資訊)

  • 一鍵啟停該組所有例項的指令碼
  • 一鍵登入資料庫指令碼
  • 一鍵重置該組所有例項的指令碼(清除所有測試資料並重新初始化成全新的主從)
  • 主從例項的資料目錄(主庫為 master,從庫分別為 node1、node2 依次遞增)
  • 各例項的配置檔案
  • 預設使用者授權命令
  • 單獨啟停例項命令
  • binlog、relaylog 解析命令
4.1 使用示例
[root@mysql8 ~]# cd $HOME/sandboxes/group_msb_8_0_20
[root@mysql8 group_msb_8_0_20]# ls
check_nodes       metadata_all  n3     node3           sbdescription.json  status_all        test_sb_all      use_all_slaves
clear_all         n1            node1  replicate_from  send_kill_all       stop_all          use_all
initialize_nodes  n2            node2  restart_all     start_all           test_replication  use_all_masters
4.2 檢視該組所有例項狀態
[root@mysql8 group_msb_8_0_20]# ./status_all 
MULTIPLE  /root/sandboxes/group_msb_8_0_20
node1 : node1 on  -  port	22021 (22021)
node2 : node2 on  -  port	22022 (22022)
node3 : node3 on  -  port	22023 (22023)
4.3 一鍵重啟該組所有例項
[root@mysql8 group_msb_8_0_20]# ./restart_all 
# executing 'stop' on /root/sandboxes/group_msb_8_0_20
executing 'stop' on node3
stop /root/sandboxes/group_msb_8_0_20/node3
executing 'stop' on node2
stop /root/sandboxes/group_msb_8_0_20/node2
executing 'stop' on node1
stop /root/sandboxes/group_msb_8_0_20/node1
# executing 'start' on /root/sandboxes/group_msb_8_0_20
executing "start" on node 1
. sandbox server started
executing "start" on node 2
. sandbox server started
executing "start" on node 3
. sandbox server started
4.4 單獨操作某一節點,需進入對應節點資料目錄
[root@mysql8 group_msb_8_0_20]# cd ~/sandboxes/group_msb_8_0_20/node1
[root@mysql8 node1]# ls
add_option            clone_from       data          metadata        replicate_from      send_kill      start      test_sb
after_start           connection.conf  grants.mysql  my              restart             show_binlog    start.log  tmp
clear                 connection.json  init_db       my.sandbox.cnf  sbdescription.json  show_log       status     use
clone_connection.sql  connection.sql   load_grants   mysqlsh         sb_include          show_relaylog  stop
4.5 登陸指定例項
[root@mysql8 node1]# ./use
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.20 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

node1 [localhost:22021] {msandbox} ((none)) > 
4.6 msyql日誌管理

每個例項下有便捷日誌指令碼

show_log    #顯示錯誤日誌或常規日誌
show_binlog
show_relaylog
4.7 相關mysql使用者密碼
user name password privileges
root@localhost msandbox all on *.* with grant option
msandbox@localhost msandbox all on *.*
rsandbox@127.% rsandbox REPLICATION SLAVE
4.8 本地root使用者登陸
mysql -uroot -pmsandbox -S /tmp/mysql_sandbox8020.sock

五、dbdeployer 常用管理命令

以下是使用 dbdeployer 過程中總結的常用命令,詳細使用方式可檢視文末 dbdeployer 文件連結。

5.1 更新dbdeployer
dbdeployer update
5.2 列出以解壓的MySQL版本
dbdeployer versions
5.3 列出已安裝的沙箱例項
dbdeployer sandboxes
5.4 刪除已安裝沙箱例項
dbdeployer delete multi_msb_8_0_21
5.5 鎖定及解鎖一個或多個沙箱例項,以防止刪除
[root@mysql8 ~]# dbdeployer admin lock group_msb_8_0_20
Sandbox group_msb_8_0_20 locked
[root@mysql8 ~]# dbdeployer admin unlock group_msb_8_0_20
Sandbox group_msb_8_0_20 unlocked

六、相關連線

因為有悔,所以披星戴月;因為有夢,所以奮不顧身! 個人部落格首發:easydb.net 微信公眾號:easydb 關注我,不走丟!

相關文章