全國知名互聯(lián)網(wǎng)會(huì)展平臺,提供展會(huì)設(shè)計(jì)、展覽設(shè)計(jì)搭建、會(huì)展策劃、會(huì)展搭建等全方位解決方案!

如何在一臺windows主機(jī)上搭建mysql主從復(fù)制

提問者: 逄凝|瀏覽 124 次|提問時(shí)間: 2016-12-30

已有 1 條回答

劉瑾葦

2017-01-08 最終答案
1、首先要在本地建立兩個(gè)mysql服務(wù)(參考這里),指定不同的端口。我這里一個(gè)主(3306),一個(gè)從(3307)。2、然后修改主配置文件:[mysqld]server-id = 1 binlog-do-db=test #要同步的數(shù)據(jù)庫#binlog-ignore-db=mysql #不同步的數(shù)據(jù)庫,如果指定了binlog-do-db這里應(yīng)該可以不用指定的log-bin=mysql-bin #要生成的二進(jìn)制日記文件名稱修改從配置文件:[mysqld]server-id = 2log-bin = mysql-binreplicate-do-db=test3、在主庫添加一個(gè)用戶 repl 并指定replication權(quán)限create user 'repl'@'127.0.0.1' identified by 'asdf';GRANT REPLICATION SLAVE ON *.* TO 'repl'@'127.0.0.1'; -- --這里我指定數(shù)據(jù)庫(test.*)時(shí)報(bào)錯(cuò),而指定全庫(*.*)時(shí)會(huì)成功。4、保持主從mysql的test數(shù)據(jù)庫初始狀態(tài)一致。一般是先將所有的表加讀鎖,然后copy磁盤上的數(shù)據(jù)庫文件夾。我這里直接停止服務(wù),然后將數(shù)據(jù)文件拷貝過去。5、在主數(shù)據(jù)庫里面運(yùn)行show master status;記下file和position字段對應(yīng)的參數(shù)。mysql> show master status;+------------------+----------+--------------+------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+--------------+------------------+| mysql-bin.000001 | 107 | test | |+------------------+----------+--------------+------------------+1 row in set (0.00 sec)6、在從庫設(shè)置它的master:mysql> change master to master_host='127.0.0.1',master_port=3306,master_user='repl',master_password='asdf',master_log_file='mysql-bin.000001',master_log_pos=107;Query OK, 0 rows affected (0.19 sec)這里的master_log_file和master_log_pos對應(yīng)剛才show master status記下的參數(shù)。7、在從庫開啟從數(shù)據(jù)庫復(fù)制功能。slave start;mysql> slave start;Query OK, 0 rows affected (0.00 sec)在訂鼎斥刮儷鈣籌水船驚從庫可以通過show slave status來查看一些參數(shù)。8. 此時(shí)在主庫創(chuàng)建表或插入數(shù)據(jù),在從庫就會(huì)很快也能看到了。-- 主庫mysql> create table tianyc_02(b int);Query OK, 0 rows affected (0.16 sec)mysql> insert into tianyc_02 values(2013);Query OK, 1 row affected (0.13 sec)-- 從庫mysql> show tables;+----------------+| Tables_in_test |+----------------+| tianyc_01 || tianyc_02 |+----------------+2 rows in set (0.00 sec)mysql> select * from tianyc_02;+------+| b |+------+| 2013 |+------+1 row in set (0.00 sec)同理,可以搭建第二、第三個(gè)從節(jié)點(diǎn)。備注:兩個(gè)服務(wù)的serve_id必須不同,否則在開啟復(fù)制功能時(shí)會(huì)提示錯(cuò)誤mysql> slave start;ERROR 1200 (HY000): The server is not configured as slave; fix in config file or with CHANGE MASTER TO