博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MySQL环境搭建利器---Sandbox
阅读量:2440 次
发布时间:2019-05-10

本文共 14345 字,大约阅读时间需要 47 分钟。

MySQL环境搭建利器---Sandbox

经常看见新同学对于安装MySQL十分惧怕,这是因为不熟悉的缘故,确实源码编译比较麻烦,缺少各种依赖包也够折腾的。当然还有预编译包,但是对于新同学来说好像都挺麻烦的。当然有yum安装,但是我们需要多实例,测试多个版本就不方便了。那么我来说说比较简单的方法。使用沙箱环境。MySQL Sandbox是什么呢?简单来说就是一个沙盒,可以用于快速搭建mysql的环境,让我们可以不用费劲的去安装,或者编译。通常对于学习来说是不错的选择。毕竟我们是自己学习,而不是线上环境。项目主页: 。对于安装也是非常简单的。我这里就采用  cpan来安装,当然各位童鞋也可以采用源码安装。看你口味了。我反正喜欢简单,能满足需求就行,或许这也是我的性格哈。:)

1.安装cpan

yum install cpan -y

2.安装软件依赖的包

yum install perl-Test-Simple -y

3.安装MySQL Sandbox

cpan MySQL::Sandbox

4.设置环境变量(否则会抛错)

[root@localhost ~]# echo 'export SANDBOX_AS_ROOT=1' >> /root/.bash_profile[root@localhost ~]# source /root/.bash_profile

5.下载mysql二进制软件包(我这里下载mysql5.6和mariadb-10,软件各位童鞋自己搜索,我这里已经下载,如下)

[root@localhost mysql]# pwd/opt/mysql[root@localhost mysql]# lltotal 588236-rw-r--r--. 1 root root 295874759 Jun  4 04:56 mariadb-10.0.12-linux-x86_64.tar.gz-rw-r--r--. 1 root root 306470256 Jun  4 04:56 mysql-5.6.12-linux-glibc2.5-x86_64.tar.gz[root@localhost mysql]#

6.在沙箱环境中运行我们mysql两个实例

(1)首先创建mysql5.6的实例

[root@localhost mysql]# cd /opt/mysql/[root@localhost mysql]# make_sandbox mysql-5.6.12-linux-glibc2.5-x86_64.tar.gz unpacking /opt/mysql/mysql-5.6.12-linux-glibc2.5-x86_64.tar.gzExecuting low_level_make_sandbox --basedir=/opt/mysql/5.6.12 \        --sandbox_directory=msb_5_6_12 \        --install_version=5.6 \        --sandbox_port=5612 \        --no_ver_after_name \        --my_clause=log-error=msandbox.err    The MySQL Sandbox,  version 3.0.44    (C) 2006-2013 Giuseppe Maxiainstalling with the following parameters:upper_directory                = /root/sandboxessandbox_directory              = msb_5_6_12sandbox_port                   = 5612check_port                     = no_check_port                  = datadir_from                   = scriptinstall_version                = 5.6basedir                        = /opt/mysql/5.6.12tmpdir                         = my_file                        = operating_system_user          = rootdb_user                        = msandboxremote_access                  = 127.%bind_address                   = 127.0.0.1ro_user                        = msandbox_rorw_user                        = msandbox_rwrepl_user                      = rsandboxdb_password                    = msandboxrepl_password                  = rsandboxmy_clause                      = log-error=msandbox.errmaster                         = slaveof                        = high_performance               = prompt_prefix                  = mysqlprompt_body                    =  [\h] {\u} (\d) > force                          = no_ver_after_name              = 1verbose                        = load_grants                    = 1no_load_grants                 = no_run                         = no_show                        = do you agree? ([Y],n)

看见各种提示都给出了,相信童鞋们都看的懂,选择Y同意。

do you agree? ([Y],n) yloading grants.... sandbox server startedYour sandbox server was installed in $HOME/sandboxes/msb_5_6_12[root@localhost mysql]#

最后会有安装路径的提示,默认在家目录下的sandboxes下。我们可以看看

[root@localhost sandboxes]# pwd/root/sandboxes[root@localhost sandboxes]# lltotal 40-rwxr-xr-x. 1 root root   54 Jun  4 05:25 clear_alldrwxr-xr-x. 4 root root 4096 Jun  4 05:25 msb_5_6_12-rw-r--r--. 1 root root 3621 Jun  4 05:25 plugin.conf-rwxr-xr-x. 1 root root   56 Jun  4 05:25 restart_all-rwxr-xr-x. 1 root root 2139 Jun  4 05:25 sandbox_action-rwxr-xr-x. 1 root root   58 Jun  4 05:25 send_kill_all-rwxr-xr-x. 1 root root   54 Jun  4 05:25 start_all-rwxr-xr-x. 1 root root   55 Jun  4 05:25 status_all-rwxr-xr-x. 1 root root   53 Jun  4 05:25 stop_all-rwxr-xr-x. 1 root root   52 Jun  4 05:25 use_all[root@localhost sandboxes]#

