今天又犯二了,居然把mysql的复制给忘了,也算醉了,再次总结mysql 主从:
公司的db01-fk(172.33.4.155)机器出了问题,需要换新的机器。这台机器上的mysql也是从中国db01复制的,所以解决思路:1、从中国db01备份数据库,并将备份文件传至FK;2、在FK site 新db01-fk上还原数据库,并设置master地址及相关参数,实现复制;
具体操作如下:
1、CN db01 备份数据库,并传至新db01-fk,操作如下:
[[email protected] ~]# mysqldump -A --master-data=1 --single-transaction --events | gzip > /tmp/fulldb-16-12-05.sql.gz
[[email protected] ~]# ll -h /tmp/fulldb-16-12-05.sql.gz
-rw-r--r-- 1 root root 411M Dec 5 15:15 /tmp/fulldb-16-12-05.sql.gz
[[email protected] ~]# aws s3 cp /tmp/fulldb-16-12-05.sql.gz s3://yeecalllogs-mb/
upload: ../tmp/fulldb-16-12-05.sql.gz to s3://yeecalllogs-mb/fulldb-16-12-05.sql.gz
[[email protected] ~]#
2、在db01-fk(new) 复制原db01-fk的配置文件、密钥文件,启动服务,设置远程master主机的相关参数
[[email protected] ~]# rsync -avzoptgl --progress 172.33.4.155:/home/mysql/ /home/mysql/
[[email protected] ~]# rsync -avzoptgl --progress 172.33.4.155:/etc/my.cnf /etc/my.cnf
[[email protected] ~]# service mysqld start
[[email protected] ~]# mysql
mysql> change master to
-> master_host=‘1.1.1.1‘,
-> master_user=‘repl‘,
-> master_password=‘123456789‘,
-> master_port=13306,
-> master_ssl=1,
-> master_ssl_ca=‘/home/mysql/ssl/ca.crt‘,
-> master_ssl_cert=‘/home/mysql/ssl/4.idc.ycall.com.crt‘,
-> master_ssl_key=‘/home/mysql/ssl/4.idc.ycall.com.key‘;
Query OK, 0 rows affected (0.02 sec)
mysql> exit
在db01-fk(new)上还原数据库,进行复制
[[email protected] ~]# aws s3 cp s3://yeecalllogs-mb/fulldb-16-12-05.sql.gz /tmp/
download: s3://yeecalllogs-mb/fulldb-16-12-05.sql.gz to ../tmp/fulldb-16-12-05.sql.gz
[[email protected] ~]# gzip -d /tmp/fulldb-16-12-05.sql.gz
[[email protected] ~]# grep -P -io "change master to .*\d;$" /tmp/fulldb-16-12-05.sql
CHANGE MASTER TO MASTER_LOG_FILE=‘mysql-bin.000069‘, MASTER_LOG_POS=8105870;
[[email protected] ~]# mysql < /tmp/fulldb-16-12-05.sql
[[email protected] ~]# mysql
mysql> start slave ;
mysql> show slave status \G
[[email protected] ~]#
说明:
mysqldump --help
--master-data[=#] This causes the binary log position and filename to be
appended to the output. If equal to 1, will print it as a
CHANGE MASTER command; if equal to 2, that command will
be prefixed with a comment symbol. This option will turn
--lock-all-tables on, unless --single-transaction is
specified too (in which case a global read lock is only
taken a short time at the beginning of the dump; don‘t
forget to read about --single-transaction below). In all
cases, any action on logs will happen at the exact moment
of the dump. Option automatically turns --lock-tables
off.
--master-data[=#] 在备份导出的文件里追加二进制binlog文件的位置和名称
如果值等于1,就会添加一个CHANGE MASTER语句
如果值等于2,就会在CHANGE MASTER语句前添加注释
这个参数会--lock-all-tables锁表,除非指定了--single-transaction
这种情况下,锁表只会在dump开始的时候持续一小段时间,照理说
在dump的时候,任何动作都会影响到binlog文件
dump结束之后,选项会自动关闭锁表功能
分析:如果在恢复DB之前指定 change master to 的 host port .....,然后再恢复DB, 这样 dump 文件中带的 master_log_file 和 master_log_pos 才会生效。因为一旦修改master host(即使修改前后的值相同),就会生成新的file和pos,原来dump文件的语句就被冲掉了。所以这种方式必须首先指定host,然后恢复,最后 start slave;
当然:我上次操作,在mysqldump时没加这两参数,使用了-x 锁表功能 ,但在mysqldump过程中,在主库上 show master status , 查看了 position 及 logfile,然后记下这两项值,以便在从库change master to 操作时指定这些参数。