自動化運維——一鍵安裝MySQL

根據項目須要,前段時間在搞EMM系統各類安裝包的自動化部署工做,主要包括一鍵安裝和一鍵啓動\中止功能。總結記錄下來,以供後用。
本文主要是自動安裝MySQL5.7.11版,Linux版腳本在CentOS7系統下測試經過。
流程以下:
a. 增長mysql用戶和組
b. 檢查系統是否安裝有老版MySQL,若是有就卸載。
c.  卸載OS預裝的Maria DB. 因爲MariaDB與MySQL水火不容,須將系統預裝的MariaDB卸載後再安裝mysql.
b. 安裝MySQL, 本次是採用RPM包的方式安裝,相對簡單些;固然也能夠使用源碼包編譯安裝,稍微耗時。
c. 修改配置文件my.ini,添加一條語句,增長UTF8字符支持。
d. 修改root用戶密碼及權限。先在my.ini文件中添加skip-grant-tables,使其跳過認證,而後無密碼登錄數據庫,配置密碼和權限後,要把配置文件中剛纔加入的skip-grant-tables註釋掉,不然會報錯。
e. 根據須要建立相應emm數據庫及用戶. 
f.  初始化數據庫。
#!/bin/bash

#created by Kevin 2016/03/30, modify 2016/04/20

# -----------------------------------------------------------------------------
# Installation Script for the auto-deployment EMM(Linux edition)
# -----------------------------------------------------------------------------

# -----------------------------------------------------------------------------
# shell script to install MySQL (default version mysql-community-5.7.11)


echo "-----------------------start install mysql----------------------"


# Add to mysql user and mysql group

if [ `grep "mysql" /etc/passwd | wc -l` -eq 0 ];then
echo "adding user mysql"
groupadd mysql
useradd -r -g mysql mysql
else
echo "mysql user is exist"
fi

# check installed mysql or not
for i in `rpm -qa | grep "mysql"`
do 
rpm -e --allmatches $i --nodeps
done

# Remove pre-installed on OS MariaDB if exists

for i in $(rpm -qa | grep mariadb | grep -v grep)
do
  echo "Deleting rpm --> "$i
  rpm -e --nodeps $i
done


# Install mysqlserver

rpm -ivh mysql-community-server-5.7.11-1.el7.x86_64.rpm mysql-community-client-5.7.11-1.el7.x86_64.rpm mysql-community-common-5.7.11-1.el7.x86_64.rpm mysql-community-libs-5.7.11-1.el7.x86_64.rpm


# check the installtation was successful or not 
rpm -qa |grep "mysql"
if [ $? != 0 ];then
echo "mysql install fail"| tee $mysql_instlog
exit 1
else 
echo "mysql isntall success"| tee $mysql_instlog
fi

# modify configuration files  
cd /etc/
echo "character_set_server=utf8" >> my.cnf

# startup the mysql
systemctl start mysqld 
systemctl status mysqld
/etc/init.d/mysqld start 
/etc/init.d/mysqld stop

echo "MySQL Server install successfully!"

# configuration
cat /etc/my.cnf
sed -i '/mysqld/a\skip-grant-tables' /etc/my.cnf
systemctl restart mysqld
# mysql -u root mysql
mysql -u root mysql -e "use mysql;"
# use mysql
# update mysql.user set authentication_string=password('root') where user='root' ;
mysql -u root mysql -e "update mysql.user set authentication_string=password('root') where user='root' ;"
mysql -u root mysql -e "flush privileges;"

cat /etc/my.cnf
sed -i '/skip-grant-tables/s/^/#/' /etc/my.cnf
# mysql -u root -p
# SET PASSWORD = PASSWORD('root');
mysql -u root -proot --connect-expired-password -e "SET PASSWORD = PASSWORD('root');"

# mysql -u root mysql
# use mysql;
mysql -u root -proot -e "use mysql;"
# update user set host = '%' where user ='root';
mysql -u root -proot -e "update user set host = '%' where user ='root';"
# select host, user from user; 
mysql -u root -proot -e "select host, user from user;"
# exit

