溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MySQL不停機添加從庫

發(fā)布時間:2020-06-20 11:16:06 來源:網(wǎng)絡 閱讀:1298 作者:何小帥 欄目:系統(tǒng)運維

背景

主節(jié)點上線運行了一段時間后,數(shù)據(jù)量日益增大,故需要添加slave節(jié)點來更好地支撐業(yè)務的發(fā)展。但是,此時也有一定量的用戶了,所以不能把主節(jié)點停機,而是在不停機的情況下添加slave節(jié)點,具體實現(xiàn)方法如下:

環(huán)境

防火墻和selinux都關閉
master slave
IP 192.168.7.71 192.168.7.72
主機名 master01 slave01
OS CentOS7 CentOS7
MySQL版本 5.7.28 5.7.28

操作步驟

1.在master上對數(shù)據(jù)庫做完全備份,并且拷貝到slave節(jié)點
1.1 查看當前master上的數(shù)據(jù)庫
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| db2                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
1.2 使用mysqldump工具進行全庫備份
[root@master01 ~]#mysqldump -p -A -F --single-transaction --master-data=1 > /backup/fullbackup_`date +%F_%T`.sql
Enter password: 
[root@master01 ~]#ll /backup/
total 832
-rw-r--r-- 1 root root 849153 Nov 29 20:22 fullbackup_2019-11-29_20:22:04.sql
[root@master01 ~]#scp -p /backup/fullbackup_2019-11-29_20\:22\:04.sql root@192.168.7.72:/data/
fullbackup_2019-11-29_20:22:04.sql                   100%  829KB  14.9MB/s   00:00
1.3 創(chuàng)建授權復制賬號
grant replication slave on *.* to repluser@'192.168.7.%' identified by '123.com';
2.在slave節(jié)點將master節(jié)點拷貝過來的完全備份進行導入
2.1 MySQL安裝過程略
2.2 將以下內容添加到my.cnf文件中,然后重新啟動數(shù)據(jù)庫。
[mysqld]
server-id = 2
read_only = ON  #設為只讀,但是對超級用戶無效。
2.3 導入并查看
[root@slave01 ~]#mysql -p < /data/fullbackup_2019-11-29_20\:22\:04.sql 
Enter password: 
[root@slave01 ~]#mysql -p -e "show databases"
Enter password: 
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| db2                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
可以看到已經(jīng)將master節(jié)點上的所有庫導入成功了。
3.在slave節(jié)點執(zhí)行以下命令,指定連接主服務器的信息,以進行數(shù)據(jù)同步。
3.1 找到完全備份的position信息
[root@slave01 ~]#grep '^CHANGE MASTER' /data/fullbackup_2019-11-29_20\:22\:04.sql 
CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000003', MASTER_LOG_POS=154;
3.2 配置連接主服務的信息(從完全備份的位置之后開始復制)
[root@slave01 ~]#mysql -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.28 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> CHANGE MASTER TO
    ->   MASTER_HOST='192.168.7.71',
    ->   MASTER_USER='repluser',
    ->   MASTER_PASSWORD='123.com',
    ->   MASTER_PORT=3306,
    ->   MASTER_LOG_FILE='mysql-bin.000003',
    ->   MASTER_LOG_POS=154;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
3.3 啟動slave節(jié)點,并查看同步狀態(tài)。
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.7.71
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 154
               Relay_Log_File: slave01-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
...省略信息...
Slave_IO_Running和Slave_SQL_Running線程都為Yes,表示主從同步成功,至此,在線添加從庫已經(jīng)實現(xiàn)。
向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI