標籤:mysql5.7 初始 stat htm mysqld text tle rpm else
一、簡單實現mysql一鍵安裝
參考:【第二章】MySQL資料庫基於Centos7.3-部署
此指令碼前提條件是防火牆,selinux都已經設定完畢:
[[email protected] ~]# cat InstallMysql.sh #!/bin/bash#2018-09-04#旅行者-Travel#卸載內建maridbyum -y remove mari*rm -rf /var/lib/mysql/*#下載yum源URL="https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm"wget $URL -P /etc/yum.repos.d/yum -y install /etc/yum.repos.d/mysql80-community-release-el7-1.noarch.rpmyum-config-manager --disable mysql80-communityyum-config-manager --enable mysql57-communityyum -y install mysql-community-serversystemctl start mysqld systemctl enable mysqldsystemctl status mysqldif [ $? -eq 0 ];thenecho -e "install succefull"fi
二、修改Mysql初始密碼
1)由於mysql5.7初始化是有密碼的,需要找到密碼才能登陸,所以我們直接通過指令碼修改密碼,並實現登陸:
[[email protected] ~]# cat i.sh #!/bin/bashresult="`grep ‘temporary password‘ /var/log/mysqld.log`"p1="`echo $result |awk ‘{print $NF}‘`"read -p "請輸入資料庫密碼:" num1read -s -p "請輸入資料庫密碼:" num2if [ $num1 = $num2 ];then mysqladmin -uroot -p"$p1" password "$num1" echo "passwd change successful" mysql -uroot -p$num1else
2)最佳化以上指令碼實菜單操作:
[[email protected] ~]# cat ChangePasswd1.sh #!/bin/bashfunction init_passwd(){result="`grep ‘temporary password‘ /var/log/mysqld.log`"p1="`echo $result |awk ‘{print $NF}‘`"read -p "請輸入資料庫密碼:" num1read -s -p "請輸入資料庫密碼:" num2if [ $num1 = $num2 ];then mysqladmin -uroot -p"$p1" password "$num1" echo "passwd change successful" mysql -uroot -p$num1else echo "兩次輸入密碼不一致" exit 0fi}function menu(){cat<<EOF-----------主菜單-----------------1)初始化資料庫密碼2)登陸資料庫Q)退出EOFread -p "請輸入序號:" num3case $num3 in 1) init_passwd ;; 2) mysql -uroot -p$num1 ;; Q|q) exit 0 ;; *) echo "-------請輸入正確序號-------" menuesac}menu[[email protected] ~]#
可通過此指令碼來登陸資料庫效果如:
此指令碼比較粗糙,純屬為了叢集環境寫,後期最佳化>>
【shell 每日一練6】初始化安裝Mysql並修改密碼