【工具】MySQL 安裝利器---Sandbox

楊奇龍發表於2017-02-25
一 sandbox是什麼?
MySQL Sandbox是一個非常簡單快捷的安裝搭建MySQL例項的工具,它可以非常快速地滿足我們對MySQL環境各種需求:單機例項,主從,一主多從等等架構(區別於自己安裝MySQL 軟體)。比如 新的資料庫版本發行之後,想要儘快嚐鮮 ,又不想花太多資源去安裝,就可以使用sandbox幫助我們完成建立單個或者主從結構的例項。對於那些不懂MySQL安裝的開發,測試同學而言,可以使用sandbox的快速搭建一個符合要求的資料庫。MySQL Sandbox 快速,是以秒來衡量的,誰用誰知道。
二 如何安裝和使用
2.1 安裝sandbox
本文的案例是基於Centos虛擬機器測試。 
  1. yum install cpan -y
  2. yum install perl-Test-Simple -y
  3. cpan MySQL::Sandbox
  4. echo 'export SANDBOX_AS_ROOT=1' >> /root/.bash_profile && source /root/.bash_profile
獲取Percona server 5.7.17 版本
  1. wget "https://www.percona.com/downloads/Percona-Server-5.7/Percona-Server-5.7.17-11/binary/tarball/Percona-Server-5.7.17-11-Linux.x86_64.ssl101.tar.gz"
2.2 常用命令
安裝完成之後預設會在 /usr/local/bin/ 目錄下產生make_開頭的檔案。
  1. make_sandbox                 基於二進位制壓縮包建立MySQL例項
  2. make_sandbox_from_source     基於原始碼建立MySQL例項,引數是而執行 ./configure && make 成功的原始碼存放目錄
  3. make_sandbox_from_installed  基於已經安裝好的mysql可執行檔案目錄安裝MySQL例項
  4. make_sandbox_from_url        從網上下載docker 映象進行安裝,具體參考 --help 命令
  5. make_multiple_sandbox        建立多個相同版本的MySQL例項
  6. make_multiple_custom_sandbox 建立不同版本的MySQL例項
  7. make_replication_sandbox     搭建主從複製結構,可以是一主一從,也可以是一主多從。
  8. sbtool :                     sandbox管理工具
要深入瞭解各個命令的具體用法,請參考原始碼目錄下的README文件,然後再自己動手實踐,能理解更深刻,畢竟紙上來得終覺淺,絕知此事要躬行。下面主要通過
make_sandbox 和 make_replication_sandbox 來介紹如何使用 。
2.3 使用sandbox
安裝單個例項
  1. root@rac4:/data/mysql# >make_sandbox /data/mysql/Percona-Server-5.7.17-11-Linux.x86_64.ssl101.tar.gz
  2. unpacking /data/mysql/Percona-Server-5.7.17-11-Linux.x86_64.ssl101.tar.gz
  3. Executing low_level_make_sandbox --basedir=/data/mysql/5.7.17 \
  4. --sandbox_directory=msb_5_7_17 \
  5. --install_version=5.7 \
  6. --sandbox_port=5717 \
  7. --no_ver_after_name \
  8. --my_clause=log-error=msandbox.err
  9.     The MySQL Sandbox, version 3.2.05
  10.     (C) 2006-2016 Giuseppe Maxia
  11. Installing with the following parameters:
  12. upper_directory = /root/sandboxes
  13. sandbox_directory = msb_5_7_17
  14. sandbox_port = 5717
  15. check_port =
  16. no_check_port =
  17. datadir_from = script
  18. install_version = 5.7
  19. basedir = /data/mysql/5.7.17
  20. tmpdir =
  21. my_file =
  22. operating_system_user = root
  23. db_user = msandbox
  24. remote_access = 127.%
  25. bind_address = 127.0.0.1
  26. ro_user = msandbox_ro
  27. rw_user = msandbox_rw
  28. repl_user = rsandbox
  29. db_password = msandbox
  30. repl_password = rsandbox
  31. my_clause = log-error=msandbox.err
  32. ...... 省略部分內容
  33. prompt_prefix = mysql
  34. prompt_body = [\h] {\u} (\d) >
  35. force =
  36. no_ver_after_name = 1
  37. verbose =
  38. load_grants = 1
  39. no_load_grants =
  40. no_run =
  41. no_show =
  42. keep_uuid =
  43. history_dir =
  44. do you agree? ([Y],n) Y
