shell一键安装mysql,亲测可用

安装mysql过程比较繁琐,直接写个脚本一键安装,方便快捷。
亲测可用。可以直接拿去执行。

#!/bin/bash
# 一键安装mysql5.6

MYSQL_INSTALL_DIR=/usr/programs/soft
  # mysql安装目录
IS_CINTINUE='y';

# 下载mysql
install_mysql() {
        cd /data/soft
        read -p "是否下载?【y/n】" IS_CINTINUE
        if [[ $IS_CINTINUE == 'y' ]]; then
        #       yum install -y wget && wget -c http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.29.tar.gz;
                cp mysql-5.6.29.tar.gz $MYSQL_INSTALL_DIR
        else
                echo "请您自行下载安装包!";
                exit;
        fi
}

# 先切换到用户目录
cd ~
echo "======> 检测 $MYSQL_INSTALL_DIR 目录是否存在 <======";
if [ -d $MYSQL_INSTALL_DIR ]; then
        echo "======> success! 目录已经存在! <======";
else
        echo "======> warning! 目录不存在,将创建! <======";
        mkdir -p $MYSQL_INSTALL_DIR
fi

if [ $# != 0 ]; then
        if [ -f $1 ]; then
                echo "======> 将安装文件复制到制定目录 <======";
                cp $1 $MYSQL_INSTALL_DIR
                if [ $? -eq 0 ]; then
                        echo "======> 复制成功! <======";
                        sleep 2s
                else
                        echo "======> 复制失败! <======";
                        install_mysql
                        exit;
                fi
        else
                echo "======> error! 文件不存在 <======";
                install_mysql

        fi
else
        install_mysql
fi


echo "======> 依赖安装! <======";
yum -y install gcc make cmake ncurses-devel libxml2-devel libtool-ltdl-devel gcc-c++ autoconf automake bison zlib-devel
echo "======> 解压文件 <======";
cd $MYSQL_INSTALL_DIR && tar -zxvf mysql-5.6.29.tar.gz
if [ $? -eq 0 ];then
        echo "======> 解压成功! <======";
        sleep 2s
        cd mysql-5.6.29
        echo "======> 安装前的设置 <======";
        cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
        -DMYSQL_DATADIR=/usr/local/mysql/data \
        -DSYSCONFDIR=/etc \
        -DWITH_MYISAM_STORAGE_ENGINE=1 \
        -DWITH_INNOBASE_STORAGE_ENGINE=1 \
        -DWITH_MEMORY_STORAGE_ENGINE=1 \
        -DWITH_READLINE=1 \
        -DMYSQL_UNIX_ADDR=/tmp/mysqld.sock \
        -DMYSQL_TCP_PORT=3306 \
        -DENABLED_LOCAL_INFILE=1 \
        -DWITH_PARTITION_STORAGE_ENGINE=1 \
        -DEXTRA_CHARSETS=all \
        -DDEFAULT_CHARSET=utf8 \
        -DDEFAULT_COLLATION=utf8_general_ci
        if [ $? -eq 0 ];then
                echo "======> 设置成功!开始编译安装,大约需要20分钟,耐心等待...<======";
                sleep 2s
                make && make install
                if [ $? -eq 0 ]; then
                        cd ~
                        echo "======> 编译成功!开始设置组和用户! <======";
                        sleep 2s
                        groupadd mysql &&
                        useradd mysql -g mysql -M -s /sbin/nologin &&
                        chown mysq:mysql /usr/local/mysql
                else
                        echo "======> 编译失败! <======";
                        exit;
                fi
        else
                echo "======> 设置失败! <======";
                exit;
        fi
else
        echo "======> 解压失败!<=======";
        exit;

fi


echo "======> 开始配置mysql <======";
cd /usr/local/mysql

echo "======> 初始化mysql <======";
./scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql/data
cp support-files/my-default.cnf  /etc/my.cnf

sleep 2s

echo "======> 开始修改mysql的配置文件 <======";
sed -i 's/\# basedir \= ...../basedir \= \/usr\/local\/mysql/g' /etc/my.cnf
sed -i 's/\[mysqld\]/&\ndefault-storage-engine=InnoDB/' /etc/my.cnf
sed -i 's/\[mysqld\]/&\nlower_case_table_names=1/' /etc/my.cnf


echo "======> 制作服务启动 <======";
/bin/cp -rf support-files/mysql.server /etc/init.d/mysqld
if [ $? -eq 0 ]; then
        echo "======> 服务设置成功! <======";
        cd ~
        service mysqld start
        sleep 2s
        service mysqld stop
        sleep 2s
else
        echo "======> 服务设置失败! <======";
fi

echo "======> 添加到开机启动项! <======";
chkconfig --add mysqld
if [ $? -eq 0 ]; then
        echo "======> 设置开机启动项成功! <======";
else
        echo "======> 设置开机启动项失败! <======";
fi


service mysqld stop
echo "======> 配置环境变量 <======";
echo "export PATH=$PATH:/usr/local/mysql/bin/" >>/etc/profile
source /etc/profile

echo "启动mysql服务 ======>"service mysqld start


cd /usr/local/mysql
echo "======> 设置root密码为: “123456” <======";
./bin/mysqladmin -u root password "123456"
if [ $? -eq 0 ];then
        echo "设置root密码成功!";
else
        echo "设置root密码失败!";
fi
source /etc/profile

验证一下,方法如下,可以看到mysql已经启动起来了。

[root@one shell]# service mysqld status
 SUCCESS! MySQL running (1392)

你可能感兴趣的:(shell一键安装mysql,亲测可用)