"Shell daily Practice 6" Initialize install MySQL and change password

Source: Internet
Author: User

One, simple implementation of MySQL one-click installation

Reference:"chapter II" MySQL database based on centos7.3-deployment

This script is conditional on the firewall, and SELinux is already set up:

[Email protected] ~]#CatInstallmysql.SH #!/bin/bash#2018- the-Geneva#旅行者-travel# unloading with 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 STA Tus mysqldif [$?-eq 0];thenecho-e "Install succefull" fi
Second, modify the MySQL initial password

1) Since mysql5.7 initialization is a password, need to find a password to login, so we directly through the script to modify the password, and achieve login:

[Email protected] ~]#CatI.SH #!/bin/Bashresult="' grep ' temporary password '/var/log/mysqld.log '"P1="' echo $result |awk ' {print $NF} '"Read-P"Please enter the database password:"Num1read-s-p"Please enter the database password:"num2if[$num 1 = $num 2]; Thenmysqladmin-uroot-p"$p 1"Password"$num 1"     Echo "passwd Change Successful"MySQL-uroot-P$num1Else

2) Optimize the above script real menu operation:

[Email protected] ~]#CatChangePasswd1.SH #!/bin/Bashfunctioninit_passwd () {result="' grep ' temporary password '/var/log/mysqld.log '"P1="' echo $result |awk ' {print $NF} '"Read-P"Please enter the database password:"Num1read-s-p"Please enter the database password:"num2if[$num 1 = $num 2]; Thenmysqladmin-uroot-p"$p 1"Password"$num 1"     Echo "passwd Change Successful"MySQL-uroot-P$num1Else    Echo "two times the input password is inconsistent"Exit0fi}functionmenu () {Cat<<EOF-----------Main Menu-----------------1) initializes the database password2) Login Database Q) exit Eofread-P"Please enter the serial number:"num3 Case$num 3inch     1) init_passwd;; 2) MySQL-uroot-p$num1;; Q|q) Exit0    ;; *)    Echo "-------Please enter the correct serial number-------"MenuEsac}menu[[email protected]~]#

This script can be used to log into the database effects such as:

This script is rough, is purely for the cluster environment write, post-optimization >>

"Shell daily Practice 6" Initialize install MySQL and change password

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.