博主本人热爱学习,读者阅读过程中如果发现有错误的地方或是有更好的实现方式,请与本人联系(qq:1805608587),或是在评论区留言,谢谢!
文章说明:本文是作者原创,请尊重个人劳动成果,转载需注明出处
要求:rpm包已经上传至/opt/mysql目录下面,现在需要自动安装MySQL,并且将root初始密码设置成为123456。
背景:连续帮忙几个同事安装了MySQL,感觉挺烦人的,于是就写了自动化部署脚本。再有人过来找我,我就让他们自己上传软件包跑脚本。
参考代码:
#!/bin/bash package_dir="/opt/mysql" cd ${package_dir} server=`find -maxdepth 1 -name '*mysql*'|grep server|awk -F "/" '{print $2}'` client=`find -maxdepth 1 -name '*mysql*'|grep client|awk -F "/" '{print $2}'` echo $server echo $client function install_mysql(){ echo "正在安装MySQL-server..." echo "" echo "正在卸载系统自带数据库" yum remove all mysql -y rm -rf /var/lib/mysql/* rm -rf /etc/my.cnf rm -rf /etc/init.d/mysqld rm -rf /var/log/mysql* timedatectl set-timezone Asia/Shanghai yum install libaio\* apt-get\* numactl -y clear echo "正在安装server..." rpm -ivh $server --force --nodeps if [ $? -ne 0 ];then break else clear echo "MySQL-server安装完成!" fi echo "正在安装MySQL-client..." rpm -ivh $client --force --nodeps if [ $? -ne 0 ];then break else clear echo "MySQL-client安装完成!" fi } function auto_start(){ echo "正在设置开机启动..." chmod +x /etc/rc.d/rc.local #因为有一次发现软连接失效了,所以还是直接使用这个文件保险 chmod +x /etc/rc.local sed -i '$a\mkdir -p /var/run/mysqld/' /etc/rc.d/rc.local sed -i '$a\service mysqld start' /etc/rc.d/rc.local sed -i '$a\chmod 777 /var/run/mysqld/' /etc/rc.d/rc.local echo "开机启动设置完成!" } function startup(){ echo "正在启动数据库..." service mysqld start if [ $? -eq 0 ]; then if netstat -ntulp |grep -q 3306 then echo "启动成功!" else echo "数据库启动失败,请检查日志..." fi else echo "数据库启动失败,请检查日志" fi } function reset_password(){ read -p "修改密码请按:enter 键(首次登录修改密码请按1)" option if [ $option == "1" ]; then echo "正在重置密码(仅限第一次修改密码使用)..." sed -i '$a validate-password=off' /etc/my.cnf service mysqld restart old_password=`grep 'temporary password' /var/log/mysqld.log|awk '{print $NF}'` echo "" echo -e "\033[5;34m 首次登录修改密码的命令为:\033[0m \033[31m ALTER USER 'root'@'localhost' IDENTIFIED BY \033[0m '\033[32mnew_password\033[0m' " echo "" sleep 3 mysql -uroot -p"${old_password}" echo "重置完成!" else read -p "请输入原密码:" pswd echo -e "\033[5;34m 修改密码的命令为:\033[0m \033[31m update mysql.user set authentication_string=password('new_password') where User='USER'\033[0m " echo "" mysql -uroot -p"${pswd}" echo "密码重置完成!" fi } function menu { clear echo echo -e "\t\t\tMySQLauto install Menu\n" echo -e "\t1. MySQL软件安装" echo -e "\t2. 设置开机启动" echo -e "\t3. 启动数据库" echo -e "\t4. 重置数据库密码" echo -e "\t0. Exit program\n\n" echo -en "\t\tEnter option: " read -n 1 option } while [ 1 ] do menu case $option in 0) break ;; 1) echo "" install_mysql;; 2) echo "" auto_start ;; 3) echo "" startup ;; 4) echo "" reset_password ;; *) clear echo "Sorry, wrong selection";; esac echo -en "\n\n\t\t\tHit any key to continue" read -n 1 line done clear
来源:https://www.cnblogs.com/tanshouke/p/12579273.html