MySQL複制的優點:
1.如果主伺服器出現問題,可以快速切換到從伺服器提供的服務
2.可以在從伺服器上執行查詢操作,降低主伺服器的通路壓力
3.可以在從伺服器上執行備份,以避免備份期間影響主伺服器的服務
注意:一般隻有更新不頻繁的資料或者對實時性要求不高的資料可以通過從伺服器查詢,實時性要求高的資料仍然需要從主伺服器獲得。
1、測試環境
作業系統 :Windows 7 32位作業系統(安裝雙資料庫端口分别為3306、3308)
資料庫版本:MySQL 5.7.18
主機A:192.168.1.103 (Master)
主機B:192.168.1.103(Slave)
配置的設定同樣适用于CentOS,Centos的配置檔案/etc/my.cnf
service mysqld stop #停止資料庫
service mysqld start #啟動資料庫
service mysqld restart #重新開機資料庫
2、資料庫安裝
這裡貼一下Slave資料庫安裝的日志
Microsoft Windows [版本 6.1.7601]
版權所有 (c) 2009 Microsoft Corporation。保留所有權利。
C:\Windows\system32>cd C:\Program Files\mysql-5.7.18-win32-slave\bin
C:\Program Files\mysql-5.7.18-win32-slave\bin>mysqld --initialize
C:\Program Files\mysql-5.7.18-win32-slave\bin>cd ../data
C:\Program Files\mysql-5.7.18-win32-slave\data>TYPE Javen-PC.err
2017-06-29T02:41:51.068120Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is
deprecated. Please use --explicit_defaults_for_timestamp server option (see doc
umentation for more details).
2017-06-29T02:41:51.573560Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-06-29T02:41:51.643760Z 0 [Warning] InnoDB: Creating foreign key constraint
system tables.
2017-06-29T02:41:51.699920Z 0 [Warning] No existing UUID has been found, so we a
ssume that this is the first time that this server has been started. Generating
a new UUID: 818f5c2f-5c74-11e7-8dff-000c29b2597f.
2017-06-29T02:41:51.699920Z 0 [Warning] Gtid table is not ready to be used. Tabl
e 'mysql.gtid_executed' cannot be opened.
2017-06-29T02:41:51.713960Z 1 [Note] A temporary password is generated for [email protected]
localhost: =rc%=eBVg0AY
C:\Program Files\mysql-5.7.18-win32-slave\data>cd ..
C:\Program Files\mysql-5.7.18-win32-slave>cd bin
C:\Program Files\mysql-5.7.18-win32-slave\bin>mysqld -install MySQL2
Service successfully installed.
C:\Program Files\mysql-5.7.18-win32-slave\bin>net start MySQL2
MySQL2 服務正在啟動 .
MySQL2 服務已經啟動成功。
C:\Program Files\mysql-5.7.18-win32-slave\bin>mysql -u root -p=rc%=eBVg0AY -P3308
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.18
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT
OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
1、在mysql-5.7.18-win32-slave根目錄中建立my.ini初始化配置檔案
[mysqld]
# set basedir to your installation path
basedir=C:\\Program Files\\mysql-5.7.18-win32-slave
# set datadir to the location of your data directory
datadir=C:\\Program Files\\mysql-5.7.18-win32-slave\\data
port = 3308
2、進入bin目錄mysqld –initialize初始化資料庫檔案
3、TYPE Javen-PC.err 檢視處理初始化密碼。Javen-PC.err 是你電腦的名稱 ,這裡的初始化密碼為=rc%=eBVg0AY
4、注冊mysql服務 mysqld -install MySQL2
5、啟動服務net start MySQL2
6、登入本地mysql:mysql -u root -p=rc%=eBVg0AY -P3308
7、修改本地root使用者密碼
ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
8、授權遠端登入(需要關閉防火牆或者配置指定端口可以通路)
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
FLUSH PRIVILEGES;
3、配置主伺服器Master
3.1 給從伺服器設定授權使用者(建立複制帳号)
建立一個帳戶javen,并且隻能允許從192.168.1.103這個位址上來登陸,密碼是123456。
mysql> grant replication slave on *.* to 'javen'@'192.168.1.103' identified by '123456';
mysql> flush privileges;
3.2 主伺服器Master配置
[mysqld]
# set basedir to your installation path
basedir=C:\\Program Files\\mysql-5.7.18-win32
# set datadir to the location of your data directory
datadir=C:\\Program Files\\mysql-5.7.18-win32\\data
port = 3306
log-bin = mysql-bin #[必須]啟用二進制日志
server-id = 1 #[必須]伺服器唯一ID,預設是1
expire-logs-days = 7 #隻保留7天的二進制日志,以防磁盤被日志占滿
#replicate-do-db = test #需要做複制的資料庫名;這裡不設定隻配置備份的資料庫
binlog-ignore-db = mysql #不備份的資料庫
binlog-ignore-db = information_schema
binlog-ignore-db = performation_schema
binlog-ignore-db = sys
#binlog-do-db=test #需要做複制的資料庫名
3.3 重新開機MySQL服務并設定讀取鎖定
net stop MySQL
net start MySQL
在主伺服器上設定讀取鎖定有效,確定沒有資料庫操作,以便獲得一個一緻性的快照
mysql -u root -proot -P3306
mysql> flush tables with read lock;
3.4 檢視主伺服器上目前的二進制日志名和偏移量值
mysql> show master status;
+------------------+----------+--------------+----------------------------------
----------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB
| Executed_Gtid_Set |
+------------------+----------+--------------+----------------------------------
----------------+-------------------+
| mysql-bin.000001 | 2519 | | mysql,information_schema,performa
tion_schema,sys | |
+------------------+----------+--------------+----------------------------------
----------------+-------------------+
1 row in set (0.00 sec)
這裡的 File 、Position 是在配置Salve的時候要使用到的,Binlog_Do_DB表示要同步的資料庫,Binlog_Ignore_DB 表示Ignore的資料庫,這些都是在配置的時候進行指定的。
另外:如果執行這個步驟始終為Empty set(0.00 sec),那說明前面的my.init沒配置對。
4、配置從伺服器Slave
4.1 修改從資料庫的配置
[mysqld]
# set basedir to your installation path
basedir=C:\\Program Files\\mysql-5.7.18-win32-slave
# set datadir to the location of your data directory
datadir=C:\\Program Files\\mysql-5.7.18-win32-slave\\data
port = 3308
log-bin=mysql-bin
server-id=3
binlog-ignore-db = mysql #不備份的資料庫
binlog-ignore-db = information_schema
binlog-ignore-db = performation_schema
binlog-ignore-db = sys
log-slave-updates
slave-skip-errors=all
slave-net-timeout=60
4.2 重新開機從資料庫并設定Slave資料庫
net stop MySQL2
net start MySQL2
登入從資料庫并做如下設定
mysql> stop slave; #關閉Slave
mysql> change master to master_host='192.168.1.103',master_user='javen',master_password='123456',master_log_file='mysql-bin.000001', master_log_pos= 2519;
mysql> start slave; #開啟Slave
注意:在這裡指定Master的資訊,master_log_file是在配置Master的時候的File選項, master_log_pos是在配置Master的Position 選項,這裡要進行對應。
4.3 檢視Slave配置的資訊
show slave status 檢視配置的資訊:
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.103
Master_User: mysync
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1192
Relay_Log_File: Javen-PC-relay-bin.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: mysql
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: 1192
Relay_Log_Space: 530
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: 818f5c2f-5c74-11e7-8dff-000c29b2597f
Master_Info_File: C:\Program Files\mysql-5.7.18-win32-slave\data\ma
ster.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more up
dates
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
mysql>
5、關閉掉主資料庫的讀取鎖定
mysql> unlock tables;
6、在主資料庫中建立一個表以及添加資料測試
在主資料庫(Master)添加一個test資料庫并在其中添加t1的資料表。如下圖

在從資料庫(Slave)自動同步,如下圖
如有問題歡迎留言….