MySQL Sandbox安装使用

前言

经常看见新同学对于安装MySQL十分惧怕,这是因为不熟悉的缘故,确实源码编译比较麻烦,缺少各种依赖包也够折腾的。当然还有预编译包,但是对于新同学来说好像都挺麻烦的。当然有yum安装,但是我们需要多实例,测试多个版本就不方便了。那么我来说说比较简单的方法。使用沙箱环境。MySQL Sandbox是什么呢?简单来说就是一个沙盒,可以用于快速搭建mysql的环境,让我们可以不用费劲的去安装,或者编译。通常对于学习来说是不错的选择。毕竟我们是自己学习,而不是线上环境。项目主页:http://mysqlsandbox.net/。对于安装也是非常简单的。我这里就采用 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]# ll
total 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.gz
Executing 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 Maxia
installing with the following parameters:
upper_directory                = /root/sandboxes
sandbox_directory              = msb_5_6_12
sandbox_port                   = 5612
check_port                     = 
no_check_port                  = 
datadir_from                   = script
install_version                = 5.6
basedir                        = /opt/mysql/5.6.12
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
master                         = 
slaveof                        = 
high_performance               = 
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                        = 
do you agree? ([Y],n) 

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

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

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

[root@localhost sandboxes]# pwd
/root/sandboxes
[root@localhost sandboxes]# ll
total 40
-rwxr-xr-x. 1 root root   54 Jun  4 05:25 clear_all
drwxr-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 mysql
2151 /bin/sh /opt/mysql/5.6.12/bin/mysqld_safe --defaults-file=/root/sandboxes/msb_5_6_12/my.sandbox.cnf
2331 /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 2
Server 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 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.

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 5
Server version: 10.0.10-MariaDB MariaDB Server

Copyright (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 mysqld
tcp        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]# ll
total 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 n3
drwxr-xr-x 4 root root 4096 Jul 13 16:17 node1
drwxr-xr-x 4 root root 4096 Jul 13 16:17 node2
drwxr-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]# ./n1
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 10.0.12-MariaDB-log MariaDB Server

Copyright (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经典讲义(因为被墙,故放在网盘给同学们下载)

http://pan.baidu.com/s/1sjLMJCx

参考资料

http://search.cpan.org/~gmax/MySQL-Sandbox-3.0.44/lib/MySQL/Sandbox.pm

posted @ 2014-06-04 11:56  yayun  阅读(4124)  评论(0编辑  收藏  举报