You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
shell-project/database/install_db_v3.sh

116 lines
3.5 KiB

2 years ago
#!/usr/bin/bash
:<<!
此版本修改了进行了更多的判断和优化处理,使程序运行更加稳定
关于密码获取问题,采用了日志形式,增加了手动方式,如果自动初始化密码失败,可以采用手动修改方式。
!
# 数据库密码
custom_pass='QianFeng@123'
# 数据库安装位置
install_path=/usr/local/mysql
cmd_path=${install_path}/bin
tmp_path=/usr/local/tmp
log_path=/usr/local/log
# 定义清理环境的函数
clear_env(){
yum erase mariadb mariadb-server mariadb-libs mariadb-devel -y
userdel -r mysql
rm -rf /etc/my*
rm -rf /var/lib/mysql
if [ -e $install_path ];then
mv $install_path ${install_path}.bak
fi
echo "系统环境清理完成"
sleep 1
}
init_env(){
useradd -s /sbin/nologin mysql &>/dev/null
mkdir -p /usr/local/{mysql,log,tmp}
echo "系统环境初始化完成"
sleep 1
}
install_pack(){
if [ ! -e mysql-boost-5.7.27.tar.gz ];then
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.27.tar.gz
fi
tar xvzf mysql-boost-5.7.27.tar.gz && \
cd ./mysql-5.7.27/
yum -y install ncurses ncurses-devel openssl-devel bison gcc gcc-c++ make cmake && \
echo "软件包处理完成"
sleep 1
}
make_software(){
cmake . -DWITH_BOOST=boost/boost_1_59_0/ -DCMAKE_INSTALL_PREFIX=$install_path -DSYSCONFDIR=/etc -DMYSQL_DATADIR=$install_path/data -DINSTALL_MANDIR=/usr/share/man -DMYSQL_TCP_PORT=3306 -DMYSQL_UNIX_ADDR=$tmp_path/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 && \
make -j `cat /proc/cpuinfo |grep process|wc -l` && make install && \
echo "编译安装完成"
cat > /etc/my.cnf <<EOF
[mysqld]
basedir = $install_path #安装目录
datadir = $install_path/data #数据存放目录
tmpdir = $tmp_path #/tmp缓存目录
socket = $tmp_path/mysql.sock #指定socket文件的位置
pid_file = $tmp_path/mysqld.pid #指定pid文件的位置
log_error = $log_path/mysql_error.log #错误日志的位置
slow_query_log_file = $log_path/slow_warn.log #慢日志查询
server_id = 1 #server-id=??
user = mysql #指定用户
port = 3306 #指定端口
bind-address = 0.0.0.0 #监听地址(允许所以ip访问)
character-set-server = utf8 #字符集
default_storage_engine = InnoDB #引擎
EOF
sleep 1
}
init_db(){
chown -R mysql.mysql /usr/local/{mysql,log,tmp}
pkill -9 mysql
rm -rf /usr/local/mysql/data/*
${cmd_path}/mysqld --initialize --user=mysql --basedir=$install_path --datadir=$install_path/data
password=$(grep "temporary password" $log_path/mysql_error.log |awk '{a=$NF}END{print a}')
${cmd_path}/mysqld_safe --user=mysql &
sleep 10
${cmd_path}/mysqladmin -uroot -p"$password" password $custom_pass
cat > /etc/profile.d/mysql.sh <<EOF
export PATH=${cmd_path}:\$PATH
EOF
}
info(){
cat <<EOF
mysql 安装完成
安装目录: $install_path
命令目录: $cmd_path
初始密码: $custom_pass
数据目录: ${install_path}/data
注意:已修改环境变量,需退出终端重新登录
EOF
}
main(){
clear_env
init_env
install_pack
make_software
init_db
info
}
case $1 in
install)
echo "开始编译安装mysql"
main
;;
uninstall)
echo "开始卸载yum 数据库环境"
clear_env
;;
*)
echo "Usage $0 sh $0 [install|uninstall]"
esac