安装pkg部分
[[email protected] pkg]# pwd/srv/salt/pkg[[email protected] pkg]# cat install.sls pkg-install: pkg.installed: - pkgs: - gcc - pcre-devel - openssl-devel - libxml2-devel - libcurl-devel - libpng-devel.x86_64 - freetype-devel - gmp-devel - net-snmp-devel - libtool-ltdl-devel.x86_64 - ncurses-devel.x86_64 - gcc-c++ - libnfnetlink.x86_64 - libjpeg-turbo-devel
建立用户部分
[[email protected] user]# pwd/srv/salt/user[[email protected] user]# cat mysql.sls mysql-group: group.present: - name: mysql - gid: 1002 mysql-user: user.present: - name: mysql - uid: 1002 - gid: 1002 - shell: /sbin/nologin
安装mysql部分
[[email protected] mysql]# cat install.sls include: - pkg.install - user.mysql mysql-install: pkg.installed: - pkgs: - mysql-community-client - mysql-community-common - mysql-community-libs - mysql-community-libs-compat - mysql-community-server - creates: /usr/local/mysql - require: - pkg: pkg-install - user: mysql-user
主配置文件
[[email protected] files]# pwd/srv/salt/mysql/files[[email protected] files]# cat my.cnf# For advice on how to change settings please see# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html [mysqld]server-id={{ id }}gtid-mode=ONenforce-gtid-consistency{{ GG }}
配置部分
高亮部分为:从日志中提取密码
[[email protected] mysql]# cat service.sls include: - mysql.install mysql-password: cmd.run: - name: /etc/init.d/mysqld start ;cd /var/log ;passwd=`cat mysqld.log |grep ‘temporary password‘|awk {‘print $NF‘}` ;mysql -p$passwd -e "alter user [email protected] identified by ‘Zoe++007‘;" --connect-expired-password#mysql5.1.7,如果密码为明文输入模式,加参数 - unless: test -d /var/lib/mysql/mysql mysql-config: file.managed: - name: /etc/my.cnf - source: salt://mysql/files/my.cnf - template: jinja {% if grains[‘fqdn‘] == ‘server2.lalala.com‘ %} - id: 2 - GG: log-bin= mysql-bin {% elif grains[‘fqdn‘] == ‘server3.lalala.com‘ %} - id: 3 - GG: read-only {% endif %} #使用jinja模板,给不同主机配置不同信息 replication-config: cmd.run: {% if grains[‘fqdn‘] == ‘server2.lalala.com‘ %} - name: mysql -pZoe++007 -e "grant replication slave on *.* to [email protected]‘172.25.88.%‘ identified by ‘Zoe++007‘;" --connect-expired-password {% elif grains[‘fqdn‘] == ‘server3.lalala.com‘ %} - name: mysql -pZoe++007 -e "change master to master_host=‘172.25.88.2‘,master_user=‘miao‘,master_password=‘Zoe++007‘ ,master_auto_position=1;start slave; " --connect-expired-password {% endif %} - unless: test -f /var/lib/mysql/relay-log.info mysql-init: cmd.run: - name: chkconfig --add mysqld - unless: chkconfig --list mysqld - require: - file: mysql-config service.running: - name: mysqld - enable: True - reload: True - watch: - file: mysql-config
运行过程
安装mysql
[[email protected] mysql]# salt ‘*‘ state.sls mysql.installserver2.lalala.com:---------- ID: pkg-install Function: pkg.installed Result: True Comment: All specified packages are already installed Started: 00:12:07.825421 Duration: 981.668 ms Changes: ---------- ID: mysql-group Function: group.present Name: mysql Result: True Comment: Group mysql is present and up to date Started: 00:12:08.808725 Duration: 1.228 ms Changes: ---------- ID: mysql-user Function: user.present Name: mysql Result: True Comment: User mysql is present and up to date Started: 00:12:08.812821 Duration: 2.448 ms Changes: ---------- ID: mysql-install Function: pkg.installed Result: True Comment: 5 targeted packages were installed/updated. Started: 00:12:08.816644 Duration: 104155.617 ms Changes: ---------- mysql-community-client: ---------- new: 5.7.17-1.el6 old: mysql-community-common: ---------- new: 5.7.17-1.el6 old: mysql-community-libs: ---------- new: 5.7.17-1.el6 old: mysql-community-libs-compat: ---------- new: 5.7.17-1.el6 old: mysql-community-server: ---------- new: 5.7.17-1.el6 old: Summary for server2.lalala.com------------Succeeded: 4 (changed=1)Failed: 0------------Total states run: 4Total run time: 105.141 s
gtid主从复制过程
[[email protected] ~]# salt ‘*‘ state.sls mysql.service server3.lalala.com:---------- ID: pkg-install Function: pkg.installed Result: True Comment: All specified packages are already installed Started: 00:57:19.765213 Duration: 1153.547 ms Changes: ---------- ID: mysql-group Function: group.present Name: mysql Result: True Comment: Group mysql is present and up to date Started: 00:57:20.920669 Duration: 1.525 ms Changes: ---------- ID: mysql-user Function: user.present Name: mysql Result: True Comment: User mysql is present and up to date Started: 00:57:20.925427 Duration: 3.069 ms Changes: ---------- ID: mysql-install Function: pkg.installed Result: True Comment: All specified packages are already installed Started: 00:57:20.930287 Duration: 1.757 ms Changes: ---------- ID: mysql-password Function: cmd.run Name: /etc/init.d/mysqld start ;cd /var/log ;passwd=`cat mysqld.log |grep ‘temporary password‘|awk {‘print $NF‘}` ;mysql -p$passwd -e "alter user [email protected] identified by ‘Zoe++007‘;" --connect-expired-password Result: True Comment: unless execution succeeded Started: 00:57:20.934090 Duration: 14.475 ms Changes: ---------- ID: mysql-config Function: file.managed Name: /etc/my.cnf Result: True Comment: File /etc/my.cnf is in the correct state Started: 00:57:20.956275 Duration: 138.318 ms Changes: ---------- ID: replication-config Function: cmd.run Name: mysql -pZoe++007 -e "change master to master_host=‘172.25.88.2‘,master_user=‘miao‘,master_password=‘Zoe++007‘ ,master_auto_position=1;start slave; " --connect-expired-password Result: True Comment: unless execution succeeded Started: 00:57:21.094874 Duration: 18.675 ms Changes: ---------- ID: mysql-init Function: cmd.run Name: chkconfig --add mysqld Result: True Comment: unless execution succeeded Started: 00:57:21.115328 Duration: 15.975 ms Changes: ---------- ID: mysql-init Function: service.running Name: mysqld Result: True Comment: The service mysqld is already running Started: 00:57:21.134324 Duration: 102.086 ms Changes: Summary for server3.lalala.com------------Succeeded: 9Failed: 0------------Total states run: 9Total run time: 1.449 sserver2.lalala.com:---------- ID: pkg-install Function: pkg.installed Result: True Comment: All specified packages are already installed Started: 00:57:19.640339 Duration: 1109.781 ms Changes: ---------- ID: mysql-group Function: group.present Name: mysql Result: True Comment: Group mysql is present and up to date Started: 00:57:20.751699 Duration: 1.176 ms Changes: ---------- ID: mysql-user Function: user.present Name: mysql Result: True Comment: User mysql is present and up to date Started: 00:57:20.755638 Duration: 2.436 ms Changes: ---------- ID: mysql-install Function: pkg.installed Result: True Comment: All specified packages are already installed Started: 00:57:20.759704 Duration: 1.455 ms Changes: ---------- ID: mysql-password Function: cmd.run Name: /etc/init.d/mysqld start ;cd /var/log ;passwd=`cat mysqld.log |grep ‘temporary password‘|awk {‘print $NF‘}` ;mysql -p$passwd -e "alter user [email protected] identified by ‘Zoe++007‘;" --connect-expired-password Result: True Comment: unless execution succeeded Started: 00:57:20.763005 Duration: 13.151 ms Changes: ---------- ID: mysql-config Function: file.managed Name: /etc/my.cnf Result: True Comment: File /etc/my.cnf is in the correct state Started: 00:57:20.783348 Duration: 120.448 ms Changes: ---------- ID: replication-config Function: cmd.run Name: mysql -pZoe++007 -e "grant replication slave on *.* to [email protected]‘172.25.88.%‘ identified by ‘Zoe++007‘;" --connect-expired-password Result: True Comment: Command "mysql -pZoe++007 -e "grant replication slave on *.* to [email protected]‘172.25.88.%‘ identified by ‘Zoe++007‘;" --connect-expired-password" run Started: 00:57:20.904105 Duration: 248.09 ms Changes: ---------- pid: 2697 retcode: 0 stderr: mysql: [Warning] Using a password on the command line interface can be insecure. stdout:---------- ID: mysql-init Function: cmd.run Name: chkconfig --add mysqld Result: True Comment: unless execution succeeded Started: 00:57:21.154072 Duration: 14.785 ms Changes: ---------- ID: mysql-init Function: service.running Name: mysqld Result: True Comment: The service mysqld is already running Started: 00:57:21.171797 Duration: 100.02 ms Changes: Summary for server2.lalala.com------------Succeeded: 9 (changed=1)Failed: 0------------Total states run: 9Total run time: 1.611 s
时间: 2024-11-09 00:34:01