吾生有涯,而知无涯,适当止学.循序渐进,步步提升 talk is cheap, show me the code.
分类: mysql/postgresql
2018-06-06 08:33:40
步骤:
1)两个mysql实例:
ip地址:10.0.0.201 端口:3306 (mysql的master)
ip地址:10.0.0.201 端口:3307 (mysql的slave)
2)两台数据库实例,需要开启log-bin、gtid、和级联复制的选线log_slave_updates
my.cnf配置参数如下:
master(3306)
server-id = 1203306
log-bin = /data/mysqldata/3306/binlog/mysql-bin
#*********** gtid settting*******************
gtid_mode=on
enforce-gtid-consistency=true
binlog_format= row
skip-slave-start=1
log_slave_updates = 1
slave(3307)
server-id = 1203307
log-bin = /data/mysqldata/3307/binlog/mysql-bin
#*********** gtid settting*******************
gtid_mode=on
enforce-gtid-consistency=true
binlog_format= row
skip-slave-start=1
log_slave_updates = 1
3)为replication创建一个用户
在master上,配置一个新用户,可以让slave服务器能读取master服务器
mysql> create user identified by ;
mysql> grant replication slave on *.* to ;
4)master保证数据一致性,设置为只读
mysql> set @@global.read_only = on;
() [(none)]> show master status\g;
*************************** 1. row ***************************
file: mysql-bin.000006
position: 737
binlog_do_db:
binlog_ignore_db:
executed_gtid_set: 4160e9b3-58d9-11e8-b174-005056af6f24:1-21
1 row in set (0.00 sec)
********备份******************
/usr/local/mysql/bin/mysqldump -uroot -s /data/mysqldata/3306/mysql.sock -a --flush-logs --single-transaction -e | gzip > mysql_3306_full_backup_`date %f`.sql.gz
-a参数 参数解释:--all-databases, -a。备份所有数据库的所有表。
--flush-logs参数 参数解释:在数据dump之前,刷新数据库的日志。
--single-transaction参数 参数解释:服务器dump之前,发起一个begin sql的语句, start transaction 放进一个事务里面。
-e参数 参数解释:--extended-insert, -e 产生一个小的dump文件,加速插入的速度
master导出备份
/usr/local/mysql/bin/mysqldump -uroot -s /data/mysqldata/3306/mysql.sock --all-databases --triggers --routines --events --flush-logs --single-transaction -e --default-character-set=utf8 | gzip > mysql_3306_full_backup_`date %f`.sql.gz
slave导入备份
/usr/local/mysql/bin/mysql -uroot -s /data/mysqldata/3307/mysql.sock --default-character-set=utf8 < mysql_3306_full_backup_2018-06-01.sql
5)slave追日志
------------------抽象语句-----------------------------
mysql> change master to
> master_host = host,
> master_port = port,
> master_user = user,
> master_password = password,
> master_auto_position = 1;
-----------------实战语句------------------------------
change master to master_host = '10.0.0.201',master_port = 3306,master_user = 'repl',master_password = = 1;
6)启动slave
mysql> start slave;
7)master服务,关闭只读
mysql> set @@global.read_only = off;
完全删除replication的拓扑结构
() [(none)]> stop slave;
() [(none)]> reset slave all;