基于mysqldump快速搭建从库
mysql主从搭建总的来说大致分为3个步骤,一是为主从实例添加复制所需参数以及创建复制用的账户,二在是需要在主库建立快照,三是在从库上添加指向主库IP,端口,用户名,密码,binlog位置等。而对于主从搭建的快照方式有很多种,如使用InnoDB hotbak,xtrabackup,mysqldump以及直接使用tar方式来建立快照。本文主要介绍使用mysqldump方式来建立快照,适用于不超过20GB左右的数据库。
与本文有关的相关参考:
1、实例级别的主从搭建
-- 演示环境,另,本文演示基于同一主机的多实例,主端口为3406,从端口为3506
master@localhost[(none)]> show variables like 'version';
+---------------+------------+
| Variable_name | Value |
+---------------+------------+
| version | 5.6.12-log |
+---------------+------------+
master@localhost[(none)]> system cat /etc/issue
CentOS release 5.4 (Final)
Kernel \r on an \m
--有关参主从数配置,请参考MySQL 复制简要描述及示例
--创建用于复制的账户
master@localhost[(none)]> grant replication slave on *.* to 'repl'@'192.168.1.177' identified by 'xxx';
Query OK, 0 rows affected (0.01 sec)
--全局读锁
master@localhost[(none)]> flush tables with read lock;
Query OK, 0 rows affected (0.02 sec)
master@localhost[(none)]> system pwd
/data/inst3406
--获取master binlog位置
master@localhost[(none)]> show master status;
+--------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+--------------------+----------+--------------+------------------+-------------------+
| inst3406bin.000001 | 2169 | | | |
+--------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
--使用mysqldump导出实例
master@localhost[(none)]> system mysqldump -uroot -pxxx -S /tmp/mysql3406.sock --routines --all-databases --opt >alldb.sql
master@localhost[(none)]> system ls
alldb.sql data3406
--解锁
master@localhost[(none)]> unlock tables;
master@localhost[(none)]> exit
--从库上面导入dump
[mysql@app inst3406]$ mysql -uroot -pxxx -S /tmp/mysql3506.sock <alldb.sql
--从库上设置主库的相关信息(host,port等等)
[mysql@app inst3506]$ mysqls
slave@localhost[(none)]> change master to
-> MASTER_HOST='192.168.1.177',
-> MASTER_USER='repl',
-> MASTER_PASSWORD='xxx',
-> MASTER_PORT=3406,
-> MASTER_LOG_FILE='inst3406bin.000001',
-> MASTER_LOG_POS=2169;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
--启动slave
slave@localhost[(none)]> start slave;
2、部分库从库搭建
--以下演示为仅搭建部分从库,为只同步sakila tempdb 2个库
--重置从库
slave@localhost[(none)]> stop slave;
Query OK, 0 rows affected (0.01 sec)
slave@localhost[(none)]> reset slave all;
Query OK, 0 rows affected (0.01 sec)
--为从库添加只同步sakila tempdb 2个库,以下为修改后的结果
[mysql@app ~]$ grep replicate /data/inst3506/data3506/my3506.cnf
replicate-do-db=test
replicate-do-db=sakila
[mysql@app ~]$ grep skip-slave /data/inst3506/data3506/my3506.cnf
skip-slave-start
--修改后重启3506以使从配置生效
[mysql@app ~]$ mysqladmin -uroot -pxxx -S /tmp/mysql3506.sock shutdown
[mysql@app ~]$ mysqld_safe --defaults-file=/data/inst3506/data3506/my3506.cnf &
--从主库仅导出sakila tempdb
[mysql@app ~]$ mysqldump -uroot -pxxx -S /tmp/mysql3406.sock --single-transaction --master-data=2 -R --database sakila tempdb>multidb.sql
--在从库端登陆执行dump文件
[mysql@app ~]$ mysqls
slave@localhost[(none)]> source multidb.sql
--查看dump期间的master binlog位置
slave@localhost[tempdb]> system grep -i "change master" multidb.sql
-- CHANGE MASTER TO MASTER_LOG_FILE='inst3406bin.000001', MASTER_LOG_POS=3293117;
--从库上设置主库的相关信息(host,port等等)
slave@localhost[tempdb]> change master to
-> MASTER_HOST='192.168.1.177',
-> MASTER_USER='repl',
-> MASTER_PASSWORD='xxx',
-> MASTER_PORT=3406,
-> MASTER_LOG_FILE='inst3406bin.000001',
-> MASTER_LOG_POS=3293117;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
--启动从库
slave@localhost[tempdb]> start slave;
Query OK, 0 rows affected (0.01 sec)
--需要注意的是,本文的mysqldump期间,--single-transaction 仅仅对innodb引擎有效
--如果仅使用--master-data,则会开启--lock-all-tables
The --master-data and --single-transaction options can be used simultaneously, which provides a convenient way to
make an online backup suitable for use prior to point-in-time recovery if tables are stored using the InnoDB
storage engine.