#!/bin/bash
#版本是mysql-5.7
#!!!如果最后安装完mysql说找不到命令,可以执行一下: source /etc/profile
#本脚本是将数据放在/data 目录下
#若是需要修改
uu=/data
#先移除系统默认的
yum remove mariadb-libs -y
#开始创建Mysql的用户
useradd mysql -s /sbin/nologin
echo "获取mysql包"
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
#创建目录
mkdir -p ${uu}/mysql
mkdir -p /var/log/mysql
mkdir -p ${uu}/mysql/data
mkdir -p ${uu}/mysql/binlog
#授权目录
chown -R mysql.mysql /var/log/mysql
chown -R mysql.mysql ${uu}/mysql/
echo "开始解压"
tar -xf ./mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
echo "解压完成,开始移动"
mv mysql-5.7.30-linux-glibc2.12-x86_64 /usr/local/mysql
echo "移动重命名完成"
#添加环境变量,使mysql命令能直接使用
echo "export PATH=/usr/local/mysql/bin:\$PATH" >> /etc/profile
echo "配置文件添加完成"
source /etc/profile
#补充包
yum install libaio-devel -y
#写配置文件
cat > /etc/my.cnf << EOF
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=${uu}/mysql/data
server_id=6
log-error=/var/log/mysql/error.log
pid-file=${uu}/mysql/mysql.pid
port=3306
socket=${uu}/mysql/mysql.sock
[mysql]
socket=${uu}/mysql/mysql.sock
EOF
echo "开始初始化"
mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=${uu}/mysql/data
echo "初始化完成"
#重新加载命令
echo "配置文件写入完成"
echo “启动完成”
/usr/local/mysql/support-files/mysql.server start
echo “启动完成”
source /etc/profile
#设置密码
mysql -e 'update mysql.user set authentication_string=password("123456") where user="root" and host="localhost";'
mysql -e "flush privileges;"
echo -e '\033[31m 部署完成,root用户密码为:123456 \033[0m'
#!!!如果最后安装完,执行mysql 说找不到命令,可以执行一下: source /etc/profile