环境:
salt-master:centos7.8
salt-minion(2台):centos6.10
salt-minion(1台):centos7.8

关闭selinux和firewalld,iptables开放4505和4506端口

目的:通过salt-master实现对salt-minion的批量自动安装mysql

步骤(都在master执行):
1,检测连通性
salt '*' test.ping
2,下载mysql的源码包至/srv/salt/mysql/files/
/srv/salt/mysql/files/mysql-5.6.22.tar.gz
3,编辑安装文件:/srv/salt/mysql/
[20:33:20-root@jack mysql]#tree -L 1
.
├── cmake.sls
├── conf
├── files
└── mysql.sls
主要cmake.sls和mysql.sls文件
cmake.sls安装编译软件包
mysql.sls编译安装mysql
[20:33:29-root@jack mysql]#cat cmake.sls
cmake-install:
pkg.installed:

  • pkgs:
    • cmake
    • gcc-c++
    • ncurses-devel
    • git
    • perl-devel
      [20:35:17-root@jack mysql]#cat mysql.sls
      mysql-install:
      file.managed:
  • name: /mnt/mysql-5.6.22.tar.gz
  • source: salt://mysql/files/mysql-5.6.22.tar.gz

    cmd.run:

  • name: cd /mnt/ && tar xzvf mysql-5.6.22.tar.gz && cd mysql-5.6.22 && groupadd mysql && mkdir -pv /data/mysql && useradd -r -g mysql -d /data/mysql -s /sbin/nologgin mysql && chown -R mysql.mysql /data/mysql && cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DENABLED_LOCAL_INFILE=1 -DMYSQL_DATADIR=/data/mysql -DWITH_EXTRA_CHARSETS=all -DWITH_READLINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DMYSQL_TCP_PORT=3306 -DDEFAULT_COLLATION=utf8_general_ci && make && make install && chmod -R 755 /usr/local/mysql && cd /usr/local/mysql && ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
  • creates: /usr/local/mysql
    #####:一定要注意语法格式和缩进
    4,执行安装
    [20:40:06-root@jack mysql]#salt '*' state.sls mysql.cmake

     [20:41:01-root@jack mysql]#salt '*' state.sls mysql.mysql
    
     执行时间会很长,如果语法或者没有依赖包,会报错,飘红的

5,更改配置,亦可以在本地改好,直接用cp.get_file/cp.get_dir,批量发送
由于配置简单,这边直接更改了:
[20:41:01-root@jack mysql]#cat conf
salt '' cmd.run "cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf"
salt '
' cmd.run "sed -i '/mysqld/a\datadir = /data/mysql' /etc/my.cnf"
salt '' cmd.run "sed -i '/datadir=/s#\/var\/lib\/mysql#\/data\/mysql#g' /etc/my.cnf"
salt '
' cmd.run "cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld"
salt '' cmd.run "chmod 755 /etc/init.d/mysqld;chkconfig --add mysqld;chkconfig mysqld on"
salt '
' cmd.run "echo 'export PATH=/usr/local/mysql/bin:$PATH' >> /etc/profile.d/mysqld.sh"
salt '*' cmd.run "/etc/init.d/mysqld restart"

*:这边是所有机器安装,如何阁下做了主机群分组,可以使用:-N G1 指定分组执行安装

6,检测安装版本号
[20:21:04-root@jack ~]#salt '*' cmd.run "/usr/local/mysql/bin/mysql --version"
jack-6:
/usr/local/mysql/bin/mysql Ver 14.14 Distrib 5.6.22, for Linux (x86_64) using EditLine wrapper
jack-6-1:
/usr/local/mysql/bin/mysql Ver 14.14 Distrib 5.6.22, for Linux (x86_64) using EditLine wrapper
jack-7:
/usr/local/mysql/bin/mysql Ver 14.14 Distrib 5.6.22, for Linux (x86_64) using EditLine wrapper

如何需要安装其他版本:http://mirrors.sohu.com/mysql/