主從復(fù)制包含兩個(gè)步驟: 在 master 主服務(wù)器(組)上的設(shè)置,以及在 slave 從屬服務(wù)器(組)上的設(shè)置.
在網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站過(guò)程中,需要針對(duì)客戶(hù)的行業(yè)特點(diǎn)、產(chǎn)品特性、目標(biāo)受眾和市場(chǎng)情況進(jìn)行定位分析,以確定網(wǎng)站的風(fēng)格、色彩、版式、交互等方面的設(shè)計(jì)方向。成都創(chuàng)新互聯(lián)公司還需要根據(jù)客戶(hù)的需求進(jìn)行功能模塊的開(kāi)發(fā)和設(shè)計(jì),包括內(nèi)容管理、前臺(tái)展示、用戶(hù)權(quán)限管理、數(shù)據(jù)統(tǒng)計(jì)和安全保護(hù)等功能。
配置主服務(wù)器 master
1、如果沒(méi)有啟用,則需要開(kāi)啟二進(jìn)制日志.
給 master 設(shè)置唯一的 server_id ,所有的 slave 從屬服務(wù)器也要設(shè)置 server_id; server_id值可以是整數(shù)型的數(shù)字(1 ~ 2^31-1), 在同一個(gè)復(fù)制組(replicating group)中的每臺(tái)服務(wù)器的server_id都必須是唯一的.
[MySQLd]
server-id=1
log-bin=mysql-bin
binlog_format=mixed
2、創(chuàng)建一個(gè)復(fù)制賬號(hào),并授予replication slave權(quán)限。slave 從屬服務(wù)器需要有連接并從master復(fù)制的權(quán)限. 通常是為每一臺(tái)slave 創(chuàng)建一個(gè)單獨(dú)的用戶(hù)(user),并且只授予復(fù)制的權(quán)限(REPLICATION SLAVE 權(quán)限).
示例
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)
需要注意,有一些系統(tǒng)配置選項(xiàng)可能會(huì)影響主從復(fù)制,查看下面的變量以避免發(fā)生問(wèn)題:
skip-networking,如果 "skip-networking=1",則服務(wù)器將限制只能由localhost連接,阻止其他機(jī)器遠(yuǎn)程連到此服務(wù)器上。
bind_address,類(lèi)似地,如果 服務(wù)器只監(jiān)聽(tīng) 127.0.0.1(localhost)的TCP/IP連接,則遠(yuǎn)程的 slave也不能連接到此服務(wù)器.
3、在主庫(kù)上,設(shè)置讀鎖定有效,這個(gè)操作是為了確保沒(méi)有數(shù)據(jù)庫(kù)操作,以便獲得一個(gè)一致性的快照:
MariaDB [(none)]> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
4、然后得到主庫(kù)上當(dāng)前二進(jìn)制文件名和偏移量值。這個(gè)操作的目的是在數(shù)據(jù)庫(kù)啟動(dòng)以后,從這個(gè)點(diǎn)開(kāi)始進(jìn)行數(shù)據(jù)恢復(fù)。
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、現(xiàn)在主數(shù)據(jù)已經(jīng)停止了更新操作,需要進(jìn)行主數(shù)據(jù)庫(kù)備份。 如果主數(shù)據(jù)庫(kù)可以停止那么直接復(fù)制數(shù)據(jù)文件應(yīng)該是最快的方法。
[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、主庫(kù)備份完畢以后,可以恢復(fù)寫(xiě)操作,剩下的操作只需要在從庫(kù)上操作。
MariaDB [(none)]> unlock tables;
Query OK, 0 rows affected (0.00 sec)
7、將主數(shù)據(jù)庫(kù)的一致性備份數(shù)據(jù)傳送到從庫(kù)上。
[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
配置從屬服務(wù)器 slave
1、給 slave 指定唯一的 server_id. 所有服務(wù)器,不管是主服務(wù)器,還是從服務(wù)器,都要設(shè)置 server_id. server_id值可以是整數(shù)型的數(shù)字(1 ~ 2^31-1), 在同一個(gè)復(fù)制組(replicating group)中的每臺(tái)(/個(gè))服務(wù)器的server_id都必須是唯一的.
要讓此配置項(xiàng)生效,需要重新啟動(dòng)服務(wù).
[mysqld]
server-id=2
2、在從庫(kù)上恢復(fù)數(shù)據(jù)
[root@localhost ~]# mysql </tmp/data-all.sql
3、在從庫(kù)上使用--skip-salve-start選項(xiàng)啟動(dòng)從服務(wù)器,這樣不會(huì)立即啟動(dòng)從數(shù)據(jù)庫(kù)服務(wù)器上的復(fù)制進(jìn)程,方便我們對(duì)數(shù)據(jù)庫(kù)的服務(wù)進(jìn)
行進(jìn)一步的設(shè)置。
[root@www ~]# /usr/local/mysql/bin/mysqld_safe --skip-slave-start &
4、對(duì)從數(shù)據(jù)庫(kù)做相應(yīng)的設(shè)置,指定復(fù)制使用的用戶(hù),指定復(fù)制使用的用戶(hù),主數(shù)據(jù)庫(kù)服務(wù)器的IP、端口以及開(kāi)始執(zhí)行復(fù)制的日志文件和
位置等。
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、在從庫(kù)上,啟動(dòng)slave進(jìn)程
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)
6、這時(shí)在salve上,執(zhí)行show processlist命令將顯示類(lèi)似如下進(jìn)程:
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已經(jīng)連接上master了,并開(kāi)始接收日志。
主庫(kù)上的進(jìn)程:
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、查看從庫(kù)復(fù)制狀態(tài)
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)
驗(yàn)證復(fù)制服務(wù)的正確性,在主數(shù)據(jù)庫(kù)上執(zhí)行一個(gè)更新操作,觀(guān)察是否在從庫(kù)上的是否同步。
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)
查看從庫(kù):
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)
本文標(biāo)題:MariaDB設(shè)置主從復(fù)制
網(wǎng)址分享:http://chinadenli.net/article30/pddcpo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、網(wǎng)站維護(hù)、面包屑導(dǎo)航、品牌網(wǎng)站制作、靜態(tài)網(wǎng)站、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)