本次主庫:192.168.100.18
本次從庫:192.168.100.45
主庫配置
在主數(shù)據(jù)庫中新建數(shù)據(jù)庫:backup_test (如果不指定數(shù)據(jù)庫則省略)
編輯主數(shù)據(jù)庫的my.ini文件
在[mysqld]節(jié)點中增加如下內(nèi)容:
server_id=1 #指定唯一的ID扒吁,1至32催蝗,必須的
log_bin=mysql-log-bin #指定二進制日志存放路徑误阻,必須的
binlog_do_db=backup_test #指定要同步的數(shù)據(jù)庫,建議加上妈橄,(如果省略,默認操作整個mysql)
#binlog_ignore_db=mysql #指定不要同步的數(shù)據(jù)庫轿秧,如果指定了binlog-do-db就不用再指定該項
修改數(shù)據(jù)庫之后需要重啟數(shù)據(jù)庫钙姊,才能使配置生效,然后建立一個備份賬戶须妻,用于從庫備份主庫的數(shù)據(jù)仔蝌。
#1.使用cmd窗口,進入路徑 %mysql%/bin璧南,然后登陸數(shù)據(jù)庫mysql -uroot -p掌逛,以下步驟都是在mysql中完成
#2.運行以下命令,創(chuàng)建備份用戶并賦予權(quán)限
# ip的位置可以為%司倚,代表所有ip都能連接
# identified by 'slave' 是為備份賬戶設(shè)置密碼
grant replication slave on *.* to 'slave'@'192.168.100.45' identified by 'slave' ;
#3.刷新權(quán)限
flush privileges;
#4.查詢是否創(chuàng)建成功
select user,host from mysql.user;
+-----------+-----------+
| user | host |
+-----------+-----------+
| root | % |
| slave | % |
| mysql.sys | localhost |
| root | localhost |
+-----------+-----------+
4 rows in set (0.00 sec)
#5.顯示主服務(wù)器的狀態(tài)信息豆混,并且找到File 和 Position 的值記錄下來;
show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
從庫配置
在從數(shù)據(jù)庫中新建數(shù)據(jù)庫:backup_test (如果不指定數(shù)據(jù)庫則省略)
然后編輯從數(shù)據(jù)庫的my.ini文件
在[mysqld]節(jié)點中增加如下內(nèi)容:
server_id=2 #指定唯一的ID动知,2至32皿伺,必須的,并且不能跟主數(shù)據(jù)庫一樣
relay_log=relay-log #需要設(shè)置盒粮,否則會報錯
replicate_do_db=rep_test #指定要同步的數(shù)據(jù)庫
#replicate_ignore_db=mysql #指定不要同步的數(shù)據(jù)庫
重啟從數(shù)據(jù)庫鸵鸥,讓配置生效,設(shè)置登錄主數(shù)據(jù)庫的賬號和密碼等信息丹皱,然后啟動slave
#1.使用cmd窗口妒穴,進入從庫路徑 %mysql%/bin,然后登陸數(shù)據(jù)庫mysql -uroot -p摊崭,以下步驟都是在mysql中完成
#2.設(shè)置連接主庫的信息讼油,使用剛才新建的備份賬戶
# master_host 主庫ip
# master_user 之前新建的備份用戶
# master_password 備份用戶密碼
# master_log_file 對應(yīng)主庫狀態(tài)信息中的File字段
# master_log_pos 對應(yīng)主庫狀態(tài)信息中的Position字段
change master to master_host='192.168.100.18',master_user='slave',master_password='slave', master_log_file='mysql-bin.000001',master_log_pos=154;
#3.接下來啟動slave
start slave;
#4.查看從庫狀態(tài)信息
show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.100.18
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 154
Relay_Log_Space: 521
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 79727c64-1b7f-11e8-bc4c-509a4c0dbe20
Master_Info_File: D:\Program Files\MySQL\ProgramData\MySQL Server 5.7\Data\master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
ERROR:
No query specified
**********************************************************************************************************
以上兩項:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
都為Yes,則證明配置成功
測試主從復(fù)制是否有效果
在主數(shù)據(jù)庫中創(chuàng)建一個新的數(shù)據(jù)庫(或者數(shù)據(jù)表)呢簸,然后切換到從數(shù)據(jù)庫查看是否同樣多出同名的數(shù)據(jù)庫矮台,或者數(shù)據(jù)表。有則表示主從復(fù)制成功根时。
遇到問題
1.修改mysql配置文件時不生效
查看修改的配置文件是否正確瘦赫,我的mysql(5.7.12),安裝生成的配置文件不在mysql安裝目下蛤迎,而是在mysql存放數(shù)據(jù)庫的 /ProgramData 目錄下面确虱。另外,5.7的mysql中替裆,不在使用“-”蝉娜,而是使用“_”唱较,注意編輯配置文件時不要出錯。
2.配置時正常召川,但是測試是否同步時不能成功南缓,查看狀態(tài)之后發(fā)現(xiàn):
Slave_SQL_Running: no
報錯:Last_SQL_Errno: 找不到數(shù)據(jù)庫/數(shù)據(jù)表不存在
這種情況是因為主庫和從庫不同步,或者可能是在從庫上進行過寫的操作(增刪改)荧呐,此時汉形,刪除主庫和從庫相關(guān)的數(shù)據(jù)庫(為了保證清理干凈),然后重新在主庫中新建對應(yīng)的數(shù)據(jù)庫倍阐,再查看從庫概疆,同步成功。
當(dāng)Last_SQL_Errno: 0時峰搪,代表配置正確岔冀,主從復(fù)制正常。