saltstack实现数据库基于GTID的主从复制

安装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

saltstack实现数据库基于GTID的主从复制的相关文章

基于GTID的主从复制数据库

基于GTID的主从复制数据库 全局身份识别 GTID(global transaction identifier) 为了实现主备数据库的强一致性 GTID = source_id:transaction_id source_id 表示执行事务的主库 transaction_id 是一个序列号,表示这个主库上执行的第 n 个事务. server_uuid是系统自动生成的,用来的替代server_id,因为source_id是手工设置的,可能会有冲突 数据库的安装和初始化 server33,44:

Mysql 基于GTID的主从复制及切换

参考 http://imysql.com/tag/gtid http://mysqllover.com/?p=594 Mysql 基于GTID的主从复制及切换 一.主从复制配置 两个mysql服务的my.cnf 中相关内容配置 [mysqld] #从复制数据库表设置 replicate-wild-ignore-table = mysql.%,information_schema.%,innodb.%,innodb_log.%,performance_schema.%,test.%,tmp.% #

基于GTID的主从复制搭建

前置检查 server-id = 10,master/slave不允许重复 log-bin gtid-mode = ON enforce-gtid-consistency = ON 1,利用mysqlpump复制master数据到slave,搭建基于GTID的主从复制,缺少GTID处理方法,暂不成功. mysqlpump --host= --user= --password= --single-transaction --default-parallelism=4 --compress-outp

企业——MYSQL(基于GTID)的主从复制

一.什么是主从复制? 主从复制,是用来建立一个和主数据库完全一样的数据库环境,称为从数据库:主数据库一般是准实时的业务数据库. 二.主从复制的作用(好处,或者说为什么要做主从) 1.做数据的热备,作为后备数据库,主数据库服务器故障后,可切换到从数据库继续工作,避免数据丢失. 2.架构的扩展.业务量越来越大,I/O访问频率过高,单机无法满足,此时做多库的存储,降低磁盘I/O访问的频率,提高单个机器的I/O性能. 3.读写分离,使数据库能支撑更大的并发.在报表中尤其重要.由于部分报表sql语句非常的

mysql主从之基于gtid的主从复制

一 GITD介绍 1.1 gtid的含义 Global Transaction Identifier,全局事务标识 阿里云的rds目前已经使用gtid 基于gtid的主从复制原理 每个mysql数据库上都有一个唯一uuid 每个事务生成一个id gtid由上面两者组合: uuid+事务id 1.2 优势 相对使用binlog+位置的方法来说 gtid让配置主从更加方便 从提升为主时比较方便 二 配置 2.1 主库的配置 [mysqld] bind-address=0.0.0.0 port=330

MySQL 基于 GTID 的主从复制和多实例配置

配置基于 GTID 的主从复制 1.修改 master 和 slave 的配置文件 server-id=113 gtid_mode=on enforce-gtid-consistency=on replicate-do-db=gateway_target # 如果只需同步部分表,就在 slave 上配置这两个额外项 replicate-do-table=gateway_target.t_target_snapshot 2.导出 master 的库和表结构到 slave,先停止 master my

MySQL/MariaDB数据库基于SSL实现主从复制

前言 备份数据库是生产环境中的首要任务,有时候不得不通过网络进行数据库的复制,由于MySQL/MariaDB的主从复制是明文传送的,如果在生产环境中跨网络传送,数据的安全性就无法完全保证,为了解决这一问题,我们需要一种安全的方式进行传送,即基于SSL加密进行数据传输. 部署配置 实验拓扑 实验环境 系统环境:CentOS6.6 数据库版本:mariadb-5.5.36 #注意:主从服务器数据库版本须一致:主从服务器时间须同步 #此实验从服务器只做一组为例 配置主从复制 安装mariadb [[e

docker下部署MySQL8基于GTID的主从复制

安装docker#yum install docker添加docker镜像仓库#vim /etc/docker/daemon.json {"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]} 拉取mysql镜像*# docker pull mysql 创建mysql容器# docker run -dit --name lisamysql001 -p 33307:3307 -e MYSQLROOTPAS

Mysql主从复制、二进制日志、基于GTID的主从复制、双主复制

 一.主从复制的工作原理   Mysql在Master与slave之间实现整个复制的过程由3个线程来完成的,   其中两个线程(SQL线程和IO线程)在 Slave端,   另外一个线程(IO)在Master端   要实现Mysql的复制必须首先打开Master端的binary log(也就是二进制日志)否则无法实现. Mysql复制基本过程如下:   (1)Slave上面的IO 线程链接上Master,并且请求指定日志文件的位置(或者 从开始的日志之后的日志内容)   (2)Master接收到