您的当前位置:首页正文

linux系统mysql一键安装脚本

2024-11-07 来源:个人技术集锦
#! /bin/bash


function install_mysql(){
  echo '开始安装...'
  
  echo '卸载mariadb数据库...'
  old=`rpm -qa | grep mariadb`
  rpm -e --nodeps $old
  
  echo '解压获取mysql安装包...'
  mkdir /root/mysql8
  tar -xvf /root/mysql-8.0.26-1.el7.x86_64.rpm-bundle.tar -C /root/mysql8
  
  echo '切换到/root/mysql8目录下,rpm开始安装...'
  cd /root/mysql8
  
  rpm -ivh mysql-community-common-8.0.26-1.el7.x86_64.rpm
  rpm -ivh mysql-community-client-plugins-8.0.26-1.el7.x86_64.rpm
  rpm -ivh mysql-community-libs-8.0.26-1.el7.x86_64.rpm
  rpm -ivh mysql-community-client-8.0.26-1.el7.x86_64.rpm
  yum install -y net-tools
  yum install -y perl
  yum install libaio
  rpm -ivh mysql-community-server-8.0.26-1.el7.x86_64.rpm
  
  echo '启动mysql服务...'
  systemctl start mysqld
  
  grep password /var/log/mysqld.log >/root/result.txt
  
  tmp_password=`cat /root/result.txt | grep password | tail -n 1 | awk -F ' ' '{print $NF}'`
  
mysql -uroot -p$tmp_password --connect-expired-password <<EOF
	set global validate_password.policy=LOW;
	set global validate_password.length=4;
	set global validate_password.mixed_case_count=0;
	set global validate_password.number_count=0;
	set global validate_password.special_char_count=0;
	
	alter user root@localhost identified by '123456';
	create user 'root'@'%' identified by  '123456';
	grant all privileges on *.* to 'root'@'%' with grant option;
	ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
	flush privileges;
	quit
EOF

}

function uninstall_mysql(){
  echo '开始卸载...'
  systemctl stop mysqld
   
  arr=`rpm -qa | grep mysql`

  for i in $arr
  do
     echo "卸载 $i "
	 rpm -e --nodeps $i
  done
  
  list2=`find / -name mysql`
  for a2 in $list2
  do
     echo "删除: $a2"
     rm -rf $a2
  done
}


if [ "$1" = "install" ];then
  install_mysql
elif [ "$1" = 'uninstall' ];then 
  uninstall_mysql
else 
  echo '无效参数'
fi

根据自己的tar包版本和路径自行修改

Top