輸入Y 然後sandbox就會啟動一個例項,需要等待20s 左右。
  1. # Starting server
  2. . sandbox server started
  3. # Loading grants
  4. Your sandbox server was installed in $HOME/sandboxes/msb_5_7_17
因為本案例採用root使用者安裝測試,新生成的資料庫目錄在 /root/sandboxes/msb_5_7_17,其中的檔案如下 

大家可以研究各個可執行檔案的具體內容。常用的有use,stop,start,restart 等等,例如
  1. root@rac4:~/sandboxes/msb_5_7_17# >./use --登陸資料庫
  2. Welcome to the MySQL monitor. Commands end with ; or \g.
  3. Your MySQL connection id is 9
  4. Server version: 5.7.17-11 Percona Server (GPL), Release 11, Revision f60191c
  5. Copyright (c) 2009-2016 Percona LLC and/or its affiliates
  6. mysql [localhost] {msandbox} ((none)) > show databases;
  7. +--------------------+
  8. | Database           |
  9. +--------------------+
  10. | information_schema |
  11. | mysql              |
  12. | performance_schema |
  13. | sys                |
  14. | test               |
  15. +--------------------+
  16. 5 rows in set (0.00 sec)
搭建主從,本例中啟用gtid 並且設定建立1個slave. 因為上例已經建立了一個5.7.17 源程式目錄,我們可以基於該目錄建立主從,當然也可以基於原始碼的壓縮包。
  1. root@rac4:/data/mysql# >make_replication_sandbox --gtid --how_many_slaves=1 5.7.17
  2. installing and starting master
  3. installing slave 1
  4. starting slave 1
  5. .. sandbox server started
  6. initializing slave 1
  7. replication directory installed in $HOME/sandboxes/rsandbox_5_7_17
根據結果提示sandbox建立的主從在目錄$HOME/sandboxes/rsandbox_5_7_17,進入該目錄檢視有如下檔案 

其中master 和node1 分別是主庫和備庫的資料庫目錄, m和n1 都是登陸主庫的命令,s1 和n2 都是登陸slave 的命令,其他的可以從檔名知道具體用途。這裡介紹兩個命令test_replication和check_slaves 兩個命令功能類似,都是檢查slave 的狀態資訊。check_slaves會把主庫相關資訊輸出。
  1. root@rac4:~/sandboxes/rsandbox_5_7_17# >sh test_replication 檢查主備關係
  2. # Master log: mysql-bin.000001 - Position: 10732 - Rows: 20
  3. # Testing slave #1
  4. ok - Slave #1 acknowledged reception of transactions from master
  5. ok - Slave #1 IO thread is running
  6. ok - Slave #1 SQL thread is running
  7. ok - Table t1 found on slave #1
  8. ok - Table t1 has 20 rows on #1
  9. # TESTS : 5
  10. # FAILED: 0 ( 0.0%)
  11. # PASSED: 5 (100.0%)
  12. # exit code: 0
  13. root@rac4:~/sandboxes/rsandbox_5_7_17# >./check_slaves  #
  14. master
  15. port: 20192
  16.              File: mysql-bin.000001
  17.          Position: 10732
  18. Executed_Gtid_Set: 00020192-1111-1111-1111-111111111111:1-40
  19. slave # 1
  20. port: 20193
  21.               Master_Log_File: mysql-bin.000001
  22.           Read_Master_Log_Pos: 10732
  23.              Slave_IO_Running: Yes
  24.             Slave_SQL_Running: Yes
  25.           Exec_Master_Log_Pos: 10732
  26.            Retrieved_Gtid_Set: 00020192-1111-1111-1111-111111111111:1-40
  27.             Executed_Gtid_Set: 00020192-1111-1111-1111-111111111111:1-40
三 小結
     按照之前要部署虛擬機器安裝MySQL的時間和精力來看,使用sandbox的感覺就是一個字-,只需簡單的命令即可完成而且對使用者幾乎是透明的當你需要快速搭建最小化測試環境時,完全可以使用sandbox助你一臂之力。當然本文僅僅只是本人在比較短時間內測試的總結,需要更加深入瞭解sandbox使用的,可以多看看原始碼和各個命令。推薦 官方文件 

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

相關文章