Shell自动化安装mysql-glibc

tech2025-04-30  8

Shell自动化安装mysql-glibc

#!/bin/bash # Desc :GLIBC install mysql # explain : mysql版本号5.7.31 自定义目录/mysql_3306 端口号3306 自定义mysqlroot密码123456 sock套结字/tmp #安装MySQL依赖库软件 while [ -f /usr/share/doc/libaio-0.3.109/COPYING ] do a=`echo $?` if [ $a -eq 0 ];then echo "依赖软件libaio已安装" break else echo "正在安装libaio软件" yum install libaio -y fi done # 下载mysql-5.7.31软件包 while [ -f /usr/bin/wget ] b=`echo $?` do if [ $b -eq 0 ];then echo "wget软件已安装" break else echo "正在安装wget软件" yum install wget -y fi done wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz # 解压mysql软件包 echo "正在解压mysql软件包请耐心等待~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" tar -zxf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz echo "mysql-5.7.31软件包已解压完成" # 软件安装配置 #创建mysql专用账号mysql 所属主组也是mysql useradd -r -s /sbin/nologin mysql echo "mysql账号创建完成" #清空原来mariadb配置/etc/my.cnf rm -rf /etc/my.cnf echo "mariadb配置/etc/my.cnf已清空完毕" #把mysql解压后的压缩包(GBLIC版本)移动到/根目录下,然后更名为mysql_3306 mkdir /mysql_3306 mv /root/mysql-5.7.31-linux-glibc2.12-x86_64/* /mysql_3306 #切换到mysql工作目录/mysql_3306,创建一个mysql-files cd /mysql_3306 mkdir mysql-files #更改mysql-files权限 chown mysql:mysql mysql-files chmod 750 mysql-files #初始化数据库 bin/mysqld --initialize --user=mysql --basedir=/mysql_3306 >>/root/dic 2>&1 # 设置安全加密连接(SSL) bin/mysql_ssl_rsa_setup --datadir=/mysql_3306/data echo "安全加密连接已完成" # 启动MySQL数据库 cp support-files/mysql.server /etc/init.d/mysql_3306 sed -ri '/^basedir/c\basedir=/mysql_3306/' /etc/init.d/mysql_3306 sed -ri '/^datadir/c\datadir=/mysql_3306/data' /etc/init.d/mysql_3306 # 启动MySQL数据库 service mysql_3306 start chkconfig --add mysql_3306 chkconfig mysql_3306 on echo "mysql数据库已启动并添加到开机启动项" # MySQL GLIBC版本后续配置 #更改管理员root账号的密码 passwd=`awk -F "host: " '/root@localhost/{print $2}' /root/dic` yum install expect -y expect << EOF cd /mysql_3306/ spawn bin/mysqladmin -uroot password 123456 -p expect { "Enter password:" { send "$passwd\r" }; } expect eof EOF # 把mysql客户端命令添加到环境变量,以后随时可以访问 echo 'export PATH=$PATH:/mysql_3306/bin' >> /etc/profile source /etc/profile # 手工定义MySQL的配置文件 cat > /mysql_3306/my.cnf <<EOF [mysqld] basedir=/mysql_3306 datadir=/mysql_3306/data socket=/tmp/mysql.sock EOF echo "/mysql_3306/my.cnf已配置完成 正在重启" # 安全配置 expect << EOF spawn mysql_secure_installation expect { "Enter password for user root:" { send "123456\r";exp_continue } "Press y|Y for Yes, any other key for No:" { send \r;exp_continue } "Change the password for root ? ((Press y|Y for Yes, any other key for No) :" { send "\r";exp_continue } "Remove anonymous users? (Press y|Y for Yes, any other key for No) :" { send "y\r";exp_continue } "Disallow root login remotely? (Press y|Y for Yes, any other key for No) :" { send "y\r";exp_continue } "Remove test database and access to it? (Press y|Y for Yes, any other key for No) :" { send "y\r";exp_continue } "Reload privilege tables now? (Press y|Y for Yes, any other key for No) :" { send "y\r";exp_continue }; } expect eof EOF mysql -uroot -p

刚接触写的不好勿喷

最新回复(0)