#!/bin/bash
echo "+--------------------------+"
echo "| a.编译安装mysql |"
echo "| q.退出 |"
echo "+------------------------- +"
panduan(){
if [ $? -eq 0 ]
then
echo $1
else
echo $2
exit 99
fi
}
install(){
echo "清理环境中......"
yum erase mariadb mariadb-server mariadb-libs mariadb-devel -y &> /dev/null
userdel -r mysql &> /dev/null && rm -rf /etc/my* && rm -rf /var/lib/mysql &> /dev/null
useradd -r mysql -M -s /bin/false &> /dev/null
panduan "清理成功" "清理失败"
echo "下载安装包中......"
yum -y install wget &> /dev/null
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-boost-5.7.44.tar.gz &> /dev/null
panduan "下载成功" "下载失败"
echo "下载编译工具中......"
yum -y install ncurses ncurses-devel openssl-devel bison gcc gcc-c++ make cmake &> /dev/null
panduan "下载成功" "下载失败"
mkdir -p /usr/local/mysql/log &> /dev/null
touch /usr/local/mysql/log/error.log
echo "解压安装包中......."
tar xzvf mysql-boost-5.7.44.tar.gz -C /usr/local/ &> /dev/null
panduan "解压成功" "解压失败"
cd /usr/local/mysql-5.7.44/
echo "编译安装中......."
cmake . \
-DWITH_BOOST=boost/boost_1_59_0/ \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DSYSCONFDIR=/etc \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DINSTALL_MANDIR=/usr/share/man \
-DMYSQL_TCP_PORT=3306 \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_READLINE=1 \
-DWITH_SSL=system \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 &> /dev/null
echo "当前时间: `date +%T`,时间有点长请耐心等待(大约半小时)......."
make &> /dev/null
make install &> /dev/null
panduan "成功" "失败"
cd /usr/local/mysql
chown -R mysql.mysql .
echo "初始化......"
./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --log-error=/usr/local/mysql/log/error.log &> /dev/null
panduan "初始化成功" "初始化失败"
cat > /etc/my.cnf <<-EOF
[client]
port = 3306
socket = /tmp/mysql.sock
default-character-set = utf8
[mysqld]
port = 3306
user = mysql
log-error=/usr/local/mysql/log/error.log
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
socket = /tmp/mysql.sock
character_set_server = utf8
EOF
cd /usr/local/mysql
echo "启动Mysql中......."
./bin/mysqld_safe --user=mysql & &> /dev/null
sleep 5
panduan "启动成功" "启动失败"
echo
echo "+-----------------更改初始密码------------------+"
echo "| 1.密码的最小长度是8个字符 |"
echo "| 2.密码中至少需要包含1个大写字母 |"
echo "| 3.密码中至少需要包含1个数字 |"
echo "| 4.密码中至少需要包含1个特殊字符 |"
echo "+-----------------------------------------------+"
echo
password=`grep root@localhost /usr/local/mysql/log/error.log | awk '{print $NF}'`
read -p "请输入你要设置的密码:" new_password
/usr/local/mysql/bin/mysqladmin -u root -p"$password" password "$new_password"
echo "------------------------------------------------"
echo
echo "安装成功,您现在可以登录啦!"
echo
echo 登录命令提示:/usr/local/mysql/bin/mysql -uroot -p"'你的密码'"
}
read -p "请输入你的选择:" num
if [ "$num" == "a" ]
then
install
elif [ "$num" == "q" ]
then
exit 0
else
echo "请输入正确的选择!"
fi