saltstack實現數據庫基於GTID的主從複製

安裝pkg部分

[root@server1 pkg]# pwd
/srv/salt/pkg
[root@server1 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

創建用戶部分

[root@server1 user]# pwd
/srv/salt/user
[root@server1 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部分

[root@server1 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

主配置文件

[root@server1 files]# pwd
/srv/salt/mysql/files
[root@server1 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=ON
enforce-gtid-consistency
` GG `

配置部分

高亮部分爲:從日誌中提取密碼html

[root@server1 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 root@localhost 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  miao@'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

[root@server1 mysql]# salt '*' state.sls  mysql.install
server2.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:     4
Total run time: 105.141 s

gtid主從複製過程mysql

[root@server1 ~]# 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 root@localhost 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 root@localhost 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  miao@'172.25.88.%' identified by 'Zoe++007';" --connect-expired-password      Result: True     Comment: Command "mysql -pZoe++007 -e "grant replication slave on *.* to  miao@'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
相關文章
相關標籤/搜索