mysql主从搭建总的来说大致分为3个步骤,一是为主从实例添加复制所需参数以及创建复制用的账户,二在是需要在主库建立快照,三是在从库上添加指向主库IP,端口,用户名,密码,binlog位置等。而对于主从搭建的快照方式有很多种,如使用InnoDB hotbak,xtrabackup,mysqldump以及直接使用tar方式来建立快照。本文主要介绍使用mysqldump方式来建立快照,适用于不超过20GB左右的数据库。
与本文有关的相关参考:
使用mysqldump导出数据库 MySQL 复制简要描述及示例 MySQL多实例配置(一) MySQL多实例配置(二)
1、实例级别的主从搭建
-- 演示环境,另,本文演示基于同一主机的多实例,主端口为3406,从端口为3506 [email protected][(none)]> show variables like ‘version‘; +---------------+------------+ | Variable_name | Value | +---------------+------------+ | version | 5.6.12-log | +---------------+------------+ [email protected][(none)]> system cat /etc/issue CentOS release 5.4 (Final) Kernel \r on an \m --有关参主从数配置,请参考MySQL 复制简要描述及示例 --创建用于复制的账户 [email protected][(none)]> grant replication slave on *.* to ‘repl‘@‘192.168.1.177‘ identified by ‘xxx‘; Query OK, 0 rows affected (0.01 sec) --全局读锁 [email protected][(none)]> flush tables with read lock; Query OK, 0 rows affected (0.02 sec) [email protected][(none)]> system pwd /data/inst3406 --获取master binlog位置 [email protected][(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导出实例 [email protected][(none)]> system mysqldump -uroot -pxxx -S /tmp/mysql3406.sock --routines --all-databases --opt >alldb.sql [email protected][(none)]> system ls alldb.sql data3406 --解锁 [email protected][(none)]> unlock tables; [email protected][(none)]> exit --从库上面导入dump [[email protected] inst3406]$ mysql -uroot -pxxx -S /tmp/mysql3506.sock <alldb.sql --从库上设置主库的相关信息(host,port等等) [[email protected] inst3506]$ mysqls [email protected][(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 [email protected][(none)]> start slave;
2、部分库从库搭建
--以下演示为仅搭建部分从库,为只同步sakila tempdb 2个库 --重置从库 [email protected][(none)]> stop slave; Query OK, 0 rows affected (0.01 sec) [email protected][(none)]> reset slave all; Query OK, 0 rows affected (0.01 sec) --为从库添加只同步sakila tempdb 2个库,以下为修改后的结果 [[email protected] ~]$ grep replicate /data/inst3506/data3506/my3506.cnf replicate-do-db=test replicate-do-db=sakila [[email protected] ~]$ grep skip-slave /data/inst3506/data3506/my3506.cnf skip-slave-start --修改后重启3506以使从配置生效 [[email protected] ~]$ mysqladmin -uroot -pxxx -S /tmp/mysql3506.sock shutdown [[email protected] ~]$ mysqld_safe --defaults-file=/data/inst3506/data3506/my3506.cnf & --从主库仅导出sakila tempdb [[email protected] ~]$ mysqldump -uroot -pxxx -S /tmp/mysql3406.sock --single-transaction --master-data=2 -R --database sakila tempdb>multidb.sql --在从库端登陆执行dump文件 [[email protected] ~]$ mysqls [email protected][(none)]> source multidb.sql --查看dump期间的master binlog位置 [email protected][tempdb]> system grep -i "change master" multidb.sql -- CHANGE MASTER TO MASTER_LOG_FILE=‘inst3406bin.000001‘, MASTER_LOG_POS=3293117; --从库上设置主库的相关信息(host,port等等) [email protected][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) --启动从库 [email protected][tempdb]> start slave; Query OK, 0 rows affected (0.01 sec) -- Author : Leshami -- Blog : http://blog.csdn.net/leshami --校验结果 [email protected][tempdb]> show slave status \G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.177 Master_User: repl Master_Port: 3406 Connect_Retry: 60 Master_Log_File: inst3406bin.000001 Read_Master_Log_Pos: 3293117 Relay_Log_File: relay-bin.000002 Relay_Log_Pos: 285 Relay_Master_Log_File: inst3406bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: test,sakila --需要注意的是,本文的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.
时间: 2024-11-04 23:32:45