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) -- Author : Leshami -- Blog : http://blog.csdn.net/leshami --校驗結果 slave@localhost[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.