shell安装mysql

1.执行/bin/bash install_mysql.sh | tee /opt/logs/mysql.log

2. install_mysql.sh内容如下:

#!/bin/bash

# Author: zhangdc
# DESC: 部署mysql

echo "如果/opt/soft/mysql/不存在创建"
if [ ! -d "/opt/soft/mysql/" ];then
mkdir -p /opt/soft/mysql/
fi

echo "如果/home/mysql/data/不存在创建"
if [ ! -d "/home/mysql/data" ];then
mkdir -p /home/mysql/data
fi

echo "解压源文件到/opt/soft/mysql"
tar -xvf /opt/soft/mysql-8.0.15-1.el7.x86_64.rpm-bundle.tar -C /opt/soft/mysql >/dev/null 2>&1

echo "验证系统是不是已经安装mariadb-libs"
rpm -qa|grep mariadb-libs
if [ $? -ne 1 ];then
rpm -e --nodeps mariadb-libs
if [ $? -ne 0 ];then
echo "mariadb-libs删除失败"
fi
fi

echo "开始安装mysql库"
rpm -ivh /opt/soft/mysql/mysql-community-common-8.0.15-1.el7.x86_64.rpm --nodeps --force
if [ $? -ne 0 ];then
echo "安装mysql-community-common失败"
fi
rpm -ivh /opt/soft/mysql/mysql-community-libs-8.0.15-1.el7.x86_64.rpm --nodeps --force
if [ $? -ne 0 ];then
echo "安装mysql-community-libs失败"
fi

rpm -ivh /opt/soft/mysql/mysql-community-libs-compat-8.0.15-1.el7.x86_64.rpm --nodeps --force
if [ $? -ne 0 ];then
echo "安装mysql-community-libs-compat失败"
fi
rpm -ivh /opt/soft/mysql/mysql-community-client-8.0.15-1.el7.x86_64.rpm --nodeps --force
if [ $? -ne 0 ];then
echo "安装mysql-community-client失败"
fi
rpm -ivh /opt/soft/mysql/mysql-community-devel-8.0.15-1.el7.x86_64.rpm --nodeps --force
if [ $? -ne 0 ];then
echo "安装mysql-community-devel失败"
fi
rpm -ivh /opt/soft/mysql/mysql-community-server-8.0.15-1.el7.x86_64.rpm --nodeps --force
if [ $? -ne 0 ];then
rpm -e --nodeps mariadb-libs
echo "安装mysql-community-server失败"
fi

echo "mysql授权"
chmod -R 777 /home/mysql;
chown -R mysql:mysql /home/mysql;

echo "mysql初始化"
mysqld --initialize --user=mysql --datadir=/home/mysql/data --basedir=/home/mysql --lower-case-table-names=1;
echo "替换配置文件"
mv /etc/my.cnf /etc/my.cnfbak
cp -f /opt/conf/my.cnf /etc/
echo "mysql启动服务"
systemctl start mysqld;
echo "mysql安装完成"
echo "修改密码"
findpass=$(grep 'temporary password' /var/log/mysqld.log)
initpass=${findpass##* }
echo $initpass
NEWPASS="dhccvte"
echo $NEWPASS
SQL="ALTER USER 'root'@'localhost' IDENTIFIED BY \"${NEWPASS}\";flush privileges;"
echo $SQL
mysql --connect-expired-password --password="$initpass" -e "${SQL}"
if [ $? -eq 0 ];then
echo "change password success"
else
echo "change password fail"
exit
fi

echo "修改远程连接"
mysql -uroot -pdhccvte -e "
tee /tmp/temp.log
create user 'root'@'%' identified with mysql_native_password by 'dhccvte';
grant all privileges on *.* to 'root'@'%' with grant option;
flush privileges;
notee
quit"

3.验证

 mysql -u root -pdhccvte

4./opt/conf/my.cnf文件内容

posted @ 2021-01-12 16:27  ToBeContinue得  阅读(450)  评论(0编辑  收藏  举报