那么如何启动mysql呢,默认安装以后就启动了。

[root@localhost ~]# pgrep -fl mysql2151 /bin/sh /opt/mysql/5.6.12/bin/mysqld_safe --defaults-file=/root/sandboxes/msb_5_6_12/my.sandbox.cnf2331 /opt/mysql/5.6.12/bin/mysqld --defaults-file=/root/sandboxes/msb_5_6_12/my.sandbox.cnf --basedir=/opt/mysql/5.6.12 --datadir=/root/sandboxes/msb_5_6_12/data --plugin-dir=/opt/mysql/5.6.12/lib/plugin --user=root --log-error=/root/sandboxes/msb_5_6_12/data/msandbox.err --pid-file=/root/sandboxes/msb_5_6_12/data/mysql_sandbox5612.pid --socket=/tmp/mysql_sandbox5612.sock --port=5612[root@localhost ~]#

可以杀掉进程,自己启动看看,启动停止脚本在/root/sandboxes/msb_5_6_12

[root@localhost msb_5_6_12]# pkill -9 mysqld[root@localhost msb_5_6_12]# ./start sandbox server already started (found pid file /root/sandboxes/msb_5_6_12/data/mysql_sandbox5612.pid) sandbox server started[root@localhost msb_5_6_12]# rm -f /root/sandboxes/msb_5_6_12/data/mysql_sandbox5612.pid[root@localhost msb_5_6_12]# ./start . sandbox server started[root@localhost msb_5_6_12]#

登陆mysql瞧瞧

[root@localhost msb_5_6_12]# pwd/root/sandboxes/msb_5_6_12[root@localhost msb_5_6_12]# ./use Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 2Server version: 5.6.12 MySQL Community Server (GPL)Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql [localhost] {msandbox} ((none)) >

就这么简单哈,当然有复杂的用法,大家可以试试,通常这样能快速部署一个mysql用于测试已经足够了。(下面的是mariadb实例,安装实例都是一样的)

[root@localhost msb_10_0_10]# pwd/root/sandboxes/msb_10_0_10[root@localhost msb_10_0_10]# ./use Welcome to the MariaDB monitor.  Commands end with ; or \g.Your MariaDB connection id is 5Server version: 10.0.10-MariaDB MariaDB ServerCopyright (c) 2000, 2014, Oracle, SkySQL Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql [localhost] {msandbox} ((none)) >

各个mysql实例的端口号就是mysql的版本号哦:)

[root@localhost msb_10_0_10]# netstat -nltp | grep mysqldtcp        0      0 127.0.0.1:5612              0.0.0.0:*                   LISTEN      2547/mysqld         tcp        0      0 127.0.0.1:10010             0.0.0.0:*                   LISTEN      3015/mysqld         [root@localhost msb_10_0_10]#

对于需要部署多个实例呢?同一个版本部署多实例,其实也非常的简单,提供了make_multiple_sandbox这个命令,具体的参数自行help。或者查阅官方文档。

对于部署多个实例,我们还可以添加一个环境变量,就是将实例安装在指定的位置,

[root@mysql-server-01 mysql]# echo "export SANDBOX_HOME=/data/" >> /root/.bash_profile [root@mysql-server-01 mysql]# source /root/.bash_profile

下面默认部署3个实例(相同的版本)

[root@mysql-server-01 mysql]# make_multiple_sandbox mariadb-10.0.12-linux-x86_64.tar.gz 

installing node 1

installing node 2

installing node 3

group directory installed in $SANDBOX_HOME/multi_msb_mariadb-10_0_12

[root@mysql-server-01 mysql]# 

默认部署3个实例,想要部署更多实例可以加参数--how_many_nodes = number,上面部署完成以后我们看看。

