您好,登錄后才能下訂單哦!
主從復制包含兩個步驟: 在 master 主服務器(組)上的設置,以及在 slave 從屬服務器(組)上的設置.
配置主服務器 master
1、如果沒有啟用,則需要開啟二進制日志.
給 master 設置唯一的 server_id ,所有的 slave 從屬服務器也要設置 server_id; server_id值可以是整數型的數字(1 ~ 2^31-1), 在同一個復制組(replicating group)中的每臺服務器的server_id都必須是唯一的.
[mysqld]
server-id=1
log-bin=mysql-bin
binlog_format=mixed
2、創建一個復制賬號,并授予replication slave權限。slave 從屬服務器需要有連接并從master復制的權限. 通常是為每一臺slave 創建一個單獨的用戶(user),并且只授予復制的權限(REPLICATION SLAVE 權限).
示例
GRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'slave_host' IDENTIFIED BY 'bigs3cret';
FLUSH PRIVILEGES;
MariaDB [(none)]> grant replication slave on *.* to 'repl_user'@'192.168.1.53' identified by 'pancou';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
需要注意,有一些系統配置選項可能會影響主從復制,查看下面的變量以避免發生問題:
skip-networking,如果 "skip-networking=1",則服務器將限制只能由localhost連接,阻止其他機器遠程連到此服務器上。
bind_address,類似地,如果 服務器只監聽 127.0.0.1(localhost)的TCP/IP連接,則遠程的 slave也不能連接到此服務器.
3、在主庫上,設置讀鎖定有效,這個操作是為了確保沒有數據庫操作,以便獲得一個一致性的快照:
MariaDB [(none)]> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
4、然后得到主庫上當前二進制文件名和偏移量值。這個操作的目的是在數據庫啟動以后,從這個點開始進行數據恢復。
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 | 509 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
5、現在主數據已經停止了更新操作,需要進行主數據庫備份。 如果主數據庫可以停止那么直接復制數據文件應該是最快的方法。
[root@localhost ~]# mysqldump -uroot -p --quick --all-databases --lock-all-tables --master-data=2 > /opt/data-all.sql
Enter password:
[root@localhost ~]# ll -h /opt/data-all.sql
-rw-r--r-- 1 root root 3.7M May 2 15:08 /opt/data-all.sql
6、主庫備份完畢以后,可以恢復寫操作,剩下的操作只需要在從庫上操作。
MariaDB [(none)]> unlock tables;
Query OK, 0 rows affected (0.00 sec)
7、將主數據庫的一致性備份數據傳送到從庫上。
[root@localhost ~]# rsync -avH --progress '-e ssh -p 22' /opt/data-all.sql root@192.168.1.53:/tmp/
The authenticity of host '192.168.1.53 (192.168.1.53)' can't be established.
RSA key fingerprint is 75:b3:14:47:e1:73:10:24:a8:8f:b8:05:29:3e:7d:30.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.53' (RSA) to the list of known hosts.
reverse mapping checking getaddrinfo for bogon [192.168.1.53] failed - POSSIBLE BREAK-IN ATTEMPT!
root@192.168.1.53's password:
sending incremental file list
data-all.sql
3863888 100% 23.88MB/s 0:00:00 (xfer#1, to-check=0/1)
sent 3864436 bytes received 31 bytes 594533.38 bytes/sec
total size is 3863888 speedup is 1.00
配置從屬服務器 slave
1、給 slave 指定唯一的 server_id. 所有服務器,不管是主服務器,還是從服務器,都要設置 server_id. server_id值可以是整數型的數字(1 ~ 2^31-1), 在同一個復制組(replicating group)中的每臺(/個)服務器的server_id都必須是唯一的.
要讓此配置項生效,需要重新啟動服務.
[mysqld]
server-id=2
2、在從庫上恢復數據
[root@localhost ~]# mysql </tmp/data-all.sql
3、在從庫上使用--skip-salve-start選項啟動從服務器,這樣不會立即啟動從數據庫服務器上的復制進程,方便我們對數據庫的服務進
行進一步的設置。
[root@www ~]# /usr/local/mysql/bin/mysqld_safe --skip-slave-start &
4、對從數據庫做相應的設置,指定復制使用的用戶,指定復制使用的用戶,主數據庫服務器的IP、端口以及開始執行復制的日志文件和
位置等。
CHANGE MASTER TO
MASTER_HOST='master.domain.com',
MASTER_USER='replication_user',
MASTER_PASSWORD='bigs3cret',
MASTER_PORT=3306,
MASTER_LOG_FILE='mariadb-bin.000096',
MASTER_LOG_POS=568,
MASTER_CONNECT_RETRY=10;
MariaDB [(none)]> change master to master_host='192.168.1.78',master_user='repl_user',master_password='pancou',master_log_file='mysql-bin.000002',master_log_pos=509;
Query OK, 0 rows affected (0.79 sec)
5、在從庫上,啟動slave進程
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)
6、這時在salve上,執行show processlist命令將顯示類似如下進程:
MariaDB [(none)]> show processlist\G
*************************** 1. row ***************************
Id: 5
User: root
Host: localhost
db: NULL
Command: Query
Time: 0
State: init
Info: show processlist
Progress: 0.000
*************************** 2. row ***************************
Id: 8
User: system user
Host:
db: NULL
Command: Connect
Time: 10
State: Waiting for master to send event
Info: NULL
Progress: 0.000
*************************** 3. row ***************************
Id: 9
User: system user
Host:
db: NULL
Command: Connect
Time: 4
State: Slave has read all relay log; waiting for the slave I/O thread to update it
Info: NULL
Progress: 0.000
3 rows in set (0.00 sec)
這表明slave已經連接上master了,并開始接收日志。
主庫上的進程:
MariaDB [(none)]> show processlist\G
*************************** 1. row ***************************
Id: 7
User: root
Host: localhost
db: NULL
Command: Query
Time: 0
State: init
Info: show processlist
Progress: 0.000
*************************** 2. row ***************************
Id: 9
User: repl_user
Host: 192.168.1.53:57532
db: NULL
Command: Binlog Dump
Time: 183
State: Master has sent all binlog to slave; waiting for binlog to be updated
Info: NULL
Progress: 0.000
2 rows in set (0.00 sec)
7、查看從庫復制狀態
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.78
Master_User: repl_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 479
Relay_Log_File: localhost-relay-bin.000004
Relay_Log_Pos: 767
Relay_Master_Log_File: mysql-bin.000003
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: 479
Relay_Log_Space: 1112
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_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Replicate_Do_Domain_Ids:
Replicate_Ignore_Domain_Ids:
Parallel_Mode: conservative
1 row in set (0.00 sec)
驗證復制服務的正確性,在主數據庫上執行一個更新操作,觀察是否在從庫上的是否同步。
MariaDB [(none)]> create database pancou;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> use database pancou;
ERROR 1049 (42000): Unknown database 'database'
MariaDB [(none)]> use pancou;
Database changed
MariaDB [pancou]> create table rpel_table(id int(3));
Query OK, 0 rows affected (0.39 sec)
MariaDB [pancou]> insert rpel_table value(1),(2),(3),(4),(5);
Query OK, 5 rows affected (0.04 sec)
Records: 5 Duplicates: 0 Warnings: 0
MariaDB [pancou]> select * from rpel_table;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
+------+
5 rows in set (0.00 sec)
MariaDB [pancou]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 | 913 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
查看從庫:
MariaDB [(none)]> select * from pancou.rpel_table;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
+------+
5 rows in set (0.00 sec)
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.78
Master_User: repl_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 913
Relay_Log_File: localhost-relay-bin.000004
Relay_Log_Pos: 1201
Relay_Master_Log_File: mysql-bin.000003
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: 913
Relay_Log_Space: 1546
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_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Replicate_Do_Domain_Ids:
Replicate_Ignore_Domain_Ids:
Parallel_Mode: conservative
1 row in set (0.00 sec)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。