Shell脚本自动安装centos7及Mysql

发布于:2022-12-10 ⋅ 阅读:(840) ⋅ 点赞:(0)

安装centos7

function init(){

#第一步是给用户起名字

hostnamectl set-hostname $1

#第二步绑定ip跟刚才起的名字(注意传入的Ip需要跟本来的前三位相同)

sed -i '$a '$2' '$1' ' /etc/hosts

#第三步关闭防火墙

systemctl stop firewalld

systemctl disable firewalld

第四步设置相关网关信息

sed -i 's/dhcp/static/' /etc/sysconfig/network-scripts/ifcfg-ens33
    sed -i '$a IPADDR='$2'' /etc/sysconfig/network-scripts/ifcfg-ens33
    sed -i '$a NETMASK=255.255.255.0' /etc/sysconfig/network-scripts/ifcfg-ens33
    #这句话的意思是在$2输出的Ip地址管道中切割字符串
    gw=`echo $2 | awk -F. '{print $1"."$2"."$3"."2}'`
    sed -i '$a GATEWAY='$gw'' /etc/sysconfig/network-scripts/ifcfg-ens33
    sed -i '$a DNS1=114.114.114.114' /etc/sysconfig/network-scripts/ifcfg-ens33
    sed -i '$a DNS2=8.8.8.8' /etc/sysconfig/network-scripts/ifcfg-ens33
    systemctl restart network
    #download vim wget
    echo'=================download vim wget====================================='
    yum install -y wget vim
   echo "===============change yum source========================="}

init $1 $2

脚本安装Mysql

#insatll mysql 5.6
function mysqlSetup(){
#先找到mariadb卸载
mars=`rpm -qa | grep mariadb`
rpm -e --nodeps $mars
#切到opt目录下载mysql的安装包
cd /opt/
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
# 切换到下面的目录安装阿里的数据源
cd /etc/yum.repos.d/
mv CentOS-Base.repo CentOS-Base.repo.bak
 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
 yum clean all
  yum makecache
#切回到opt目录下载Mysql服务
  cd /opt/
  yum install mysql-server -y
  chown -R root:root /var/lib/mysql
  chown root /var/lib/mysql/
  sed -i '21a character-set-server=utf8' /etc/my.cnf
  echo '[client]' >> /etc/my.cnf
  echo 'default-character-set=utf8' >> /etc/my.cnf
  echo '[mysql]' >> /etc/my.cnf
  echo 'default-character-set=utf8' >> /etc/my.cnf
  service mysqld restart
  mysql -uroot -e "use mysql;update user set password=password('ok') where user='root';"
  mysql -uroot -e 'flush privileges;'
  mysql -uroot -pok -e 'GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "ok";'
  mysql -uroot -pok -e 'flush privileges;'

}
mysqlSetup

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到