[root@mysql-server-01 multi_msb_mariadb-10_0_12]# pwd/data/multi_msb_mariadb-10_0_12[root@mysql-server-01 multi_msb_mariadb-10_0_12]# lltotal 72-rwxr-xr-x 1 root root  365 Jul 13 16:18 check_slaves-rwxr-xr-x 1 root root  432 Jul 13 16:18 clear_all-rw-r--r-- 1 root root 7811 Jul 13 16:18 connection.json-rw-r--r-- 1 root root  626 Jul 13 16:18 default_connection.json-rwxr-xr-x 1 root root   48 Jul 13 16:18 n1-rwxr-xr-x 1 root root   48 Jul 13 16:18 n2-rwxr-xr-x 1 root root   48 Jul 13 16:18 n3drwxr-xr-x 4 root root 4096 Jul 13 16:17 node1drwxr-xr-x 4 root root 4096 Jul 13 16:17 node2drwxr-xr-x 4 root root 4096 Jul 13 16:18 node3-rw-r--r-- 1 root root 1088 Jul 13 16:18 README-rwxr-xr-x 1 root root  204 Jul 13 16:18 restart_all-rwxr-xr-x 1 root root  460 Jul 13 16:18 send_kill_all-rwxr-xr-x 1 root root  432 Jul 13 16:18 start_all-rwxr-xr-x 1 root root  232 Jul 13 16:18 status_all-rwxr-xr-x 1 root root  425 Jul 13 16:18 stop_all-rwxr-xr-x 1 root root  315 Jul 13 16:18 use_all[root@mysql-server-01 multi_msb_mariadb-10_0_12]#

可以可以已经有3个节点了,我们登陆其中一个看看。

[root@mysql-server-01 multi_msb_mariadb-10_0_12]# ./n1Welcome to the MariaDB monitor.  Commands end with ; or \g.Your MariaDB connection id is 5Server version: 10.0.12-MariaDB-log MariaDB ServerCopyright (c) 2000, 2014, Oracle, SkySQL Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.node1 [localhost] {msandbox} ((none)) >

现在是不是开始爱上MySQL Sandbox了呢?那你还等什么?心动就上吧!哈哈,就写到这里了。

 

mysql sandbox经典讲义(因为被墙,故放在网盘给同学们下载)

参考资料



一 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使用的,可以多看看源码和各个命令。推荐 



About Me

........................................................................................................................

● 本文作者:小麦苗,部分内容整理自网络,若有侵权请联系小麦苗删除

● 本文在itpub、博客园、CSDN和个人微 信公众号( )上有同步更新

● 本文itpub地址:

● 本文博客园地址:

● 本文CSDN地址:

● 本文pdf版、个人简介及小麦苗云盘地址:

● 数据库笔试面试题库及解答:

● DBA宝典今日头条号地址:

........................................................................................................................

● QQ群号: 230161599 、618766405

● 微 信群:可加我微 信,我拉大家进群,非诚勿扰

● 联系我请加QQ好友 646634621 ,注明添加缘由

● 于 2019-08-01 06:00 ~ 2019-08-31 24:00 在西安完成

● 最新修改时间:2019-08-01 06:00 ~ 2019-08-31 24:00

● 文章内容来源于小麦苗的学习笔记,部分整理自网络,若有侵权或不当之处还请谅解

● 版权所有,欢迎分享本文,转载请保留出处

........................................................................................................................

小麦苗的微店

小麦苗出版的数据库类丛书

小麦苗OCP、OCM、高可用网络班

小麦苗腾讯课堂主页

........................................................................................................................

使用 微 信客户端扫描下面的二维码来关注小麦苗的微 信公众号( )及QQ群(DBA宝典)、添加小麦苗微 信, 学习最实用的数据库技术。

........................................................................................................................

 

 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26736162/viewspace-2655520/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/26736162/viewspace-2655520/

你可能感兴趣的文章
linux网络知识:TCP/IP设置内容(转)
查看>>
GNOME帮助Linux应用于商业桌面环境(转)
查看>>
linux网络知识:与网络设置有关的几个文件(转)
查看>>
Linux文件内容查询命令(转)
查看>>
libc.a 文件恢复(转)
查看>>
SCO UNIX上cpio命令详细用法(转)
查看>>
Linux系统可卸载内核模块完全指南(下)(转)
查看>>
思考-两个大表的关联.txt
查看>>
WIDTH_BUCKET和NTILE函数.txt
查看>>
sql plan baseline(二)
查看>>
第十章 sqlplus的安全性
查看>>
第十三章 sqlplus命令(一)
查看>>
第三章(backup and recovery 笔记)
查看>>
第一章(backup and recovery 笔记)
查看>>
第六章(backup and recovery 笔记)
查看>>
oracle备份功能简述
查看>>
[转]数据库三大范式
查看>>
恢复编录的创建和使用.txt
查看>>
truncate 命令使用
查看>>
[script]P_CHECK_BLACK.sql 检查当前用户下是否有varchar2字段的末尾包含空格
查看>>