#!/bin/bash
# 把安装过程写到日志文件中
touch /tmp/ins_mysql.log
echo "开始安装数据库啦!!!"
# 1、解压
mysql_path=`find / -name mysql-5.7.39*`
echo "正在解压 ... 请稍等"
tar -xvf $mysql_path &> /dev/null
echo "解压中 ... [OK]"
# 2、移动位置
mysql_jy_path=`find / -name *-x86_64`
mv $mysql_jy_path /usr/local/mysql
# 3、修改配置文件
echo "[mysqld]
datadir=/usr/local/mysql/data
port=3306" > /etc/my.conf
cd /usr/local/mysql
# 4、添加用户
useradd mysql
# 5、初始化mysql
echo "准备初始化数据库 ...请稍等"
touch /tmp/pas.txt
echo "" > /tmp/pas.txt
bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/mysql.pid --tmpdir=/tmp &> /tmp/pas.txt
echo "数据库初始化中 ... [OK]"
# 6、删除mariadb
mariadb_dep=`yum list installed |grep mariadb | awk '{print $1}'`
rpm -e --nodeps $mariadb_dep
# 7、启动
echo "准备启动数据啦,高兴吧,一定能成功的"
support-files/mysql.server start
echo "启动完成啦"
# 8、编辑环境变量
echo 'export PATH="$PATH:/usr/local/mysql/bin:/usr/local/mysql/support-files"' >> /etc/profile
source /etc/profile
# 9、修改密码
mysql_pas=`sed -n "/temporary password/p" /tmp/pas.txt | awk '{print $NF}'`
/usr/local/mysql/bin/mysql -u root -p$mysql_pas -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPassword';"
#/usr/local/mysql/bin/mysql -u root -p$mysql_pas -e "grant all privileges on *.* to root@'localhost' identified by 'NewPassword';"
/usr/local/mysql/bin/mysql -u root -p$mysql_pas -e "flush privileges;"
echo "请记住下面的密码"
echo $mysql_pas
# rm -rf /tmp/pas.txt
echo "登录测试中..."
/usr/local/mysql/bin/mysql -u root -p$mysql_pas
# 需要删除重新安装的话-停止数据库后删除/usr/local/mysql就行了
#ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPassword';
#grant all privileges on *.* to root@'localhost' identified by 'NewPassword';