MySQL系列之一键安装脚本—-单实例/多实例

最近在搞MySQL,由于经常测试一些东西。因此,就写了一个一键安装脚本。

脚本用途:

用于在CentOS/RHEL6.x系统上快速部署出Mysql的单实例或者多实例环境

脚本说明:

该脚本运行情况良好针对脚本中,每一步命令执行的正误判断以及提醒非常醒目,可协助执行者快速定位错误源脚本诸多内容都以声明变量,增加了脚本的灵活性和扩展性脚本以做模块化处理,对应功能对应函数,方便SA快速更改和了解该脚本

该脚本使用注意事项:

1、能够通公网或者mysql源码包已经放置到/usr/local/src目录下2、本脚本运行环境要求yum源已经配好3、注意使用的mysql版本,为了稳定期间,本脚本暂稳定支持5.5以后的mysql源码包。本脚本默认使用5.6.16版本的源码包。如果你需要使用其它版本,请更改MYSQL_SOFT变量以及源码包的下载路径4、mysql安装默认位置为/usr/local/mysql,如需更改请自行修改INSTALL_PATH变量5、由于我基本每条命令都有做注释,因此其它一些参数的修改,请自己研究,此处不再啰嗦6、系统环境要求CentOS/RHEL6.x版本

以下为脚本内容:

