如何進(jìn)行mysql5.6不停機(jī)主主搭建

這篇文章將為大家詳細(xì)講解有關(guān)如何進(jìn)行MySQL 5.6不停機(jī)主主搭建,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

創(chuàng)新互聯(lián)公司主營(yíng)碑林網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,手機(jī)APP定制開(kāi)發(fā),碑林h5微信小程序定制開(kāi)發(fā)搭建,碑林網(wǎng)站營(yíng)銷推廣歡迎碑林等地區(qū)企業(yè)咨詢

環(huán)境說(shuō)明:
版本 version 5.6.25-log 
主1庫(kù)ip: 10.219.24.25
主2庫(kù)ip:10.219.24.22
os 版本: centos 6.7
已安裝熱備軟件:xtrabackup 
防火墻已關(guān)

補(bǔ)充:
主從復(fù)制原理: http://blog.csdn.net/zhang123456456/article/details/72972701
mysql 5.6安裝 :http://blog.csdn.net/zhang123456456/article/details/53608554
xtrabackup 安裝: http://blog.csdn.net/zhang123456456/article/details/72836184

全程:先搭建一主一從,然后反過(guò)來(lái)搭建,就是雙主復(fù)制:

先一主一從搭建:

1、 主庫(kù)參數(shù)調(diào)整
-- 停止主庫(kù)mysql
[root@mysql02 ~]# service mysql stop
[root@mysql02 ~]# netstat -nltp|grep mysql|grep 3606
-- 主庫(kù)創(chuàng)建relay log目錄
[root@mysql02 full]# mkdir -p /data/mysql/relaylog/
[root@mysql02 full]# chown -R mysql:mysql /data/mysql/relaylog
-- 調(diào)整 my.cnf 參數(shù)
[root@mysql02 ~]# cat /etc/my.cnf
[client]
password = oracle
port = 3306
socket = /data/mysql/mysql.sock
[mysqld]
server-id=25
port = 3306
socket = /data/mysql/mysql.sock
character_set_server = utf8
character_set_client = utf8
collation-server=utf8_general_ci
lower_case_table_names = 1
max_connections = 1000
datadir = /data/mysql
log_bin = /data/mysql/binarylog/binlog 
log_bin_index = /data/mysql/binarylog/binlog
relay-log = /data/mysql/relaylog/relay
relay-log-index = /data/mysql/relaylog/relay
relay_log_purge = on
binlog_format = mixed
innodb_data_file_path = ibdata1:12M:autoextend 
auto_increment_increment = 10 
auto_increment_offset = 1 
[mysql]
default-character-set = utf8

說(shuō)明:
a、主庫(kù)必須配置的參數(shù)
server-id (主從的server-id必須不同)、log_bin、binlog_format、relay-log、relay-log-index、relay_log_purge
auto-increment-offset、auto-increment-increment

b、log-slave-updates 意思是,中繼日志執(zhí)行之后,這些變化是否需要計(jì)入自己的binarylog。 當(dāng)你的B服務(wù)器需要作為另外一個(gè)服務(wù)器的主服務(wù)器的時(shí)候需要打開(kāi)。 就是雙主互相備份,或者多主循環(huán)備份。 我們這里需要, 所以打開(kāi)。