mysql -u root -proot -e "source /usr/src/tools/user.sql;"
mysql -u root -proot -e "source /usr/src/tools/emm_saas_base.sql;"
# create a new database, name as "emm_saas_base"
# mysql -u root -p
# create database emm_saas_base;
# mysql -u root -proot -e "create database emm_saas_base;"
# show databases;
# mysql -u root -proot -e "show databases;"

# initdb 
# for x in find . -name "*.sql"
# do source emm_saas_base.sql
# done

# create user & authentication
# mysql -u root -p
# CREATE USER 'emm'@'%' IDENTIFIED BY 'emm';
# GRANT ALL ON *.* TO 'emm'@'%';

# show user in the DB
# select host,user from mysql.user;
mysql -u root -proot -e "select host,user from mysql.user;"

echo "The MySQL install and config complete! "
 
Windows .bat版本:
@echo off
:: created by Kevin Ji 2016/04/08,modify 2016/05/18 
:: -----------------------------------------------------------------------------
:: Installation Script for the auto-deployment EMM(Windows edition-copy)
:: -----------------------------------------------------------------------------
:: Modify EMM_Install script code for windows edition. Add to automatic configure install directory feature. 
:: creat an source package directory,name as "EMM_SRC" and an destination install directory,name as "EMM_DEST".

md C:\EMM_SRC 
md C:\EMM_DEST

:: ------------Install MySQL----------------------------------
C:
cd C:\EMM_SRC
start winrar x -r %cd%\mysql-5.7.11-winx64.zip C:\EMM_DEST
pause 

:: ------------Config MySQL environment variable---------------
rem set MYSQL_HOME=C:\mysql-5.7.11-winx64
rem set PATH=%PATH%;C:\mysql-5.7.11-winx64\bin
setx /M MYSQL_HOME C:\EMM_DEST\mysql-5.7.11-winx64
setx /M PATH %PATH%;C:\EMM_DEST\mysql-5.7.11-winx64\bin

:: ------------MySQL installation and initialization------------
xcopy %cd%\user.sql C:\EMM_DEST\mysql-5.7.11-winx64\bin\
xcopy %cd%\emm_saas_base.sql C:\EMM_DEST\mysql-5.7.11-winx64\bin\
C:
cd C:\EMM_DEST\mysql-5.7.11-winx64\bin
mysqld -install
cd C:\EMM_DEST\mysql-5.7.11-winx64\bin
mysqld --initialize 

:: ------------modify MySQL configuration file-------------------------
C:
cd C:\EMM_DEST\mysql-5.7.11-winx64\
rename C:\EMM_DEST\mysql-5.7.11-winx64\my-default.ini my.ini
echo character_set_server=utf8 >> my.ini
xcopy %cd%\my.ini C:\ /e /i /y

:: ------------modify MySQL-root password------------------------------
net stop mysql
echo skip-grant-tables >> my.ini
ping -n 2 127.0.0.1 >nul
taskkill /F /IM mysqld.exe

net start mysql
cd C:\EMM_DEST\mysql-5.7.11-winx64\bin
mysql -e "use mysql"
mysql -e "update mysql.user set authentication_string=password('root') where user='root' ;"
mysql -e "flush privileges;"

cd C:\EMM_DEST\mysql-5.7.11-winx64\
rename my.ini myold.ini
cd C:\
xcopy C:\my.ini C:\EMM_DEST\mysql-5.7.11-winx64\

:: ------------restart MySQL service-------------------------------
:: taskkill /F /IM mysqld.exe
net start mysql

:: ------------Initialization DB-----------------------------------
cd C:\EMM_DEST\mysql-5.7.11-winx64\
echo [client] >> my.ini
echo user=root >> my.ini
echo password=root >> my.ini
cd C:\EMM_DEST\mysql-5.7.11-winx64\bin
mysql --connect-expired-password -e "SET PASSWORD = PASSWORD('root');"
mysql --connect-expired-password -uroot -proot < C:\EMM_DEST\mysql-5.7.11-winx64\bin\user.sql
mysql --connect-expired-password -e "use emm_saas_base;"
mysql --connect-expired-password -uroot -proot < C:\EMM_DEST\mysql-5.7.11-winx64\bin\emm_saas_base.sql
pause
echo MySQL Install and configuration complete

 

至此完結。node

相關文章
相關標籤/搜索