MySQL 基于事務(wù)的 Replication(利用 GTID 實(shí)現(xiàn)主從復(fù)制)

MySQL 安裝

1. Adding the MySQL Yum Repository

  1. Go to the Download MySQL Yum Repository page (https://dev.mysql.com/downloads/repo/yum/) in the MySQL Developer Zone.

  2. Select and download the release package for your platform.

  3. Install the downloaded release package

    sudo yum install mysql80-community-release-el7-{version-number}.noarch.rpm
    
  4. check

    yum repolist all | grep mysql
    

2. installation

sudo yum-config-manager --disable mysql57-community
sudo yum-config-manager --enable mysql80-community
sudo yum install mysql-community-server

3. Starting MySQL Server

sudo systemctl start mysqld.service

sudo systemctl status mysqld.service

4. reset password

shell> sudo grep 'temporary password' /var/log/mysqld.log
shell> mysql -uroot -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';

MySQL 基于事務(wù)的 Replication(利用 GTID 實(shí)現(xiàn)主從復(fù)制)

GTID(全局事務(wù)標(biāo)示符)最初由google實(shí)現(xiàn),在MySQL 5.6中引入.GTID在事務(wù)提交時(shí)生成,由UUID和事務(wù)ID組成.uuid會(huì)在第一次啟動(dòng)MySQL時(shí)生成,保存在數(shù)據(jù)目錄下的auto .CNF文件里,事務(wù)ID則從1開始自增使用GTID的好處主要有兩點(diǎn):

不再需要指定傳統(tǒng)復(fù)制中的 master_log_files 和 master_log_pos,使主從復(fù)制更簡單可靠
可以實(shí)現(xiàn)基于庫的多線程復(fù)制,減小主從復(fù)制的延遲

1. 主庫配置

[mysqld]
log-bin=/var/log/mysql/mysql-bin
server-id=1
gtid_mode=ON
enforce_gtid_consistency=1   # 強(qiáng)制執(zhí)行GTID一致性。

2. 創(chuàng)建日志目錄并賦予權(quán)限

shell> mkdir /var/log/mysql
shell> chown mysql.mysql /var/log/mysql

3. 重啟服務(wù)

shell> systemctl restart mysqld.service

4. 在主服務(wù)器創(chuàng)建一個(gè)僅具有復(fù)制過程權(quán)限的單獨(dú)帳戶,以最大程度地降低對(duì)其他帳戶的危害

mysql> CREATE USER 'repl'@'%' IDENTIFIED BY 'Www.1.com';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';

5. 在主服務(wù)器創(chuàng)建一個(gè)僅具有all權(quán)限的單獨(dú)帳戶,用于從服務(wù)器遠(yuǎn)程拷貝原有的數(shù)據(jù)

mysql> CREATE USER 'tom'@'%' IDENTIFIED BY 'Www.1.com';
mysql> GRANT ALL ON *.* TO 'tom'@'%';
mysql> flush privileges;

6. 在從服務(wù)器上使用剛才的用戶進(jìn)行測試連接

shell> mysql -urepl -p'Www.1.com' -hMASTER_IP
shell> mysql -utom -p'Www.1.com' -hMASTER_IP

7. 配置從服務(wù)器 /etc/my.cnf

[mysqld]
server-id=2
gtid_mode=ON
enforce_gtid_consistency=1

# 可選項(xiàng), 把連接到 master 的信息存到數(shù)據(jù)庫中的表中
master-info-repository=TABLE
relay-log-info-repository=TABLE

8. 重啟從服務(wù)器

shell> systemctl restart mysqld.service

9. 進(jìn)入從服務(wù)器mysql,執(zhí)行如下操作

mysql>  CHANGE MASTER TO MASTER_HOST='192.168.235.128', MASTER_PORT=3306, MASTER_USER='repl',MASTER_PASSWORD='Www.1.com', master_auto_position=1;

10. 檢查是否成功

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.235.128
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 196
               Relay_Log_File: localhost-relay-bin.000004
                Relay_Log_Pos: 411
        Relay_Master_Log_File: mysql-bin.000002
             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

輸出結(jié)果中應(yīng)該看到 I/O 線程和 SQL 線程都是 YES, 就表示成功。

             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容