[root@room9pc01 ~]# scp -r /var/ftp/local/ 139.9.60.12:/var/ftp/local/
[root@ecs-abc local]# cat /etc/yum.repos.d/local.repo
[local]
name=local
baseurl=file:///var/ftp/local
enabled=1
gpgcheck=0
[root@ecs-abc local]# ls
mysql-community-client-5.7.17-1.el7.x86_64.rpm
mysql-community-common-5.7.17-1.el7.x86_64.rpm
mysql-community-devel-5.7.17-1.el7.x86_64.rpm
mysql-community-embedded-5.7.17-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.17-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.17-1.el7.x86_64.rpm
mysql-community-libs-5.7.17-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.17-1.el7.x86_64.rpm
mysql-community-minimal-debuginfo-5.7.17-1.el7.x86_64.rpm
mysql-community-server-5.7.17-1.el7.x86_64.rpm
mysql-community-test-5.7.17-1.el7.x86_64.rpm
nginx-1.12.2-10.x86_64.rpm
percona-xtrabackup-24-2.4.7-1.el7.x86_64.rpm
[root@ecs-abc local]# createrepo --update .Spawning worker 0 with 1 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete
[root@ecs-abc local]# ls
... ...
repodata
###########################################################3
[root@ecs-abc ~]# vim /etc/ansible/hosts
... ...
[mysql-master]
192.168.1.11
[mysql-slave]
192.168.1.12
192.168.1.13
192.168.1.14
192.168.1.15
192.168.1.16
[mysql:children]
mysql-master
mysql-slave
[root@ecs-abc ~]# ansible mysql --list-host
hosts (6):
192.168.1.12
192.168.1.13
192.168.1.14
192.168.1.15
192.168.1.16
192.168.1.11
[root@ecs-abc local]# yum clean all
[root@ecs-abc local]# yum repolist
[root@ecs-abc ~]# yum makecache
[root@ecs-abc ~]# yum search mysql
... ...
mysql-community-server.x86_64
percona-xtrabackup-24.x86_64
... ...
[root@ecs-abc ~]# ansible mysql -m shell -a 'yum -y makecache'
[root@ecs-abc ~]# ansible mysql -m yum -a 'name="mysql-community-server,percona-xtrabackup-24" state=latest'
[root@ecs-abc ~]# ssh 192.168.1.11
[root@mysql-1 ~]# cd /var/lib/mysql
[root@mysql-1 mysql]# ll
总用量 0
[root@mysql-1 mysql]# mysqld --verbose --help | less
... ...
--initialize Create the default database and exit. Create a super user
with a random expired password and store it into the log.
--initialize-insecure
Create the default database and exit. Create a super user
with empty password. 允许空密码创建
... ...
[root@mysql-1 mysql]# mysqld --user mysql --initialize-insecure 初始化数据库,如果你不初始化,用systemctl启动时,它也会帮你初始化
[root@mysql-1 mysql]# ll
总用量 110620
-rw-r----- 1 mysql mysql 56 1月 10 11:38 auto.cnf
-rw-r----- 1 mysql mysql 413 1月 10 11:38 ib_buffer_pool
-rw-r----- 1 mysql mysql 12582912 1月 10 11:38 ibdata1
-rw-r----- 1 mysql mysql 50331648 1月 10 11:38 ib_logfile0
-rw-r----- 1 mysql mysql 50331648 1月 10 11:38 ib_logfile1
drwxr-x--- 2 mysql mysql 4096 1月 10 11:38 mysql
drwxr-x--- 2 mysql mysql 4096 1月 10 11:38 performance_schema
drwxr-x--- 2 mysql mysql 12288 1月 10 11:38 sys
[root@mysql-1 mysql]# cd ..
[root@mysql-1 lib]# ll
总用量 104
drwxr-xr-x. 2 root root 4096 1月 5 19:31 alternatives
drwx------. 3 root root 4096 12月 2 12:17 authconfig
drwxr-xr-x 2 chrony chrony 4096 1月 10 11:31 chrony
drwxr-xr-x 8 root root 4096 1月 10 11:31 cloud
drwxr-xr-x. 2 root root 4096 4月 11 2018 dbus
drwxr-xr-x. 2 root root 4096 5月 15 2018 dhclient
drwxr-xr-x. 2 root root 4096 4月 11 2018 games
drwxr-xr-x. 2 root root 4096 8月 16 22:45 initramfs
drwxr-xr-x. 2 root root 4096 4月 11 2018 logrotate
drwx------. 2 root root 4096 12月 2 12:15 machines
drwxr-xr-x. 2 root root 4096 1月 5 19:31 misc
drwxr-xr-x 5 mysql mysql 4096 1月 10 11:38 mysql
drwxr-x--- 2 mysql mysql 4096 11月 29 2016 mysql-files
drwxr-x--- 2 mysql mysql 4096 11月 29 2016 mysql-keyring
drwx------. 2 root root 4096 1月 10 11:31 NetworkManager
drwxr-xr-x. 2 root root 4096 11月 6 2016 os-prober
drwxr-xr-x. 2 root root 4096 12月 2 12:15 plymouth
drwxr-x---. 3 root polkitd 4096 12月 2 12:15 polkit-1
drwx------. 2 postfix root 4096 12月 2 12:18 postfix
drwxr-xr-x. 2 root root 4096 1月 10 11:31 rpm
drwxr-xr-x. 2 root root 4096 4月 11 2018 rpm-state
drwx------. 2 root root 4096 1月 10 11:34 rsyslog
drwxr-xr-x. 4 root root 4096 12月 2 12:15 stateless
drwxr-xr-x. 4 root root 4096 9月 27 03:11 systemd
drwxr-xr-x. 2 root root 4096 8月 21 02:50 tuned
drwxr-xr-x. 6 root root 4096 1月 10 11:29 yum
[root@mysql-1 lib]# systemctl start mysqld
[root@mysql-1 lib]# ss -tunl
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
... ...
tcp LISTEN 0 80 :::3306 :::*
[root@mysql-1 ~]# mysql -uroot -p
Enter password: 空密码,直接回车就行
此时已经进入msyql界面
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
[root@mysql-1 ~]# systemctl stop mysqld
[root@mysql-1 ~]# exit
[root@ecs-abc ~]# scp 192.168.1.11:/etc/my.cnf ./
Warning: Permanently added '192.168.1.11' (ECDSA) to the list of known hosts.
my.cnf 100% 960 667.9KB/s 00:00
[root@ecs-abc ~]# ls
... ...
my.cnf
过滤出当前主机IP地址的最后一个.后面的数字。方便我们将其作为server-id
[root@ecs-abc ~]# ifconfig eth0 | grep -Po "(?<=inet 192.168.1.)\d+"
252
[root@ecs-abc ~]# vim mycnf.yml
---
- hosts: mysql
remote_user: root
tasks:
- shell: ifconfig eth0 | grep -Po "(?<=inet 192.168.1.)\d+"
register: result
- template:
src: my.cnf
dest: /etc/my.cnf
owner: root
group: root
mode: 0644
[root@ecs-abc ~]# vim my.cnf
... ...
[mysqld]
bind-address = 0.0.0.0
server-id = {{result.stdout}}
binlog-format = mixed
log_bin = /var/log/mysql/mysql-bin
... ...
[root@ecs-abc ~]# ansible-playbook mycnf.yml
PLAY [mysql] *******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.1.13]
ok: [192.168.1.16]
ok: [192.168.1.12]
ok: [192.168.1.14]
ok: [192.168.1.15]
ok: [192.168.1.11]
TASK [shell] *******************************************************************
changed: [192.168.1.15]
changed: [192.168.1.16]
changed: [192.168.1.12]
changed: [192.168.1.14]
changed: [192.168.1.13]
changed: [192.168.1.11]
TASK [template] ****************************************************************
changed: [192.168.1.12]
changed: [192.168.1.15]
changed: [192.168.1.13]
changed: [192.168.1.16]
changed: [192.168.1.14]
changed: [192.168.1.11]
PLAY RECAP *********************************************************************
192.168.1.11 : ok=3 changed=2 unreachable=0 failed=0
192.168.1.12 : ok=3 changed=2 unreachable=0 failed=0
192.168.1.13 : ok=3 changed=2 unreachable=0 failed=0
192.168.1.14 : ok=3 changed=2 unreachable=0 failed=0
192.168.1.15 : ok=3 changed=2 unreachable=0 failed=0
192.168.1.16 : ok=3 changed=2 unreachable=0 failed=0
[root@ecs-abc ~]# ansible mysql -m shell -a 'grep "server-id" /etc/my.cnf'
192.168.1.14 | CHANGED | rc=0 >>
server-id = 14
192.168.1.15 | CHANGED | rc=0 >>
server-id = 15
192.168.1.13 | CHANGED | rc=0 >>
server-id = 13
192.168.1.16 | CHANGED | rc=0 >>
server-id = 16
192.168.1.12 | CHANGED | rc=0 >>
server-id = 12
192.168.1.11 | CHANGED | rc=0 >>
server-id = 11
[root@ecs-abc ~]# ansible mysql -m shell -a 'mkdir /var/log/mysql'
[root@ecs-abc ~]# ansible mysql -m shell -a 'chown mysql.mysql /var/log/mysql'
[root@ecs-abc ~]# ssh 192.168.1.11
[root@mysql-1 ~]# systemctl start mysqld
[root@mysql-1 ~]# mysql -uroot -p
Enter password:
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000001
Position: 154
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
mysql> create user 'repl'@'%' IDENTIFIED BY 'lper';
Query OK, 0 rows affected (0.02 sec)
mysql> grant replication client,replication slave on *.* to repl@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> set password="toor";
Query OK, 0 rows affected (0.00 sec)
mysql> exit
[root@mysql-1 ~]# exit
[root@ecs-abc ~]# ansible mysql -m shell -a 'echo a | passwd --stdin root'
[root@ecs-abc ~]# ssh 192.168.1.11
[root@mysql-1 ~]# innobackupex --user="root" --host="localhost" --password="toor" --slave-info --no-timestamp ./backup
... ...
190110 12:11:34 completed OK!
[root@mysql-1 ~]# scp -r backup 192.168.1.12:./ 拷贝给12
The authenticity of host '192.168.1.12 (192.168.1.12)' can't be established.
ECDSA key fingerprint is SHA256:pFtWSJwjbR88oX8sMsjs7aamWY4lpU9+7J0ggtSV+8c.
ECDSA key fingerprint is MD5:02:68:7f:0a:e7:fe:89:09:b5:7f:7c:2f:af:5a:b4:c5.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.12' (ECDSA) to the list of known hosts.
root@192.168.1.12's password: 输入密码a
[root@mysql-1 ~]# ssh 192.168.1.12
[root@msyql-2 ~]# innobackupex --apply-log backup/
... ...
190110 12:50:09 completed OK!
[root@msyql-2 ~]# ll /var/lib/mysql
总用量 0
[root@msyql-2 ~]# innobackupex --copy-back ./backup
... ...
190110 12:51:06 completed OK!
[root@msyql-2 ~]# chown -R mysql.mysql /var/lib/mysql
[root@msyql-2 ~]# cd /var/lib/mysql
[root@msyql-2 mysql]# ll
总用量 122908
... ...
-rw-r----- 1 mysql mysql 509 1月 10 12:51 xtrabackup_info
[root@msyql-2 mysql]# cat xtrabackup_info
uuid = c851146d-1492-11e9-8ff7-fa163e53f72a
name =
tool_name = innobackupex
tool_command = --user=root --host=localhost --password=... --slave-info --no-timestamp ./backup
tool_version = 2.4.7
ibbackup_version = 2.4.7
server_version = 5.7.17-log
start_time = 2019-01-10 12:47:04
end_time = 2019-01-10 12:47:07
lock_time = 0
binlog_pos = filename 'mysql-bin.000001', position '859' 这个就是偏移量
innodb_from_lsn = 0
innodb_to_lsn = 2534608
partial = N
incremental = N
format = file
compact = N
compressed = N
encrypted = N
[root@msyql-2 mysql]# systemctl start mysqld
[root@msyql-2 mysql]# mysql -uroot -ptoor
mysql> change master to master_host='192.168.1.11',master_user='repl',master_password='lper',master_log_file="mysql-bin.000001", master_log_pos=859;
Query OK, 0 rows affected, 2 warnings (0.04 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.11
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 859
Relay_Log_File: msyql-2-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000001
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: 859
Relay_Log_Space: 529
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: 11
Master_UUID: 161e47bb-148d-11e9-8ff7-fa163e53f72a
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
mysql> use mysql;
mysql> select * from user\G
*************************** 1. row ***************************
Host: localhost 授权登陆的主机是本机,如果我们在主库上改为所有%主机都可以登陆,如果这里也改变,说明它是从库
User: root
... ...
[root@msyql-2 mysql]# ssh 192.168.1.11
[root@mysql-1 ~]# mysql -uroot -ptoor
mysql> use mysql;
mysql> update user set host="%" where user="root";
mysql> flush privileges;
mysql> exit
[root@mysql-1 ~]# mysql -uroot -ptoor -h192.168.1.12 直接mysql连接登陆从库
mysql> use mysql;
mysql> select * from user\G
*************************** 1. row ***************************
Host: % 授权登陆的主机是所有%主机,说明它是从库
User: root
[root@mysql-1 ~]# ssh 192.168.1.12 进入从库12
root@192.168.1.12's password: 输入密码a
[root@msyql-2 ~]# cd /var/lib/mysql
[root@msyql-2 mysql]# ll
总用量 122936
... ...
-rw-r----- 1 mysql mysql 209 1月 10 12:56 msyql-2-relay-bin.000001 这里的relay log默认命名是:主机名-relay-bin.序号
-rw-r----- 1 mysql mysql 823 1月 10 13:01 msyql-2-relay-bin.000002
-rw-r----- 1 mysql mysql 54 1月 10 12:56 msyql-2-relay-bin.index
... ...
[root@msyql-2 mysql]# exit
退回到跳板机
[root@ecs-abc ~]# ls
... ...
my.cnf
[root@ecs-abc ~]# vim my.cnf
[mysqld]
bind-address = 0.0.0.0
server-id = {{result.stdout}}
binlog-format = mixed
log_bin = /var/log/mysql/mysql-bin
relay-log = /var/log/mysql/relay-log 添加这三行
relay-log-index = /var/log/mysql/relay-log.index 添加这三行
relay-log-info-file = /var/log/mysql/relay-log.info 添加这三行
[root@ecs-abc ~]# ansible-playbook mycnf.yml
[root@ecs-abc ~]# ssh 192.168.1.11
[root@mysql-1 ~]# systemctl restart mysqld
[root@mysql-1 ~]# exit
[root@ecs-abc ~]# ssh 192.168.1.12
[root@msyql-2 ~]# mysql -uroot -ptoor
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.11
Master_User: repl
Master_Port: 3306
... ...
Exec_Master_Log_Pos: 154 查看当前的偏移量是154
... ...
mysql> exit
[root@msyql-2 ~]# systemctl stop mysqld
[root@msyql-2 ~]# rm -rf /var/lib/mysql/*
[root@msyql-2 ~]# innobackupex --copy-back ./backup
... ...
190110 13:20:20 completed OK!
[root@msyql-2 ~]# chown -R mysql.mysql /var/lib/mysql
[root@msyql-2 ~]# cd /var/lib/mysql
[root@msyql-2 mysql]# ls
... ...
-rw-r----- 1 mysql mysql 509 1月 10 13:20 xtrabackup_info
[root@msyql-2 mysql]# cat xtrabackup_info
... ...
binlog_pos = filename 'mysql-bin.000001', position '859' 这个就是偏移量
... ...
[root@msyql-2 mysql]# rm -rf xtrabackup_info
[root@msyql-2 mysql]# systemctl start mysqld
[root@msyql-2 mysql]# mysql -uroot -ptoor
mysql> show slave status\G
Empty set (0.00 sec)
mysql> change master to master_host='192.168.1.11',master_user='repl',master_password='lper',master_log_file="mysql-bin.000001", master_log_pos=859;
mysql> start slave;
mysql> use mysql;
mysql> select * from user\G
*************************** 1. row ***************************
Host: % 授权登陆的主机是所有%主机,说明它是从库
User: root
mysql> exit
[root@msyql-2 mysql]# cd /var/lib/mysql
[root@msyql-2 mysql]# ll 可以发现现在所有的从库配置都一样,文件名也都一样
总用量 122916
-rw-r----- 1 mysql mysql 56 1月 10 13:23 auto.cnf
-rw-r----- 1 mysql mysql 306 1月 10 13:20 ib_buffer_pool
-rw-r----- 1 mysql mysql 12582912 1月 10 13:23 ibdata1
-rw-r----- 1 mysql mysql 50331648 1月 10 13:23 ib_logfile0
-rw-r----- 1 mysql mysql 50331648 1月 10 13:20 ib_logfile1
-rw-r----- 1 mysql mysql 12582912 1月 10 13:23 ibtmp1
-rw-r----- 1 mysql mysql 124 1月 10 13:26 master.info
drwxr-x--- 2 mysql mysql 4096 1月 10 13:20 mysql
srwxrwxrwx 1 mysql mysql 0 1月 10 13:23 mysql.sock
-rw------- 1 mysql mysql 6 1月 10 13:23 mysql.sock.lock
drwxr-x--- 2 mysql mysql 4096 1月 10 13:20 performance_schema
drwxr-x--- 2 mysql mysql 12288 1月 10 13:20 sys
[root@msyql-2 mysql]# mysql -uroot -ptoor
mysql> stop slave IO_THREAD; 停止从库IO线程
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.1.11
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 154 现在读主库的binlog日志,偏移量到154了
Relay_Log_File: relay-log.000003
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: No 这里的IO线程已经没有同步主库了,状态为NO。不从主库上读binlog日志了
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: 154 现在执行主库的binlog日志,偏移量到154了,全都执行完了,那么现在的relaylog已经完成任务,没用了
Relay_Log_Space: 1237
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: NULL
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: 11
Master_UUID: 161e47bb-148d-11e9-8ff7-fa163e53f72a
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
mysql> exit
[root@msyql-2 mysql]# systemctl stop mysqld
[root@msyql-2 mysql]# rsync -aSH --delete /var/lib/mysql 192.168.1.13:/var/lib/
显示如下:
The authenticity of host '192.168.1.13 (192.168.1.13)' can't be established.
ECDSA key fingerprint is SHA256:pDcZD+0dJUb62g8TmqcB0V2f3bmKfqwAWg2782kseAM.
ECDSA key fingerprint is MD5:81:95:a0:a9:d1:02:71:e3:dc:e5:a1:9c:31:e4:35:5a.
Are you sure you want to continue connecting (yes/no)? yes 输入yes
Warning: Permanently added '192.168.1.13' (ECDSA) to the list of known hosts.
root@192.168.1.13's password: 输入密码a
[root@msyql-2 mysql]# ssh 192.168.1.13
[root@mysql-3 ~]# rm -rf /var/lib/mysql/auto.cnf
[root@mysql-3 ~]# systemctl start mysqld
[root@mysql-3 ~]# mysql -uroot -ptoor -e "start slave"
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@mysql-3 ~]# mysql -uroot -ptoor
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.11
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 154
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes 正常
Slave_SQL_Running: Yes 正常
mysql> exit
任何一个主机,只要是主从同步,只要把它的IO一停,然后偏移量的位置一对上,只要把/var/lib/mysql目录拷贝过去就行了,不用change master了。实际上,执行这个命令,就是在/var/lib/mysql目录下,生成一个master.info文件,里面有对应的信息。我们把这个文件拷贝完后,主从同步就成功了。这是一个技巧。
[root@mysql-3 ~]# cd /var/lib/mysql
[root@mysql-3 mysql]# ls
... ...
master.info
[root@mysql-3 mysql]# cat master.info
25 id
mysql-bin.000002 读的哪一个文件
154 偏移量
192.168.1.11 主库
repl 用户名
lper 密码
3306 端口号
60 延时?
0
0
30.000
0
161e47bb-148d-11e9-8ff7-fa163e53f72a
86400
0
[root@mysql-3 mysql]# exit
[root@msyql-2 ~]# rsync -aSH --delete /var/lib/mysql 192.168.1.14:/var/lib/
[root@msyql-2 ~]# rsync -aSH --delete /var/lib/mysql 192.168.1.15:/var/lib/
[root@msyql-2 ~]# rsync -aSH --delete /var/lib/mysql 192.168.1.16:/var/lib/
在14,15,16这剩下的几台从库上都执行以下操作
[root@msyql-2 ~]# ssh 192.168.1.14 远程相应虚拟机,然后执行一样的操作
[root@mysql-4 ~]# rm -rf /var/lib/mysql/auto.cnf
[root@mysql-4 ~]# systemctl start mysqld
[root@mysql-4 ~]# mysql -uroot -ptoor -e "start slave"
[root@msyql-2 ~]# ssh 192.168.1.12
[root@mysql-2 ~]# systemctl start mysqld
然后退回到主库11
[root@mysql-2 ~]# ssh 192.168.1.11
[root@mysql-1 ~]# mysql -uroot -ptoor
mysql> show processlist; 查看现在有多少从库
+----+------+--------------------+------+-------------+------+---------------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+--------------------+------+-------------+------+---------------------------------------------------------------+------------------+
| 5 | repl | 192.168.1.13:49376 | NULL | Binlog Dump | 5153 | Master has sent all binlog to slave; waiting for more updates | NULL |
| 6 | repl | 192.168.1.14:46262 | NULL | Binlog Dump | 4265 | Master has sent all binlog to slave; waiting for more updates | NULL |
| 7 | repl | 192.168.1.15:42896 | NULL | Binlog Dump | 4104 | Master has sent all binlog to slave; waiting for more updates | NULL |
| 8 | repl | 192.168.1.16:44394 | NULL | Binlog Dump | 4088 | Master has sent all binlog to slave; waiting for more updates | NULL |
| 9 | root | localhost | NULL | Sleep | 784 | | NULL |
| 11 | repl | 192.168.1.12:42932 | NULL | Binlog Dump | 135 | Master has sent all binlog to slave; waiting for more updates | NULL |
| 12 | root | localhost | NULL | Query | 0 | starting | show processlist |
+----+------+--------------------+------+-------------+------+---------------------------------------------------------------+------------------+
7 rows in set (0.00 sec)
查看到没有半同步插件
mysql> show plugins;
+----------------------------+----------+--------------------+---------+---------+
| Name | Status | Type | Library | License |
+----------------------------+----------+--------------------+---------+---------+
| binlog | ACTIVE | STORAGE ENGINE | NULL | GPL |
| mysql_native_password | ACTIVE | AUTHENTICATION | NULL | GPL |
| sha256_password | ACTIVE | AUTHENTICATION | NULL | GPL |
| CSV | ACTIVE | STORAGE ENGINE | NULL | GPL |
| MEMORY | ACTIVE | STORAGE ENGINE | NULL | GPL |
| InnoDB | ACTIVE | STORAGE ENGINE | NULL | GPL |
| INNODB_TRX | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_LOCKS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_LOCK_WAITS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_CMP | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_CMP_RESET | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_CMPMEM | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_CMPMEM_RESET | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_CMP_PER_INDEX | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_CMP_PER_INDEX_RESET | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_BUFFER_PAGE | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_BUFFER_PAGE_LRU | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_BUFFER_POOL_STATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_TEMP_TABLE_INFO | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_METRICS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_FT_DEFAULT_STOPWORD | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_FT_DELETED | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_FT_BEING_DELETED | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_FT_CONFIG | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_FT_INDEX_CACHE | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_FT_INDEX_TABLE | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_SYS_TABLES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_SYS_TABLESTATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_SYS_INDEXES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_SYS_COLUMNS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_SYS_FIELDS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_SYS_FOREIGN | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_SYS_FOREIGN_COLS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_SYS_TABLESPACES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_SYS_DATAFILES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_SYS_VIRTUAL | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| MyISAM | ACTIVE | STORAGE ENGINE | NULL | GPL |
| MRG_MYISAM | ACTIVE | STORAGE ENGINE | NULL | GPL |
| PERFORMANCE_SCHEMA | ACTIVE | STORAGE ENGINE | NULL | GPL |
| ARCHIVE | ACTIVE | STORAGE ENGINE | NULL | GPL |
| BLACKHOLE | ACTIVE | STORAGE ENGINE | NULL | GPL |
| FEDERATED | DISABLED | STORAGE ENGINE | NULL | GPL |
| partition | ACTIVE | STORAGE ENGINE | NULL | GPL |
| ngram | ACTIVE | FTPARSER | NULL | GPL |
+----------------------------+----------+--------------------+---------+---------+
44 rows in set (0.00 sec)
查看到没有半同步插件
mysql> show global variables like "%rpl%";
+------------------------+----------+
| Variable_name | Value |
+------------------------+----------+
| rpl_stop_slave_timeout | 31536000 |
+------------------------+----------+
mysql> exit
[root@mysql-1 ~]# vim /etc/my.cnf
[mysqld] 追加下面几行
... ...
plugin-load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
rpl_semi_sync_slave_enabled = 1
rpl_semi_sync_master_enabled = 1
rpl_semi_sync_master_timeout = 3000
[root@mysql-1 ~]# systemctl restart mysqld
[root@mysql-1 ~]# mysql -uroot -ptoor
mysql> show global variables like "%rpl%";
+-------------------------------------------+------------+
| Variable_name | Value |
+-------------------------------------------+------------+
| rpl_semi_sync_master_enabled | ON | 主库半同步状态打开了
| rpl_semi_sync_master_timeout | 3000 |
| rpl_semi_sync_master_trace_level | 32 |
| rpl_semi_sync_master_wait_for_slave_count | 1 |
| rpl_semi_sync_master_wait_no_slave | ON |
| rpl_semi_sync_master_wait_point | AFTER_SYNC |
| rpl_semi_sync_slave_enabled | ON | 从库半同步状态打开了
| rpl_semi_sync_slave_trace_level | 32 |
| rpl_stop_slave_timeout | 31536000 |
+-------------------------------------------+------------+
9 rows in set (0.00 sec)
mysql> show global variables like "%rpl%";
+-------------------------------------------+------------+
| Variable_name | Value |
+-------------------------------------------+------------+
| rpl_semi_sync_master_enabled | ON |
| rpl_semi_sync_master_timeout | 3000 |
| rpl_semi_sync_master_trace_level | 32 |
| rpl_semi_sync_master_wait_for_slave_count | 1 |
| rpl_semi_sync_master_wait_no_slave | ON |
| rpl_semi_sync_master_wait_point | AFTER_SYNC |
| rpl_semi_sync_slave_enabled | ON |
| rpl_semi_sync_slave_trace_level | 32 |
| rpl_stop_slave_timeout | 31536000 |
+-------------------------------------------+------------+
9 rows in set (0.00 sec)
mysql> show global status like "%semi%";
+--------------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients | 0 | 主库半同步复制有几台从
| Rpl_semi_sync_master_net_avg_wait_time | 0 |
| Rpl_semi_sync_master_net_wait_time | 0 |
| Rpl_semi_sync_master_net_waits | 0 |
| Rpl_semi_sync_master_no_times | 0 |
| Rpl_semi_sync_master_no_tx | 0 |
| Rpl_semi_sync_master_status | ON |
| Rpl_semi_sync_master_timefunc_failures | 0 |
| Rpl_semi_sync_master_tx_avg_wait_time | 0 |
| Rpl_semi_sync_master_tx_wait_time | 0 |
| Rpl_semi_sync_master_tx_waits | 0 |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0 |
| Rpl_semi_sync_master_wait_sessions | 0 |
| Rpl_semi_sync_master_yes_tx | 0 |
| Rpl_semi_sync_slave_status | OFF |
+--------------------------------------------+-------+
15 rows in set (0.00 sec)
[root@mysql-1 ~]# ssh 192.168.1.12
[root@mysql-2 ~]# vim /etc/my.cnf
[mysqld] 追加下面几行
... ...
plugin-load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
rpl_semi_sync_slave_enabled = 1
rpl_semi_sync_master_enabled = 1
rpl_semi_sync_master_timeout = 3000
[root@mysql-2 ~]# systemctl restart mysqld
[root@msyql-2 ~]# mysql -uroot -ptoor
mysql> show variables like "%rpl%";
+-------------------------------------------+------------+
| Variable_name | Value |
+-------------------------------------------+------------+
| rpl_semi_sync_master_enabled | ON |
| rpl_semi_sync_master_timeout | 3000 |
| rpl_semi_sync_master_trace_level | 32 |
| rpl_semi_sync_master_wait_for_slave_count | 1 |
| rpl_semi_sync_master_wait_no_slave | ON |
| rpl_semi_sync_master_wait_point | AFTER_SYNC |
| rpl_semi_sync_slave_enabled | ON |
| rpl_semi_sync_slave_trace_level | 32 |
| rpl_stop_slave_timeout | 31536000 |
+-------------------------------------------+------------+
9 rows in set (0.00 sec)
[root@msyql-2 ~]# exit
[root@mysql-1 ~]# mysql -uroot -ptoor
mysql> show global status like "%semi%";
+--------------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients | 1 |
| Rpl_semi_sync_master_net_avg_wait_time | 0 |
| Rpl_semi_sync_master_net_wait_time | 0 |
| Rpl_semi_sync_master_net_waits | 0 |
| Rpl_semi_sync_master_no_times | 0 |
| Rpl_semi_sync_master_no_tx | 0 |
| Rpl_semi_sync_master_status | ON |
| Rpl_semi_sync_master_timefunc_failures | 0 |
| Rpl_semi_sync_master_tx_avg_wait_time | 0 |
| Rpl_semi_sync_master_tx_wait_time | 0 |
| Rpl_semi_sync_master_tx_waits | 0 |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0 |
| Rpl_semi_sync_master_wait_sessions | 0 |
| Rpl_semi_sync_master_yes_tx | 0 |
| Rpl_semi_sync_slave_status | OFF |
+--------------------------------------------+-------+
15 rows in set (0.00 sec)
mysql> exit
###############################
创建MHA 192.168.1.20
安装软件mha
把MHA的包准备好
tar zxf mha4mysql-node-0.56.tar.gz
ls
cd mha4mysql-node-0.56
ls
yum -y install gcc automake pkgconfig
yum list | grep -Pi "dbd|dbi"
yum install perl-DBI perl-DBD-MySQL
perl Makefile.PL
yum list | grep -Pi "ExtUtils"
yum install perl-ExtUtils-MakeMaker
yum list | grep -Pi "cpan"
yum install perl-CPAN
perl Makefile.PL
make
make install
rm -rf mha4mysql-node-0.56
退回到跳板机abc
vim mhanode.sh
curl -s0 ftp://192.168.1.252/local/mha4mysql-node-0.56.tar.gz
tar zxf mha4mysql-node-0.56.tar.gz
cd mha4mysql-node-0.56
yum -y install gcc automake pkgconfig perl-DBI perl-DBD-MySQL perl-ExtUtils-MakeMaker perl-CPAN
perl Makefile.PL
make
make install
cd ..
rm -rf mha4mysql-node-0.56
保存退出
ansible mysql -m script -a "mhanode.sh"
ssh 192.168.1.20
到MHA
提示缺啥就装啥
真机传包给MHA
tar -zxf mha4mysql-manager-0.56.tar.gz
cd mha4mysql-manager-0.56.tar.gz
perl Makefile.PL
yum list | grep -Pi "tiny"
yum list | grep -Pi "Dispatch"
yum list | grep -Pi "ForkManager"
yum -y install per-Config-Tiny per-Log-Dispatch per-Parallel-ForkManager
perl Makefile.PL
make
make install
退回到跳板机abc
vim idrsa.yml
---
- hosts: mysql
remote_user: root
tasks:
- copy:
src: /root/.ssh/id_rsa
dest: /root/.ssh/id_rsa
owner: root
group: root
mode: 0400
- copy:
src: /etc/ssh/ssh_config
dest: /etc/ssh/ssh_config
owner: root
group: root
mode: 0644
ansible-playbook idrsa.yml
scp /root/.ssh/id_rsa 192.168.1.20:./
ssh 192.168.1.20
到MHA
ls
mv id_rsa /root/.ssh/
chmod 400 /root/.ssh/id_rsa
rm -rf mha4mysql-manager-0.56*
ls
vim /etc/ssh/ssh_config
追加那行不要远程时输入yes
试着远程数据库主库11,不用输入密码就行
vim /etc/mha.conf
masterha_check_ssh --conf=/etc/mha.conf
touch /etc/masterha_default.cnf
[server default]
manager_log=/var/log/mha.log
manager_workdir=/var/lib/mha
user=root
password=toor
repl_user=repl
repl_password=lper
ssh_user=root
ping_interval=1
remote_workdir=/var/lib/mha
master_ip_failover_script=/usr/local/bin/master_ip_failover
[server18]
candidate_master=1
hostname=mysql18
[server17]
candidate_master=1
hostname=mysql17
[server16]
hostname=mysql16
no_master=1
[server15]
hostname=mysql15
no_master=1
masterha_check_repl --conf=/etc/mha.conf
在主库和备用主库的my.cnf
追加
添加参数 relay_log_purge=0
写触发的切换脚本
master_ip_failover
chmod 755 master_ip_failover
申请虚拟IP地址,绑定到主库和备用主库上
vim master_ip_failover
改成相应的vip
ssh 192.168.1.11
ifconfig 如果发现还没有VIP
那么
ifconfig eth0:1 VIP的地址
MHA
masterha_check_repl --conf=/etc/mha.conf
yum -y install screen
screen -S nsd1808 给终端起个名字nsd1808
echo 123456
sleep 2000
摁ctrl +a 再摁d
pstree -p 可以看到这个进程在后台运行叫sleep,在screen这个后台运行
记住这个名字叫nsd1808,如果我们退出来,过两天再进去,还想看这个终端当时敲的命令
那么就
screen -r nsd1808
又回到这个终端了,可以看到历史命令
masterha_manager --conf=/etc/mha.conf --ignore_last_failover
摁ctrl +a 再摁d切换回来
masterha_check_repl --conf=/etc/mha.conf
可以看到运行状态是OK的
远程到主库11
systemctl stop mysqld
ifconfig
mysql -uroot -ptoor -hVIP的地址 如果现在把主库的mysql关闭了,可以看到数据库已然可以正常使用
show processlist;发现几台从库还在
如果修复11数据库,进入任意一台从库
systemctl stop mysqld
rsync -aSH --delete /var/lib/mysql 192.168.1.11:/var/lib/
systemctl start mysqld
exit
ssh 192.168.1.11
rm -rf /var/lib/mysql/auto.cnf
systemctl start mysqld
mysql -uroot -ptoor -e "start slave"
主从同步完成,这个主从可以写成脚本的
回到mha
screen -r nsd1808
可以看到当时的状态
再执行一次
masterha_manager --conf=/etc/mha.conf --ignore_last_failover
摁ctrl +a 再摁d切换回来,再看状态
masterha_check_repl --conf=/etc/mha.conf
可以看到运行状态是OK的
进入之前的备用主库12,也就是现在的主库12,可以发现现在VIP在它这里
来源:https://www.cnblogs.com/summer2/p/10798510.html