c、auto-increment-offset、auto-increment-increment 兩個(gè)參數(shù)用于在 雙主(多主循環(huán))互相備份。 因?yàn)槊颗_(tái)數(shù)據(jù)庫(kù)服務(wù)器都可能在同一個(gè)表中插入數(shù)據(jù),如果表有一個(gè)自動(dòng)增長(zhǎng)的主鍵,那么就會(huì)在多服務(wù)器上出現(xiàn)主鍵沖突。 解決這個(gè)問(wèn)題的辦法就是讓每個(gè)數(shù)據(jù)庫(kù)的自增主鍵不連續(xù)。 上圖說(shuō)是, 我假設(shè)需要將來(lái)可能需要10臺(tái)服務(wù)器做備份, 所以auto-increment-increment 設(shè)為10. 而 auto-increment-offset=1 表示這臺(tái)服務(wù)器的序號(hào)。 從1開(kāi)始, 不超過(guò)auto-increment-increment。這樣做之后, 我在這臺(tái)服務(wù)器上插入的第一個(gè)id就是 1, 第二行的id就是 11了, 而不是2.(同理,在第二臺(tái)服務(wù)器上插入的第一個(gè)id就是2, 第二行就是12, 這個(gè)后面再介紹) 這樣就不會(huì)出現(xiàn)主鍵沖突了。 后面我們會(huì)演示這個(gè)id的效果。 
-- 啟動(dòng)主庫(kù)
[root@mysql02 ~]# mysqld_safe --defaults-file=/etc/my.cnf &
2、 從庫(kù)參數(shù)調(diào)整
-- 停止從庫(kù)mysql
[root@mysql01 ~]# service mysql stop
[root@mysql01 ~]# netstat -nltp|grep mysql|grep 3606
-- 調(diào)整 my.cnf 參數(shù)
[root@mysql01 ~]# cat /etc/my.cnf
[client]
password = oracle
port = 3306
socket = /data/mysql/mysql.sock
[mysqld]
server-id=22
port = 3306
socket = /data/mysql/mysql.sock
character_set_server = utf8
character_set_client = utf8
collation-server=utf8_general_ci
lower_case_table_names = 1
max_connections = 1000
datadir = /data/mysql
log_bin = /data/mysql/binarylog/binlog 
log_bin_index = /data/mysql/binarylog/binlog
relay-log = /data/mysql/relaylog/relay
relay-log-index = /data/mysql/relaylog/relay
relay_log_purge = on
auto_increment_increment = 10 
auto_increment_offset = 2
[mysql]
default-character-set = utf8
說(shuō)明:從庫(kù)必須配置的參數(shù)
server-id、log_bin、relay-log、relay_log_purge、auto-increment-offset、auto-increment-increment、

3、 主庫(kù)備份
-- 主庫(kù)備份目錄
[root@mysql02 full]# pwd
/xtrabackup/full
-- 主庫(kù) innobackupex 備份
[root@mysql02 ~]# innobackupex --user=root --password=oracle --port=3606 /xtrabackup/full/
170610 17:50:23 Backup created in directory '/xtrabackup/full/2017-06-10_17-50-19/'
MySQL binlog position: filename 'binlog.000010', position '120'
....
170610 17:50:23 completed OK!
-- 查看備份 binlog編號(hào) 與 截止 position
[root@mysql02 2017-06-10_17-50-19]# cat xtrabackup_binlog_info 
binlog.000010 120

4、 從庫(kù)創(chuàng)建與主庫(kù)相同的備份目錄
[root@mysql01 ~]# mkdir -p /xtrabackup/full
[root@mysql01 ~]# chown -R mysql:mysql /xtrabackup/full/

5、 主庫(kù)將備份 scp 到從庫(kù)
[root@mysql02 full]# pwd
/xtrabackup/full
[root@mysql02 full]# scp -r 2017-06-10_17-50-19 10.219.24.22:/xtrabackup/full
6、 從庫(kù)查看scp過(guò)來(lái)的備份
[root@mysql01 ~]# cd /xtrabackup/full/2017-06-10_17-50-19/
[root@mysql01 2017-06-10_17-50-19]# ll
total 12320
-rw-r-----. 1 root root 419 Jun 10 18:01 backup-my.cnf
-rw-r-----. 1 root root 12582912 Jun 10 18:01 ibdata1
drwxr-x---. 2 root root 4096 Jun 10 18:01 mysql
drwxr-x---. 2 root root 4096 Jun 10 18:01 performance_schema
drwxr-x---. 2 root root 4096 Jun 10 18:01 test
-rw-r-----. 1 root root 18 Jun 10 18:01 xtrabackup_binlog_info
-rw-r-----. 1 root root 113 Jun 10 18:01 xtrabackup_checkpoints
-rw-r-----. 1 root root 482 Jun 10 18:01 xtrabackup_info
-rw-r-----. 1 root root 2560 Jun 10 18:01 xtrabackup_logfile

