mysql5.7.26做主從復制配置


一、首先兩台服務器安裝好mysql數據庫環境

參照linux rpm方式安裝mysql5.1

https://www.cnblogs.com/sky-cheng/p/10564604.html

二、主庫master上創建主從復制賬號

mysql> grant replication slave,replication client on *.* to 'repl'@'%' identified by 'Zaq1xsw@';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> use mysql;
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 user,host from user;
+---------------+-----------+
| user          | host      |
+---------------+-----------+
| repl          | %         |
| root          | %         |
| mysql.session | localhost |
| mysql.sys     | localhost |
+---------------+-----------+
4 rows in set (0.00 sec)

三、Master、Slave上分別設置不同的Server_id,主庫上開啟二進制日志

mysql> show variables like '%server_id%';
+----------------+-------+
| Variable_name  | Value |
+----------------+-------+
| server_id      | 0     |
| server_id_bits | 32    |
+----------------+-------+

如果配置文件沒有設置server_id參數,則默認都是0

編輯/etc/my.cnf

添加service_id,它的值可以跟服務器的IP最后一位數字一樣,這樣就能保證內網中的服務器ID不重復。master上

server_id=103
log-bin=master
binlog_format=row

slave上

server_id=69

四、將主庫做一次全量備份,並恢復到從庫上

在主庫上操作

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.00 sec)

主庫有一個test數據庫

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| test           |
+----------------+
1 row in set (0.00 sec)

有一個test表

mysql> select * from test;
+------+------+
| id   | name |
+------+------+
|    1 | aaaa |
+------+------+
1 row in set (0.00 sec)

表里有一條數據,開始備份主庫

[root@node2 data]# mysqldump -uroot -p --all-databases > /home/mysql-5.7.26/bak/bak.sql
[root@node2 data]# scp -P25601 /home/mysql-5.7.26/bak/bak.sql root@172.28.18.69:/home/mysql-5.7.26/bak/

在從庫上操作,恢復主庫數據
[root@localhost log]# mysql -uroot -p < /home/mysql-5.7.26/bak/bak.sql 
Enter password: 
[root@localhost log]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.26 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.00 sec)

mysql> 

 

此時,從庫里test數據庫有了,里面也有test數據表里記錄

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.00 sec)

mysql> use test;
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 test
    -> ;
+------+------+
| id   | name |
+------+------+
|    1 | aaaa |
+------+------+
1 row in set (0.01 sec)

五、在slave上change msater 操作配置主從復制

首先獲取主庫日志文件名稱和偏移量

mysql> show master status \G;
*************************** 1. row ***************************
             File: master.000001
         Position: 154
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

ERROR: 
No query specified

mysql> 

在從庫上執行

mysql> change master to 
    -> master_host='172.28.18.103',
    -> master_port=3306,
    -> master_user='repl',
    -> master_password='Zaq1xsw@',
    -> master_log_file='master.000001',
    -> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.21 sec)

啟動從庫

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

查看從庫狀態

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.28.18.103
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 317
        Relay_Master_Log_File: master.000001
             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: 154
              Relay_Log_Space: 528
              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: 103
                  Master_UUID: ddbee8c3-76da-11e9-9174-90b11c15be09
             Master_Info_File: /home/mysql-5.7.26/data/master.info

此時:

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

主從復制配置已經生效

六、測試數據

在主庫插入一條數據

mysql> insert test values(2,'bbbb');
Query OK, 1 row affected (0.03 sec)

從庫上查詢

mysql> select * from test;
+------+------+
| id   | name |
+------+------+
|    1 | aaaa |
|    2 | bbbb |
+------+------+
2 rows in set (0.00 sec)

數據已經復制成功了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM