自动化运维——一键安装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\binxcopy %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-winx64rename 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-winx64rename 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-winx64echo [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

至此完结。

时间: 2024-09-30 04:45:07

自动化运维——一键安装MySQL的相关文章

Ansible自动化运维的安装及常用模块解释

(一)前言: Ansible是今年来越来越火的一款开源运维自动化工具,通过Ansible可以实现运维自动化,提高运维工程师的工作效率,减少人为失误.Ansible通过本身集成的非常丰富的模块可以实现各种管理任务,其自带模块超过上千个.更为重要的是,它操作简单,但提供的功能又非常丰富,在运维领域,几乎可以做任何事. Ansible自2012年发布以来,很快在全球流行,其特点如下: Ansible基于Python开发,运维工程师对其二次开发相对比较容易: Ansible丰富的内置模块,几乎可以满足一

Ansible自动化运维的安装及常用模块详解

Ansible作为今年来越来越火的一款开源运维自动化工具,通过Ansible可以实现运维自动化,提高运维工程师的工作效率,减少人为失误.Ansible通过本身集成的非常丰富的模块可以实现各种管理任务,其自带模块超过上千个.更为重要的是,它操作简单,但提供的功能又非常丰富,在运维领域,几乎可以做任何事..Ansible自2012年发布以来,很快在全球流行,其特点如下: 1.Ansible基于Python开发,运维工程师对其二次开发相对比较容易2.Ansible丰富的内置模块,几乎可以满足一切要求3

自动化运维工具安装部署 chef (二) - server的安装

经过一个晚上的下载,终于下载好了 安装前先抓个虚拟机镜像 原始文档 https://docs.chef.io/install_server.html 开始安装 安装完成 初始化下 [[email protected]_server chef]# chef-server-ctl reconfigure 初始化失败鸟.... common_name is required 用vi改了 /etc/hosts,ping了一下自己配置的FQDN名,通的,再次重新配置一样错误 谷歌了下错误,输入 hostn

自动化运维工具安装部署 chef (四) - node的安装

原文在此 https://docs.chef.io/nodes.html 节点分好几种 又加了一台机器 [[email protected] ~]# cat /etc/hosts先确保hosts里面都有解析 [[email protected] chef]# scp chefdk-2.4.17-1.el7.x86_64.rpm [email protected]:/root/chef 先安装DK rpm –ivh chefdk-2.4.17-1.el7.x86_64.rpm? 安装之后验证下 e

自动化运维工具安装部署 chef (六)命令行总结

抽空继续chef的实验,理解了它的架构,测试环境重新推到重来 server yum install vsftpd -y vi /etc/vsftp/ftp_user vi /etc/vsftp/user_list systemctl stop firewalld systemctl disable firewalld setenforce 0 vi /etc/selinux/config systemctl restart vsftpd systemctl enable vsftpd vi /e

自动化运维工具安装部署 chef (五) - node的注册和troubleshooting

上次卡在了pem认证,找了官网文档,解决了401认证错误 修改 knife.rb current_dir = File.dirname(__FILE__) log_level :info log_location STDOUT node_name "admin" client_key "#{current_dir}/admin.pem" chef_server_url "https://chefserver/organizations/pactera&qu

saltstack 自动化运维安装

saltstack --自动化运维 saltstack-master 安装:yum -y install salt-mastersaltstack-master配置:如下图,修改 salt-master本身监听地址sed -i 's/#interface: 0.0.0.0/interface: 192.168.1.26/g' /etc/salt/mastersed -e '/^$/d;/^#/d' /etc/salt/master #查看配置 可以在master 端使用自动认证(可选)vim /

应用软件学习总结(包括监控,自动化运维工具,WEB服务等)

应用软件学习总结 目录 SVN部署及使用 一健安装Zabbix Ansible自动化运维工具安装及使用 PPTP服务器端部署到客户端连接使用 fail2ban安装配置 kvm安装使用 LVS+Keepalived搭建 Keepalived+LVS维护技巧与调优 LVS概念类型及三种工作模式和十种调度算法介绍 redis单实例.多实例.主从同步实战 整合Nginx和Tomcat Nginx服务器的搭建 安装LAMP 一健安装LNMP 生产环境Nginx配置文件 Apache安装配置解析 Rsync

运维自动化之ansible playbook安装mysql

上次介绍了如何使用ansible playbook安装zabbix客户端(http://dl528888.blog.51cto.com/2382721/1436745),这次介绍一下如何使用playbook安装mysql. 下面是安装mysql的信息: mysql_basedir: /data/mysql/basedir                    源码目录 mysql_datadir: /data/mysql/datadir                    数据目录 mysql