7、 主庫(kù)創(chuàng)建同步用戶
mysql> GRANT replication slave ON *.* TO 'slave25'@'%' IDENTIFIED BY 'oracle'; 
Query OK, 0 rows affected (0.05 sec)

8、 從庫(kù)恢復(fù)主庫(kù)數(shù)據(jù)
-- 從庫(kù)將原有datadir文件夾重命名到新位置,并創(chuàng)建原文件夾 
[root@mysql01 ~]# mv /data/mysql /data/mysqlbak
[root@mysql01 ~]# mkdir -p /data/mysql
-- innobackupex apply-log
[root@mysql01 ~]# innobackupex --apply-log --user=oracle \
--password=oracle --port=3606 /xtrabackup/full/2017-06-10_17-50-19/
-- innobackupex copy 恢復(fù)的文件到原來(lái)的數(shù)據(jù)位置
[root@mysql01 mysql]# innobackupex --defaults-file=/etc/my.cnf --user=root \
--copy-back /xtrabackup/full/2017-06-10_17-50-19/

170610 18:25:11 completed OK!
-- 創(chuàng)建binlog目錄與 relaylog 目錄并賦權(quán)
[root@mysql01 ~]# mkdir -p /data/mysql/binarylog
[root@mysql01 ~]# mkdir -p /data/mysql/relaylog/
[root@mysql01 mysql]# chown -R mysql:mysql /data/mysql

9、 從庫(kù)配置與檢測(cè)
-- 從庫(kù)啟動(dòng)
[root@mysql01 mysql]# mysqld_safe --defaults-file=/etc/my.cnf &
-- 從庫(kù)指定與主庫(kù)同步的基本信息
mysql>
change master to
master_host='10.219.24.25',
master_port=3306,
master_user='slave25',
master_password='oracle', 
master_log_file='binlog.000010',
master_log_pos=120; 
Query OK, 0 rows affected, 2 warnings (0.04 sec)

參數(shù)解釋:
MASTER_HOST : 設(shè)置要連接的主服務(wù)器的ip地址
MASTER_USER : 設(shè)置要連接的主服務(wù)器的用戶名
MASTER_PASSWORD : 設(shè)置要連接的主服務(wù)器的密碼
MASTER_LOG_FILE : 設(shè)置要連接的主服務(wù)器的bin日志的日志名稱
MASTER_LOG_POS : 設(shè)置要連接的主服務(wù)器的bin日志的記錄位置
-- 啟動(dòng)slave 狀態(tài)(開(kāi)始監(jiān)聽(tīng)msater的變化)
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
-- 查看slave的狀態(tài).
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.219.24.25 #主庫(kù) IP
Master_User: slave25 # 主庫(kù)復(fù)制的用戶
Master_Port: 3306 # 主庫(kù) mysqld 
Connect_Retry: 60
Master_Log_File: binlog.000010 #io_thread 讀取主庫(kù) master_log_file
Read_Master_Log_Pos: 717 # io_thread 讀取主庫(kù) master_log_pos
Relay_Log_File: relay.000002
Relay_Log_Pos: 877
Relay_Master_Log_File: binlog.000010 #sql_thread 執(zhí)行主庫(kù)的 master_log_file
Slave_IO_Running: Yes # 關(guān)鍵的,io_thread 是否 running 
Slave_SQL_Running: Yes # 關(guān)鍵的,sql_thread 是否 running
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: 717 #sql_thread 執(zhí)行主庫(kù)的 master_log_pos
Relay_Log_Space: 1040
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 # 從庫(kù) 的延遲
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: 25
Master_UUID: 29d68531-4cf9-11e7-8e1f-000c297c4100
Master_Info_File: /data/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
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
1 row in set (0.00 sec)

ERROR: 
No query specified

