最近在研究mysql集群,至少要安装部署2台或者多台mysql数据库,操作起来即麻烦又很耗时,今天根据安装步骤写了一个Shell脚本,只需执 行以下脚本就可以快速安装mysql数据库,对初学者或者想学习mysql集群的朋友非常方便,即省时,又省力。
操作系统及其mysql配置文件说明:
Linux系统:Centos5.8
mysql:mysql-5.5.25tar.gz源码包
安装目录:/usr/local/mysql/
数据目录:/data/mysql/3306/data/
二进制日志:/data/msyql/3306/binlog/
relay日志:/data/mysql/3306/relaylog/
配置文件:/data/mysql/3306/my.cnf
mysql.sock文件:/data/mysql/3306/mysql.sock
下面是我编写的一键安装mysql数据库的脚本,如有不对的地方请多多指教。
[root@DB148 sh]# cat mysql_install_new_version.sh
#!/bin/bash
#创作日期:2012.6.16
#作者:张世锋
#Mysql install directory and configuration files.
MYSQL_DIR="/data/software"
DATA_DIR="/data/mysql/3306/data"
BASE_DIR="/usr/local/mysql"
echo "please input mysql version:"
read VERSION
echo "Your mysql version is mysql-$VERSION.tar.gz"
if [ -e "$MYSQL_DIR/mysql-$VERSION.tar.gz" ]
then
echo "Please waitting..."
sleep 3
#Install mysql package dependent.
yum -y install gcc gcc-c++ gcc-g77 autoconf automake openssl zlib* fiex** libxml* \
ncurses-devel libmcrypt* libtool-ltdl-devel* &&
#Install cmake.
echo "The system is be installed cmake,please waitting..."
sleep 3
tar -zxvf $MYSQL_DIR/cmake-2.8.8.tar.gz -C $MYSQL_DIR &&
cd $MYSQL_DIR/cmake-2.8.8 &&
./configure \
--prefix=/usr/local/cmake &&
make && make install &&
#creating mysql account and group.
/usr/sbin/groupadd mysql &&
/usr/sbin/useradd -s /sbin/nologin -g mysql -M mysql &&
#Install mysql.
tar -zxvf $MYSQL_DIR/mysql-$VERSION.tar.gz -C $MYSQL_DIR
cd $MYSQL_DIR/mysql-$VERSION
echo "PATH=$PATH:/usr/local/cmake/bin" >> /etc/profile && source /etc/profile
cmake \
$MYSQL_DIR/mysql-$VERSION
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DYSQL_TCP_PORT=3306 \
-DMYSQL_DATADIR=$DATA_DIR \
-DMYSQL_UNIX_ADDR=/data/mysql/3306/mysql.sock \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=all \
-DWITH_DEBUG=0
make && make install &&
mkdir -p $DATA_DIR
mkdir -p `dirname $DATA_DIR`/binlog
mkdir -p `dirname $DATA_DIR`/relaylog
cp $MYSQL_DIR/mysql-$VERSION/support-files/my-small.cnf.sh `dirname $DATA_DIR`/my.cnf
cp $MYSQL_DIR/mysql-$VERSION/support-files/mysql.server.sh /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
sed -i 's/^basedir=/basedir=\/usr\/local\/mysql/' /etc/init.d/mysqld
sed -i 's/^datadir=/datadir=\/data\/mysql\/3306/' /etc/init.d/mysqld
sed -i 's/`@HOSTNAME@`/DB148/' /etc/init.d/mysqld
sh /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=$DATA_DIR &&
/etc/init.d/mysqld start
else
echo "Your input mysql version is not in $MYSQL_DIR"
fi