【SaltStack】通過Master給Minion安裝MySQL

foreverfriends發表於2017-09-28

【Master】

IP: 192.168.236.100

【Minion】

IP: 192.168.236.101

 

 

二、配置SaltStack

關於SaltStack Master和Minion的安裝這裡不再贅述!

【配置Master】

1.新建目錄

mkdir  -p  /srv/salt/iso

mkdir -p   /srv/salt/install_mysql

2.新建配置檔案

(1)  /srv/top.sls

base:
   '*':
    - cpitables.*
    - install_zabbix.*
    - install_mysql.*

 

(2)  /srv/salt/install_mysql/install_mysql.sls

說明:該檔案內,執行install_mysql.py指令碼的順序和安裝mysql的順序不能亂,因為安裝mysql時需要mysql使用者和mysql使用者組必須存在,否則會報錯!

/soft/install_mysql.py:
  file.managed:
    - unless: test -e /soft/install_mysql.py
    - source: salt://install_mysql/install_mysql.py
    - user: root
    - group: root
    - mode: 775
  cmd.run:
    - name:  /usr/bin/python /soft/install_mysql.py
    - require:
      - file: /soft/install_mysql.py


/soft/mysql-5.7.18.tar.gz:
  file.managed:
    - unless: test -e /soft/mysql-5.7.18.tar.gz
    - source: salt://iso/mysql-5.7.18.tar.gz
    - user: root
    - group: root
    - mode: 644
  cmd.run:
    - cwd: /soft
    - name: mv mysql-5.7.18.tar.gz /usr/local && cd /usr/local && tar zxf /usr/local/mysql-5.7.18.tar.gz && mv mysql-5.7.18-linux-glibc2.5-x86_64 mysql && cd mysql && 
chown -R mysql . && chgrp -R mysql . && mkdir -p /usr/local/mysql/log && chown -R mysql:mysql /usr/local/mysql/log  && chmod 755 /usr/local/mysql/log && cd /usr/local/
mysql/log && touch mysql-error.log &&chmod 755 mysql-error.log && chown mysql:mysql mysql-error.log && /usr/local/mysql/bin/mysqld --initialize --user=mysql 
    - unless: test -d /usr/local/mysql
    - require:
      - file: /soft/mysql-5.7.18.tar.gz


/soft/edit_mysql.py:
  file.managed:
    - unless: test -e /soft/edit_mysql.py
    - source: salt://install_mysql/edit_mysql.py
    - user: root
    - group: root
    - mode: 775
  cmd.run:
    - name:  /usr/bin/python /soft/edit_mysql.py
    - require:
      - file: /soft/edit_mysql.py

 

(3)  /srv/salt/install_mysql/install_mysql.py

#!/usr/bin/python
# coding:utf-8

import socket
import os

content = '''
[client]    
port = 3306    
socket = /tmp/mysql.sock    

[mysqld]    
user=mysql    
port = 3306    
server_id = 1    
socket=/tmp/mysql.sock    
basedir =/usr/local/mysql    
datadir =/usr/local/mysql/data    
pid-file=/usr/local/mysql/data/mysqld.pid    
log-error=/usr/local/mysql/log/mysql-error.log  
    '''

def init_mysql():

    #修改mysql的配置檔案/etc/my.cnf
    with open('/etc/my.cnf','r+') as newfile:
        newfile.write(content)
        newfile.close()

    #新建mysql使用者組和mysql使用者
    os.system('groupadd mysql ')
    os.system('useradd -g mysql mysql')
init_mysql()

 

(4)  /srv/salt/install_mysql/edit_mysql.py

#!/usr/bin/python
# coding:utf-8

import os

def edit_mysql():

    #Setting mysql env variable
    os.system("sed -i 's/PATH=$PATH:$HOME\/bin/PATH=$PATH:$HOME\/bin:\/usr\/local\/mysql\/bin/'  ~/.bash_profile")
    os.system('source ~/.bash_profile')


    #Settting mysql boot
    os.system('cp /usr/local/mysql/support-files/mysql.server  /etc/init.d/mysql.server')
    os.system('service mysql.server start')
    #os.system("set password for root@localhost = password('mysql')")
    #os.system('flush privileges')

edit_mysql()

(6) 將MySQ安裝包mysql-5.7.18.tar.gz複製到目錄/srv/salt/iso

 

【Minion】

在客戶端新建目錄soft

mkdir /soft

 

三、操作說明

在Master執行如下命令進行操作:

salt '*'  state.sls install_mysql.install_mysql

'*' : 表示所有可偵測到的Minion端

state.sls :為固定用法

install_mysql :表示install_mysql.sls檔案所在的上次目錄,該目錄以/etc/salt/master檔案中定義的目錄為家目錄

install_mysql : 為操作用到的sls檔案

 

其他可能用到的命令:

檢測Master與Minion的是否連線:

salt '*'  test.ping

在安裝zabbix前進行驗證測試:

salt '*'  state.sls install_mysql.install_mysql test=True

 

 

 

相關文章