10、 主從同步檢查 
-- 主庫(kù)
mysql> create database repl;
Query OK, 1 row affected (0.00 sec)
mysql> use repl
Database changed
mysql> create table repl (id int);
Query OK, 0 rows affected (0.02 sec)
mysql> insert into repl values(1);
Query OK, 1 row affected (0.00 sec)

-- 從庫(kù)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| binarylog |
| mysql |
| performance_schema |
| relaylog |
| repl |
| test |
+--------------------+
7 rows in set (0.00 sec)
mysql> use repl
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from repl;
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.00 sec) >一主一從同步成功!

##################################################
#至此A到B的復(fù)制已經(jīng)配置完成,下面配置從B到A的復(fù)制。#
##################################################

聲明> 下面操作中 新主庫(kù)即為原從庫(kù)(10.219.24.22) 新從庫(kù)為原主庫(kù)(10.219.24.25)

11、 新主庫(kù)創(chuàng)建同步用戶 
mysql> GRANT replication slave ON *.* TO 'slave22'@'%' IDENTIFIED BY 'oracle';
Query OK, 0 rows affected (0.00 sec)

12、 新主庫(kù)查看 binlog 文件號(hào)與 position 點(diǎn)
mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000004 | 313 | | | |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)


13、 新從庫(kù)指定與主庫(kù)同步的基本信息
mysql>
change master to
master_host='10.219.24.22',
master_port=3306,
master_user='slave22',
master_password='oracle', 
master_log_file='binlog.000004',
master_log_pos=313; 
Query OK, 0 rows affected, 2 warnings (0.04 sec)

14、新從庫(kù)打開(kāi) slave 復(fù)制功能
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
15、 新從庫(kù)檢測(cè)同步復(fù)制狀態(tài)
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.219.24.22
Master_User: slave22
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000004
Read_Master_Log_Pos: 313
Relay_Log_File: relay.000002
Relay_Log_Pos: 280
Relay_Master_Log_File: binlog.000004
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: 313
Relay_Log_Space: 443
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: 22
Master_UUID: 70023652-4dc7-11e7-9360-000c2944297a
Master_Info_File: /data/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
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
1 row in set (0.00 sec)

ERROR: 
No query specified

-- 新從庫(kù)測(cè)試數(shù)據(jù)同步狀態(tài)
mysql> create database mm_repl;
Query OK, 1 row affected (0.00 sec)
mysql> use mm_repl;
Database changed
mysql> create table mm_repl(id int auto_increment,name varchar(10), primary key(id));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into mm_repl(name) values("andy"),("taoYe");
Query OK, 1 row affected (0.00 sec)
mysql> select * from mm_repl;
+----+-------+
| id | name |
+----+-------+
| 1 | andy |
| 11 | taoYe |
+----+-------+
2 rows in set (0.00 sec)
-- 新主庫(kù)測(cè)試數(shù)據(jù)同步狀態(tài)
mysql> select * from mm_repl;
+----+-------+
| id | name |
+----+-------+
| 1 | andy |
| 11 | taoYe |
+----+-------+
2 rows in set (0.00 sec)
mysql> insert into mm_repl(name) values("andy"),("taoYe");
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from mm_repl;
+----+-------+
| id | name |
+----+-------+
| 1 | andy |
| 11 | taoYe |
| 12 | andy |
| 22 | taoYe |
+----+-------+
4 rows in set (0.00 sec)
-- 新從庫(kù)檢查同步復(fù)制
mysql> select * from mm_repl;
+----+-------+
| id | name |
+----+-------+
| 1 | andy |
| 11 | taoYe |
| 12 | andy |
| 22 | taoYe |
+----+-------+
4 rows in set (0.00 sec) >主主同步測(cè)試成功

關(guān)于如何進(jìn)行mysql 5.6不停機(jī)主主搭建就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

當(dāng)前名稱:如何進(jìn)行mysql5.6不停機(jī)主主搭建
網(wǎng)頁(yè)網(wǎng)址:http://muchs.cn/article26/gececg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營(yíng)銷、電子商務(wù)、網(wǎng)站內(nèi)鏈、標(biāo)簽優(yōu)化外貿(mào)建站、網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)