一、若是主節點已經運行了一段時間,且有大量數據時,如何配置並啓動slave節點(寫出操做步驟)node
主配置MySQLmysql
[root@Centos8 ~]#yum -y install mariadb-server [root@Centos8 ~]#vim /etc/my.cnf.d/mariadb-server.cnf [mysqld] log_bin server-id=125 [root@Centos8 ~]#systemctl start mariadb [root@Centos8 ~]#mysql #建立複製帳號 MariaDB [(none)]> grant replication slave on *.* to 'repluser'@'192.168.80.%' identified by 'heshizhe'; Query OK, 0 rows affected (0.000 sec) #備份數據庫 [root@Centos8 ~]#mkdir /backup [root@Centos8 ~]#mysqldump -A -F --single-transaction --master-data=1 --default-character-set=utf8 > /backup/all.sql [root@Centos8 ~]#ll /backup/ total 468 -rw-r--r-- 1 root root 479022 Oct 12 19:16 all.sql
從MySQL配置
linux
[root@Centos8-2 ~]#yum -y install mariadb-server #把備份文件複製到root目錄下 [root@Centos8-2 ~]#scp 192.168.80.120:/backup/all.sql /root/ #將徹底備份還原到新的從節點 [root@Centos8-2 ~]#vim /etc/my.cnf.d/mariadb-server.cnf [mysqld] server-id=121 read-only #從徹底備份的位置以後開始複製 [root@Centos8-2 ~]#vim all.sql CHANGE MASTER TO MASTER_HOST='192.168.80.120', #主MySQL IP地址 MASTER_USER='repluser', #複製的帳號 MASTER_PASSWORD='heshizhe', #密碼 MASTER_LOG_FILE='mariadb-bin.000003', MASTER_LOG_POS=389; [root@Centos8-2 ~]#systemctl start mariadb [root@Centos8-2 ~]#mysql < all.sql [root@Centos8-2 ~]#mysql MariaDB [(none)]> start slave; #開啓進程 MariaDB [(none)]> show slave status\G; #查看進程詳細信息 *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.80.120 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mariadb-bin.000003 Read_Master_Log_Pos: 516 Relay_Log_File: mariadb-relay-bin.000002 Relay_Log_Pos: 684 Relay_Master_Log_File: mariadb-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 516 Relay_Log_Space: 995 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 #複製延時 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 125 Master_SSL_Crl: Master_SSL_Crlpath: Using_Gtid: No Gtid_IO_Pos: Replicate_Do_Domain_Ids: Replicate_Ignore_Domain_Ids: Parallel_Mode: conservative SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it Slave_DDL_Groups: 1 Slave_Non_Transactional_Groups: 0 Slave_Transactional_Groups: 0 1 row in set (0.000 sec)
二、當master服務器宕機,提高一個slave成爲新的master(寫出操做步驟)web
#找到哪一個從節點的數據庫是最新,讓它成爲新master [root@Centos8-2 ~]#cat /var/lib/mysql/relay-log.info 5 ./mariadb-relay-bin.000002 684 mariadb-bin.000003 516 0 #新master修改配置文件,關閉read-only配置 [root@Centos8-2 ~]#vim /etc/my.cnf.d/mariadb-server.cnf [mysqld] server-id=21 read-only=off log-bin [root@Centos8-2 ~]#systemctl restart mariadb.service #重啓服務 #清除舊的master複製信息 MariaDB [(none)]> set global read_only=off; MariaDB [(none)]> stop slave; MariaDB [(none)]> reset slave all; #建立複製帳號 MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'192.168.80.%' IDENTIFIED BY 'heshizhe'; #在新master上徹底備份 [root@Centos8-2 ~]#mysqldump -A -F --single-transaction --master-data=1 --default-character-set=utf8 > /root/backup_`date +%F_%T`.sql [root@Centos8-2 ~]#ll total 480 -rw-r--r-- 1 root root 488111 Oct 3 17:12 backup_2020-10-03_17:12:16.sql #其它全部 slave 從新還原數據庫,指向新的master [root@Centos8-3 ~]#scp 192.168.80.121:/root/backup_2020-10-03_17:12:16.sql /root/ [root@Centos8-2 ~]#vim backup_2020-10-03_17\:12\:16.sql CHANGE MASTER TO MASTER_HOST='192.168.80.121', #主MySQL IP地址 MASTER_USER='repluser', #複製的帳號 MASTER_PASSWORD='heshizhe', #密碼 MASTER_LOG_FILE='mariadb-bin.000003', MASTER_LOG_POS=375; MariaDB [mysql]> source /root/backup_2020-10-03_17:12:16.sql; MariaDB [mysql]> start slave; #試試有沒有同步成功 MariaDB [(none)]> create database db2; #master MySQL MariaDB [mysql]> show databases; #slave MySQL +--------------------+ | Database | +--------------------+ | db1 | | db2 | | hellodb | | information_schema | | mysql | | performance_schema | +--------------------+ 6 rows in set (0.001 sec)
三、經過 MHA 0.58 搭建一個數據庫集羣結構sql
四臺主機shell
192.168.80.125 centos7 MHA管理端數據庫
192.168.80.120 centos8 masterbootstrap
192.168.80.121 centos8 slave1vim
192.168.80.122 centos8 slave2centos
在全部節點實現相互之間ssh kye驗證
[root@MHA ~]#ssh-keygen #生成密鑰 [root@MHA ~]#ssh-copy-id 192.168.80.125 #複製密鑰到本機 [root@MHA ~]#rsync -a .ssh 192.168.80.120:/root #複製密鑰給MySQL服務端 [root@MHA ~]#rsync -a .ssh 192.168.80.121:/root [root@MHA ~]#rsync -a .ssh 192.168.80.122:/root
在管理節點創建配置文件
[root@MHA ~]#yum -y install mha4mysql-node-0.58-0.el7.noarch.rpm mhamha4mysql-manager-0.58-0.el7.noarch.rpm [root@MHA ~]#mkdir /etc/mastermha/ [root@MHA ~]#vim /etc/mastermha/app1.cnf [server default] user=mhauser #用於遠程鏈接MySQL全部節點的用戶,須要有管理員的權限 password=heshizhe #用於遠程鏈接MySQL全部節點的密碼。 manager_workdir=/data/mastermha/app1/ #工做目錄會自行生成不用手動建立 manager_log=/data/mastermha/app1/manager.log #日誌保存位置 master_binlog_dir=/data/mysql-binglog/ #指定二進制日誌存放的目錄 remote_workdir=/data/mastermha/app1/ ssh_user=root #用於實現遠程ssh基於KEY的鏈接,訪問二進制日誌 repl_user=repluser #主從複製的用戶信息 repl_password=magedu ping_interval=1 #健康性檢查的時間間隔 master_ip_failover_script=/usr/local/bin/master_ip_faliover #切換VIP的perl腳本 report_script=/usr/local/bin/sendmail.sh #當執行報警腳本 check_repl_delay=0 #默認若是slave中從庫落後主庫relaylog超過100M,主庫不會選擇這個從庫爲新的master,由於這個從庫進行恢復須要很長的時間.經過這個參數,mha觸發主從切換的時候會忽略複製的延時,經過check_repl_delay=0這個參數,mha觸發主從切換時會忽略複製的延時,對於設置candidate_master=1的從庫很是有用,這樣確保這個從庫必定能成爲最新的master [server1] hostname=192.168.80.120 [server2] hostname=192.168.80.121 candidate_master=1 #設置爲優先候選master,即便不是集羣中事件最新的slave,也會優先當master [server3] hostname=192.168.80.122
相關腳本
[root@MHA ~]#vim /usr/local/bin/master_ip_faliover #切換VIP的perl腳本 #!/usr/bin/env perl use strict; use warnings FATAL => 'all'; use Getopt::Long; my ( $command, $ssh_user, $orig_master_host, $orig_master_ip, $orig_master_port, $new_master_host, $new_master_ip, $new_master_port ); my $vip = '192.168.80.100/24'; #改爲咱們須要的vip my $key = '1'; my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip"; my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down"; GetOptions( 'command=s' => \$command, 'ssh_user=s' => \$ssh_user, 'orig_master_host=s' => \$orig_master_host, 'orig_master_ip=s' => \$orig_master_ip, 'orig_master_port=i' => \$orig_master_port, 'new_master_host=s' => \$new_master_host, 'new_master_ip=s' => \$new_master_ip, 'new_master_port=i' => \$new_master_port, ); exit &main(); sub main { print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n"; if ( $command eq "stop" || $command eq "stopssh" ) { my $exit_code = 1; eval { print "Disabling the VIP on old master: $orig_master_host \n"; &stop_vip(); $exit_code = 0; }; if ($@) { warn "Got Error: $@\n"; exit $exit_code; } exit $exit_code; } elsif ( $command eq "start" ) { my $exit_code = 10; eval { print "Enabling the VIP - $vip on the new master - $new_master_host \n"; &start_vip(); $exit_code = 0; }; if ($@) { warn $@; exit $exit_code; } exit $exit_code; } elsif ( $command eq "status" ) { print "Checking the Status of the script.. OK \n"; exit 0; } else { &usage(); exit 1; } } sub start_vip() { `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`; } sub stop_vip() { return 0 unless ($ssh_user); `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`; } sub usage { print "Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n"; } [root@MHA ~]#chmod +x /usr/local/bin/master_ip_faliover [root@MHA ~]#vim /usr/local/bin/sendmail.sh #郵箱報警腳本 echo "MySQL is down" | mail -s "MHA Warning" 456456456@qq.com [root@MHA ~]#chmod +x /usr/local/bin/sendmail.sh
實現Master
[root@master ~]#yum -y install mha4mysql-node-0.56-0.el6.noarch.rpm mariadb-server [root@master ~]#vim /etc/my.cnf.d/mariadb-server.cnf [mysqld] server-id=120 log_bin plugin-load-add = semisync_master #加載半同步複製插件 rpl_semi_sync_master_enabled=on #啓動半同步複製插件 rpl_semi_sync_master_timeout=3000 #3秒內沒法同步,也將返回成功信息給客戶端 [root@master ~]#systemctl start mariadb [root@master ~]#mysql MariaDB [(none)]> show master logs; +--------------------+-----------+ | Log_name | File_size | +--------------------+-----------+ | mariadb-bin.000001 | 28198 | | mariadb-bin.000002 | 344 | +--------------------+-----------+ 2 rows in set (0.000 sec) #建立MHA用於遠程鏈接MySQL全部節點的用戶 MariaDB [(none)]> grant all on *.* to 'mhauser'@'192.168.80.%' identified by 'heshizhe'; #建立主從複製受權用戶 MariaDB [(none)]> grant replication slave on *.* to 'repluser'@'192.168.80.%' identified by 'heshizhe'; [root@master ~]#ifconfig eth0:1 192.168.80.100/24 #配置VIP地址
實現slave1
[root@slave1 ~]#yum -y install mariadb-server mha4mysql-node-0.58-0.el7.noarch.rpm [root@slave1 ~]#vim /etc/my.cnf.d/mariadb-server.cnf [mysqld] server-id=121 read_only log_bin plugin_load_add = semisync_slave rpl_semi_sync_slave_enabled=on skip_name_resolve=1 #禁止反向解析 [root@slave1 ~]#systemctl start mariadb [root@slave1 ~]#mysql MariaDB [(none)]> CHANGE MASTER TO -> MASTER_HOST='192.168.80.120', -> MASTER_USER='repluser', -> MASTER_PASSWORD='heshizhe', -> MASTER_PORT=3306, -> MASTER_LOG_FILE='mariadb-bin.000002', -> MASTER_LOG_POS=344; Query OK, 0 rows affected (0.025 sec) MariaDB [(none)]> start slave; Query OK, 0 rows affected (0.004 sec)
實現slave2
[root@slave1 ~]#yum -y install mariadb-server mha4mysql-node-0.58-0.el7.noarch.rpm [root@slave1 ~]#vim /etc/my.cnf.d/mariadb-server.cnf [mysqld] server-id=122 read_only log_bin plugin_load_add = semisync_slave rpl_semi_sync_slave_enabled=on skip_name_resolve=1 #禁止反向解析 [root@slave1 ~]#systemctl start mariadb [root@slave1 ~]#mysql MariaDB [(none)]> CHANGE MASTER TO -> MASTER_HOST='192.168.80.120', -> MASTER_USER='repluser', -> MASTER_PASSWORD='heshizhe', -> MASTER_PORT=3306, -> MASTER_LOG_FILE='mariadb-bin.000002', -> MASTER_LOG_POS=344; Query OK, 0 rows affected (0.025 sec) MariaDB [(none)]> start slave; Query OK, 0 rows affected (0.004 sec)
檢查Mha的環境
#檢查環境 [root@mha ~]#masterha_check_ssh --conf=/etc/mastermha/app1.cnf [root@mha ~]#masterha_check_repl --conf=/etc/mastermha/app1.cnf #查看狀態 [root@mha ~]#masterha_check_status --conf=/etc/mastermha/app1.cnf
啓動MHA
#開啓MHA,默認是前臺運行 [root@MHA ~]#nohup masterha_manager --conf=/etc/mastermha/app1.cnf &> /dev/null #查看狀態 [root@MHA ~]#masterha_check_status --conf=/etc/mastermha/app1.cnf app1 (pid:3093) is running(0:PING_OK), master:192.168.80.120
測試
(一)測試原master服務宕機,MHA提高新主節點過程開啓manager管理節點日誌跟蹤,持續查看
[root@MHA ~]#tail -f /data/mastermha/manager.log
檢測到原主已經宕機
原來的主masert 192.168.80.120已經宕機了,提高192.168.80.121爲新主
四、實戰案例:Percona XtraDB Cluster(PXC 5.7)
環境準備
三臺主機
192.168.80.125 pxc1
192.168.80.126 pxc2
192.168.80.127 pxc3
關閉防火牆和SELinux,保證時間同步
注意:若是已經安裝MySQL,必須卸載
安裝 Percona XtraDB Cluster 5.7
#此處使用清華大學yum源,官方源太慢了 [root@pxc1 ~]#vim /etc/yum.repos.d/pxc.repo [percona] name=percona_repo baseurl = https://mirrors.tuna.tsinghua.edu.cn/percona/release/$releasever/RPMS/$basearch enabled = 1 gpgcheck = 0 [root@pxc1 ~]#scp /etc/yum.repos.d/pxc.repo 192.168.80.126:/etc/yum.repos.d/ [root@pxc1 ~]#scp /etc/yum.repos.d/pxc.repo 192.168.80.127:/etc/yum.repos.d/ #在三個節點都安裝好PXC 5.7 [root@pxc1 ~]#yum install Percona-XtraDB-Cluster-57 -y [root@pxc2 ~]#yum install Percona-XtraDB-Cluster-57 -y [root@pxc3 ~]#yum install Percona-XtraDB-Cluster-57 -y
在各個節點上分別配置mysql及集羣配置文件
#第一個節點 [root@pxc1 ~]#vim /etc/percona-xtradb-cluster.conf.d/mysqld.cnf [mysqld] server-id=125 [root@pxc1 ~]#vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf [root@pxc1 ~]#grep -Ev "^#|^$" /etc/percona-xtradb-cluster.conf.d/wsrep.cnf [mysqld] wsrep_provider=/usr/lib64/galera3/libgalera_smm.so wsrep_cluster_address=gcomm://192.168.80.125,192.168.80.126,192.168.80.127 binlog_format=ROW default_storage_engine=InnoDB wsrep_slave_threads= 8 wsrep_log_conflicts innodb_autoinc_lock_mode=2 wsrep_node_address=192.168.80.125 wsrep_cluster_name=pxc-cluster wsrep_node_name=pxc-cluster-node-125 pxc_strict_mode=ENFORCING wsrep_sst_method=xtrabackup-v2 wsrep_sst_auth="sstuser:123456789" #第二個節點 [root@pxc2 ~]#vim /etc/percona-xtradb-cluster.conf.d/mysqld.cnf [mysqld] server-id=126 [root@pxc2 ~]#vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf [root@pxc2 ~]#grep -Ev "^#|^$" /etc/percona-xtradb-cluster.conf.d/wsrep.cnf [mysqld] wsrep_provider=/usr/lib64/galera3/libgalera_smm.so wsrep_cluster_address=gcomm://192.168.80.125,192.168.80.126,192.168.80.127 binlog_format=ROW default_storage_engine=InnoDB wsrep_slave_threads= 8 wsrep_log_conflicts innodb_autoinc_lock_mode=2 wsrep_node_address=192.168.80.126 wsrep_cluster_name=pxc-cluster wsrep_node_name=pxc-cluster-node-126 pxc_strict_mode=ENFORCING wsrep_sst_method=xtrabackup-v2 wsrep_sst_auth="sstuser:123465789" #第三個節點 [root@pxc3 ~]#vim /etc/percona-xtradb-cluster.conf.d/mysqld.cnf [mysqld] server-id=127 [root@pxc3 ~]#vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf [root@pxc3 ~]#grep -Ev "^#|^$" /etc/percona-xtradb-cluster.conf.d/wsrep.cnf [mysqld] wsrep_provider=/usr/lib64/galera3/libgalera_smm.so wsrep_cluster_address=gcomm://192.168.80.125,192.168.80.126,192.168.80.127 binlog_format=ROW default_storage_engine=InnoDB wsrep_slave_threads= 8 wsrep_log_conflicts innodb_autoinc_lock_mode=2 wsrep_node_address=192.168.80.127 wsrep_cluster_name=pxc-cluster wsrep_node_name=pxc-cluster-node-127 pxc_strict_mode=ENFORCING wsrep_sst_method=xtrabackup-v2 wsrep_sst_auth="sstuser:123456789"
啓動PXC集羣中第一個節點
root@pxc1 ~]#systemctl start mysql@bootstrap.service #啓動服務 [root@pxc1 ~]#ss -ntlu Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port udp UNCONN 0 0 127.0.0.1:323 *:* udp UNCONN 0 0 [::1]:323 [::]:* tcp LISTEN 0 128 *:4567 *:* tcp LISTEN 0 100 127.0.0.1:25 *:* tcp LISTEN 0 128 *:22 *:* tcp LISTEN 0 100 [::1]:25 [::]:* tcp LISTEN 0 80 [::]:3306 [::]:* tcp LISTEN 0 128 [::]:22 [::]:* #取出數據庫臨時密碼 [root@centos7 ~]#awk '/root@/{print $NF}' /var/log/mysqld.log tb!56)WOKde- [root@pcx1 ~]#mysql -uroot -p'tb!56)WOKde-' #登入數據庫 mysql> alter user 'root'@'localhost' identified by '123456789'; #更改數據密碼 #建立相關用戶並受權 mysql> create user 'sstuser'@'locatlhost' identified by '123456789'; Query OK, 0 rows affected (0.00 sec) mysql> grant reload,lock tables , process,replication client on *.* to 'root'@'localhost'; Query OK, 0 rows affected (0.01 sec) mysql> show status like 'wsrep%'; +----------------------------------+--------------------------------------+ | Variable_name | Value | +----------------------------------+--------------------------------------+ | wsrep_local_state_uuid | d12ab403-0fa7-11eb-b8db-4a1add9f3294 | | ....... | ..... | | wsrep_local_state | 4 | | wsrep_local_state_comment | Synced | | ..... | ...... | | wsrep_cluster_size | 1 | | wsrep_cluster_status | Primary | | wsrep_connected | ON | | wsrep_local_bf_aborts | 0 | | .... | ...... | | wsrep_ready | ON | +----------------------------------+--------------------------------------+ 71 rows in set (0.01 sec)
啓動PXC集羣中其它全部節點
[root@pxc2 ~]#systemctl start mysql.service [root@pxc2 ~]#ss -ntl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:4567 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 *:22 *:* LISTEN 0 100 [::1]:25 [::]:* LISTEN 0 80 [::]:3306 [::]:* LISTEN 0 128 [::]:22 [::]:* #第三個節點也同樣,MySQL密碼已經自動同步爲123456了。
測試
五、經過 ansible 部署二進制 mysql 8.0
[root@centos7-4 ~]#ssh-keygen [root@centos7-4 ~]#ssh-copy-id 192.168.80.128 [root@centos7-4 ~]#rsync -a .ssh 192.168.80.120:/root [root@centos7-4 ~]#rsync -a .ssh 192.168.80.121:/root [root@centos7-4 ~]#ll total 473736 -rw-r--r-- 1 root root 210 Oct 18 12:07 fuzhi.yaml -rw-r--r-- 1 root root 1455 Oct 18 16:49 install_mysql.yaml -rw-r--r-- 1 root root 297 Oct 18 11:53 my.cnf -rw-r--r-- 1 root root 485074552 Oct 18 11:52 mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz -rw-r--r-- 1 root root 117 Oct 17 17:38 mysql_backup.sh -rwxr-xr-x 1 root root 118 Oct 18 15:37 mysql_pass.sh -rw-r--r-- 1 root root 45 Oct 18 15:00 mysql.sh -rw-r--r-- 1 root root 365 Oct 18 16:27 mysql.yaml [root@centos7-4 ~]#cat my.cnf [mysqld] datadir=/data/mysql #指定數據庫目錄 socket=/data/mysql/mysql.sock #套接字文件 skip_name_resolve=1 #禁止域名解析 log-error=/data/mysql/mysql.log #日誌文件位置 pid-file=/data/mysql/mysql.pid #記錄當前 mysqld 進程的 pid [client] socket=/data/mysql/mysql.sock [root@centos7-4 ~]#cat mysql_pass.sh #!/bin/bash mysqladmin -uroot -p`awk '/A temporary password/{print $NF}' /data/mysql/mysql.log` password heshizhe [root@centos7-4 ~]#cat install_mysql.yaml --- - hosts: dbserver remote_user: root gather_facts: no tasks: - name: 安裝相關軟件 yum: name=libaio,numactl-libs - name: 建立MySQL用戶組 group: name=mysql gid=306 system=yes - name: 建立MySQL用戶 user: name=mysql uid=306 home=/data/mysql group=mysql shell=/sbin/nologin system=yes create_home=no - name: 解壓數據庫到遠程主機 unarchive: src=/root/mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz dest=/usr/local owner=mysql group=mysql - name: 建立軟連接 file: src=/usr/local/mysql-8.0.19-linux-glibc2.12-x86_64 dest=/usr/local/mysql state=link - name: 複製配置文件 copy: src=/root/my.cnf dest=/etc/my.cnf owner=mysql group=mysql - name: 配置環境變量 copy: content='PATH=/usr/local/mysql/bin:$PATH' dest=/etc/profile.d/mysql.sh - name: 運行環境變量 shell: source /etc/profile.d/mysql.sh - name: 複製啓動文件 shell: /usr/bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld - name: 初始化數據庫 shell: /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/data/mysql - name: 啓動數據庫 shell: chkconfig --add mysqld; systemctl start mysqld - name: 複製取出臨時密碼腳本到遠程主機 copy: src=/root/mysql_pass.sh dest=/root/mysql_pass.sh mode=755 - name: 更改數據庫密碼 shell: bash /root/mysql_pass.sh [root@centos7-4 ~]#cat /etc/ansible/hosts [webserver] 192.168.80.120 192.168.80.121 [appserver] 192.168.80.122 192.168.80.120 [dbserver] 192.168.80.121 192.168.80.120 [root@centos7-4 ~]#ansible-playbook install_mysql.yaml