mysql 5.7自动安装脚本

时间:2022-08-05 21:36:56

 

mysql_version=mysql-5.7.23-linux-glibc2.12-x86_64
install_pack=/root/$mysql_version.tar.gz
# 将mysql安装包复制到安装目录并解压
cp $install_pack /usr/local/
cd /usr/local && tar -xvf $install_pack
rm -f $mysql_version.tar.gz

# 建立安装路径软连接
ln -s $mysql_version mysql


#添加mysql用户和用户组
groupadd mysql
useradd -r -g mysql -s /bin/false mysql

#生成mysql数据导入路径
cd mysql
mkdir mysql-files
chown mysql:mysql mysql-files
chmod 750 mysql-files

#生成数据库数据路径并赋权限
datadir=/home/mysqldata
basedir=/usr/local/mysql
mkdir $datadir
chown mysql:mysql $datadir

# 生成mysql配置文件
cat > /etc/my.cnf << EOF
[mysqld]
basedir=$basedir
datadir=$datadir
socket=$datadir/mysql.sock
port=3306
[client]
socket=$datadir/mysql.sock
EOF

# 将mysql.sh中的path加载到登陆用户的环境变量中
cat > /etc/profile.d/mysql.sh << EOF
export PATH=$PATH:$basedir/bin
EOF

# 初始化数据库 -insecure 无密 basedir:安装路径 datadir:数据路径
bin/mysqld --initialize-insecure --user=mysql --basedir=$basedir --datadir=$datadir

#配置加密连接
bin/mysql_ssl_rsa_setup

# mysql 数据库启动
bin/mysqld_safe --user=mysql &


#wait for mysqld
while :
do
    $basedir/support-files/mysql.server status | grep SUCCESS && break
    sleep 1
done

# 配置远程访问权限
cat > init_root.sql << EOF
delete from mysql.user where host='localhost' and user='root';
grant all on *.* to 'root'@'%' identified by '123456' WITH GRANT OPTION;
flush privileges;
EOF
bin/mysql -u root --skip-password < init_root.sql
rm init_root.sql

#shutdown firewalld
systemctl stop firewalld
systemctl disable firewalld


# 将mysql.server托管到systemd
cp support-files/mysql.server /etc/init.d/mysqld

# 配置mysql的开机启动
chkconfig --add mysqld
chkconfig mysqld on


systemctl restart mysqld