最近在研究saltstack,搞了一下自动安装mysql,弄的云里雾里的,参照网上的教程,大部分都是有部分错误的,可能和saltstack版本有关系,折腾了半天,搞定了;

    具体配置如下:

    利用saltstack在OEL6.7系统上安装源码mysql,(CentOS5的系统没试过,安装时可能会提示缺少依赖包的错)。系统初始化的sls文件均放置到/srv/salt目录下,在Master端配置文件中打开file_roots目录。

[root@cacti salt]# cat top.sls 

base:

   '*':

       - mysql   #引用mysql目录下的sls文件

创建/srv/salt/mysql目录;

[root@cacti salt]# tree mysql

mysql

├── conf.sls

├── conf.sls.bak

├── files

│?? ├── conf.sh

│?? ├── my.cnf

│?? ├── mysql-5.5.37.tar.gz

│?? ├── mysqld

│?? └── mysqllns.sh

├── init.sls

├── install.sls

└── install.sls.bak

[root@cacti mysql]# cat init.sls  #引用conf.sls和install.sll文件

include:

  - mysql.conf

  - mysql.install

[root@cacti mysql]# cat install.sls

#mysql_source

mysql_source:

  file.managed:

    - name: /home/mysql-5.5.37.tar.gz

    - unless: test -e /home/mysql-5.5.37.tar.gz

    - source: salt://mysql/files/mysql-5.5.37.tar.gz

    - user: root

    - group: root

    - mode: 644

#extract_mysql

extract_mysql:

  cmd.run:

    - name: cd /home && tar zxf mysql-5.5.37.tar.gz && chown root.root /home/mysql-5.5.37

    - unless: test -d /home/mysql-5.5.37

    - require:

        - file: mysql_source

#user

mysql_user:

  user.present:

    - name: mysql

    - uid: 1024

    - createhome: True

    - gid_from_name: True

    - shell: /sbin/nologin

#mysql_pkg

mysql_pkg:

  pkg.installed:

    - pkgs:

      - gcc

      - gcc-c++

      - autoconf

      - automake

      - openssl

      - openssl-devel

      - zlib

      - zlib-devel

      - ncurses-devel

      - cmake

##mysql_commpile

mysql_commpile:

  cmd.run:

    - name: cd /home/mysql-5.5.37 && cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/home/data -DSYSCONFDIR=/etc && make && make install

    - require:

        - cmd: extract_mysql

        - pkg: mysql_pkg

    - unless: test -d /usr/local/mysql 

[root@cacti mysql]# cat conf.sls

include:

  - mysql.install

/etc/my.cnf:

  file.managed:

    - name: /etc/my.cnf

    - user: root

    - mode: 755

    - source: salt://mysql/files/my.cnf

salt://mysql/files/conf.sh:  #执行mysql初始化数据,不然启动时提示错误:缺少PID文件

  cmd.script:

    - env:

      - BATCH: 'yes'

    - require:

      - cmd: mysql_commpile

      - pkg: mysql_pkg

mysql_service:

  file.managed:

    - name: /etc/init.d/mysqld

    - user: root

    - mode: 755

    - source: salt://mysql/files/mysqld

  cmd.run:

    - names:

      - /sbin/chkconfig --add mysqld

#      - /sbin/chkconfig --level 35 mysqld on

    - unless: /sbin/chkconfig --list mysqld

  service.running:

    - name: mysqld

    - enable: True

    - reload: True

    - watch:

        - file: mysql_service

最后在salt服务端执行:salt 'minions' state.highstate test=True测试安装过程,然后执行

salt 'minions' state.highstate就开始安装了,安装时间较长

saltstck源码安装mysql_第1张图片