您的位置:宽带测速网 > mysql教程 > MySQL中Sandbox怎么安装

MySQL中Sandbox怎么安装

2025-06-24 08:06来源:互联网 [ ]

一 sandbox是什么?
MySQL Sandbox是一个非常简单快捷的安装搭建MySQL实例的工具,它可以非常快速地满足我们对MySQL环境各种需求:单机实例,主从,一主多从等等架构(区别于自己安装MySQL 软件)。比如 新的数据库版本发行之后,想要尽快尝鲜 ,又不想花太多资源去安装,就可以使用sandbox帮助我们完成创建单个或者主从结构的实例。对于那些不懂MySQL安装的开发,测试同学而言,可以使用sandbox的快速搭建一个符合要求的数据库。MySQL Sandbox 快速,是以秒来衡量的,谁用谁知道。
二 如何安装和使用
2.1 安装sandbox
本文的案例是基于Centos虚拟机测试。

    yum install cpan -y

    yum install perl-Test-Simple -y

    cpan MySQL::Sandbox

    echo 'export SANDBOX_AS_ROOT=1' >> /root/.bash_profile && source /root/.bash_profile

获取Percona server 5.7.17 版本

    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_开头的文件。

    make_sandbox 基于二进制压缩包创建MySQL实例

    make_sandbox_from_source 基于源码创建MySQL实例,参数是而执行 ./configure && make 成功的源码存放目录

    make_sandbox_from_installed 基于已经安装好的mysql可执行文件目录安装MySQL实例

    make_sandbox_from_url从网上下载docker 镜像进行安装,具体参考 --help 命令

    make_multiple_sandbox创建多个相同版本的MySQL实例

    make_multiple_custom_sandbox 创建不同版本的MySQL实例

    make_replication_sandbox 搭建主从复制结构,可以是一主一从,也可以是一主多从。

    sbtool : sandbox管理工具

要深入了解各个命令的具体用法,请参考源码目录下的README文档,然后再自己动手实践,能理解更深刻,毕竟纸上来得终觉浅,绝知此事要躬行。下面主要通过
make_sandbox 和 make_replication_sandbox 来介绍如何使用 。
2.3 使用sandbox
安装单个实例

    root@rac4:/data/mysql# >make_sandbox /data/mysql/Percona-Server-5.7.17-11-Linux.x86_64.ssl101.tar.gz

    unpacking /data/mysql/Percona-Server-5.7.17-11-Linux.x86_64.ssl101.tar.gz

    Executing low_level_make_sandbox --basedir=/data/mysql/5.7.17 \

    --sandbox_directory=msb_5_7_17 \

    --install_version=5.7 \

    --sandbox_port=5717 \

    --no_ver_after_name \

    --my_clause=log-error=msandbox.err

    The MySQL Sandbox, version 3.2.05

    (C) 2006-2016 Giuseppe Maxia

    Installing with the following parameters:

    upper_directory = /root/sandboxes

    sandbox_directory = msb_5_7_17

    sandbox_port = 5717

    check_port =

    no_check_port =

    datadir_from = script

    install_version = 5.7

    basedir = /data/mysql/5.7.17

    tmpdir =

    my_file =

    operating_system_user = root

    db_user = msandbox

    remote_access = 127.%

    bind_address = 127.0.0.1

    ro_user = msandbox_ro

    rw_user = msandbox_rw

    repl_user = rsandbox

    db_password = msandbox

    repl_password = rsandbox

    my_clause = log-error=msandbox.err

    ...... 省略部分内容

    prompt_prefix = mysql

    prompt_body = [\h] {\u} (\d) >

    force =

    no_ver_after_name = 1

    verbose =

    load_grants = 1

    no_load_grants =

    no_run =

    no_show =

    keep_uuid =

    history_dir =

    do you agree? ([Y],n) Y

输入Y 然后sandbox就会启动一个实例,需要等待20s 左右。

    # Starting server

    . sandbox server started

    # Loading grants

    Your sandbox server was installed in $HOME/sandboxes/msb_5_7_17

因为本案例采用root用户安装测试,新生成的数据库目录在 /root/sandboxes/msb_5_7_17,其中的文件如下

大家可以研究各个可执行文件的具体内容。常用的有use,stop,start,restart 等等,例如

    root@rac4:~/sandboxes/msb_5_7_17# >./use --登陆数据库

    Welcome to the MySQL monitor. Commands end with ; or \g.

    Your MySQL connection id is 9

    Server version: 5.7.17-11 Percona Server (GPL), Release 11, Revision f60191c

    Copyright (c) 2009-2016 Percona LLC and/or its affiliates

    mysql [localhost] {msandbox} ((none)) > show databases;

    +--------------------+

    | Database|

    +--------------------+

    | information_schema |

    | mysql |

    | performance_schema |

    | sys |

    | test |

    +--------------------+

    5 rows in set (0.00 sec)

搭建主从,本例中启用gtid 并且设置创建1个slave. 因为上例已经创建了一个5.7.17 源程序目录,我们可以基于该目录创建主从,当然也可以基于源码的压缩包。

    root@rac4:/data/mysql# >make_replication_sandbox --gtid --how_many_slaves=1 5.7.17

    installing and starting master

    installing slave 1

    starting slave 1

    .. sandbox server started

    initializing slave 1

    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会把主库相关信息输出。

    root@rac4:~/sandboxes/rsandbox_5_7_17# >sh test_replication检查主备关系

    # Master log: mysql-bin.000001 - Position: 10732 - Rows: 20

    # Testing slave #1

    ok - Slave #1 acknowledged reception of transactions from master

    ok - Slave #1 IO thread is running

    ok - Slave #1 SQL thread is running

    ok - Table t1 found on slave #1

    ok - Table t1 has 20 rows on #1

    # TESTS : 5

    # FAILED: 0 ( 0.0%)

    # PASSED: 5 (100.0%)

    # exit code: 0

    root@rac4:~/sandboxes/rsandbox_5_7_17# >./check_slaves #

    master

    port: 20192

    File: mysql-bin.000001

    Position: 10732

    Executed_Gtid_Set: 00020192-1111-1111-1111-111111111111:1-40

    slave # 1

    port: 20193

    Master_Log_File: mysql-bin.000001

    Read_Master_Log_Pos: 10732

    Slave_IO_Running: Yes

    Slave_SQL_Running: Yes

    Exec_Master_Log_Pos: 10732

    Retrieved_Gtid_Set: 00020192-1111-1111-1111-111111111111:1-40

    Executed_Gtid_Set: 00020192-1111-1111-1111-111111111111:1-40