#!/bin/bash##ThescriptusedinCentOS/RHEL6.Xsystemautomaticallydeploymysqlsingleinstanceandmultipleinstancesoftheenvironment#Writtenbysunsky#Mail:nolinux@126.com#QQ:274546888#Date:2014-7-1914:23:00#./etc/init.d/functionstac(){if[$?==0];thenaction”/bin/trueelseaction”/bin/falsefi}pre_instance(){echo’–AddMySQLUser<1>’useradd-r306mysql;tacecho’–InstallSomePackages<1>’yuminstallwgetmakecmakegccgcc-c++ncursesncurses-develperl-y&>/dev/null;tacecho’–DownloadingMySQL<2>’cd/usr/local/src;tac#wget${MYSQL_SOFT}.tar.gz;tacecho’–Unpackthesourcecodepackage<2>’tar-zxf/usr/local/src/${MYSQL_SOFT}.tar.gz-C/usr/local/src/;taccd/usr/local/src/${MYSQL_SOFT};tacecho’–InstallMySQL<3>’cmake-DCMAKE_INSTALL_PREFIX=$INSTALL_PATH-DINSTALL_DATADIR=$DATA_DIR-DDEFAULT_CHARSET=utf8-DWITH_EXTRA_CHARSETS=all-DMYSQL_USER=mysql-DDEFAULT_COLLATION=utf8_general_ci&>/dev/null;tacmake&>/dev/null;tacmakeinstall&>/dev/null;tacecho’–Changethedirectoryownerandgroup<1>’chown-Rmysql.mysql/usr/local/mysql;tacecho’–Createmy.cnf<1>’echo|cp/usr/local/src/${MYSQL_SOFT}/support-files/my-default.cnf/etc/my.cnf;tacecho’–CreateMysqldScripts<2>’echo|cp/usr/local/mysql/support-files/mysql.server/etc/init.d/mysqld;tacchmod+x/etc/init.d/mysqld;tacecho’–StartMysqldServiceTest<1>’sleep3/etc/init.d/mysqldstart>/dev/null;tacecho’–ViewMySQLDatabaseStatus<1>’/etc/init.d/mysqldstatus|grep”SUCCESS”>/dev/null;tacif[$?==0];thenaction’–MySQLInstallDone!’/bin/true;elseaction’–MySQLInstallFailed!’false;fiaction’–MySQLCommandGlobalPath<2>’echo’exportPATH=/usr/local/mysql/bin/:$PATH’>>/etc/profile;tacsleep1source/etc/profile;tac}single_instance(){echo’–InitializedMySQLdatabase,thedefaultport3306<1>’$INIT_DB–datadir=$DATA_DIR&>/dev/null;tacecho’–StartMysqldServiceTest<1>’/etc/init.d/mysqldstart>/dev/null;tacecho’–ViewMySQLDatabaseStatus<1>’/etc/init.d/mysqldstatus|grep”SUCCESS”>/dev/null;tacif[$?==0];thenaction’–MySQLInstallDone!’/bin/true;elseaction’–MySQLInstallFailed!’false;fiaction’–MySQLCommandGlobalPath<2>’echo’exportPATH=/usr/local/mysql/bin/:$PATH’>>/etc/profile;tacsleep1source/etc/profile;tac}single_of_multiple(){action’–AddMysqld_MultiUser<2>’user=”mysql”;tacpassword=”sunsky”;tacaction’–StopMySQLDatabase<1>’/etc/init.d/mysqldstop&>/dev/null;tacaction”–Mysqld_multiConfigure<2>”sed-i’s/^[^#]/#/g’/etc/my.cnf;taccat>/etc/my.cnf<<EOF[mysqld_multi]mysqld=/usr/local/mysql/bin/mysqld_safemysqladmin=/usr/local/mysql/bin/mysqladminuser=$userpassword=$password[mysqld3306]server-id=1port=3306socket=/tmp/mysql3306.sockpid-file=/tmp/mysql3306.pidbasedir=/usr/local/mysqldatadir=/usr/local/mysql/datakey_buffer_size=16kmax_allowed_packet=1Mtable_open_cache=4sort_buffer_size=64Kread_buffer_size=256Kread_rnd_buffer_size=256Knet_buffer_length=2Kthread_stack=128KEOFtacecho’–preparemy_default_print<1>’echo|cp/usr/local/mysql/bin/my_print_defaults/usr/bin/;tacsleep1action”–Initializemultipleinstanceofthe3306instance<1>”echo’–Startmultipleinstancesofthe3306instance<1>’$MULTI_DB3306;tacsleep5action”–Examplesof3306authorizedusers<1>”$MYSQL_DB-S/tmp/mysql3306.sock-e”grantshutdownon*.*to’$user’@’%’identifiedby’$password’;”;tacaction”–Instance3306hasuserandpasswordtoshutdown<1>”}multiple_instances(){user=”mysql”password=”sunsky”forportin$*;donetstat-lntp|grep$port&>/dev/nullif[$?-eq0];thenaction”–Instance$portisrunning<1>”elsecat/etc/my.cnf|grepmysqld${port}>/dev/nullif[$?-eq0];thenecho”–$portinstanceisalreadyexists,pleaseinputotherportnumber!”$MULTI_DB$port&&action”–Instance$portopentocomplete”/bin/true||action”–Instance$portopenfailed”/bin/falseelsecat>>/etc/my.cnf<<EOF[mysqld${port}]server-id=$[${port}%3305]port=${port}socket=/tmp/mysql${port}.sockpid-file=/tmp/mysql${port}.pidbasedir=/usr/local/mysqldatadir=/usr/local/mysql/data${port}key_buffer_size=16kmax_allowed_packet=1Mtable_open_cache=4sort_buffer_size=64Kread_buffer_size=256Kread_rnd_buffer_size=256Knet_buffer_length=2Kthread_stack=128KEOFecho”–Create$portInstanceDatadir<2>”mkdir-p${DATA_DIR}${port};tacchown-Rmysql.mysql${DATA_DIR}${port};tacecho”–Initializemultipleinstanceofthe$portinstance<1>”$INIT_DB–datadir=${DATA_DIR}${port}&>/dev/null;tacecho”–Startmultipleinstancesofthe$portinstance<1>”$MULTI_DB$port&&action”–Instance$portopentocomplete”||action”–Instance$portopenfailed”;tacecho”–Examplesof$portauthorizedusers”sleep3$MYSQL_DB-S/tmp/mysql${port}.sock-e”grantshutdownon*.*to’$user’@’localhost’identifiedby’$password’;”&&action”–Instance$porthasuserandpasswordtoshutdown”/bin/true||action”–Instance$porthasuserandpasswordtoshutdown”/bin/falsefifidone}#MySQLInstallPathINSTALL_PATH=’/usr/local/mysql’#MySQLDATA_DRDATA_DIR=”$INSTALL_PATH/data”#MySQLCommandPathINSTALL_DB=”$INSTALL_PATH/scripts/mysql_install_db”MYSQL_DB=”$INSTALL_PATH/bin/mysql”MYSQL_MULTI=”$INSTALL_PATH/bin/mysqld_multi”INIT_DB=”$INSTALL_DB–user=mysql–basedir=$INSTALL_PATH–defaults-file=/etc/my.cnf”MULTI_DB=”$MYSQL_MULTI–defaults-file=/etc/my.cnfstart”MYSQL_SOFT=’mysql-5.6.16’case$#in0)cat<<EOFThesystemadministrator,hello!ThisisakeytoinstallmysqlsingleinstanceandmultipleinstancesofthescriptYoucanusethenameinthescriptwiththeupperslogans,todefineseveralinstancesinstalled!Example:[root@sunsky~]#bashauto_install_mysql_instance.Sh33063307Installtwomysqlinstance,portNumbers3306and3307respectively.Ifyouarefamiliarwithshell,arefreetochangethescript!Ifyouarenotfamiliarwith,pleasedonotchange!EOF;;1)echo’>Nowbegintosingleinstancedatabaseinitialization’echo’>>STEPONE:PreparetheMySQLEnvironment’pre_instanceecho’>>>STEPTWO:InstalltheMySQLsingleinstance’single_instanceecho’>>>>SETPTHREE:MySQLsingleinstanceinstallationiscomplete!’;;*)echo’>Nowstartmulti-instancedatabaseinitialization’echo’>>STEPONE:PreparetheMySQLEnvironment’pre_instancesingle_instanceecho’>>>STEPTWO:InstalltheMySQLsingleofmultipleinstance’single_of_multipleecho’>>>>STEPTHREE:InstalltheMySQLothersofmultipleinstance’multiple_instances$*echo’>>>>>SETPFOUR:MySQLmultipleinstanceinstallationiscomplete’;;esac

世界上那些最容易的事情中,拖延时间最不费力。

MySQL系列之一键安装脚本—-单实例/多实例

相关文章:

你感兴趣的文章:

标签云: